coding of collective trailing Stop Loss shows erratic behaviour.

 

Hello there,


I'm trying to code a collective SL for all the buy positions, that is triggered once that the outcome is a positive collective Profit, and updated just in increasing sense (for buy), decreasing sense (for sell).

Of course such procedure relies on the correct evaluation of the positions collective profit and volume, which I coded as:

   buypos=0;
   sellpos=0;
   buyvol=0.0;
   sellvol=0.0;
   buyreq=0.0;
   sellreq=0.0;
for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
        {
         if(m_position.PositionType()==POSITION_TYPE_BUY)
           {
            buypos+=1;
            buyvol=buyvol+m_position.Volume();
            buyreq=buyreq+(m_position.Profit());
           }
         if(m_position.PositionType()==POSITION_TYPE_SELL)
           {
            sellpos+=1;
            sellvol=sellvol+m_position.Volume();
            sellreq=sellreq+(m_position.Profit());
           }
        }

such code is called every tick, in order to obtain updated value for buy equity (buyreq) and buy volume (buyvol). The updating of the SL is thus called:


 if(((buyreq-(double)SL*buyvol)>0.0) )
     {
      for(int i=PositionsTotal()-1; i>=0; i--)
        {
         if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
           {
            if(m_position.PositionType()==POSITION_TYPE_BUY)
              {
               ulong ticket = PositionGetTicket(i);
               PositionSelectByTicket(ticket);
               double tp = PositionGetDouble(POSITION_TP);                                       // symbol
               double price=currentBid;
               double dprice=(SL/(double)pipscale);
               double slprice=(price-dprice);                                           // order triggering price
               //double point=SymbolInfoDouble(_Symbol,SYMBOL_POINT);                // value of point
               //int digits=SymbolInfoInteger(_Symbol,SYMBOL_DIGITS);
               double sl    =NormalizeDouble(slprice,digits);
               if (sl  > PositionGetDouble(POSITION_SL)){
               myTradingControlPanel.PositionModify(ticket, sl, tp);}
             } 
           }
        }
     }

being SL the distance in pips below the actual price. Now, in the backtesting the code works nicely, but sometimes just "forget" to update the SL, or to enforce it at all. It does not return error messages, it just not executes that part of the code. This could be due to the wrong evaluation of buyreq or buyvol. As a result, the behaviour is erratic and lead to a crash and burn, since obviously SL is what saves your life.

SL is not set

I tried different codes for calculating the profit but the problem is still there. Please help. 

 
MetHarlock75:

Hello there,


I'm trying to code a collective SL for all the buy positions, that is triggered once that the outcome is a positive collective Profit, and updated just in increasing sense (for buy), decreasing sense (for sell).

Of course such procedure relies on the correct evaluation of the positions collective profit and volume, which I coded as:

such code is called every tick, in order to obtain updated value for buy equity (buyreq) and buy volume (buyvol). The updating of the SL is thus called:


being SL the distance in pips below the actual price. Now, in the backtesting the code works nicely, but sometimes just "forget" to update the SL, or to enforce it at all. It does not return error messages, it just not executes that part of the code. This could be due to the wrong evaluation of buyreq or buyvol. As a result, the behaviour is erratic and lead to a crash and burn, since obviously SL is what saves your life.


I tried different codes for calculating the profit but the problem is still there. Please help. 

it could be a bad use of CopyBuffer in an other part of the code...removing it, apparently stopped this chaotic behaviour. But I'm not 100% sure. 

 
Holy shit I got it. The condition 
sl  > PositionGetDouble(POSITION_SL)

in case of sell position becames:

sl  < PositionGetDouble(POSITION_SL)

which is never true starting from 0, which is SL default value. That's why the code of SL updating for sell positions was not executed. 

 

Hi

When you modify the SL you should also check stopelevel/freezelevel – when new sl is t close to current close price, modify request may be blocked.

This should be analyzed on the results for the trades (not visible on graph). Check the logs.

Also for the future – when you are going to use those functions on the real account – add some limitations for the symbol of the trades – to choose only chosen symbol (otherwise calculations won’t be correct if you would have a lot of different trades from different pairs).

Best Regards