How to get the latest open position?

 

Can anyone share to me how to get the price of latest open position if I have 2 or more open positions. 

double OpenTrade = PositionGetDouble(POSITION_PRICE_OPEN);

I guess this code does not get the latest open position I opened.

 
It is necessary to take into account the position time and store it in a variable. Implementation example: Last position type:
 

Vladimir Karputov:
It is necessary to take into account the position time and store it in a variable. Implementation example: Last position type


Can it be possible to add the position type?

As long as its the last opened position.

 
MAD1995 :

Can it be possible to add the position type?

As long as its the last opened position.

Run the example and see. 

 
Vladimir Karputov:

Run the example and see. 

ok sir I'll try. thanks.

 
Vladimir Karputov:

Run the example and see. 

Can You add the price value of last opened position sir?

 
MAD1995 :

Can You add the price value of last opened position sir?

You can do it yourself: 

PriceOpen

Gets the price of position opening

Documentation on MQL5: Standard Library / Trade Classes / CPositionInfo / PriceOpen
Documentation on MQL5: Standard Library / Trade Classes / CPositionInfo / PriceOpen
  • www.mql5.com
PriceOpen - CPositionInfo - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Vladimir Karputov:

You can do it yourself: 

PriceOpen

Gets the price of position opening

ok sir, thanks.

 

You could go ahead and assume following two states:

First, the Ticket-Number is sequencial and increases.

Second position index used internally by MT5 is: highest = last added position.


Then the following code will give you access to the last positions data:

PositionSelectByTicket(PositionGetTicket(PositionsTotal()-1));


or what ever your need is:

https://www.mql5.com/en/docs/constants/tradingconstants/positionproperties#enum_position_property_integer


 
Dominik Egert:

You could go ahead and assume following two states:

First, the Ticket-Number is sequencial and increases.

Second position index used internally by MT5 is: highest = last added position.


Then the following code will give you access to the last positions data:


or what ever your need is:

https://www.mql5.com/en/docs/constants/tradingconstants/positionproperties#enum_position_property_integer


Can You please elaborate, i'm just learning how to code. I just need the value of last open price to make a condition for closing orders. Example is if the last open position is 100 pips below the current price it will execute the command to close all open positions. Thats all I need for the last open position.

 
MAD1995 :

Can You please elaborate, i'm just learning how to code. I just need the value of last open price to make a condition for closing orders. Example is if the last open position is 100 pips below the current price it will execute the command to close all open positions. Thats all I need for the last open position.

In this case, you need to know the type of position and two prices:

Access to double type properties

 

PriceOpen

Gets the price of position opening

PriceCurrent

Gets the current price by position symbol

Documentation on MQL5: Standard Library / Trade Classes / CPositionInfo / PositionType
Documentation on MQL5: Standard Library / Trade Classes / CPositionInfo / PositionType
  • www.mql5.com
PositionType - CPositionInfo - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5