什么是MQL4、MQL5語言?


1.什么是MQL4語言?

MetaQuotes Language 4 (MQL4) 是一種新的內置型程序用來編寫交易策略。 這種語言可以創建你自己的智能交易,使自己的交易策略能夠完全自動地執行。而且,MQL4 還能自定義客戶指標,腳本和數據庫。
內包含了大量可以分析當前及歷史報價所必須的函數,以及一些基本的運算和邏輯操作。並內置了一些基本的指標和操作命令。
MetaEditor 4集合了編寫 MQL4 程序代碼的各種語句,它能幫助使用者方便地寫出規范的代碼。 MetaQuotes Language Dictionary 是 MQL4 語言的幫助工具,它包含了我們在使用工程中所有可能用到的函數。
MetaQuotes Language 4 可以編寫不同作用的程序代碼:
智能交易 是一種連接到特定圖表的自動交易系統。它能夠根據設置的節點自動啟動 ,當它開始運行后,它不會同時去處理另一個新的指令(也就是說必須等到當前程序完成)。 這種交易系統能夠在提醒用戶可以交易的同時,將交易定單自動送到交易服務器。與大多數交易系統一樣, 它也能夠用歷史數據測試交易策略,並在圖表上顯示出來。 智能交易存儲在 terminal_directory\experts。
自定義指標 可用來編寫新的技術指標,和內置的指標一樣,它不能用來進行自動交易, 只能作為分析數據的工具。自定義指標儲存在 terminal_directory\experts\indicators。
腳本 是執行單一功能的一段程序,和 智能交易不同,腳本不能單獨執行,只能被調用。腳本存儲在 terminal_dictionary\experts\scripts。
數據庫 常被使用的自定義函數的集合。數據庫不能單獨運行。數據庫建議存儲在terminal_directory\experts\libraries。
包含文件 包含文件常被使用的程序塊源代碼,這些文件能夠被包含在智能交易,腳本,客戶指標和數據庫 的源代碼中。 使用包含文件比調用資料庫更靈活快捷。 包含文件交易儲存在 terminal_directory\experts\include。

2.什么是MQL5語言?

MQL5是一種內置式計算機語言,用於設計交易的策略。這種語言是基於MetaQuotes Software Corp. 長期的網上交易平台經驗開發的。通過這種語言,可以創建你自己的智能交易,使自己的交易策略能夠完全自動地執行。而且,MQL5還能自定義客戶指標,腳本和數據庫。
MQL5內包含了大量可以分析當前及歷史報價所必須的函數,並內置的內基本指標和函數來管理和支配這些交易。
MetaEditor 5(文本編輯器)集合了編寫 MQL5程序代碼的各種語句。它能幫助使用者方便地寫出規范的代碼。 MetaQuotes Language Dictionary 是 MQL5 語言的幫助工具。
簡要指南包括功能、操作、儲備詞庫,和其他語言結構分類,以便查找所使用的要素之相關語言描述。
MQL5 可以編寫不同作用的程序代碼:
EA交易運行處理它:加載函數和卸載函數,項目鈴聲提醒,定時項目,深度變化的市場事件,圖標事件和自定義事件。
EA交易能夠在提醒用戶可以交易的同時,將交易定單自動送到交易服務器。EA交易儲存在 terminal_directory\MQL5\Experts 中。
自定義指標 可用來編寫新的技術指標,和內置的指標一樣,它不能用來進行自動交易, 只能作為分析數據的工具。 自定義指標儲存在terminal_directory\MQL5\Indicators中。
腳本 是執行單一功能的一段程序,和EA交易不同,腳本不處理任何行動,除了開始事件(需要在腳本中親自處理函數)。腳本是儲存terminal_directory\MQL5\Scripts。
數據庫 被使用的自定義函數的集合,用來儲存和分發常用的自定義程序塊。數據庫儲存在 terminal_directory\MQL5\Libraries。
包含文件 常被使用的程序塊源代碼,這些文件能夠被包含在EA交易,腳本,客戶指標和數據庫 的源代碼中。 使用包含文件比調用資料庫更靈活快捷。
包含可以存儲在與源文件相同的目錄—在這種情況下,指令“#include”。另一個儲存包含文件是terminal_directory\MQL5\Include,指令<#include>。

3.MQL4與MQL5哪個好?有什么區別?

從理論和性能上來說,MQL5比MQL4性能上有很大的優化,特別是歷史回測方面以及代碼適用性方面。目前MQL4仍然是主流,但MQL5是個趨勢。

