|
/*我刚开始学习EA ,想编程一个 1.当价格等于或大于(入场价减去止损价或加上止损价)时,止损移动到 盈亏平衡点加减一点处(因有手续费)。 2.按照20EMA均线上下20个点进行移动止损,多单是20EMA之下20个点进行移动止损,空单相反。 注:EA脚本存在的问题:在我能正常运行并且编程无报错的情况下,为什么挂上EA脚本没有根据我的条件进行保本和移动止损,并且当价格达到条件时会报错,而不执行,希望各位大大前辈帮忙看下,帮我进行修改,谢谢!*/ //+------------------------------------------------------------------+ //| EMA保本追踪止损程序.mq4 | //| Copyright 2015, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2015, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict //---- input parameters extern double 平衡止损增加点数=10; extern double 追踪止损=1; extern double Magic=0; extern string 参数说明="ZHUCHAOJIAN"; extern double MAPeriod=20; //指数均线周期 extern double CloseSpred = 200; //盈亏平衡后止损离均线的点数 int start() { //---- //追踪止损 _MoveStop(Magic, 追踪止损); //设置止损 _StopLoss(Magic,平衡止损增加点数); //---- return(0); } //+------------------------------------------------------------------+ //| _MoveStop 移动止损函数 function | //+------------------------------------------------------------------+ int _MoveStop(int MAGIC, int MOVE)//_MoveStop(MAGIC, MOVE); { //---- if (MOVE<=0) return(0); double MoveStopPrice; int Ma=iMA(NULL,5,MAPeriod,0,MODE_EMA,PRICE_CLOSE,1); for ( int z = OrdersTotal() - 1; z >= 0; z -- ) { if ( !OrderSelect( z, SELECT_BY_POS ) ) { Print("OrderSelect(", z, ",SELECT_BY_POS) - Error #",GetLastError() ); continue; } if (OrderSymbol()!=Symbol())continue; if (OrderMagicNumber() != MAGIC )continue; switch (OrderType()) { case OP_BUY : { MoveStopPrice=NormalizeDouble(Ma-CloseSpred*Point,Digits); if (MoveStopPrice>OrderStopLoss() && OrderOpenPrice()<=OrderStopLoss()) { if(!OrderModify(OrderTicket(),OrderOpenPrice(),MoveStopPrice,OrderTakeProfit(),OrderExpiration())) { Alert("MoveStop_OrderModify Error #",GetLastError()); return(-1); } } continue; } case OP_SELL: { MoveStopPrice=NormalizeDouble(Ma+CloseSpred*Point,Digits); if (MoveStopPrice<OrderStopLoss() && OrderOpenPrice()>=OrderStopLoss()) { if(!OrderModify(OrderTicket(),OrderOpenPrice(),MoveStopPrice,OrderTakeProfit(),OrderExpiration())) { Alert("MoveStop_OrderModify Error #",GetLastError()); return(-1); } } continue; } default: continue; } } //---- return(0); } //+------------------------------------------------------------------+ //| _MoveStop 移动止损函数 function | //+------------------------------------------------------------------+ int _StopLoss(int MAGIC, int SL)//_MoveStop(MAGIC, MOVE); { //---- if (SL<=0) return(0); double StopLoss; for ( int z = OrdersTotal() - 1; z >= 0; z -- ) { if ( !OrderSelect( z, SELECT_BY_POS ) ) { Print("OrderSelect(", z, ",SELECT_BY_POS) - Error #",GetLastError()); continue; } if (OrderSymbol()!=Symbol())continue; if (OrderMagicNumber() != MAGIC )continue; switch (OrderType()) { case OP_BUY : { StopLoss=NormalizeDouble(OrderOpenPrice()+SL*Point,Digits); if (Ask-OrderOpenPrice()>OrderOpenPrice()-OrderStopLoss() && OrderOpenPrice()>OrderStopLoss()) { if(!OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss,OrderTakeProfit(),OrderExpiration())) { Alert("StopLoss_OrderModify Error #",GetLastError()); } } continue; } case OP_SELL: { StopLoss=NormalizeDouble(OrderOpenPrice()-SL*Point,Digits); if (OrderOpenPrice()-Bid>OrderStopLoss()-OrderOpenPrice() && OrderOpenPrice()<OrderStopLoss()) { if(!OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss,OrderTakeProfit(),OrderExpiration())) { Alert("StopLoss_OrderModify Error #",GetLastError()); } } continue; } default: continue; } } //---- return(0); } //+------------------------------------------------------------------+ |
EA发布区