Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an amount of open trades in MQL5

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.

like image 833
Freek Nortier Avatar asked Sep 13 '25 20:09

Freek Nortier


2 Answers

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.

like image 138
Freek Nortier Avatar answered Sep 17 '25 18:09

Freek Nortier


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);
}
like image 24
mehdi hamedi Avatar answered Sep 17 '25 18:09

mehdi hamedi