使用 MQL5 向专家顾问添加追踪止损

Adding a trailing Stop loss to an expert advisor using MQL5

您好,我是 MQL5 的新手,我想为我的专家顾问添加追踪止损,但由于某些原因它没有添加。这是代码:

 if(PositionSelect(_Symbol) && UseTrailingStop == true)
  {
   double TrailingStop = (atr[1] * 3) + close[1];
   Trail.TrailingStop(_Symbol,TrailingStop,0,0);
  } 

请注意,close[1] 是前一根柱线的收盘价,atr[1] 是真实波动幅度的平均值。我做错了什么?????

到此为止:希望这对您有所帮助。

//--- trailing position  
   for(i=0;i<PositionsTotal();i++)
     {
      if(Symbol()==PositionGetSymbol(i))
        {
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
           {
            sl=MathMax(PositionGetDouble(POSITION_PRICE_OPEN)+Spread*_Point,Bid-SL*_Point);

            if(sl>PositionGetDouble(POSITION_SL) && (Bid-StopLevel*_Point-Spread*_Point)>PositionGetDouble(POSITION_PRICE_OPEN))
              {
               request.action = TRADE_ACTION_SLTP;
               request.symbol = _Symbol;
               request.sl = NormalizeDouble(sl,_Digits);
               request.tp = PositionGetDouble(POSITION_TP);
               OrderSend(request,result);
               if(result.retcode==10009 || result.retcode==10008) // request executed
                  Print("Moving Stop Loss of Buy position #",request.order);
               else
                 {
                  Print(ResultRetcodeDescription(result.retcode));
                  return;
                 }
               return;
              }
           }

         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
           {
            sl=MathMin(PositionGetDouble(POSITION_PRICE_OPEN)-Spread*_Point,Ask+SL*_Point);

            if(sl<PositionGetDouble(POSITION_SL) && (PositionGetDouble(POSITION_PRICE_OPEN)-StopLevel*_Point-Spread*_Point)>Ask)
              {
               request.action = TRADE_ACTION_SLTP;
               request.symbol = _Symbol;
               request.sl = NormalizeDouble(sl,_Digits);
               request.tp = PositionGetDouble(POSITION_TP);
               OrderSend(request,result);
               if(result.retcode==10009 || result.retcode==10008) // request executed
                  Print("Moving Stop Loss of Sell position #",request.order);
               else
                 {
                  Print(ResultRetcodeDescription(result.retcode));
                  return;
                 }
               return;
              }
           }
        }
     }

您可以探索 MQL5 向导的工作原理。它非常强大。有了它,您可以在两分钟内创建一个 EA 而无需编程,但您也可以添加自己的指标和 类。它带有一系列追踪止损 类。 这里有一些关于向导的文章。

basic intro on the wizard

MQL5 WIZARD FOR DUMMIES

CREATE YOUR OWN TRADING ROBOT IN 6 STEPS!

MQL5 WIZARD: NEW VERSION