Please Help, N Step Hedging works only with 1 pair

 
void Long()
  {

   if(MathAbs(MaxLossPerOrder) > 0)
     {
      for(L_Count = OrdersTotal(); L_Count >= 0; L_Count--)
        {
         RefreshRates();
         OrderSelect(L_Count, SELECT_BY_POS, MODE_TRADES);
         if(OrderSymbol() == Symbol())
           {
            if(OrderType() == OP_BUY && OrderMagicNumber() == LongMagicNumber && OrderProfit() <= MathAbs(MaxLossPerOrder) * (-1))
              {
               OrderClose(OrderTicket(), OrderLots(), Bid, L_Slippage, White);
              }
            if(OrderType() == OP_SELL && OrderMagicNumber() == LongMagicNumber && OrderProfit() <= MathAbs(MaxLossPerOrder) * (-1))
              {
               OrderClose(OrderTicket(), OrderLots(), Ask, L_Slippage, White);
              }
           }
        }
     }

   L_Profit = 0;
   L_OpenOrders = 0;
   for(L_Count = 0; L_Count < OrdersTotal(); L_Count++)
     {
      OrderSelect(L_Count, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == LongMagicNumber)
        {
         L_OpenOrders++;
         L_Profit = L_Profit + OrderProfit();
        }
     }

   L_PipValue = MarketInfo(Symbol(), MODE_TICKVALUE);
   if(L_PipValue == 0)
     {
      L_PipValue = 5;
     }

   if(L_PreviousOpenOrders > L_OpenOrders)
     {
      for(L_Count = OrdersTotal(); L_Count >= 0; L_Count--)
        {
         OrderSelect(L_Count, SELECT_BY_POS, MODE_TRADES);
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == LongMagicNumber && OrderType() == OP_BUY)
           {
            int m_Ticket = OrderTicket();
            OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), L_Slippage, Blue);
            Print("Closing Buy Order ", m_Ticket);
            return (0);
           }
        }
     }

   L_PreviousOpenOrders = L_OpenOrders;
   if(L_OpenOrders >= LongMaxTrades)
     {
      L_ContinueOpening = False;
     }
   else
     {
      L_ContinueOpening = True;
     }

   if(L_LastPrice == 0)
     {
      for(L_Count = 0; L_Count < OrdersTotal(); L_Count++)
        {
         OrderSelect(L_Count, SELECT_BY_POS, MODE_TRADES);
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == LongMagicNumber && OrderType() == OP_BUY)
           {
            L_LastPrice = OrderOpenPrice();
            L_OrderType = 2;
           }
        }
     }

   if(L_OpenOrders < 1)
     {
      L_OrderType = OpenOrdersBasedOnTrendRSX();
     }

