How to can I read the Position type?

 

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
 

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));  

         
         
        }

 
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!
 
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.

 
int intTYPE=-1;

intTYPE=PositionGetInteger(POSITION_TYPE);

if(intTYPE==0) Comment("BUY");
if(intTYPE==1) Comment("SELL");

The PositionGetInteger(POSITION_TYPE) command gives you an integer with the value of either 0 or 1.

0 = Buy (POSITION_TYPE_BUY)

1 = Sell (POSITION_TYPE_SELL)

-1 = WRONG_VALUE

I usually just create an integer variable and assign an initial value of -1, then use PositionGetInteger(POSITION_TYPE) to refill the value of my integer.

Using ENUM_POSITION_TYPE is probably best, but I'm a lazy programmer. Typing "0" and "1" is a lot easier, don't you think?.

 
robertm.new21:Using ENUM_POSITION_TYPE is probably best, but I'm a lazy programmer. Typing "0" and "1" is a lot easier, don't you think?.

No. Do not hard code numbers. What if other types are added. Are you going to remember what zero and one mean months from now? Write self-documenting code.

 
robertm.new21:

The PositionGetInteger(POSITION_TYPE) command gives you an integer with the value of either 0 or 1.

0 = Buy (POSITION_TYPE_BUY)

1 = Sell (POSITION_TYPE_SELL)

-1 = WRONG_VALUE

I usually just create an integer variable and assign an initial value of -1, then use PositionGetInteger(POSITION_TYPE) to refill the value of my integer.

Using ENUM_POSITION_TYPE is probably best, but I'm a lazy programmer. Typing "0" and "1" is a lot easier, don't you think?.

Thank you man,

After searching for days, finally found a working solution

 
Rashid Umarov:

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

So the code might be such


Thanks Rash, i, too benefited from your tutorial above but my question now is about

         type          =(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);

I'm a newbie so my qn. is about proper syntax writing. in the above, why is there a "()" enclosing EMUM_POSITION_TYPE? how do you know that a parenthesis is required and in what type of situation?

I would thought that it should be simply 

         type          =PositionGetInteger(POSITION_TYPE);


since the variable 'type' has already been declared by : 

   ENUM_POSITION_TYPE      type;

Thanks. Jim

 
JimSingadventure #:

Thanks Rash, i, too benefited from your tutorial above but my question now is about


I'm a newbie so my qn. is about proper syntax writing. in the above, why is there a "()" enclosing EMUM_POSITION_TYPE? how do you know that a parenthesis is required and in what type of situation?

I would thought that it should be simply 

         type          =PositionGetInteger(POSITION_TYPE);


since the variable 'type' has already been declared by : 


Thanks. Jim

(ENUM_POSITION_TYPE) is like int or double... it means it is useful to set the following text like this. In this case PositionGetInteger is of type int (and not ENUM) so, only for 1 line, and not for the whole code, it became of type ENUM instead of int... now it can receive ENUM values...

Reason: