How to get the most recent open position when using buy limit and buy stop?

 
I am implementing the EA with mql5. I would like to know how can I retrieve the most recent open position. As I am using buy limit and buy stop, it can't use the highest ticket number to refer to the latest open position. What function should I use? Could anyone please give any suggestion?
 
Tanitsak Jirawuttanakit:
I am implementing the EA with mql5. I would like to know how can I retrieve the most recent open position. As I am using buy limit and buy stop, it can't use the highest ticket number to refer to the latest open position. What function should I use? Could anyone please give any suggestion?

You can make use of PositionsTotal() and OrdersTotal() - check out https://www.mql5.com/en/docs/trading. (Or, if you're more adventurous, can also check out  https://www.mql5.com/en/docs/standardlibrary/tradeclasses).

Time comparison will be necessary, rather than ticket number.

Documentation on MQL5: Trade Functions
Documentation on MQL5: Trade Functions
  • www.mql5.com
An order is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels.A deal is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is...
 
Tanitsak Jirawuttanakit:
I am implementing the EA with mql5. I would like to know how can I retrieve the most recent open position. As I am using buy limit and buy stop, it can't use the highest ticket number to refer to the latest open position. What function should I use? Could anyone please give any suggestion?

Try this
https://www.mql5.com/en/docs/event_handlers/ontrade

Documentation on MQL5: Event Handling / OnTrade
Documentation on MQL5: Event Handling / OnTrade
  • www.mql5.com
//|                                               OnTrade_Sample.mq5 | //|                        Copyright 2018, MetaQuotes Software Corp. | //|                                             https://www.mql5.com | //| Expert initialization function                                   |...
 
Thanks to every of you. I really appreciate every suggestion. I got the answer already. I think I will use time compare of each position or OnTrade(). Thanks you guys a lot :)
Reason: