Virtual Trailing StopLoss for multiple Positions of the same type

 

Hi,

I am trying to update and reference multiple SL values using a function TrailingStop()

When I have two positions open and I iterate through them selecting ticket by index I am able to update the two different SL Values.

I do this for each position and store the SL value in an Array

double StopLossClose = 0.5;

double SLArr[2];

int OnInit()
  {   
      SLArr[0] = 0.00;
      SLArr[1] = 0.00; 
      return(INIT_SUCCEEDED); 
  }



void TrailingStop(double SL)
  {
   double posProfit = 0.0;
   int totPosi = PositionsTotal();
   double SLClose = SL;

   for(int count =0; count < totPosi; count++)
   {
      ulong TicketNo = PositionGetTicket(count); 				// Get Position Ticket number using the 'index' of the position.
      double slProfit = 0.0;
      double posPrice = 0.0;
      double posPOpen = 0.0;
      if(posiInfo.SelectByTicket(TicketNo))
      {
        if(posiInfo.PositionType()==POSITION_TYPE_BUY)
         {
            posProfit = PositionGetDouble(POSITION_PROFIT);  //0.25
            posPrice = PositionGetDouble(POSITION_PRICE_CURRENT);
            posPOpen = PositionGetDouble(POSITION_PRICE_OPEN);

            if(slProfit==0.0)                                                    //If no SL has been set for position set one
              {
               slProfit = posPrice-SLClose;
               SLArr[count] = slProfit;
              }    
            if(posPrice <= (SLArr[count]))                                       // Check to see if current position price is less than the value stored in Array if it is close position and reset SL value
            {
               Trade.PositionClose(TicketNo);
               SLArr[count] = 0.0;                                               //Reset the SL value to 0

            }
            
            if((posPrice-SLClose) > SLArr[count])                                //Check if position price minus SL step is greater than value stored in Array
            {
               if(posPrice > posPOpen && SLArr[count] < (posPrice-SLClose))      //If position price is greater than position opening price and Value stored in SL Array is less than             
                                                                                 //position price - SL Step.  If so Modify Value in Array
               {
                  slProfit = posPrice-SLClose;                                   //Keep Position Open and change Stop Level 
                  SLArr[count] = slProfit;
               }
               if(posPrice < SLArr[count] && PositionsTotal() > 0)               //If Position price falls below SL from Array close position and reset array value to 0
               {
                  Trade.PositionClose(TicketNo);
                  SLArr[count] = 0.0;                
                }
            }   
         }

//Repeat proceedure for Sell Positions
         if(posiInfo.PositionType()==POSITION_TYPE_SELL)
         {
            posProfit = PositionGetDouble(POSITION_PROFIT);  
            posPrice = PositionGetDouble(POSITION_PRICE_CURRENT);
            posPOpen = PositionGetDouble(POSITION_PRICE_OPEN);

             if(slProfit==0.0)                                                   //If no SL has been set for position set one
              {
               slProfit = posPrice-SLClose;
               SLArr[count] = slProfit;
              }    
            
             if(posPrice >= (SLArr[count]))                                      // Check to see if current position price is greater than the value stored in Array if it is close position and reset SL value
            {
               Trade.PositionClose(TicketNo);
               SLArr[count] = 0.0;                                               //Reset the SL value to 0

            }
            if((posPrice+SLClose) < SLArr[count])                                //Check if position price plus SL step is less than value stored in Array
            {
               if(posPrice < posPOpen && SLArr[count] > (posPrice + SLClose))    //If position price is less than position opening price and Value stored in SL Array is greater than             
                                                                                 //position price + SL Step.  If so Modify Value in Array 
               {
                  slProfit = posPrice + SLClose;                                 //Keep Position Open and change Stop Level              
                  SLArr[count] = slProfit;
               }
               if(posPrice > SLArr[count] && PositionsTotal() > 0)              //If Position price rises above SL from Array close position and reset array value to 0
               { 
                  Trade.PositionClose(TicketNo);
                  SLArr[count] = 0.00;
               }
            }   
        }
   }
      Comment(  "Position 1 SL: ", SLArr[0], 
                        "\nPosition 2 SL: ", SLArr[1]
                     );  
   }
  }
void OnTick()
        {
                if (PositionsTotal() < 2)
                {
                        Trade.Buy(0.01);
                        Trade.Sell(0.01);
                }
                
                TrailingStop(StopLossClose);
        }

The TrailingStop Function Successfully Iterates through position[0] and position[1] and I end up with correct values in the SLArr

But when I have closed one position and there is only one position left (normally position number 2 at index 1) the for iteration having decreased total positions only ever loops over position[0] which is now the old position[1]

As I reference the SL Array using the for iteration count I am only ever able to get the value for SLArr[0] while the value I need is stored in SLArr[1] 

 I am having trouble accessing the second value because the for loop is at position 0 and thus when I attempt to reference the SL value for the second value using the one function it is not able to recognize that position[0] has just been closed and to reference the values for position[1]


I hope this makes sense I recon if I was able to define what is happening better and describe it more correctly I would be half way to a solution.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

I have come up with a way to get around this but it requires that I am able to perform some function when a position is closed and so far I have not been able to find an easy way to run some code for example updating a variable when a position closes using the inbuilt stoploss's built into the CTrade Trade.Buy and Trade.Sell functions.

If anyone has an easy way to execute some code when a SL on a position is triggered and the position is closed let me know.