zhuyz9018 发表于 2018-8-7 23:39:39

我自己编程的一个平衡止损加移动止损脚本,总报错请大大帮忙看看

/*我刚开始学习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);
}
//+------------------------------------------------------------------+


小美 发表于 2018-9-9 12:00:47

谢谢楼主,共同发展

zypew 发表于 2020-5-18 19:02:53

好好 学习了 确实不错

hdmgo 发表于 2020-6-12 14:08:09

没看完~~~~~~ 先顶,好同志

始终没人懂 发表于 2020-6-30 19:06:14

我是来刷分的,嘿嘿

bf91frg4 发表于 2020-7-25 18:01:51

谢谢楼主分享

毒蘑菇 发表于 2020-8-4 22:21:42

谢谢楼主分享

随遇而安 发表于 2020-8-5 15:51:06

帮你顶下哈!!

Gold_digger 发表于 2020-8-17 19:13:06

帮你顶下哈!!

refuse 发表于 2020-8-29 16:35:07

帮你顶下哈!!
页: [1] 2
查看完整版本: 我自己编程的一个平衡止损加移动止损脚本,总报错请大大帮忙看看