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