PositionSelect(_Symbol) call causes MT5 free

 

Greetings,

I'm a bit puzzled by the following. The documentation recommends calling PositionSelect() before calling PositionGetInteger(), which I'm doing as follows:

int sellPositionCount = 0;
if (PositionSelect(_Symbol)) {
  for (long i = 0; i < PositionsTotal() ; i++) {
       if (PositionGetInteger(POSITION_TYPE, i) == POSITION_TYPE_SELL) {
           sellPositionCount++;
       }
  }
}
/* ... etc */

Improperly formatted code edited by moderator.

Only problem is, the MT5 back-test freezes if I do the PositionSelect() call. If I comment out that check, everything works fine.

Any idea what may be happening here?

Thanks

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

  int CountSellPositions()
  {
      int totalPositions = PositionsTotal();
      int sellPositionCount = 0;

      for (int i = 0; i < totalPositions; i++) 
      {
         if (PositionGetTicket(i)==0) 
         {
             Print("ERRROR CODE: ", GetLastError());
             continue;
         }
         if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) 
             sellPositionCount++;
      }
      return sellPositionCount;   
  }

Please check documentation for the highlighted functions.

Also using long for counting loops is not logical. 

 
Yashar Seyyedin #:
Please check documentation for the highlighted functions.

Also using long for counting loops is not logical. 

Thanks Yashar, that solved the issue.

I was using long since that was the type required for the second argument of  PositionGetInteger().

It's interesting though that you are using PositionGetTicket(), when the docs say:


PositionGetInteger

The function returns the requested property of an open position, pre-selected using PositionGetSymbol or PositionSelect.


The docs for PositionGetTicket() also seem to say the same thing regarding PositionGetInteger():


PositionGetTicket

The function returns the ticket of a position with the specified index in the list of open positions and automatically selects the position to work with using functions PositionGetDouble, PositionGetInteger, PositionGetString.


Hence my use of PositionSelect(), which other tutorials have also used, but in your example you use  PositionGetTicket().

It's strange to me that there appears to be two ways according to the docs to do the same thing, but one fails silently.


Anyway, thanks for your help

Much appreciated.

Regards

 

@HenZen, Improperly formatted code edited by moderator. In the future, please use the CODE button (Alt-S) when inserting code.

Code button in editor

Reason: