How to read yesterday orders by list of tickets and see if order closed by SL or TP

 

I am trying to code EA/Script, so when I start MT5 it use a list of yesterdays tickets of orders I created. and check each order by ticket to see if order still open or if closed by SL or TP.

But no matter what I try I can't find if order closed by TP/SL. This data I can't find in order details, once I select by ticket.

Any ideas how to do that? Let's say for single trade order I created some days ago and I need EA to check. For example order with ticket = 123456.

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

When requesting help with your code, remember that the forum is for helping to learn and improve your coding, not for writing the code for you.

So, show your code and your attempt at solving the issue, explaining how you are trying to solve it.

Here is a hint ...

ORDER_REASON / ENUM_ORDER_REASON

ORDER_REASON_SL

The order was placed as a result of Stop Loss activation

ORDER_REASON_TP

The order was placed as a result of Take Profit activation

DEAL_REASON / ENUM_DEAL_REASON

DEAL_REASON_SL

The deal was executed as a result of Stop Loss activation

DEAL_REASON_TP

The deal was executed as a result of Take Profit activation

Also read the following ...

Articles

Orders, Positions and Deals in MetaTrader 5

MetaQuotes, 2011.02.01 16:13

Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.

 

I found how to do that. it was (I think) over complicated. My order ticket just did not contain data about how order was closed (SL or TP or else).

I managed by follow this logic:

1. I have my order ticket ID - so I go and select that order, pull datetime of the order.

2. I do HistorySelect from order time to now (TimeCurrent). Then read all orders for selected period of time:

for (int i=0; i<HistoryOrdersTotal(); i++) {
      ulong ticket=HistoryOrderGetTicket(i);
      if(ticket==0) continue;
      long thisPos=HistoryOrderGetInteger(ticket, ORDER_POSITION_ID);
      long thisReason=HistoryOrderGetInteger(ticket, ORDER_REASON);
      
      // compare if this order is related to my stored ticket
      if (thisPos == storedTicket) { 
                // check if it is closed by SL or TP, based on thisReason (SL = 4, TP = 5)
      }
}

3. For each of orders I check if is related to my stored ticket and if closed by reason =4 or 5 (SL or TP).


Sorry for my post was wrong category (first time I post, did not see any way to add to proper category).


" When requesting help with your code, remember that the forum is for helping to learn and improve your coding, not for writing the code for you. "

Also sorry I did not post my code, as I had at least 10 code attempts and they were not working, it was more of a question what logic to use and what functions and etc to use to get to working code, not to write me all the code or EA. I'll try be more specific next time and provide code if possible.


thanks.

 
geost77 #: For each of orders I check if is related to my stored ticket and if closed by reason =4 or 5 (SL or TP)
Don't use the numbers 4 or 5. Always use the enumerations in your code, not their values (e.g. ORDER_REASON_SL or ORDER_REASON_TP).