都说MT5程序比MT4好,MT4一行代码就能开仓的指令,MT5要用一堆代码才能搞定!作者本人跟许多小伙伴一样,喜欢把复杂的问题简单化。这是本人使用的最实用MT5开仓代码,不用库文件,不用mqh,一个文件搞定。mq5源码包括两个多空单按钮,点击即可测试开仓效果,欢迎喜欢的一起测试研究。
//+------------------------------------------------------------------+ //| MT5开仓模块.mq5 | //| 诚万三工作室 | //| http://www.taoea.cn | //+------------------------------------------------------------------+ #property copyright "诚万三工作室" #property link "http://www.taoea.cn" #property version "1.00" //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { ButtonCreate("cws.B","Buy",20,100,120,40); ButtonCreate("cws.S","Sell",20,150,120,40); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam) { if(id==CHARTEVENT_OBJECT_CLICK) { if(sparam=="cws.B") { NowOpenOrder(0,0.01,500,800,"b1"); ObjectSetInteger(0,sparam,OBJPROP_STATE,false); } if(sparam=="cws.S") { NowOpenOrder(1,0.01,500,800,"s1"); ObjectSetInteger(0,sparam,OBJPROP_STATE,false); } } } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- } //+------------------------------------------------------------------+ //| //+------------------------------------------------------------------+ ulong NowOpenOrder(ENUM_ORDER_TYPE type, double lots, double sl=0, double tp=0, string com="") { double ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK); double bid=SymbolInfoDouble(Symbol(),SYMBOL_BID); double point=SymbolInfoDouble(Symbol(),SYMBOL_POINT); int digits=(int)SymbolInfoInteger(Symbol(),SYMBOL_DIGITS); MqlTradeRequest request= {}; MqlTradeResult result= {}; request.action=TRADE_ACTION_DEAL; request.symbol=Symbol(); request.type=type; request.volume=lots; request.deviation=20; request.comment=com; if(type==ORDER_TYPE_BUY) { request.price=ask; if(sl>0) request.sl=NormalizeDouble(request.price-sl*point,digits); if(tp>0) request.tp=NormalizeDouble(request.price+tp*point,digits); } if(type==ORDER_TYPE_SELL) { request.price=bid; if(sl>0) request.sl=NormalizeDouble(request.price+sl*point,digits); if(tp>0) request.tp=NormalizeDouble(request.price-tp*point,digits); } if(!OrderSendAsync(request,result)) PrintFormat("OrderSend error %d",GetLastError()); return(result.order); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void ButtonCreate(string nm,string txt,int x,int y,int w,int h) { ObjectCreate(0,nm,OBJ_BUTTON,0,0,0); ObjectSetString(0,nm,OBJPROP_TEXT,txt); ObjectSetInteger(0,nm,OBJPROP_XDISTANCE,x); ObjectSetInteger(0,nm,OBJPROP_YDISTANCE,y); ObjectSetInteger(0,nm,OBJPROP_XSIZE,w); ObjectSetInteger(0,nm,OBJPROP_YSIZE,h); ObjectSetString(0,nm,OBJPROP_FONT,"Microsoft YaHei"); ObjectSetInteger(0,nm,OBJPROP_FONTSIZE,16); ObjectSetInteger(0,nm,OBJPROP_BGCOLOR,DodgerBlue); ObjectSetInteger(0,nm,OBJPROP_COLOR,White); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { ObjectsDeleteAll(0,"cws*",0,-1); } //+------------------------------------------------------------------+
1条评论
2022-11-05 00:07:04 回复TA