如下是MT4關於MACD策略的一段完整交易代碼:

  1 //+------------------------------------------------------------------+
  2 //|                                                  MACD Sample.mq4 |
  3 //|                   Copyright 2005-2014, MetaQuotes Software Corp. |
  4 //|                                              http://www.mql4.com |
  5 //+------------------------------------------------------------------+
  6 #property copyright   "2005-2014, MetaQuotes Software Corp."
  7 #property link        "http://www.mql4.com"
  8 
  9 input double TakeProfit    =50;
 10 input double Lots          =0.1;
 11 input double TrailingStop  =30;
 12 input double MACDOpenLevel =3;
 13 input double MACDCloseLevel=2;
 14 input int    MATrendPeriod =26;
 15 //+------------------------------------------------------------------+
 16 //|                                                                  |
 17 //+------------------------------------------------------------------+
 18 void OnTick(void)
 19   {
 20    double MacdCurrent,MacdPrevious;
 21    double SignalCurrent,SignalPrevious;
 22    double MaCurrent,MaPrevious;
 23    int    cnt,ticket,total;
 24 //---
 25 // initial data checks
 26 // it is important to make sure that the expert works with a normal
 27 // chart and the user did not make any mistakes setting external 
 28 // variables (Lots, StopLoss, TakeProfit, 
 29 // TrailingStop) in our case, we check TakeProfit
 30 // on a chart of less than 100 bars
 31 //---
 32    if(Bars<100)
 33      {
 34       Print("bars less than 100");
 35       return;
 36      }
 37    if(TakeProfit<10)
 38      {
 39       Print("TakeProfit less than 10");
 40       return;
 41      }
 42 //--- to simplify the coding and speed up access data are put into internal variables
 43    MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
 44    MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
 45    SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
 46    SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
 47    MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
 48    MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);
 49 
 50    total=OrdersTotal();
 51    if(total<1)
 52      {
 53       //--- no opened orders identified
 54       if(AccountFreeMargin()<(1000*Lots))
 55         {
 56          Print("We have no money. Free Margin = ",AccountFreeMargin());
 57          return;
 58         }
 59       //--- check for long position (BUY) possibility
 60       if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious && 
 61          MathAbs(MacdCurrent)>(MACDOpenLevel*Point) && MaCurrent>MaPrevious)
 62         {
 63          ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
 64          if(ticket>0)
 65            {
 66             if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
 67                Print("BUY order opened : ",OrderOpenPrice());
 68            }
 69          else
 70             Print("Error opening BUY order : ",GetLastError());
 71          return;
 72         }
 73       //--- check for short position (SELL) possibility
 74       if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious && 
 75          MacdCurrent>(MACDOpenLevel*Point) && MaCurrent<MaPrevious)
 76         {
 77          ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd sample",16384,0,Red);
 78          if(ticket>0)
 79            {
 80             if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
 81                Print("SELL order opened : ",OrderOpenPrice());
 82            }
 83          else
 84             Print("Error opening SELL order : ",GetLastError());
 85         }
 86       //--- exit from the "no opened orders" block
 87       return;
 88      }
 89 //--- it is important to enter the market correctly, but it is more important to exit it correctly...   
 90    for(cnt=0;cnt<total;cnt++)
 91      {
 92       if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
 93          continue;
 94       if(OrderType()<=OP_SELL &&   // check for opened position 
 95          OrderSymbol()==Symbol())  // check for symbol
 96         {
 97          //--- long position is opened
 98          if(OrderType()==OP_BUY)
 99            {
100             //--- should it be closed?
101             if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious && 
102                MacdCurrent>(MACDCloseLevel*Point))
103               {
104                //--- close order and exit
105                if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))
106                   Print("OrderClose error ",GetLastError());
107                return;
108               }
109             //--- check for trailing stop
110             if(TrailingStop>0)
111               {
112                if(Bid-OrderOpenPrice()>Point*TrailingStop)
113                  {
114                   if(OrderStopLoss()<Bid-Point*TrailingStop)
115                     {
116                      //--- modify order and exit
117                      if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
118                         Print("OrderModify error ",GetLastError());
119                      return;
120                     }
121                  }
122               }
123            }
124          else // go to short position
125            {
126             //--- should it be closed?
127             if(MacdCurrent<0 && MacdCurrent>SignalCurrent && 
128                MacdPrevious<SignalPrevious && MathAbs(MacdCurrent)>(MACDCloseLevel*Point))
129               {
130                //--- close order and exit
131                if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet))
132                   Print("OrderClose error ",GetLastError());
133                return;
134               }
135             //--- check for trailing stop
136             if(TrailingStop>0)
137               {
138                if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
139                  {
140                   if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
141                     {
142                      //--- modify order and exit
143                      if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))
144                         Print("OrderModify error ",GetLastError());
145                      return;
146                     }
147                  }
148               }
149            }
150         }
151      }
152 //---
153   }
154 //+------------------------------------------------------------------+

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM