Count pending orders in a period MQL5

 

Hello I have the following code that calculates the open orders in a given Timeframe, but I don't know how to count also the pending orders.

int TradeCount(ENUM_TIMEFRAMES TimeFrame) {
   int      Cnt;
   ulong    Ticket;
   long     EntryType;
   datetime DT[1];

   Cnt = 0;

   if(CopyTime(_Symbol, TimeFrame, 0, 1, DT) <= 0) {
      Cnt = -1;
   }
   else {
      HistorySelect(DT[0], TimeCurrent());
      for(int i = HistoryDealsTotal() - 1; i >= 0; i--) {
         Ticket    = HistoryDealGetTicket(i);
         EntryType = HistoryDealGetInteger(Ticket, DEAL_ENTRY);
         int PositionMagicNumber = (int)PositionGetInteger(POSITION_MAGIC);
         
         if(EntryType == DEAL_ENTRY_IN) {
            if(_Symbol == HistoryDealGetString(Ticket, DEAL_SYMBOL)
            && PositionMagicNumber == Magic) {
               Cnt++;
            }
         }
      }
   }
   return(Cnt);
}
 

Check the documentations for these: HistoryOrdersTotal, HistoryOrderGetDouble, HistoryOrderGetInteger


 
Yashar Seyyedin #:

Check the documentations for these: HistoryOrdersTotal, HistoryOrderGetDouble, HistoryOrderGetInteger


Hello, first of all thank you for your response. I have tried the following way but it does not detect any pending order.

int TradeCount(ENUM_TIMEFRAMES TimeFrame) {
   int      Cnt;
   ulong    Ticket;
   long     EntryType;
   datetime DT[1];

   Cnt = 0;

   if(CopyTime(_Symbol, TimeFrame, 0, 1, DT) <= 0) {
      Cnt = -1;
   }
   else {
      HistorySelect(DT[0], TimeCurrent());
      for(int i = HistoryOrdersTotal() - 1; i >= 0; i--) {
         Ticket    = HistoryOrderGetTicket(i);
         EntryType = HistoryOrderGetInteger(Ticket, ORDER_TYPE);
         int PositionMagicNumber = (int)PositionGetInteger(POSITION_MAGIC);
         
         if(EntryType==ORDER_TYPE_BUY || EntryType == ORDER_TYPE_SELL ||
         EntryType == ORDER_TYPE_BUY_STOP || EntryType == ORDER_TYPE_BUY_LIMIT ||
         EntryType == ORDER_TYPE_SELL_STOP || EntryType == ORDER_TYPE_SELL_LIMIT) {
            if(PositionMagicNumber == Magic) {
               Cnt++;
            }
         }
      }
   }
   return(Cnt);
}
 
Dolors #:
HistoryOrdersTotal


Check the state of the order rather than the order type
 

Hi

You shouldn’t use PositionGetInteger – when selecting orders you can simply use something like that:

int TradeCount(ENUM_TIMEFRAMES TimeFrame) {

   int      Cnt;

   ulong    Ticket;

   long     EntryType;

   datetime DT[1];

 

   Cnt = 0;

 

   if(CopyTime(_Symbol, TimeFrame, 0, 1, DT) <= 0) {

      Cnt = -1;

   }

   else {

      HistorySelect(DT[0], TimeCurrent());

      for(int i = HistoryOrdersTotal() - 1; i >= 0; i--) {

         Ticket    = HistoryOrderGetTicket(i);

         EntryType = HistoryOrderGetInteger(Ticket, ORDER_TYPE);

         int OrderMagicNumber = (int)HistoryOrderGetInteger(Ticket,ORDER_MAGIC);

        

 

         if(OrderMagicNumber == Magic) {

            Cnt++;

         }

        

      }

   }

   return(Cnt);

}

Best Regards

 
Thank you all for your contributions.
Reason: