3

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?

Bub Espinja
  • 4,029
  • 2
  • 29
  • 46

2 Answers2

5

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

Daniel Kniaz
  • 4,603
  • 2
  • 14
  • 20
  • just a note: [sl] is added to the comment even if a stop profit is hit. So if you want to detect if a SL was hit (i.e. the order closed with a loss) you should check if the OrderProfit() is <0 – Val Dec 10 '19 at 15:13
1

You could probably also use: OrderPrint():

if(OrderSelect(10, SELECT_BY_TICKET)==true) 
    OrderPrint(); 
else 
    Print("OrderSelect failed error code is",GetLastError());

From the MetaEditor Reference:

Prints information about the selected order in the log in the following format:

ticket number; 
open time; 
trade operation; 
amount of lots; 
symbol; 
open price; 
Stop Loss; 
Take Profit; 
close time; 
close price; 
commission; 
swap; 
profit; 
comment; 
magic number; 
pending order expiration date.
not2qubit
  • 14,531
  • 8
  • 95
  • 135