Forum on trading, automated trading systems and testing trading strategies
No DEAL_ENTRY_OUT? How do I determine a deal closes a Position?
TraderTogami, 2025.01.06 03:36
for(int i = total_deals - 1; i >= 0 && !found_position; i--){ ulong curr_ticket = HistoryDealGetTicket(i); bool ticket_accessible = HistoryDealSelect(curr_ticket);
HistoryDealSelect() clears in a mql5-program the list of deals available for reference, and copies the single deal, if the execution of HistoryDealSelect() has been completed successfully. If you need to go through all deals selected by the HistorySelect() function, you should better use HistoryDealGetTicket().
Forum on trading, automated trading systems and testing trading strategies
No DEAL_ENTRY_OUT? How do I determine a deal closes a Position?
TraderTogami, 2025.01.06 03:36
found_position = ticket_accessible && curr_id == id && deal_entry == DEAL_ENTRY_OUT;
Just the EA "Magic" Number?
Edit: Whoops didn't include that line (this is a simplified function of all the data I look at up. Include this line in the loop:
ulong curr_id = (ulong) HistoryDealGetInteger(curr_ticket, DEAL_MAGIC);
Anybody else having this issue? If not, it might be a version/build problem.
Just print out all properties of the deals and see. Probably your positions closed by SL/TP (some brokers/markets may clear magics for these) or inverted by DEAL_ENTRY_INOUT, or collapsed by DEAL_ENTRY_OUT_BY.
Just print out all properties of the deals and see. Probably your positions closed by SL/TP (some brokers/markets may clear magics for these) or inverted by DEAL_ENTRY_INOUT, or collapsed by DEAL_ENTRY_OUT_BY.
Thanks for your time! Here is a print out of a buy deal corresponding to the closure of a sell position. The Loop finds the ticket and corresponding deal just fine, but the DEAL_ENTRY type is still DEAL_ENTRY_IN. Code used to print said values (For some reason the spacing gets weird when you copy code, Edit: Fixed I think):
void get_most_recent_trade_data_v2(const datetime from, const ulong id, bool& found_position, double& profit){ HistorySelect(from, TimeCurrent()); int total_deals = HistoryDealsTotal(); found_position = false; for(int i = total_deals - 1; i >= 0 && !found_position; i--){ ulong curr_ticket = HistoryDealGetTicket(i); profit = HistoryDealGetDouble(curr_ticket, DEAL_PROFIT) + HistoryDealGetDouble(curr_ticket, DEAL_COMMISSION); ulong curr_id = (ulong) HistoryDealGetInteger(curr_ticket, DEAL_MAGIC); ENUM_DEAL_ENTRY deal_entry = (ENUM_DEAL_ENTRY) HistoryDealGetInteger(curr_ticket, DEAL_ENTRY); ENUM_DEAL_TYPE deal_type = (ENUM_DEAL_TYPE) HistoryDealGetInteger(curr_ticket, DEAL_TYPE); datetime deal_time = (datetime) HistoryDealGetInteger(curr_ticket, DEAL_TIME); PrintFormat("Found Ticket: %llu with ID: %llu, Looking for Tickets with ID: %llu", curr_ticket, curr_id, id); PrintFormat("Deal Entry Type: %s, Ticket #: %llu, Deal Type: %s Deal Time: %s", EnumToString(deal_entry), curr_ticket, EnumToString(deal_type), TimeToString(deal_time)); found_position = curr_id == id && deal_entry == DEAL_ENTRY_OUT; } }
Thanks for your time! Here is a print out of a buy deal corresponding to the closure of a sell position. The Loop finds the ticket and corresponding deal just fine, but the DEAL_ENTRY type is still DEAL_ENTRY_IN. Code used to print said values (For some reason the spacing gets weird when you copy code, Edit: Fixed I think):
Closing a position by a SL or TP doesn't trigger DEAL_ENTRY_OUT.
Try to check:
HistoryDealGetInteger(curr_ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT || HistoryDealGetInteger(curr_ticket, DEAL_REASON) == DEAL_REASON_SL || HistoryDealGetInteger(curr_ticket, DEAL_REASON) == DEAL_REASON_TP
Please show the sample of your test trade history as shown by MetaTrader, that shows "ins" and "outs". Here is an example of only the deals ...
Then show debugging log output from your program for the same deals shown in your trade history.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use

I am running into an issue, where every time I request the DEAL_ENTRY, only DEAL_ENTRY_IN is returned. This occurs no matter if I am looking at a ticket for the entry of a position vs the ticket for the exit of a position. Based on some research, this may be a bug. If this is correct, what is the work around? If this is expected behavior, how am I supposed to tell if a Deal is responsible for the closure of a position? Related code found below (simplified). Note: it is not the "from" parameter causing an issue because I have confirmed that all available tickets are being found no problem by printing out necessary information. "from" is to make sure there is not redundancy in my search for a slight speed up.