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
Print("Position type is ",PositionGetInteger(POSITION_TYPE));
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.
See below for usage example
***
#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///////////// }
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.
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; };
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
.
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!
- www.mql5.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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?