Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

algorithmic trading - Adding a trailing Stop loss to an expert advisor using MQL5

Hi I'm new to MQL5 and I wanted to add a trailing stop loss to my expert advisor but some reason it does not add. Here is the code:

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

Please note that close[1] is for the close price of the previous bar and atr[1] is for the value of the average true range. What am i doing wrong?????

question from:https://stackoverflow.com/questions/65859574/adding-a-trailing-stop-loss-to-an-expert-advisor-using-mql5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There you go: hope this is helpful.

//--- 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;
              }
           }
        }
     }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...