Download MetaTrader 5

How to can I read the Position type?

To add comments, please log in or register
Interested in earning money via MQL5 Cloud Network? Enter your profile!
FilipIordan
15
FilipIordan 2012.09.12 10:07 

Hello, everyone I am new to MQL5, and I need some help in reading the opened Position Type (if it is Buy or Sell - int type)

//--- variables for returning values from position properties

   double   open_price;
   double   initial_volume;
   long     positionID;
   double   position_profit;

   int      type;

//--- number of current positions

   uint     total=PositionsTotal();
//--- go through orders in a loop
   for(uint i=0;i<total;i++)
     {
      //--- return order ticket by its position in the list
      if(PositionSelect(symbol)==true)
        {
         //--- return order properties
         open_price    =PositionGetDouble(POSITION_PRICE_OPEN);
         positionID    =PositionGetInteger(POSITION_IDENTIFIER);
         initial_volume=PositionGetDouble(POSITION_VOLUME);
         position_profit=PositionGetDouble(POSITION_PROFIT);
         
         type          =PositionGetInteger(POSITION_TYPE);
         Alert(type);  

         
         
        }

 With this code i was able to read the open price, position ID, initial volume and position profit, but when I alert the order type, it always shows me the value 0, even i have buy or sell position opened. How can I read the opened Posision Type? Thank you in advance & have a great day!

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Order Properties - Documentation on MQL5
Rashid Umarov
Admin
11837
Rashid Umarov 2012.09.12 11:31  

According to MQL5 Documentation the position type is value from the ENUM_POSITION_TYPE  enumeration :

ENUM_POSITION_TYPE

Identifier

Description

POSITION_TYPE_BUY

Buy

POSITION_TYPE_SELL

Sell

 

So the code might be such

//--- variables for returning values from position properties

   double   open_price;
   double   initial_volume;
   long     positionID;
   double   position_profit;

   ENUM_POSITION_TYPE      type;

//--- number of current positions

   uint     total=PositionsTotal();
//--- go through orders in a loop
   for(uint i=0;i<total;i++)
     {
      //--- return order ticket by its position in the list
      if(PositionSelect(symbol)==true)
        {
         //--- return order properties
         open_price    =PositionGetDouble(POSITION_PRICE_OPEN);
         positionID    =PositionGetInteger(POSITION_IDENTIFIER);
         initial_volume=PositionGetDouble(POSITION_VOLUME);
         position_profit=PositionGetDouble(POSITION_PROFIT);
         
         type          =(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
         Alert(EnumToString(type));  

         
         
        }

FilipIordan
15
FilipIordan 2012.09.12 17:20  
Rosh:

According to MQL5 Documentation the position type is value from the ENUM_POSITION_TYPE  enumeration :

So the code might be such


Thank you very much! The code worked perfectly :) Have a great day!
Heinz Traub
314
Heinz Traub 2012.09.12 18:55  
FilipIordan:

Hello, everyone I am new to MQL5, and I need some help in reading the opened Position Type (if it is Buy or Sell - int type)

 With this code i was able to read the open price, position ID, initial volume and position profit, but when I alert the order type, it always shows me the value 0, even i have buy or sell position opened. How can I read the opened Posision Type? Thank you in advance & have a great day!

That's weird, it should work as is. The base type for the ENUM_POSITION_TYPE enum is of integer type (either int or long). Replacing the int type for the long type works great for me without casting to ENUM_POSITION_TYPE.

Anyway, if ENUM_POSITION_TYPE works for you as suggested by Rosh then stick to it.

To add comments, please log in or register