Selecting the last order opened of the chart in MQL4

 

I'm a beginner in MQL4, I just want to ask if the code I've written is correct - or is there a better way to write it.

It's working well as expected on backtests, but things go wrong when I try it on a demo bot with multiple charts.

I want to get info about the last order's price of the same chart:

   int AdditionalOrders = 0;

   for (int n=0;n<OrdersTotal();n++) {
      if (OrderSymbol() == Symbol()) {
         Select = OrderSelect(n, SELECT_BY_POS, MODE_TRADES);
            if (OrderMagicNumber() != MagicNumber && OrderSymbol() != Symbol()) continue;
            if (OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol())
               AdditionalOrders++;
      }
   }
  
  
  double LastPrice;
  LastPrice = OrderOpenPrice();

Also, I added the "AdditonalOrders" part because I wanted to count how many Martingale positions are open. The magic number of the martingale positions and the initial entries are different.

 
Austin John Villarico Salvador:

I'm a beginner in MQL4, I just want to ask if the code I've written is correct - or is there a better way to write it.

It's working well as expected on backtests, but things go wrong when I try it on a demo bot with multiple charts.

I want to get info about the last order's price of the same chart:

Also, I added the "AdditonalOrders" part because I wanted to count how many Martingale positions are open. The magic number of the martingale positions and the initial entries are different.

     double lastPrice = 0;
        
     for (i = OrdersTotal()-1; i>=0; i--)
      {
       if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
       if (OrderSymbol()      != Symbol())            continue;
       if (OrderMagicNumber() != MagicNumber)         continue;

       lastPrice = OrderOpenPrice();
       if(lastPrice!=0) break;
      }
 
  1. Don't use negative logic, e.g. if not condition continue. Simplify.
         double lastPrice = 0;
            
         for (i = OrdersTotal()-1; i>=0; i--) if (
            OrderSelect(i,SELECT_BY_POS)
         && OrderMagicNumber() == MagicNumber
         && OrderSymbol()      == _Symbol           // String comparison is the slowest
         ){
           lastPrice = OrderOpenPrice();
           if(lastPrice!=0) break;
          }

  2. Hedging, grid trading, same as Martingale.
              Martingale, Hedging and Grid : MHG - General - MQL5 programming forum (2016)

    Martingale, guaranteed to blow your account eventually. If it's not profitable without, it is definitely not profitable with.
              Martingale vs. Non Martingale (Simplified RoR vs Profit and the Illusions) - MQL5 programming forum (2015)

    Why it won't work:
              Calculate Loss from Lot Pips - MQL5 programming forum (2017)
              THIS Trading Strategy is a LIE... I took 100,000 TRADES with the Martingale Strategy - YouTube (2020.12.12)

 
William Roeder #:
  1. Don't use negative logic, e.g. if not condition continue. Simplify.
  2. Hedging, grid trading, same as Martingale.
              Martingale, Hedging and Grid : MHG - General - MQL5 programming forum (2016)

    Martingale, guaranteed to blow your account eventually. If it's not profitable without, it is definitely not profitable with.
              Martingale vs. Non Martingale (Simplified RoR vs Profit and the Illusions) - MQL5 programming forum (2015)

    Why it won't work:
              Calculate Loss from Lot Pips - MQL5 programming forum (2017)
              THIS Trading Strategy is a LIE... I took 100,000 TRADES with the Martingale Strategy - YouTube (2020.12.12)

Thanks! It works well on backtests, I'll just have to see its behavior on demo accounts with multiple charts.

I'm aware of the dangers of Martingale. I'm just practicing my MQL4 coding skills in the meantime while looking for better strategies to make as an EA. But thanks for the heads up.