What is the error in this code? I want to calculate the number of open orders (Buystops/sellstops or Buylimits/selllimits)

 

Some experts please tell what is wrong with the below code, it is not counting the open orders?


int Nropen(int option){
   int nrop=0;
   for(int ord=HistoryOrdersTotal()-1; ord>=0; ord--){
      ulong ticket=OrderGetTicket(ord);
      if(OrderSelect(ticket)){
      if(OrderGetInteger(ORDER_MAGIC)==Magic){
         if(option==0) nrop++;
         if(option==1 && OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_BUY)  nrop++;
         if(option==2 && OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_SELL) nrop++;
      }
   }
 }
   return nrop;
}
 
Sathish Justin: Some experts please tell what is wrong with the below code, it is not counting the open orders?

The name of the function says it all ... HistoryOrdersTotal

"History" is "old", not "current". Currently open orders are not in the "history".

Instead use ...

OrdersTotal

Returns the number of orders

Documentation on MQL5: Trade Functions / OrdersTotal
Documentation on MQL5: Trade Functions / OrdersTotal
  • www.mql5.com
OrdersTotal - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:

The name of the function says it all ... HistoryOrdersTotal

"History" is "old", not "current". Currently open orders are not in the "history".

Instead use ...

OrdersTotal

Returns the number of orders

Thank you sir

Reason: