Can't get my orders count filtering by Magic Number, can someone help my code to check, please? (MT4)

 

Hello everyone, I hope someone can help to check my code.

I wrote my own function that counts the number of orders aggregated separately by the magic number.

However, when I ran the Strategy Tester, I found that the function might not work as well.

Here is what I wanted to do in the code. (I will write only part of the code, not all of it. The main function "MyCurrentOrders" is written at the last.)

======================================================

int Magic_M1 = 10099990;
int Magic_M1_H = 10099991;
int Magic_M5 = 50099990;
int Magic_M5_H = 50099991;
int Magic_M15 = 15099990;
int Magic_M15_H = 15099991;
int Magic_H1 = 60099990;
int Magic_H1_H = 60099991;
int Magic_H4 = 24099990;
int Magic_H4_H = 24099991;
int Magic_D1 = 14409990;
int Magic_D1_H = 14409991;
int Magic_W1 = 10080990;
int Magic_W1_H = 10080991;
int Magic_MN1 = 43200990;
int Magic_MN1_H = 43200991;
int Magic_STOP = 99999999;
int Magic_Manual = 0;

double Lots_M1_B =0.0, Lots_M1_S =0.0, Lots_M1_H =0.0, Lots_M5_B =0.0, Lots_M5_S =0.0, Lots_M5_H =0.0, Lots_M15_B =0.0, Lots_M15_S =0.0, Lots_M15_H =0.0, Lots_H1_B =0.0, Lots_H1_S =0.0, Lots_H1_H =0.0, Lots_H4_B =0.0, Lots_H4_S =0.0, Lots_H4_H =0.0, Lots_D1_B =0.0, Lots_D1_S =0.0, Lots_D1_H =0.0, Lots_W1_B =0.0, Lots_W1_S =0.0, Lots_W1_H =0.0, Lots_MN1_B =0.0, Lots_MN1_S =0.0, Lots_MN1_H =0.0, Lots_STOP =0.0, Lots_Manual =0.0;

MyCurrentOrders(Lots_M1_B, Lots_M1_S, Lots_M1_H, Lots_M5_B, Lots_M5_S, Lots_M5_H, Lots_M15_B, Lots_M15_S, Lots_M15_H, Lots_H1_B, Lots_H1_S, Lots_H1_H, Lots_H4_B, Lots_H4_S, Lots_H4_H, Lots_D1_B, Lots_D1_S, Lots_D1_H, Lots_W1_B, Lots_W1_S, Lots_W1_H, Lots_MN1_B, Lots_MN1_S, Lots_MN1_H, Lots_STOP, Lots_Manual);

double Lot_B[9];
ArrayInitialize(Lot_B,0);
Lot_B[0] = Lots_M1_B;
Lot_B[1]  = Lots_M5_B;
Lot_B[2]  = Lots_M15_B;
Lot_B[3]  = Lots_H1_B;
Lot_B[4]  = Lots_H4_B;
Lot_B[5]  = Lots_D1_B;
Lot_B[6]  = Lots_W1_B;
Lot_B[7]  = Lots_MN1_B;
Lot_B[8]  = 0;

double Lot_S[9];
ArrayInitialize(Lot_S,0);
Lot_S[0] = Lots_M1_S;
Lot_S[1]  = Lots_M5_S;
Lot_S[2]  = Lots_M15_S;
Lot_S[3]  = Lots_H1_S;
Lot_S[4]  = Lots_H4_S;
Lot_S[5]  = Lots_D1_S;
Lot_S[6]  = Lots_W1_S;
Lot_S[7]  = Lots_MN1_S;
Lot_S[8]  = 0;

//============================================================
//After this it seems the value of Lot_B[0] until Lot_B[8] and  Lot_S[0] until Lot_S[8] return 0, even it may have some BUY/SELL positions.
//Below is the code of my own function.
//============================================================ 

void MyCurrentOrders(double& lots_M1_B, double& lots_M1_S, double& lots_M1_H, double& lots_M5_B, double& lots_M5_S, double& lots_M5_H, double& lots_M15_B, double& lots_M15_S, double& lots_M15_H, double& lots_H1_B, double& lots_H1_S, double& lots_H1_H, double& lots_H4_B, double& lots_H4_S, double& lots_H4_H, double& lots_D1_B, double& lots_D1_S, double& lots_D1_H, double& lots_W1_B, double& lots_W1_S, double& lots_W1_H, double& lots_MN1_B, double& lots_MN1_S, double& lots_MN1_H, double& lots_STOP, double& lots_Manual)
{
   for(int i=0; i<OrdersTotal(); i++)
   {
      if(OrderSelect(i, SELECT_BY_POS) == false) break;
      if(OrderSymbol() != Symbol()) continue;
      if(OrderType() != OP_BUY || OrderType() != OP_SELL) continue;
      
      if(OrderType() == OP_BUY)
      {
         switch(OrderMagicNumber())
         {
            case 10099990:
               lots_M1_B += OrderLots();
               break;
            case 10099991:
               lots_M1_H += OrderLots();
               break;            

            case 50099990:
               lots_M5_B += OrderLots();
               break;
            case 50099991:
               lots_M5_H += OrderLots();
               break;            

            case 15099990:
               lots_M15_B += OrderLots();
               break;
            case 15099991:
               lots_M15_H += OrderLots();
               break;            

            case 60099990:
               lots_H1_B += OrderLots();
               break;
            case 60099991:
               lots_H1_H += OrderLots();
               break;            
            
            case 24099990:
               lots_H4_B += OrderLots();
               break;
            case 24099991:
               lots_H4_H += OrderLots();
               break;            
            
            case 14409990:
               lots_D1_B += OrderLots();
               break;
            case 14409991:
               lots_D1_H += OrderLots();
               break;            

            case 10080990:
               lots_W1_B += OrderLots();
               break;
            case 10080991:
               lots_W1_H += OrderLots();
               break;            

            case 43200990:
               lots_MN1_B += OrderLots();
               break;
            case 43200991:
               lots_MN1_H += OrderLots();
               break;            

            case 99999999:
               lots_STOP += OrderLots();
               break;

            case 0:
               lots_Manual += OrderLots();
               break;            
         }
      
      }

      else if(OrderType() == OP_SELL)
      {
         switch(OrderMagicNumber())
         {
            case 10099990:
               lots_M1_S -= OrderLots();
               break;
            case 10099991:
               lots_M1_H -= OrderLots();
               break;            

            case 50099990:
               lots_M5_S -= OrderLots();
               break;
            case 50099991:
               lots_M5_H -= OrderLots();
               break;            

            case 15099990:
               lots_M15_S -= OrderLots();
               break;
            case 15099991:
               lots_M15_H -= OrderLots();
               break;            

            case 60099990:
               lots_H1_S -= OrderLots();
               break;
            case 60099991:
               lots_H1_H -= OrderLots();
               break;            
            
            case 24099990:
               lots_H4_S -= OrderLots();
               break;
            case 24099991:
               lots_H4_H -= OrderLots();
               break;            
            
            case 14409990:
               lots_D1_S -= OrderLots();
               break;
            case 14409991:
               lots_D1_H -= OrderLots();
               break;            

            case 10080990:
               lots_W1_S -= OrderLots();
               break;
            case 10080991:
               lots_W1_H -= OrderLots();
               break;            

            case 43200990:
               lots_MN1_S -= OrderLots();
               break;
            case 43200991:
               lots_MN1_H -= OrderLots();
               break;            

            case 99999999:
               lots_STOP -= OrderLots();
               break;

            case 0:
               lots_Manual -= OrderLots();
               break;            
         }
      }      
   }
}
The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
What are the differences between the three modes of testing in MetaTrader 5, and what should be particularly looked for? How does the testing of an EA, trading simultaneously on multiple instruments, take place? When and how are the indicator values calculated during testing, and how are the events handled? How to synchronize the bars from different instruments during testing in an "open prices only" mode? This article aims to provide answers to these and many other questions.
 
  1. Don't double post! You already had this thread open.
              General rules and best pratices of the Forum. - General - MQL5 programming forum #6 (2017)

  2. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Forum rules and recommendations - General - MQL5 programming forum (2023)
              Messages Editor

  3. If it is a buy order, then it isn't a sell and you continue.
    Loop does nothing, ever.
     for(int i=0; i<OrdersTotal(); i++)
       {
          if(OrderSelect(i, SELECT_BY_POS) == false) break;
          if(OrderSymbol() != Symbol()) continue;
          if(OrderType() != OP_BUY || OrderType() != OP_SELL) continue; 
    Don't use negative logic.
     for(int i=0; i<OrdersTotal(); i++) if(
        OrderSelect(i, SELECT_BY_POS)
     &&(OrderType() == OP_BUY || OrderType() == OP_SELL)
     && OrderSymbol() == Symbol()
     ){

  4. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
              Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

    If trading multiple timeframes, and filter by symbol requires use a range of MN (base MN plus timeframe).
              Why are MT5 ENUM_TIMEFRAMES strange? - General - MQL5 programming forum - Page 2 #11 (2020)

 

Hello, thank you for your reply.

Maybe I could have done my edit following your reply No.1 & 2.

The last No3, I can't figure it out what you are talking about. 

It means, 1 EA can only have 1 Magic Number by each Period?

So what is your suggested code to manage/count number of orders by each Magic Number by each period?

I need 3 Magic Numbers for each period.(Buy, Sell and Hedging settlement )


William Roeder #:
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Forum rules and recommendations - General - MQL5 programming forum (2023)
              Messages Editor

  2. If it is a buy order, then it isn't a sell and you continue.
    Loop does nothing, ever.
    Don't use negative logic.

  3. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
              Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

    If trading multiple timeframes, and filter by symbol requires use a range of MN (base MN plus timeframe).
              Why are MT5 ENUM_TIMEFRAMES strange? - General - MQL5 programming forum - Page 2 #11 (2020)