MT4

Git Book傳送門

更進階完整的資訊會陸陸續續整理至 Gitbook 中 (持續更新中)。

MT4 (Metatrade 4) 以及 MT5 (Metatrade 5) 都是由俄羅斯公司 MetaQuotes Software Corp 所開發的交易平台, 並由各個外匯公司對該平台提供服務。

MQL4  
|-- Experts (EA 又稱專家系統, 執行交易策略的文件)
|-- Files (程式中使用到的外部檔案)
|-- Images (程式中使用的點陣圖)
|-- Include (Header檔)
|-- Indicators (建立指標的文件)
|-- Libraries
|-- Logs
|-- Presets (預設屬性區)
|-- Projects
`-- Scripts (腳本檔)

檔案存放路徑結構

MQL4/MQL5 為該交易平台所撰寫的程式語言, 語法的部分類似 C/C++ (筆者的感覺其實只是 C/C++ 的子集, 所以有學過 C/C++的人應該不太陌生), 故詳細語法在這邊不是著墨的重點。

MQL4/5 撰寫的程式碼用途

  • EA 連結至特定圖表後的自動交易系統, 可用於回測
  • Indicators 編寫透過自訂公式來計算數據產生的指標
  • Scripts 執行單一功能的程式碼, 附加至圖表時會立即執行一次

MQL4 語法與標準 C 不同之處

Data Type (範例皆轉自官方)
  • string
string svar="This is a character string";  
string svar2=StringSubstr(svar,0,4);  
Print("Copyright symbol\t\x00A9");  
FileWrite(handle,"This string contains a new line symbols \n");  
string MT4path="C:\\Program Files\\MetaTrader 4";  

  • color
//--- Literals
C'128,128,128'    // Gray  
C'0x00,0x00,0xFF' // Blue  
//color names
clrRed               // Red  
clrYellow            // Yellow  
clrBlack             // Black  
//--- Integral representations
0xFFFFFF          // White  
16777215          // White  
0x008000          // Green  
32768             // Green  

  • Datetime
datetime NY=D'2015.01.01 00:00';     // Time of beginning of year 2015  
datetime d1=D'1980.07.19 12:30:27';  // Year Month Day Hours Minutes Seconds  
datetime d2=D'19.07.1980 12:30:27';  // Equal to D'1980.07.19 12:30:27';  
datetime d3=D'19.07.1980 12';        // Equal to D'1980.07.19 12:00:00'  
datetime d4=D'01.01.2004';           // Equal to D'01.01.2004 00:00:00'  
datetime compilation_date=__DATE__;             // Compilation date  
datetime compilation_date_time=__DATETIME__;    // Compilation date and time  
datetime compilation_time=__DATETIME__-__DATE__;// Compilation time  
//--- Examples of declarations after which compiler warnings will be returned
datetime warning1=D'12:30:27';       // Equal to D'[date of compilation] 12:30:27'  
datetime warning2=D'';               // Equal to __DATETIME__  

提供 Preprocessor 對程式屬性的設定

#Properties (參考官方介紹)

MQL4 程式介紹

報價系統跳動單位

外匯交易單位為一手 (部份券商交易提供較小單位的操作 ex. 0.1手、0.01手), 而這邊一手實際上指的是交易量 (而非價) 的單位, 一手表示為 100K 單位貨幣 (依前方的貨幣為主 ex. 一手 EURUSD 的單, 金額為 100K EUR)

而在外匯交易的報價是以點 (Pips) 為跳動單位, 但在程式交易內的跳動則是用 Points 來替代, 包含允許滑點 (Slippage) 的參數也都是以 Points 來設定, 但 Points 在各券商報價系統中實際為何不一定 (如果報價單位有提供到步 則 1 Point = 1步, 若只有到點 則 1 Points = 1點)。

Pips 稱 , 而比 Pips 再小一位數的單位則稱 小點 或是 (1點 = 10步)

下單種類

  • 市價單 (OPBUY, OPSELL)
  • 順勢單 (OPBUYSTOP, OPSELLSTOP) 超過指定價格買 BUY單 或 SELL單 (不保證履約價)
  • 逆勢單 (OPBUYLIMIT, OPSELLLIMIT) 成交價在限制價格內才會買進 (會保證成交價)

(外匯券商所允許的下單方式為)

  • 現價以上, 可以下 Buy-Stop, Sell-Limit
  • 現價以下, 可以下 Buy-Limit, Sell-Stop

特殊函數

More Detail (參考官方介紹)

  • OnInit()-程式起始時會執行一次
  • OnTick()-圖表 Event 會觸發執行的函式
  • OnTimer()-時間 Event 會觸發執行的函式
  • OnDeinit()-結束時會執行的函式
  • OnTester()-測試 Event 會觸發執行的函式

關於時序物件的 Index

當前的物件皆為 0, 時間愈往倒退值愈大 ex. 收盤價, 今日為 Close[0], 昨日為 Close[1]...etc

交易 API 介紹

這邊介紹一些關於撰寫 EA 時會經常使用到的 API

Order Send : 下單 API, 預掛單也是透過此 API

OrderSend( str symbol, int cmd=OP_BUY|OP_SELL|OP_BUYSTOP|OP_SELLSTOP|OP_BUYLIMIT|OP_SELLLIMIT, dbl volume, dbl price, int slippage, dbl stoploss, dbl takeprofit, str comment=NULL, int magic=0, dtt expiration=0, clr arrow_color=CLR_NONE )  
  • CMD - 顧名思義, 下單可執行的操作 (四種下單操作)
  • Volume - 幾手 (外匯下單的單位)
  • Price - 價位
  • Slippage - 允許最大滑點
  • Stop Loss - 停損價位
  • Take Profit - 停利價位
  • Comment - 註解
  • Magic - 訂單識別碼
  • DTT Expiration - 此單的有效期, 大部分交易商取消
  • Arrow Color - 標記在走勢圖上的下單點, 顏色參考

Order Modify : 修改訂單的 API

OrderModify( int ticket, dbl price, dbl stoploss, dbl takeprofit, dtt expiration, clr color=CLR_NONE )  

EA 程式架構

(待補充)

Captain Vincent

Read more posts by this author.