4

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.

user3666197
  • 1
  • 6
  • 50
  • 92
Freek Nortier
  • 780
  • 1
  • 13
  • 27
  • **Would you mind to add a few PrintScreens and source code**, to show exactly the colliding case ( having Orders in StrategyTester while having no such reported in OrdersTotal() )? – user3666197 Jan 09 '16 at 01:04
  • 1
    `PostionsTotal()` always gets the number of open positions just as `OrdersTotal()` gets the number of open pending orders, doesnt matter which client of `MetaTrader5` you are running. If you want more help you need to provide a code sample. – Vitor Santos Apr 05 '17 at 01:41

2 Answers2

2

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.

Freek Nortier
  • 780
  • 1
  • 13
  • 27
2

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);
}