Seeking some help, why Trailing Stop Loss loop trails only one [oldest] position at a time, instead of trailing all open position(s)

 

Hi

I am using Class for trade operations, wherein method is defined with conditions for trail StopLoss. This method sends request to Trade.POSITION_TrailSL method in Trade Class.

The problem I am facing is, this method tracks oldest open position only and if other positions (same Symbol / Magic No) are opened, it does not track them.

Once the oldest position is hit by SL, then it start tracking the next one.

void Cs_MACD::Trail_SL()
  {
    double trailSL_Buy  = 0;
    double trailSL_Sell = 0;
  //--- Refresh Data Buffer
    mql_EMA   EMA[];        EMA_Main.Get_DataEMA(5,EMA);

  //| Iterate over 'all' open positions
    int positionTotal = PositionsTotal();                       // get number of open positions
    for(int i = 0; i < positionTotal; i++)
      {
          ulong  pos_Ticket           = PositionGetTicket(i);
          string pos_Symbol           = PositionGetString(POSITION_SYMBOL);
          ulong  pos_Magic            = PositionGetInteger(POSITION_MAGIC);
          double pos_PriceOpen        = PositionGetDouble(POSITION_PRICE_OPEN);
          double pos_SL               = PositionGetDouble(POSITION_SL);
          double pos_TP               = PositionGetDouble(POSITION_TP);
          ENUM_POSITION_TYPE pos_Type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);

        if((pos_Magic == m_MagicNo) && (pos_Symbol == m_Symbol))
          {
            if(pos_Type == POSITION_TYPE_BUY)
              {
              //--- Check (EntryPrice - Bid()) > Minimum 'points' to start trailing SL
                double priceGap = NormalizeDouble(MathAbs(pos_PriceOpen - Rates_Main.Bid()),_Digits);
                if(priceGap > Auxiliary.Get_PipValue(m_TrailStartPoints))
                  {
                    Get_Lowest(m_TF_Main,1,3);  // refresh m_Lowest_Main structure array
                    trailSL_Buy = MathMin(EMA[1].slowSMA,m_Lowest_Main.price);
                  }
              //--- CHECK if pos_SL = 0 'or' > current pos_SL
                if((pos_SL == 0) || (trailSL_Buy > pos_SL))
                  Trade.POSITION_TrailSL(pos_Symbol,pos_Magic,NormalizeDouble(trailSL_Buy,_Digits));			// it Loops only for the oldest position
              } // END Of if..loop for modify Buy Position SL
            else if(pos_Type == POSITION_TYPE_SELL)
              {
              //--- Check (EntryPrice - Ask()) > Minimum 'points' to start trailing SL
                double priceGap = NormalizeDouble(MathAbs(pos_PriceOpen - Rates_Main.Ask()),_Digits);
                if(priceGap > Auxiliary.Get_PipValue(m_TrailStartPoints))
                  {
                    Get_Highest(m_TF_Main,1,3);  // refresh m_Highest_Main structure array
                    trailSL_Sell = MathMax(EMA[1].slowSMA,m_Highest_Main.price);
                  }
              //--- CHECK if pos_SL = 0 'or' < current pos_SL
                if((pos_SL == 0) || (trailSL_Sell < pos_SL))
                  Trade.POSITION_TrailSL(pos_Symbol,pos_Magic,NormalizeDouble(trailSL_Sell,_Digits));			// it loops only for the oldest position
              } // END Of if..loop for modify SELL Position SL
          } // END Of if..loop to match Magic Number
    //---
      } // END If for..loop to iterate position totals
  } // END Of method Trail_SL()
//+----------------------------------------------------------------------------------------------------------+
//| METHOD:       POSITION_TrailSL()
//| APPLICATION:  'PRIVATE' method to trail StopLoss of an existing Open Position by Symbol + MagicNo
//+----------------------------------------------------------------------------------------------------------+
bool CTrade::POSITION_TrailSL(const string pSymbol,const ulong pStrategyMagic,const double pTrailSL)
  {
    if(Is_MqlStopped(__FUNCTION__))
      {
        return(false);
      }
  //--- Check position's existence
    if(!Select_Position(pSymbol,pStrategyMagic)) // if netting, uses pSymbol else uses pSymbol & pStrategyMagic
      return(false);
  //--- clean
    Set_ZeroMemory();  // Erase old data, if any in m_Request, m_Result & m_check_result structure's memory
  //--- setting request
    m_Request.action    = TRADE_ACTION_SLTP;
    m_Request.symbol    = pSymbol;
    m_Request.sl        = pTrailSL;
    m_Request.tp        = PositionGetDouble(POSITION_TP);
    m_Request.position  = PositionGetInteger(POSITION_TICKET);
    m_Request.magic     = pStrategyMagic;
    m_Request.comment   = "TrailSL #"+(string)PositionGetInteger(POSITION_TICKET);
  //--- action and return the result
    Print(__FUNCTION__,": TrailSL ",pTrailSL," Request SL ",m_Request.sl);
    return(OrderSend(m_Request,m_Result));
  } // END Of POSITION_TrailSL() method
Reason: