Highest OrdersTotal

 

Hi all,

Trying to find the highest amount of open orders but but this code gives the same amount for TotalOpenOrders and MaxOpenOrders.

Thx  a lot.

Amir


         int TotalOpenOrders  = 0;
         int MaxOpenOrders   = 0;
         
                   
       for(int i = OrdersTotal()-1; i >= 0; i--) 
        {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         {
         if(OrderType() == OP_BUY || OrderType() == OP_SELL)
          {                                                                    
           TotalOpenOrders = TotalOpenOrders + 1;  
          }
         } 
           if(TotalOpenOrders > MaxOpenOrders)
            {                                                                    
              MaxOpenOrders = TotalOpenOrders;  
            }                            
        }
 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
MaxOpenOrders   = 0;

Move this line to OnInit so it executes only once.

 
Yashar Seyyedin #: Move this line to OnInit so it executes only once.

Actually he needs to move it outside of any function scope (so it will be global variable).

If he'll move it to OnInit(), the above variable will only reside within OnInit() scope.

But you can also just change it to a static variable.

Simply change code to-

static int MaxOpenOrders   = 0;
 
AMI289 #: Simply change code to-
static int MaxOpenOrders   = 0;

That will not work on a symbol (chart) change; EAs are not reloaded.

Resetting static: Problem in detecting a new Bar - MQL5 programming forum #5.3 (2020)

 

Thx a lot for your help guys

I moved it to a global variable and it works perfectly.