MQL5 - How to check if Position/Order closed in EA

 

Dears,


first of all let me explain after many years working on MQL4 due to a project team decided to re-create EA on MQL5 again.

As I'm new to this language, I'm looking for some concepts from the old one in this. them please excuse me if my question looks stupid or easy to manage as I couldn't find the proper answer for this


Inside my EA, I'm following the Order and in 2 ways :

1. Close it in specific situation by --> Trade.close() (like original MA expert included in Metatrader5)

2. Putting SL/TP in order during execution

this means an order can be closed due to 2nd option before 1st condition happen. Then i have to control if position is still open or not.

on MQL4 it was easy and (OrderCloseTime==0) showed me it is still open.


How should i find if an order or ticket is still open on MQL5?
 
ArashB:

Dears,


first of all let me explain after many years working on MQL4 due to a project team decided to re-create EA on MQL5 again.

As I'm new to this language, I'm looking for some concepts from the old one in this. them please excuse me if my question looks stupid or easy to manage as I couldn't find the proper answer for this


Inside my EA, I'm following the Order and in 2 ways :

1. Close it in specific situation by --> Trade.close() (like original MA expert included in Metatrader5)

2. Putting SL/TP in order during execution

this means an order can be closed due to 2nd option before 1st condition happen. Then i have to control if position is still open or not.

on MQL4 it was easy and (OrderCloseTime==0) showed me it is still open.


How should i find if an order or ticket is still open on MQL5?

Since in MetaTrader v5 you have Orders, Positions and Deals, you may need to drill down to the Deal that closed off the Position completely, to find the correct closing time and depends somewhat on how you are managing your Positions.

However, if you are looking for a MQL4 to MQL5 conversion framework or just some more practical examples to help with your conversion, consider having a look at the following code by @fxsaber: https://www.mql5.com/en/code/16006

MT4Orders
MT4Orders
  • www.mql5.com
This library allows to work with the orders in MQL5 (MT5-hedge) in the same way as in MQL4. That is, the order language system (OLS) becomes identical to MQL4. At the same time, it is still possible to use the MQL5 order system in parallel. In particular, the standard MQL5 library will continue to fully operate. It is not necessary to choose...
 

Firstly, thanks for your comment and specially for the link you shared.

Also, you proposed a way to use Deal and control close time. can you please give me and example code of checking a closed deal?

 
ArashB: Firstly, thanks for your comment and specially for the link you shared. Also, you proposed a way to use Deal and control close time. can you please give me and example code of checking a closed deal?

Not really because I don't use the CTrade class in my own code and have build my own library, but basically, you would have to use the Position ID (or Order ID) to scan through the Deals associated with it, find the one that made the final close of the position. You obtain the "DEAL_TIME" with HistoryDealGetInteger() function.

I think that with the Trade library you would navigate through the Deals (not sure of the specifics) and use the CDealInfo.Time() method to obtain the closing time. I don't have enough experience with the standard library to give you the exact steps.

EDIT: A post by Marco vd Heijden on another similar thread:

Forum on trading, automated trading systems and testing trading strategies

OrderCloseTime Expert Advisor MQL5

Marco vd Heijden, 2018.07.03 13:42

Look at the example:

Note

Do not confuse orders, deals and positions. Each deal is the result of the execution of an order, each position is the summary result of one or more deals.

Example:

//+------------------------------------------------------------------+ 
//| Trade function                                                   | 
//+------------------------------------------------------------------+ 
void OnTrade() 
  { 
//--- receive the last deal's ticket from week's trading history 
   ulong last_deal=GetLastDealTicket(); 
   if(HistoryDealSelect(last_deal)) 
     { 
      //--- time of deal execution in milliseconds since 01.01.1970 
      long deal_time_msc=HistoryDealGetInteger(last_deal,DEAL_TIME_MSC); 
      PrintFormat("Deal #%d DEAL_TIME_MSC=%i64 => %s", 
                  last_deal,deal_time_msc,TimeToString(deal_time_msc/1000)); 
     } 
   else 
      PrintFormat("HistoryDealSelect() failed for #%d. Eror code=%d", 
                  last_deal,GetLastError()); 
//--- 
  } 
//+------------------------------------------------------------------+ 
//| Returns the last deal ticket in history or -1                    | 
//+------------------------------------------------------------------+ 
ulong GetLastDealTicket() 
  { 
//--- request history for the last 7 days 
   if(!GetTradeHistory(7)) 
     { 
      //--- notify on unsuccessful call and return -1 
      Print(__FUNCTION__," HistorySelect() returned false"); 
      return -1; 
     } 
//---  
   ulong first_deal,last_deal,deals=HistoryOrdersTotal(); 
//--- work with orders if there are any 
   if(deals>0) 
     { 
      Print("Deals = ",deals); 
      first_deal=HistoryDealGetTicket(0); 
      PrintFormat("first_deal = %d",first_deal); 
      if(deals>1) 
        { 
         last_deal=HistoryDealGetTicket((int)deals-1); 
         PrintFormat("last_deal = %d",last_deal); 
         return last_deal; 
        } 
      return first_deal; 
     } 
//--- no deal found, return -1 
   return -1; 
  } 
//+--------------------------------------------------------------------------+ 
//| Requests history for the last days and returns false in case of failure  | 
//+--------------------------------------------------------------------------+ 
bool GetTradeHistory(int days) 
  { 
//--- set a week period to request trade history 
   datetime to=TimeCurrent(); 
   datetime from=to-days*PeriodSeconds(PERIOD_D1); 
   ResetLastError(); 
//--- make a request and check the result 
   if(!HistorySelect(from,to)) 
     { 
      Print(__FUNCTION__," HistorySelect=false. Error code=",GetLastError()); 
      return false; 
     } 
//--- history received successfully 
   return true; 
  }

See also

HistoryDealsTotal(), HistorySelect(), HistoryDealGetTicket(), Deal Properties


 
ArashB:

Dears,


first of all let me explain after many years working on MQL4 due to a project team decided to re-create EA on MQL5 again.

As I'm new to this language, I'm looking for some concepts from the old one in this. them please excuse me if my question looks stupid or easy to manage as I couldn't find the proper answer for this


Inside my EA, I'm following the Order and in 2 ways :

1. Close it in specific situation by --> Trade.close() (like original MA expert included in Metatrader5)

2. Putting SL/TP in order during execution

this means an order can be closed due to 2nd option before 1st condition happen. Then i have to control if position is still open or not.

on MQL4 it was easy and (OrderCloseTime==0) showed me it is still open.


How should i find if an order or ticket is still open on MQL5?

If you are talking about mql4 OrderCloseTime(), I suppose you are knowing the ticket ? Then you just have to use PositionSelectByTicket(ticket) which return a bool, true if the position is still open.

No need to check deals just to know if a position is open, as if it is open there is no "out" deal(s) (with exception of partial closing).

 
Fernando Carreiro:

Not really because I don't use the CTrade class in my own code and have build my own library, but basically, you would have to use the Position ID (or Order ID) to scan through the Deals associated with it, find the one that made the final close of the position. You obtain the "DEAL_TIME" with HistoryDealGetInteger() function.

I think that with the Trade library you would navigate through the Deals (not sure of the specifics) and use the CDealInfo.Time() method to obtain the closing time. I don't have enough experience with the standard library to give you the exact steps.

EDIT: A post by Marco vd Heijden on another similar thread:


Many thanks to your deep and detail answer
 
Alain Verleyen:

If you are talking about mql4 OrderCloseTime(), I suppose you are knowing the ticket ? Then you just have to use PositionSelectByTicket(ticket) which return a bool, true if the position is still open.

No need to check deals just to know if a position is open, as if it is open there is no "out" deal(s) (with exception of partial closing).

Really i'm using code like below :

CTrade      Ticket

...

Ticket.PositionOpen(_Symbol,...)

...

Ticket.PositionClose(_Symbol,5)
by this way should i use PositionSelectByTicket(Ticket)? i'm not sure if Ctrade Ticket is the same as Ticket you mentioned. can you please guide me how to find Ticket No.?
 

I guess that not only for this MQL5 now has this event handler:

void  OnTradeTransaction(
   const MqlTradeTransaction&    trans,        // Struktur der Handelstransaktion
   const MqlTradeRequest&        request,      // Struktur der Anfrage
   const MqlTradeResult&         result        // Struktur der Antwort
 
You can use PositionGetInteger(POSITION_MAGIC) in a loop to check if a certain trade with a certain magic number exists. If present or maybe not present then some variable equals something.
 
ArashB:

Really i'm using code like below :

by this way should i use PositionSelectByTicket(Ticket)? i'm not sure if Ctrade Ticket is the same as Ticket you mentioned. can you please guide me how to find Ticket No.?

Don't use confusing variable name, your 'Ticket' variable is not a position ticket, it's a CTrade object.

Please post your code if you need coding help, what you are asking for is pretty trivial.

 
Tonny Obare:
You can use PositionGetInteger(POSITION_MAGIC) in a loop to check if a certain trade with a certain magic number exists. If present or maybe not present then some variable equals something.
Thanks... it may help