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); }
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 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); }
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
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" ??
//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); }
//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); }
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.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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?