I want to get the amount of open trades in MQL5
on a demo account during strategy testing.
PositionsTotal()
and OrdersTotal()
always return 0
even if there are open trades.
The solution suggested here does not work.
Any help will be appreciated.
The problem occurred when running the code in the Metatrader 5 downloaded from Metaquotes' website. Running the same code in a Metatrader 5 instance from a forex broker resolved the issue.
according to https://mql5tutorial.com/mql5-tutorial-how-to-simply-count-positions-with-mql5/, you can use the following code for doing this
void OnTick()
{
int PositionForThisCurrencyPair = 0;
for (int i = PositionsTotal()-1; i>=0; i--)
{
string symbol = PositionGetSymbol(i);
if(Symbol() == symbol)
{
PositionForThisCurrencyPair+=1 ;
}
}
Comment("\n\n positions for this currency pair:",PositionForThisCurrencyPair);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With