how to get position type opened?

 
void OnTick()
  {
//--- positions already selected before
   ENUM_ORDER_TYPE signal = WRONG_VALUE;
   long posType = PositionGetInteger(POSITION_TYPE);
   double openVol=PositionGetDouble(POSITION_VOLUME);
//--- check for signal to close a trading position
   if(posType==(long)POSITION_TYPE_BUY && bar[0].close<fma[0])
      signal = ORDER_TYPE_SELL;
   if(posType==(long)POSITION_TYPE_SELL && bar[0].close>fma[0])
      signal = ORDER_TYPE_BUY;
      
    Print("Position type is ",posType);
  }
//+------------------------------------------------------------------+

please i will appreciate if anyone can help. Each time i open a trade and try to get the position type opened, it keep returning a zero (0) value both for a buy and a sell position. Can anyone help out with the correct code for getting the opened position?

 
udoh.jeremiah.emem:

please i will appreciate if anyone can help. Each time i open a trade and try to get the position type opened, it keep returning a zero (0) value both for a buy and a sell position. Can anyone help out with the correct code for getting the opened position?

Example:  Last position type

Last position type  

 
thanks very much i appreciate
 
i appreciate your effort sir, but the EA still does not get the position type opened even after using your code and then trying a new method of my own. All to no avail.
Print("Position type is ",PositionGetInteger(POSITION_TYPE));
 
 
udoh.jeremiah.emem :
i appreciate your effort sir, but the EA still does not get the position type opened even after using your code and then trying a new method of my own. All to no avail.  

I can't read your mind, so I can only give you advice: fix the mistakes in your code.

 
Azeez Adedapo Alabi:

See below for usage example

***

Please insert the code correctly: when editing a post, use the button  Code- when the popup appears, paste your code into that popup.
 
#property copyright "Copyright © 2021, DAAL TECHNOLOGY SOLUTION"
#property link      "https://www.mql5.com"
#property version   "Example"


#include <Trade\PositionInfo.mqh>

//---
CPositionInfo  positionInfo;                // object of CPositionInfo class


void OnTick()
  {


/////////////USAGE EXAMPLE/////////////

        if( EnumToString (positionInfo.PositionType()) == "POSITION_TYPE_BUY")  // EnumToString help you convert positionInfo.PositionType() to sting then you can compare with expected string value(To the right)
        {
           Print("CURRENT POSITION TYPE IS  ", EnumToString (positionInfo.PositionType()) ); ///print current or last registered position type
        }



        if( EnumToString (positionInfo.PositionType()) == "POSITION_TYPE_SELL")  // EnumToString help you convert positionInfo.PositionType() to sting then you can compare with expected string value(To the right)
        {
           Print("CURRENT POSITION TYPE IS  ", EnumToString (positionInfo.PositionType()) ); ///print current or last registered positon type
        }

/////////////END USAGE EXAMPLE/////////////
  }

 
udoh.jeremiah.emem:
i appreciate your effort sir, but the EA still does not get the position type opened even after using your code and then trying a new method of my own. All to no avail.  
Udoh, Above is a clear example to ensure resolution to your concern.
 
Azeez Adedapo Alabi: Udoh, Above is a clear example to ensure resolution to your concern.

Why are you converting the enums to strings to compare them, when it is simpler, faster and more efficient to just compare them directly?

if( positionInfo.PositionType() == POSITION_TYPE_BUY  ) { /* ... */ };
if( positionInfo.PositionType() == POSITION_TYPE_SELL ) { /* ... */ };

Alternatively, you can also use a "switch":

switch( positionInfo.PositionType() )
{
   case POSITION_TYPE_BUY:  /* ... */ break;
   case POSITION_TYPE_SELL: /* ... */ break;
   default:                 /* ... */ break;
};
 
Fernando Carreiro:

Why are you converting the enums to strings to compare them, when it is simpler, faster and more efficient to just compare them directly?

Alternatively, you can also use a "switch":

positionInfo.PositionType()

it returns Zero(0) without converting. You are welcome to test

.

 
Azeez Adedapo Alabi: it returns Zero(0) without converting. You are welcome to test

That is because the value of "POSITION_TYPE_BUY" is Zero "0" and the value of POSITION_TYPE_SELL is One "1". That is what it is supposed to be. They are enumerations!

PrintFormat( "Value of %s is %d", EnumToString(POSITION_TYPE_BUY),  (int) POSITION_TYPE_BUY  );
PrintFormat( "Value of %s is %d", EnumToString(POSITION_TYPE_SELL), (int) POSITION_TYPE_SELL );
2021.05.23 15:25:08.906 TestEnum (EURUSD,H1)    Value of POSITION_TYPE_BUY is 0
2021.05.23 15:25:08.906 TestEnum (EURUSD,H1)    Value of POSITION_TYPE_SELL is 1

Don't convert them into strings! Do it the proper way as I have shown you!

Please read the documentation on enumerations, if you still don't understand!

Documentation on MQL5: Language Basics / Data Types / Integer Types / Enumerations
Documentation on MQL5: Language Basics / Data Types / Integer Types / Enumerations
  • www.mql5.com
Enumerations - Integer Types - Data Types - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5