My Mistake getting TYPE

TicketType  = myposition.Type();              //A
TicketType  = myposition.PositionType();      //B

I used  option //A above to get the ticket type using the standard libraries

#include <Trade\Trade.mqh>             
#include <Trade\OrderInfo.mqh>         /
#include <Trade\PositionInfo.mqh>      
#include <Trade\SymbolInfo.mqh>
#include <Trade\CHistoryPositionInfo.mqh>
#include <Trade\TerminalInfo.mqh>

CTrade               mytrade;    // Trades Info and Executions library        //COrderInfo     m_order;    // Library for Orders information
CPositionInfo        myposition; // Library for all position features and information
CHistoryPositionInfo myhistory;

I now understand this was incorrect and should use option //B

BUT... I didn't get any error using //A but the result was always zero

I would have expected a 'compile' error if .Type is an invalid option.

Obviously have now tracked down my problem but curious about why the command appeared to have been accepted (and took me longer than expected to track down)

You need to look where the MetaQuotes PositionGetXxx functions are wrapped into classes, which is in Include/Trade/PositionInfo.mqh

There you press Ctrl+F with PositionInfo.mqh open in Meta Editor, search for "Type()".

Now click on continue search until you found every line where Type() method body (there is heads and bodies of methods) is located and look into how the method is defined, what it does and what value it returns.

Now if you encounter a mq proprietary function like PositionGetInteger inside the method body, mark it by double clicking and press F1 to see the documentary about the function.

Then do the same with PositionType() and look at how the two are different.
Tobias Johannes Zimmer #:


Many thanks for response

Understand your words but still a little confused.

I don't see any reference to Type() by itself - which I understand to be required to return a value from the following expression (which I incorrectly used)


Perhaps I am misunderstanding something but the following are ALL the references to Type() in PositionInfo.mqh

   return(FormatType(str,PositionType()));                      // line # 127
   FormatType(type,PositionType()),                             // line # 271
   FormatType(type,PositionType()),                             // line # 277
   m_type       =PositionType();                                // line # 344
   if(m_type==PositionType()  &&                                // line # 355

and PositionType() is found via the following

ENUM_POSITION_TYPE CPositionInfo::PositionType(void) const

I don't see where .Type is found

TicketType  = myposition.Type();

Like I said previously the above doesn't return TYPE of deal just appears to default to ZERO

Peter Williams #:
I don't see any reference to Type() by itself - which I understand to be required to return a value from the following expression (which I incorrectly used)

Oh yes, of course. I just looked into it and the method Type() is not even inside the CPositionInfo class but in the CObject class that it inherits from.

//|                                                 PositionInfo.mqh |
//|                   Copyright 2009-2020, MetaQuotes Software Corp. |
//|                                     |
#include <Object.mqh>
//| Class CPositionInfo.                                             |
//| Appointment: Class for access to position info.                  |
//|              Derives from class CObject.                         |
class CPositionInfo : public CObject
   double            m_volume;
   double            m_price;


It inherits the public aspects from CObject which you find directly in the Include folder with no subfolder whatsoever.

class CObject
   CObject          *m_prev;               // previous item of list
   CObject          *m_next;               // next item of list

                     CObject(void): m_prev(NULL),m_next(NULL)            {                 }
                    ~CObject(void)                                       {                 }
   //--- methods to access protected data
   CObject          *Prev(void)                                    const { return(m_prev); }
   void              Prev(CObject *node)                                 { m_prev=node;    }
   CObject          *Next(void)                                    const { return(m_next); }
   void              Next(CObject *node)                                 { m_next=node;    }
   //--- methods for working with files
   virtual bool      Save(const int file_handle)                         { return(true);   }
   virtual bool      Load(const int file_handle)                         { return(true);   }
   //--- method of identifying the object
   virtual int       Type(void)                                    const { return(0);      }
   //--- method of comparing the objects
   virtual int       Compare(const CObject *node,const int mode=0) const { return(0);      }

As you can see it returns 0; But this is not position type. I don't know what id does to be honest, except that you could use it to return enums of different objects types, as int types maybe. But that is not concerning you when working with PositionInfo. Not sure what it does here. The method Could be changed to give back enums of object types, which are somewhat analog to integers.



Thanks again for your swift response.

I am not going mad after all!

Seems a little weird.

Just really a warning for anyone using position.Type() - it doesn't do what one may expect

My pleasure hahaha.