程式交易教學

誠邀您參加全球知名外匯經紀商OANDA的自營交易(Prop Trader)

報名OANDA自營交易(Prop Trader),並通過我們的考核,您就可以使用OANDA提供的資金進行交易,獲得高達80%的交易利潤分成。



優化了挑戰塞交易規則
無最低交易天數限制等優化了挑戰賽的交易規則。

500,000美元交易資金
您可以使用最高500,000美元的資金進行交易。

豐富的交易商品
您可以交易包括外匯、黃金、原油、股票指數等多種商品。



透過LabelCreate函數的運用 讓數字倒數計時的方式解說

使數字倒數計時的方法

在「使用MQL程式語言中OnTimer函數的準備工作」文章中,運用了Comment函數製作簡易時鐘,並使用Label物件來顯示K線定型前的剩餘時間。

參考文章:使用MQL程式語言中OnTimer函數的準備工作

但是該處的數字並不會進行倒數計時的動作,因為LabelCreate函數設定為「成功運作一次之後,並不會繼續再次執行」,因此在一次運作完成之後,還需進行修正才能顯示數字。

在從MQL4相關參考中複製貼上的LabelCreate函數中,create a text label的if文法處添加ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);」。

//— create a text label

if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0))

{

ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

Print(__FUNCTION__,

“: failed to create text label! Error code = “,GetLastError());

return(false);

}

如此進行編譯,就會更新文字列的資訊,使數字開始倒數計時。

倒數計時結束後變成「0」

此處可依照喜好自由設定;而目前的狀況下,在倒數計時至1之後,接下來的數字並非「0」、而是回到「60」,因此要在OnTimer函數中添加「if(countDown == PeriodSeconds()) countDown = 0;」的if文法,如此便會在倒數計時到「0」之後,修正數字回到「59」。

void OnTimer()

{

//—

Comment(TimeLocal(),”\n”,TimeCurrent(),”\n”,int(TimeLocal() – TimeCurrent()) / 3600);

int countDown = PeriodSeconds() – (int)TimeLocal() % PeriodSeconds();

if(countDown == PeriodSeconds()) countDown = 0;

LabelCreate(0, “Clock”, 0, 100, 100, CORNER_LEFT_UPPER, (string)countDown, “Arial Bold”, 30, clrWhite);

}

此處的if文法條件為「倒數計時與K線秒數一致時變成0」;新增此文法後,就會在倒數計時結束時顯示「0」。

最後,當在從圖表中刪除指標時,如果要一併刪除文字與物件,只需在OnDeinit函數的位置加上「Comment(“”);」。

void OnDeinit(const int reason)

{

EventKillTimer();

ObjectDelete(“Clock”);

Comment(“”);

}

原始碼

本次製作的原始碼如下列所示。

//+——————————————————————+

//| TimerTest.mq4 |

//| Copyright 2021, MetaQuotes Software Corp. |

//| https://www.mql5.com |

//+——————————————————————+

#property copyright “Copyright 2021, MetaQuotes Software Corp.”

#property link “https://www.mql5.com”

#property version “1.00”

#property strict

#property indicator_chart_window

//+——————————————————————+

//| Custom indicator initialization function |

//+——————————————————————+

int OnInit()

{

//— indicator buffers mapping

EventSetMillisecondTimer(100);

//—

return(INIT_SUCCEEDED);

}

//+——————————————————————+

//| Custom indicator deinitialization function |

//+——————————————————————+

void OnDeinit(const int reason)

{

EventKillTimer();

ObjectDelete(“Clock”);

Comment(“”);

}

//+——————————————————————+

//| Custom indicator iteration function |

//+——————————————————————+

int OnCalculate(const int rates_total,

const int prev_calculated,

const datetime &time[],

const double &open[],

const double &high[],

const double &low[],

const double &close[],

const long &tick_volume[],

const long &volume[],

const int &spread[])

{

//—

//— return value of prev_calculated for next call

return(rates_total);

}

//+——————————————————————+

//| Timer function |

//+——————————————————————+

void OnTimer()

{

//—

Comment(TimeLocal(),”\n”,TimeCurrent(),”\n”,int(TimeLocal() – TimeCurrent()) / 3600);

int countDown = PeriodSeconds() – (int)TimeLocal() % PeriodSeconds();

if(countDown == PeriodSeconds()) countDown = 0;

LabelCreate(0, “Clock”, 0, 100, 100, CORNER_LEFT_UPPER, (string)countDown, “Arial Bold”, 30, clrWhite);

}

//+——————————————————————+

//| Create a text label |

//+——————————————————————+

bool LabelCreate(const long chart_ID=0, // chart’s ID

const string name=”Label”, // label name

const int sub_window=0, // subwindow index

const int x=0, // X coordinate

const int y=0, // Y coordinate

const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // chart corner for anchoring

const string text=”Label”, // text

const string font=”Arial”, // font

const int font_size=10, // font size

const color clr=clrRed, // color

const double angle=0.0, // text slope

const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type

const bool back=false, // in the background

const bool selection=false, // highlight to move

const bool hidden=true, // hidden in the object list

const long z_order=0) // priority for mouse click

{

//— reset the error value

ResetLastError();

//— create a text label

if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0))

{

ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

Print(__FUNCTION__,

“: failed to create text label! Error code = “,GetLastError());

return(false);

}

//— set label coordinates

ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);

ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);

//— set the chart’s corner, relative to which point coordinates are defined

ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);

//— set the text

ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

//— set text font

ObjectSetString(chart_ID,name,OBJPROP_FONT,font);

//— set font size

ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);

//— set the slope angle of the text

ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);

//— set anchor type

ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);

//— set color

ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//— display in the foreground (false) or background (true)

ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//— enable (true) or disable (false) the mode of moving the label by mouse

ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//— hide (true) or display (false) graphical object name in the object list

ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

//— set the priority for receiving the event of a mouse click in the chart

ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//— successful execution

return(true);

}

//+——————————————————————+

將EA自動程式交易應用於外匯與差價合約交易中

EA

我們以圖文形式詳細介紹有關EA自動程式交易的基本知識,以及在MT4/MT5平台上的安裝、參數設定方法、編碼等等內容。另外,對持有OANDA帳戶的客戶,還可以免費使用我們的獨有EA與指標工具。

誠邀您參加全球知名外匯經紀商OANDA的自營交易(Prop Trader)

報名OANDA自營交易(Prop Trader),並通過我們的考核,您就可以使用OANDA提供的資金進行交易,獲得高達80%的交易利潤分成。



優化了挑戰塞交易規則
無最低交易天數限制等優化了挑戰賽的交易規則。

500,000美元交易資金
您可以使用最高500,000美元的資金進行交易。

豐富的交易商品
您可以交易包括外匯、黃金、原油、股票指數等多種商品。