// Here comes the fun part we all waited for where we update those trailing stops....yippeekyeah!!

   for(L_Count = OrdersTotal(); L_Count >= 0; L_Count--)
     {
      OrderSelect(L_Count, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == LongMagicNumber && OrderType() == OP_BUY)
        {
         if(LongTrailingStop > 0 && (Bid - OrderOpenPrice() >= (LongTrailingStop + LongPips) * Point) && (OrderStopLoss() < (Bid - Point * LongTrailingStop)))
           {
            OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * LongTrailingStop, OrderClosePrice() + LongTakeProfit * Point + LongTrailingStop * Point, 800, Yellow);
            return (0);
           }
        }
     }

   L_Profit = 0;
   L_LastTicket = 0;
   L_LastType = 0;
   L_LastClosePrice = 0;
   L_LastLots = 0;
   for(L_Count = 0; L_Count < OrdersTotal(); L_Count++)
     {
      OrderSelect(L_Count, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == LongMagicNumber && OrderType() == OP_BUY)
        {
         L_LastTicket = OrderTicket();
         L_LastType = OP_BUY;
         L_LastClosePrice = OrderClosePrice();
         L_LastLots = OrderLots();
         L_Profit = L_Profit + OrderProfit();
        }
     }

   if(L_OpenOrders >= (LongMaxTrades - LongOrderstoProtect) && LongAccountProtection == true)
     {
      if(L_Profit >= LongSecureProfit)
        {
         OrderClose(L_LastTicket, L_LastLots, L_LastClosePrice, L_Slippage, Yellow);
         L_ContinueOpening = False;
         return (0);
        }
     }

   if(L_OrderType == 2 && L_ContinueOpening && ((L_LastPrice - Ask) >= LongPips * Point || L_OpenOrders < 1))
     {
      BuyPrice = Ask;
      L_LastPrice = 0;
      if(LongTakeProfit == 0)
        {
         L_tp = 0;
        }
      else
        {
         L_tp = BuyPrice + LongTakeProfit * Point;
        }
      if(LongInitialStop == 0)
        {
         L_sl = 0;
        }
      else
        {
         L_sl = NormalizeDouble(BuyPrice - LongInitialStop * Point - (LongMaxTrades - L_OpenOrders) * LongPips * Point, Digits);
        }
      if(L_OpenOrders != 0)
        {
         L_OrderLotSize = G_OrderLotSize;
         for(L_Count = 1; L_Count <= L_OpenOrders; L_Count++)
           {
            if(UseFiboLotSizeProgression == true)
              {
               L_OrderLotSize = MathRound(MathPow(1.6180339, L_Count + 1) / MathSqrt(5)) * G_OrderLotSize;
              }
            if(UseFiboLotSizeProgression == false && LongMaxTrades > 12)
              {
               L_OrderLotSize = NormalizeDouble(L_OrderLotSize * 1.5, 2);
              }
            if(UseFiboLotSizeProgression == false && LongMaxTrades < 12)
              {
               L_OrderLotSize = NormalizeDouble(L_OrderLotSize * 2, 2);
              }
           }
        }
      else
        {
         L_OrderLotSize = G_OrderLotSize;
        }
      OrderSend(Symbol(), OP_BUY, L_OrderLotSize, BuyPrice, L_Slippage, L_sl, L_tp, "Portalindo Buy", LongMagicNumber, 0, Blue);
      ArrLotBuy[totalBuy] = L_OrderLotSize;
      ArrSlBuy[totalBuy] = L_sl;
      ArrTpBuy[totalBuy] = L_tp;
      ArrBidBuy[totalBuy] = BuyPrice;
      totalBuy = totalBuy + 1;
      L_OpenOrders = L_OpenOrders + 1;

      if(EnableHedging != 0)
        {

         if(L_OpenOrders >= FirstHedging)
           {
            int OrderTotal = L_OpenOrders;

            for(L_Count = OrderTotal; L_Count >= L_OpenOrders - HedgingStep; L_Count--)
              {
               if(L_OpenOrders - HedgingStep >= L_Count)
                 {
                  OrderSelect(L_OpenOrders - 1, SELECT_BY_POS, MODE_TRADES);

                  double sell_price_n = Bid;
                  double S_sl_n = ArrTpBuy[totalBuy - 1] + spread;
                  double S_tp_n = ArrSlBuy[totalBuy - HedgingStep];

                  RefreshRates();

                  OrderSend(Symbol(), OP_SELL, ArrLotBuy[totalBuy - HedgingStep], sell_price_n, S_Slippage, S_sl_n, S_tp_n, "Portalindo Hedging Sell", SellHedgingMagicNumber, 0, Red);

                  totalHedgingSell = totalHedgingSell + 1;
                 }
              }

            int CountBuyHedging = 0;
            for(L_Count = OrderTotal + totalModifySell; L_Count >= 0; L_Count--)
              {
               OrderSelect(L_Count, SELECT_BY_POS, MODE_TRADES);

               if(OrderMagicNumber() == SellHedgingMagicNumber)
                 {
                  CountBuyHedging = CountBuyHedging + 1;
                  OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(ArrTpBuy[totalBuy - 1] + spread, Digits), OrderTakeProfit(), 800, Yellow);

                  totalModifySell = totalModifySell + 1;
                 }
              }

            if(CountBuyHedging == 1)
              {
               string subjectMail = (int)AccountInfoInteger(ACCOUNT_LOGIN) + " Pair " + Symbol() + " Hedging";
               string bodyMail = "";
               bool res = SendMail(subjectMail, bodyMail);
              }
           }
        }

      return (0);
     }

   return (0);
  }


1 Pair Forward Test Screen Shot

Forward Test

The Journal


2 Pairs Testing


It works with 1 pair on strategy tester or forward test, but when i add another pair, the hedging works with right SL, but when 2nd hedging open, it doesnt modify the whole hedging SL as i want it to be and there's no error or activity on journal. The blue background is the modification that i made, thanks for replying

 
Anyone? Im still looking for an answer
 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  3. Use the debugger or print out your variables, including _LastError and prices and . Do you really expect us to debug your code for you?

  4.      for(L_Count = OrdersTotal(); L_Count >= 0; L_Count--)
            {
             OrderSelect(L_Count, SELECT_BY_POS, MODE_TRADES);
    If there are n orders their positions are [0 … n-1]. Your first OrderSelect always fails. Always check your return codes for errors.

  5. Your code
       if(L_OpenOrders >= LongMaxTrades)
         {
          L_ContinueOpening = False;
         }
       else
         {
          L_ContinueOpening = True;
         }
    Simplified
    L_ContinueOpening = L_OpenOrders < LongMaxTrades;

  6.            int OrderTotal = L_OpenOrders;
    
                for(L_Count = OrderTotal; L_Count >= L_OpenOrders - HedgingStep; L_Count--)
                  {
                   if(L_OpenOrders - HedgingStep >= L_Count)
                     {
                      OrderSelect(L_OpenOrders - 1, SELECT_BY_POS, MODE_TRADES);
    L_OpenOrders is a count. It is not a position index.
 

1. Sorry, i thought i could paste more line with source code button

2. My mistake, didnt realize my request is on mt5 root

3. i have use print to check any output when debugging the code, will try your suggestion with lasterror

4. I will recheck that thanks

5. Thanks, im going try to simplified it

6. I will recheck that thanks