Trailing Stop Loss based on actual price

 

Hi everyone, I am looking for a trailing stop loss which is activated after after reaching a certain value and it moves repetitively.

I try to explain with an example: I buy XAU/USD from 2025.0, SL to 2023.0 (20PIPS) and traling stop loss from 2028.1 (30 PIPS). When the profit reach 31 PIPS (2028.1) the bot put the stop loss to 30 PIPS (2028.00), when the price reach 2029.1 (41 PIPS) the stop loss becomes 2029.0 (40 PIPS) and so on.

Is it possibile to find or create? I tryed to wathc some videos but all trailing stop are different from my idea

 

You can try this:

void trail_stop(int _start, int _step, int _offset)
{
   for (int i=0;i<PositionsTotal();i++)
   {
      
      ulong ticket = PositionGetTicket(i);
      string symbol=PositionGetString(POSITION_SYMBOL);
      double Bid=SymbolInfoDouble(symbol, SYMBOL_BID);
      double Ask=SymbolInfoDouble(symbol, SYMBOL_ASK);
      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
      {
         if(Bid>=PositionGetDouble(POSITION_PRICE_OPEN)+_start*_Point)
         {
            double new_sl = Bid-_offset*_Point;
            new_sl=NormalizeDouble(floor(new_sl/_Point)*_Point, _Digits);
            if(new_sl>=PositionGetDouble(POSITION_SL)+_step*_Point || PositionGetDouble(POSITION_SL)==0)
               trade.PositionModify(ticket , new_sl, PositionGetDouble(POSITION_TP));
         }
      }
      else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
      {
         if(Ask<=PositionGetDouble(POSITION_PRICE_OPEN)-_start*_Point)
         {
            double new_sl = Ask+_offset*_Point;
            new_sl=NormalizeDouble(floor(new_sl/_Point)*_Point, _Digits);
            if(new_sl<=PositionGetDouble(POSITION_SL)-_step*_Point || PositionGetDouble(POSITION_SL)==0)
               trade.PositionModify(ticket , new_sl, PositionGetDouble(POSITION_TP));
         }
      }
   }
}

 
Yashar Seyyedin #:

You can try this:

It say that there are 2 errors when I try to add
 

Hi

You need to declare an instance of “trade” - class from Trade/mqh which is used for trading and trade management. Add this at the top of your code:

#include <Trade\Trade.mqh>
CTrade *trade;

And this in OnInit:
trade = new CTrade();

Have a nice weekend

Reason: