0
votes

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?????

2
I think the easier solution will be extend the CTrailing and do the calculation in the two methods that you will overwrite there (CheckTrailingStopLong and CheckTrailingStopShort).Ricardo Lucca

2 Answers

0
votes

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;
              }
           }
        }
     }
0
votes

You could explore how the MQL5 Wizard works. It is very powerful. With it, you can create an EA without programming anything in two minutes but you can also add your own indicators and classes. It comes with a series of Trailing Stop classes. Here are some articles about the Wizard.

basic intro on the wizard

MQL5 WIZARD FOR DUMMIES

CREATE YOUR OWN TRADING ROBOT IN 6 STEPS!

MQL5 WIZARD: NEW VERSION