# Function that returns the number of orders in a specified pair

219

Hi guys!

I need of a function that returns the number of orders in a specified pair of currency.

PS: Fot MT4

Someone can help me?

1572

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++){
i++;
}
return(i);
}

int NumberOfSellOrders(){
int cnt, total;
int i = 0;
total = OrdersTotal();
for(cnt=0;cnt<total; cnt++){
if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderMagicNumber()==MAGIC)
i++;
}
return(i);
}  ```
219

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 =(((
8038

```int OrdersTotal(string symbol)
{
int count=0,total=OrdersTotal();
for(int i=0;i<total;i++)
{
if(OrderType()<=OP_SELL && OrderSymbol()==symbol)
count++;
}
return(count);
}```
1572

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;

```
30051

```

int Orderstotal(string ordertype){   /// Type OP_BUY OR OP-SELL
int count=0;
for(int i=0;i<OrdersTotal();i++){

count++;
}
}

return(count);
}```

USE THIS CODE i tested

219

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" ??
8038

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(OrderType()<=OP_SELL && OrderSymbol()==symbol)
count++;
}
return(count);
}```
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(OrderType()<=OP_SELL && OrderSymbol()==symbol)
count++;
}
return(count);
}```
219

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