在MT5上开发EA最麻烦的是订单这类的写法,因为它的持仓和历史单是分开获取,挂单和非挂单分开获取,这样导致了稍不注意就会出问题,并且magic和注释开单和平仓也可能不一样(手动平仓会丢失magic和注释) 获取订单的开单时间也麻烦,代码不严谨就会出问题。而在MT4上 挂单和成交单是在一个循环里面的也共用一套订单属性获取函数,所以本人开发这个动态库就是方便开发MT5EA,也方便MT4EA升级至MT5EA,只需要把最下面附件里面的文件放到对应的文件夹即可用该库开发EA
已重定义的EA函数:
-
- int OrdersTotal(datetime time);//OrdersTotal() 持仓单
- int OrdersHistoryTotal(datetime time);//OrdersHistoryTotal() 历史单
- int OrdersHistoryTotalW(datetime time);//OrdersHistoryTotalW(0) 历史单扩展函数 0代表所有历史单 / 其他时间代表指定时间后的历史单
- bool OrderSelect(int i,int select,int mode);
- ulong OrderTicket();
- string OrderSymbol();
- int OrderType();
- double OrderLots();
- double OrderOpenPrice();
- datetime OrderOpenTime();
- double OrderClosePrice();
- datetime OrderCloseTime();
- double OrderStopLoss();
- double OrderTakeProfit();
- double OrderSwap();
- double OrderCommission();
- double OrderProfit();
- ulong OrderMagicNumber();
- string OrderComment();
- double OrderProfitPoint();
- //平仓订单属性
- ulong OrderTicket2();
- ulong OrderMagicNumber2();
- string OrderComment2();
- double OrderStopLoss2();
- double OrderTakeProfit2();
-
- //同步交易函数
- ulong OrderSend(string symbol,int cmd,double volume,double price,int slippage,double stoploss,double takeprofit,string comment=NULL,int magic=0,datetime expiration=0,color arrow_color=clrNONE);
- bool OrderClose(ulong OrderTicket,double OrderLots,double OrderClosePrice=0,int slippage=10000);
- bool OrderDelete(ulong OrderTicket);
- bool OrderModify(ulong OrderTicket, double price, double stoploss, double takeprofit, datetime expiration=0, color arrow_color=clrBlue);
- bool OrderCloseBy(ulong ticket,ulong opposite,color arrow_color);
- //异步交易函数
- ulong OrderSendAsync(string symbol,int cmd,double volume,double price,int slippage,double stoploss,double takeprofit,string comment=NULL,int magic=0,datetime expiration=0,color arrow_color=clrNONE);
- bool OrderCloseAsync(ulong OrderTicket,double OrderLots,double OrderClosePrice=0,int slippage=10000);
- bool OrderDeleteAsync(ulong OrderTicket);
- bool OrderModifyAsync(ulong OrderTicket, double price, double stoploss, double takeprofit, datetime expiration=0, color arrow_color=clrBlue);
- bool OrderCloseByAsync(ulong ticket,ulong opposite,color arrow_color);
复制代码
已重定义的指标函数:
- double iMACD(string _symbol,ENUM_TIMEFRAMES _period,int _fast_ema_period,int _slow_ema_period,int _signal_period,ENUM_APPLIED_PRICE _applied_price,int mode,int shift);
- double iBands(string _symbol,ENUM_TIMEFRAMES _period,int _bands_period,double _deviation,int _bands_shift,ENUM_APPLIED_PRICE _applied_price,int _pos,int _shift);
- double iMA(string symbol,ENUM_TIMEFRAMES timeframe,int ma_period, int ma_shift,ENUM_MA_METHOD ma_method, ENUM_APPLIED_PRICE applied_price,int shift);
- void Releasetimeouthandle(int hour);//句柄超时释放/小时
复制代码
已重定义的其他函数:
复制代码
下面就可以在MT5里面使用我们熟悉的MT4遍历循环代码了- bool Orderclose(int type,int magic,string com,string symbol="")
- {
- bool Orderclose=false;
- for(int i=OrdersTotal()-1; i>=0; i--)
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
- if((symbol==""&&OrderSymbol()==Symbol())
- ||(symbol!=""&&OrderSymbol()==symbol))
- if(OrderMagicNumber()==magic || magic==-1)
- if(StringFind(OrderComment(),com,0)!=-1 || com=="")
- if(OrderType()==type||type==-100)
- {
- if(OrderType()<2)
- if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),2000))
- Orderclose=true;
- if(OrderType()>1)
- if(OrderDelete(OrderTicket()))
- Orderclose=true;
- }
- return(Orderclose);
- }
复制代码
- double AveragePrice(int type,int magic,string com,string sym="")
- {
- double Prices=0;
- double lots=0;
- string _sym="";
- for(int i=OrdersTotal()-1; i>=0; i--)
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
- if((sym==""&&OrderSymbol()==Symbol())
- ||(sym!=""&&OrderSymbol()==sym))
- if(OrderMagicNumber()==magic || magic==-1)
- if(StringFind(OrderComment(),com,0)!=-1 || com=="")
- if(OrderType()==type)
- {
- Prices=Prices+OrderOpenPrice()*OrderLots();
- lots=lots+OrderLots();
- _sym=OrderSymbol();
- }
- if(lots!=0)
- return(NormalizeDouble(Prices/lots,(int)SymbolInfoInteger(_sym,SYMBOL_DIGITS)));
- else
- return(0);
- }
复制代码
写MT5EA需要在顶部加上 #include <Include_MQL4CodeEnvironment.mqh>
- #property version "1.0"
- #property strict
- #include <Include_MQL4CodeEnvironment.mqh>
复制代码
|