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

 
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

 
   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.
 
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?
 
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
 
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

 
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
Reason: