Counting Buy and Sells for specific pair

 

My grid ea uses count buy and sells and close all in profit

It works perfectly on 1 pair

When 2 or more pairs is loaded it gets confused

Is it possible to count only for a specific pair? Please advise what must be added and where in the code below

int Count_Buy(){
int OpenBuyOrders=0;
for(int i=0;i<OrdersTotal(); i++ )
{
if(OrderSelect(i, SELECT_BY_POS)==true)
{

if (OrderType()==OP_BUY)
OpenBuyOrders++;
}
}
return(OpenBuyOrders);
}


Thanks in advance


 
Pieter Gerhardus Van Zyl:
Is it possible to count only for a specific pair?

https://docs.mql4.com/trading/ordersymbol

See also examples in codebase (there are a lot of similar examples there).

 

I'm thinking if trading gold:


int Count_Buy(){
int OpenBuyOrders=0;
for(int i=0;i<OrdersTotal(); i++ )
{
if(OrderSelect(i, SELECT_BY_POS)==true)
{

if (OrderType()==OP_BUY)

if(OrderSymbol()=="XAUUSD")

{

OpenBuyOrders++;

}
}
}
return(OpenBuyOrders);
}

 
Pieter Gerhardus Van Zyl #:
if(OrderSymbol()=="XAUUSD")

It looks plausible.

The cycle through orders and checking OrderSymbol() are present in most MQL4 advisors. See examples in codebase

 

It seems to work if I use if(OrderSymbol()==Symbol())


Another problem is before I start my first order I use OrdersTotal()==0 so that the ea starts with a fresh grid and functions correctly 

but when I load more pairs on the terminal, the total orders will never be 0 for all - 


Can an OrdersTotal() be done for a specific pair or must I create a int Count_Specific() to count just for the Symbol traded

 
Pieter Gerhardus Van Zyl #:
Can an OrdersTotal() be done for a specific pair

No, OrdersTotal returns exactly what is written in the documentation. That is, the number of orders for all symbols.

https://docs.mql4.com/trading/orderstotal

If you need the number of orders for a certain symbol, count them yourself. There's nothing super complicated about it. This is a typical task. Just like you will often want to know the number of orders of each type.

 
Pieter Gerhardus Van Zyl #:
or must I create a int Count_Specific()

It's a pretty bad idea to go through all the orders for each such microtask. Try to collect all the information you need about orders within one cycle of this type:

Pieter Gerhardus Van Zyl #:
for(int i=0;i<OrdersTotal(); i++ )

And your cycle is written in a bad manner. It's better to flip it:

for(int i = OrdersTotal() - 1; i >= 0; i--)
 

I don't know how to learn MQL4 from scratch now. The mql4 book is good, but very outdated, now no one programs as it is written there.

I think that if you have no experience, then it will be much easier to learn MQL5, since there is a lot of relevant literature for it.

Almost all the information on MQL4 that you can google is very outdated, like the language itself.

Starting to learn MQL4 from scratch now is a crime against your own time

 
Thanks for the help. I got it right.