Function that returns the number of orders in a specified pair

Osama Shaban
1572
Osama Shaban  

You can use the following 2 functions ... You may call both functions to get the total number of orders (Buys + Sells) or may be do them in one function ...

int NumberOfBuyOrders(){ 
   int cnt, total; 
   int i = 0;
   total = OrdersTotal(); 
   for(cnt=0;cnt<total; cnt++){ 
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
      if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==MAGIC)
         i++; 
   }
   return(i);
}


int NumberOfSellOrders(){ 
   int cnt, total; 
   int i = 0;
   total = OrdersTotal(); 
   for(cnt=0;cnt<total; cnt++){ 
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
      if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderMagicNumber()==MAGIC)
         i++; 
   }
   return(i);
}  
Jhenrique Vissotto
219
Jhenrique Vissotto  
Osama Shaban:

You can use the following 2 functions ... You may call both functions to get the total number of orders (Buys + Sells) or may be do them in one function ...

Don't work =(((
Ernst Van Der Merwe
8038
Ernst Van Der Merwe  
int OrdersTotal(string symbol)
  {
   int count=0,total=OrdersTotal();
   for(int i=0;i<total;i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderType()<=OP_SELL && OrderSymbol()==symbol)
            count++;
     }
   return(count);
  }
Osama Shaban
1572
Osama Shaban  
jhenriquevissot:
Don't work =(((

How is that? I use it in most of my EAs !!!.

It is not a ready made function ... You can do it this way ...

int N;

N = NumberOfBuyOrders() + NumberOfSellOrders();
Muhammad Mudasir
30051


  int Orderstotal(string ordertype){   /// Type OP_BUY OR OP-SELL 
    int count=0;
  for(int i=0;i<OrdersTotal();i++){
  
  if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)&&OrderSymbol()==Symbol()&&OrderType()== ordertype){
  
        count++;
  }
  }
  
     return(count);
  }




    USE THIS CODE i tested

Jhenrique Vissotto
219
Jhenrique Vissotto  
Osama Shaban:

How is that? I use it in most of my EAs !!!.

It is not a ready made function ... You can do it this way ...

what shoud I do with the word: "symbol" ??
Ernst Van Der Merwe
8038
Ernst Van Der Merwe  
jhenriquevissot:
what shoud I do with the word: "symbol" ??
//Get all open orders for GBPUSD
int totalOpenOrdersForGBPUSD=OrdersTotal("GBPUSD");

int OrdersTotal(string symbol)
  {
   int count=0,total=OrdersTotal();
   for(int i=0;i<total;i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderType()<=OP_SELL && OrderSymbol()==symbol)
            count++;
     }
   return(count);
  }
Marco vd Heijden
Moderator
13599
//Get all open orders for GBPUSD
int totalOpenOrdersForGBPUSD=Orderstotal("GBPUSD");

int Orderstotal(string symbol)
  {
   int count=0,total=OrdersTotal();
   for(int i=0;i<total;i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderType()<=OP_SELL && OrderSymbol()==symbol)
            count++;
     }
   return(count);
  }
Jhenrique Vissotto
219
Jhenrique Vissotto  
Marco vd Heijden:
This code seems work!

But, I’m not sure that all is ok, somethings are strange…

Van Der Merwe and Marco vd Heijden, why the code “if(OrderType()<=OP_SELL && OrderSymbol()==symbol)” use the function OP_SELL in the code? And about the function OP_BUY? Is it unnecessary?
Marco vd Heijden
Moderator
13599
jhenriquevissot:
This code seems work!

But, I’m not sure that all is ok, somethings are strange…

Van Der Merwe and Marco vd Heijden, why the code “if(OrderType()<=OP_SELL && OrderSymbol()==symbol)” use the function OP_SELL in the code? And about the function OP_BUY? Is it unnecessary?
if(OrderType()==OP_SELL && OrderSymbol()==symbol)

Normally you will see it written like this,

But in this case it states

 if(OrderType()<=OP_SELL

If OrderType() <= is smaller or equal to and,

ID

Value

Description

OP_BUY

0

Buy operation

OP_SELL

1

Sell operation

OP_BUYLIMIT

2

Buy limit pending order

OP_SELLLIMIT

3

Sell limit pending order

OP_BUYSTOP

4

Buy stop pending order

OP_SELLSTOP

5

Sell stop pending order

 

As you can see here OP_BUY integer value is 0 so the condition is valid for Buy as well as sell orders.