Download MetaTrader 5

OrderGetTicket() function returns zero whereas the are open orders in the account

To add comments, please log in or register
holocast
80
holocast  
void OnStart()
{Alert ("Total buy order is ",GetOrderCount(ORDER_TYPE_BUY));
}
int GetOrderCount(long type) 
  { 
   ulong order_ticket; 
   int total=0; 
//--- go through all pending orders 
   for(int i=0;i<PositionsTotal();i++)
   { 
      if((order_ticket=OrderGetTicket(i))>0) 
        { //if(magic_number == OrderGetInteger(ORDER_MAGIC)) 
          if(type == OrderGetInteger(ORDER_TYPE)) total++; 
        }
      Alert (i);
   }
//--- 
   return(total); 
  }

When I run this script I always get the total as zero, the counter i alerts up to the number of active orders in the terminal.

Also when I switch to OrdersTotal() instead of PositionsTotal(), it returns zero, that is the counter (i) does not count at all.

How do I get this script to return the total number of buy orders?

Why does OrdersTotal() return zero while PositionsTotal() returns the number of active orders in the terminal?

P.S am running this code on MT5

Konstantin Nikitin
6150
Konstantin Nikitin  
whroeder1
17932
whroeder1  
   for(int i=0;i<PositionsTotal();i++){ 
      if((order_ticket=OrderGetTicket(i))>0) 
  1. You are mixing positions and orders.
  2. You can't use any Trade Functions until you select a position or order.
holocast
80
holocast  
whroeder1:
  1. You are mixing positions and orders.
  2. You can't use any Trade Functions until you select a position or order.
I thought OrderGetTicket() is supposed to select the order and return it's ticket?
holocast
80
holocast  
Konstantin Nikitin:
Trade Functions-->
I've read the guide, isn't OrderGetTicket () supposed to select and return the order according to the index passed into it.
E.g OrderGetTicket (1); should select and return the ticket of the order with number 1 index
Konstantin Nikitin
6150
Konstantin Nikitin  
holocast :
I've read the guide, isn't OrderGetTicket () supposed to select and return the order according to the index passed into it.
E.g OrderGetTicket (1); should select and return the ticket of the order with number 1 index

You work with positions PositionsTotal. So you need PositionGetTicket, PositionGetInteger

holocast
80
holocast  
Konstantin Nikitin:

You work with positions PositionsTotal. So you need PositionGetTicket, PositionGetInteger

Wonderful, thanks a lot, it now works, changing the order to position made it.
Thanks
To add comments, please log in or register