CPositionInfo pos.Type() always return 0 (POSITION_TYPE_BUY)

 

I have strange issue. I have this code:


#include <Trade\Trade.mqh>
CTrade         trade;
CPositionInfo  pos;
COrderInfo     ord;

trade.Sell(0.01, _Symbol, 0, 0, 0, "Test SELL"); // I open sell somewhere in my code, then call myFunct() below to read that 

void myFunct() {
  for (int i = PositionsTotal() - 1; i >= 0; i--) {
      if (!pos.SelectByIndex(i)) {
         Print("Failed to select position at index ", i);
         continue;
      }
      
      Print("TYPE: ", pos.Type());
      Print("OPEN PRICE: ", pos.PriceOpen());
      Print("OPEN TIME: ", pos.Time());
   }
}


when i call myFunct() it print test data and all data is correct for my position (PriceOpen, Ticket, Time, Symbol, Magic, etc ...). Except Type() ... it just always return 0/POSITION_TYPE_BUY, even I see position opened is a Sell in my Strategy Tester.

Returns like:

2024.12.14 23:29:43.016 2024.01.02 01:02:58   TYPE: 0
2024.12.14 23:29:43.016 2024.01.02 01:02:58   OPEN PRICE: 1.1044
2024.12.14 23:29:43.016 2024.01.02 01:02:58   OPEN TIME: 2024.01.02 01:00:00


I don't know if this happen only in Tester, as I have no opened positions at my account and it is Saturday, so can't really open any to test EA there, but at Tester EA pos.Type() just keep return 0 even when I open only sell ...


Anyone ever had that strange issue? Ideas ?

The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
What are the differences between the three modes of testing in MetaTrader 5, and what should be particularly looked for? How does the testing of an EA, trading simultaneously on multiple instruments, take place? When and how are the indicator values calculated during testing, and how are the events handled? How to synchronize the bars from different instruments during testing in an "open prices only" mode? This article aims to provide answers to these and many other questions.
 
OMG ... PositionType() ! I don't know why Type() just did not show any error and just compiled, while Type() not a valid CPositionInfo funct ... I completely forgot it had to be PositionType and as no error for Type() I got impression it is same and OK ...
Documentation on MQL5: Standard Library / Trade Classes / CPositionInfo
Documentation on MQL5: Standard Library / Trade Classes / CPositionInfo
  • www.mql5.com
CPositionInfo is a class for easy access to the open position properties. CPositionInfo class provides easy access to the open position properties...
 
geost77 #: OMG ... PositionType() ! I don't know why Type() just did not show any error and just compiled, while Type() not a valid CPositionInfo funct ... I completely forgot it had to be PositionType and as no error for Type() I got impression it is same and OK ...

Type() is NOT an error. It is valid for CPosition because it inherits it from the CObject class. It does however, have a different functionality.

Documentation on MQL5: Standard Library / Basic Class CObject / Type
Documentation on MQL5: Standard Library / Basic Class CObject / Type
  • www.mql5.com
Gets the type identifier. Return Value Type identifier (for CObject - 0). Example...