Search code examples
mql4

How to check if a ticket is still open or has been closed?


If I open a position with OrderSend setting a take profit and a stop loss, how can I check if it is still open or, on the contrary, it has been closed because of the stop_loss or the take profit?


Solution

  • you must have ticketId that you received when you send OrderSend() request.
    In order to figure out whether the trade is open or not, use the following:

     int ticket; //your ticket from OrderSend in global variables
     bool isOrderExist(const int _ticket){
        if(OrderSelect(_ticket,SELECT_BY_TICKET)){
           return(OrderCloseTime()==0);
        }else{
           int error=GetLastError();
           return(error!=4108 && error!=4051);
        }
     }
    

    In order to check if the order is closed by SL or TP or any other reason - you should select the order from the OrdersHistory() and then check comment (often '[sl]' or '[tp]' is added to comment) or compare close price and SL and TP