程式交易教學

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

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



限時優惠代碼
在購買鈦金挑戰賽時輸入“TITANIUM30”,即獲得30%的折扣(優惠截止日期為2024年4月30日)。

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

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

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



運用Label元件製作可顯示標記與簡易核取方塊的自動交易程式

設定Label元件

在本章節中,將說明利用Label元件顯示標記與記號的方法。

雖然一般會透過Arrow元件來顯示標記與記號,但若利用Label元件,不僅能夠將位置鎖定(基準)置於中心點,亦可根據XY的座標進行配置。舉例來說,由於在圖表左上方角落能顯示標記與記號,因此對於製作辨識度高的指標有所助益。

首先,在製作新檔案時選擇「自訂指標」,並將檔案命名為「TextTest」,無需新增參數即可進入下一步。在「自訂指標程式的事件處理常式」中勾選「OnChartEvent」,並在下一個畫面中點擊「完成」即是雛形。

自訂指標

本次將使用集結特殊符號的文體,並指定文字編碼來顯示標記。

首先從MQL4幫助檔中複製Label元件的範例編碼。。於MQL4幫助檔中點選「Constants, Enumerations and Structures」→「Objects Constants」→「Object Types」,便會顯示元件一覽表。從中選擇「OBJ_LABEL」,並將預先準備的「Create a text label」編碼複製貼上於檔案的下方。

「//--- reset the error value」「ResetLastError();」的第2行以及「Print(__FUNCTION__,」「”: failed to create text label! Error code = “,GetLastError());」兩行無需使用,故可予以刪除。

//+------------------------------------------------------------------+

//| 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

{

//--- create a text label

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

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);

}

利用「CharToStr」指定文字編碼

已設定的LabelCreate將會記載於OnInit函數中。

參數的名稱將指定為「Text0」、X與Y位置為「10」,配置位置則是左上方,故使用「CORNER_LEFT_UPPER」。此處使用「CharToStr」將文字編碼轉變為文字列,並在括弧內輸入編碼。本次指定「221」來呈現反白的向上箭頭。

字體設定為「Wingdings」、大小為「100」、顏色則是「White」。

另外,由於希望在元件消除之後進行顯示,因此在LabelCreate上執行能夠將指定元件從圖表上全數刪除的「ObjectsDeleteAll」。

ObjectsDeleteAll(0, “Text”);

LabelCreate(0, “Text0”, 0, 10, 10, CORNER_LEFT_UPPER, CharToStr(221), “Wingdings”, 100, clrWhite);
如此進行編譯並設定圖表,即可在左上方顯示反白的向上箭頭。

向上箭頭

另外,若將利用CharToStr指定的文字編碼從「221」改成「222」,就會變成反白的向下箭頭。而與Wingdings字體編碼的編號與標記,則記載於MQL4幫助檔中。

向下箭頭

顯示帶有背景的內文

以下將介紹如何透過Label元件,運用在圖表左上方顯示反白向上與向下箭頭的方式,進而顯示帶有背景的內文。

首先是背景的準備工作。先前雖然使用集結了特殊符號的「Wingdings」字體,但本次將使用「Webdings」。

選擇Webdings文字編碼「103」的大型「■」作為背景。由於此處不僅是指定文字編碼、也將指定對應的英文字母,因此指定■對應的英文字母「g」。以「Red」將背景顏色設定為紅色。

LabelCreate(0, “Text_back”, 0, 250, 100, CORNER_LEFT_UPPER, “g”, “Webdings”, 100, clrRed);
接下來,在作為背景的■上製作內文。此處嘗試以「a」的白色文字呈現,並如以下所示加以指定。

LabelCreate(0, “Text_text”, 0, 250, 100, CORNER_LEFT_UPPER, “a”, “meiro”, 100, clrWhite);
如欲將背景與內文置於中心點,應變更記載於「Create a text label」的LabelCreate參數。將「// anchor type」的鎖定位置從「ANCHOR_LEFT_UPPER」改成「ANCHOR_CENTER」。

const ENUM_ANCHOR_POINT anchor = ANCHOR_CENTER, // anchor type
另外,若想與前次顯示的向下箭頭並列顯示,可將向下箭頭的X位置與Y位置一併從「10」改成「100」。

LabelCreate(0, “Text0”, 0, 100, 100, CORNER_LEFT_UPPER, CharToStr(222), “Wingdings”, 100, clrWhite);
如此進行編譯,便會在紅色■背景中看見白色「a」文字與向下箭頭並列顯示。

a文字與向下箭頭

增加顯示的內文數量

如此透過Label元件活用標記與記號,將能夠簡單地製作帶有背景的內文。前述雖只介紹了顯示1文字的方法,但亦可輕鬆新增內文的數量。

舉例來說,若想顯示「MQL」等三個文字,便應準備三個作為背景的■。編碼的g將對應■來顯示,因此可指定「ggg」來顯示三個■。

LabelCreate(0, “Text_back”, 0, 250, 100, CORNER_LEFT_UPPER, “ggg”, “Webdings”, 100, clrRed);
接著僅需將內文的文字變更為「MQL」即可。也就是將「a」變更為「MQL」。

LabelCreate(0, “Text_text”, 0, 250, 100, CORNER_LEFT_UPPER, “MQL”, “meiro”, 100, clrWhite);
如此進行編譯,便會在圖表的紅色■背景中呈現白色的「MQL」。

MQL

製作簡易核取方塊

接下來將製作簡易的核取方塊。

首先透過LabelCreate顯示「Wingdings」文字編碼「111」的正方形。X位置與Y位置一併設定為「250」、顏色則是「Aqua」。

LabelCreate(0, “Text_box”, 0, 250, 250, CORNER_LEFT_UPPER, CharToStr(111), “Wingdings”, 100, clrAqua);
如此進行編譯,便會在圖表中出現水藍色的正方形。

水藍色的正方形

透過設定,當點選這個水藍色正方形時,將會切換為「Wingdings」文字編碼「254」的打勾核取方塊標記(正方形中出現打勾的標記)。

在OnChartEvent中新增以下的if文體。在此內容中,勾選的元件為「Text_box」,而該元件的文字編碼「111」將會與加上勾選標記的「254」一併進行處理。若無勾選則會恢復「111」的正方形。

if (id == CHARTEVENT_OBJECT_CLICK) {

if (sparam == “Text_box”) {

if (ObjectGetString(0, sparam, OBJPROP_TEXT) == CharToStr(111)) ObjectSetString(0, sparam, OBJPROP_TEXT, CharToStr(254));

else ObjectSetString(0, sparam, OBJPROP_TEXT, CharToStr(111));

}

}
如此進行編譯,並點擊圖表中的水藍色正方形,即會切換為加上打勾的標記。另外,每次點擊都會在有打勾以及沒有打勾的標記之間反覆切換。

如此使用Label元件,即可簡單製作複選框。

複選框

原始碼

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

//+------------------------------------------------------------------+

//| TextTest.mq4 |

//| Copyright 2022, MetaQuotes Software Corp. |

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

//+------------------------------------------------------------------+

#property copyright “Copyright 2022, 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

ObjectsDeleteAll(0, “Text”);

LabelCreate(0, “Text0”, 0, 100, 100, CORNER_LEFT_UPPER, CharToStr(222), “Wingdings”, 100, clrR);

LabelCreate(0, “Text_back”, 0, 250, 100, CORNER_LEFT_UPPER, “ggg”, “Webdings”, 100, clrRed);

LabelCreate(0, “Text_text”, 0, 250, 100, CORNER_LEFT_UPPER, “MQL”, “meiro”, 100, clrWhite);

LabelCreate(0, “Text_box”, 0, 250, 250, CORNER_LEFT_UPPER, CharToStr(111), “Wingdings”, 100, clrAqua);

//---

return(INIT_SUCCEEDED);

}

//+------------------------------------------------------------------+

//| 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);

}

//+------------------------------------------------------------------+

//| ChartEvent function |

//+------------------------------------------------------------------+

void OnChartEvent(const int id,

const long &lparam,

const double &dparam,

const string &sparam)

{

if (id == CHARTEVENT_OBJECT_CLICK) {

if (sparam == “Text_box”) {

if (ObjectGetString(0, sparam, OBJPROP_TEXT) == CharToStr(111)) ObjectSetString(0, sparam, OBJPROP_TEXT, CharToStr(254));

else ObjectSetString(0, sparam, OBJPROP_TEXT, CharToStr(111));

}

}

}

//+------------------------------------------------------------------+

//| 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_CENTER, // 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

{

//--- create a text label

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

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%的交易利潤分成。



限時優惠代碼
在購買鈦金挑戰賽時輸入“TITANIUM30”,即獲得30%的折扣(優惠截止日期為2024年4月30日)。

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

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

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