PositionsTotal() is not giving the "good answer"?

 

Hello everybody,

First, thanks to all contributors, you've been helping me for years!

I'm migrating my bots from MT4 to MT5, and all have been fine until now. But I have a question, and I'm sure the answer is simple, but I can't get it...

Here is the problem :

- On MT4, to know all the open positions with the EA, I justed counted the OP_BUY positions with the same Magic and on the same symbol.

- On MT5, I open new positions, but when I want to check those positions, using the PositionsTotal(), I only get 1!


If it can helps, here is the code for the exemple :


#include <Trade\Trade.mqh>
CTrade TradesM;

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
  
//---  
void OnDeinit(const int reason)
  {
//---
  }
  
//---
void OnTick()
  {
   ulong Magic = 123;
   TradesM.SetExpertMagicNumber(Magic);
   TradesM.Sell(0.01,NULL,0,0,0, "Test"); 
   Print(PositionsTotal());
   PositionGetTicket(PositionsTotal()-1);
   Print(PositionGetDouble(POSITION_VOLUME));
  }

With this, PositionsTotal() only returns 1. But when I look the volume of the position, it goes up.

And if I look to the backtest, I have 1 trade, but 1225 operations.


So I I understand well, I'm not opening a new position but I'm working only with one position which is going up and up?

Is it possible to make independent positions? In my case, I'd need to get the infos of each position (for the TP, SL, etc.).


Thanks for your help!

 
Paul B:

Hello everybody,

First, thanks to all contributors, you've been helping me for years!

I'm migrating my bots from MT4 to MT5, and all have been fine until now. But I have a question, and I'm sure the answer is simple, but I can't get it...

Here is the problem :

- On MT4, to know all the open positions with the EA, I justed counted the OP_BUY positions with the same Magic and on the same symbol.

- On MT5, I open new positions, but when I want to check those positions, using the PositionsTotal(), I only get 1!


If it can helps, here is the code for the exemple :


With this, PositionsTotal() only returns 1. But when I look the volume of the position, it goes up.

And if I look to the backtest, I have 1 trade, but 1225 operations.


So I I understand well, I'm not opening a new position but I'm working only with one position which is going up and up?

Is it possible to make independent positions? In my case, I'd need to get the infos of each position (for the TP, SL, etc.).


Thanks for your help!

Your account is of netting type. Ask your broker if he can switch it to hedging, or find a different broker.
 

Hello Dominik,


Thanks for your fast answer! I spent hours trying to understand!


Have a nice day 😊