PositionsTotal() function doesn't get the number of the opened positions

 

Hi all,

I'm using the function PositionsTotal() to get the number of open positions, due to I'm using a hedge system.

int num_positions=PositionsTotal();

but at each iteration it returns always 0, even if I opened two positions.

two open positions


What's wrong?


Thanks

 

Please run this code: ListPositions

ListPositions
ListPositions
  • votes: 12
  • 2017.03.28
  • Vladimir Karputov
  • www.mql5.com
Список всех позиций. Пример работы с CPositionInfo.
 

But I need a function to use in my code.

Getting the open positions, I have to decide if it can open a new one.


How to do that?


Why does

PositionsTotal()

not work?

 
managertop40 :

But I need a function to use in my code.

Getting the open positions, I have to decide if it can open a new one.


How to do that?


Why does

not work?


Show your code. Please.
 

This is just the final part:

int num_position=PositionsTotal();

   if(buy_sign==1 && PositionSelect(Symbol())==0 && num_position==0)
     {
      OrderCheck(request,resultCheck);
      OrderSend(request,result);      
     }

//
  }
but num_position is always equal to zero. Even if OrderSend is processed many times
 

Here is the simplest code. He works. Hedge account.

MetaTrader 5 x64 build 1596 started (MetaQuotes Software Corp.)
//+------------------------------------------------------------------+
//|                                               PositionsTotal.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   static long count=0;
//---
   Comment(count,", PositionsTotal()=",IntegerToString(PositionsTotal()));
   count++;
  }
//+------------------------------------------------------------------+


Look for errors in your code.


Maximally reduce your code - throw out as much as possible - until you find an error.

Files:
 
managertop40:

Hi all,

I'm using the function PositionsTotal() to get the number of open positions, due to I'm using a hedge system.

but at each iteration it returns always 0, even if I opened two positions.



What's wrong?


Thanks

Did you solve this issue?  I still have the same problem with version 5.0 build 2690.

 
If you are calling  PositionsTotal() right after opening a position, try Inserting a Sleep() statement just before calling PositionsTotal() and see if that fixes the problem.
The duration of the Sleep() call is trial and error, maybe start at 50 ms ie Sleep(50) and if that fixes things reduce the duration until the problem comes back.
Documentation on MQL5: Trade Functions / PositionsTotal
Documentation on MQL5: Trade Functions / PositionsTotal
  • www.mql5.com
PositionsTotal - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
It's working fine !!! Thanks very much for this 
Reason: