I needed a reliable way to tell whether a position left the account because of Stop Loss, Take Profit, Stop Out, or an EA/manual close, including partial closes. Polling `PositionsTotal()` only tells you that something disappeared. The deal in history has the reason.
The hook is `OnTradeTransaction`, looking at `TRADE_TRANSACTION_DEAL_ADD`, then `HistoryDealSelect`, then `DEAL_ENTRY` and `DEAL_REASON`.
`DEAL_REASON_SL`, `DEAL_REASON_TP` and `DEAL_REASON_SO` are the three that matter for a guard. `DEAL_ENTRY_OUT` with a volume smaller than the position is a partial close. `DEAL_FEE` is a documented deal property and should be included with commission and swap if you are summing money.
Two traps:
If a position is closed manually, `DEAL_MAGIC` on the outgoing deal can be 0 even though the position was opened by the EA. Filtering only on magic will miss that close. I treat magic as a filter for EA exits, and I use `DEAL_POSITION_ID` (stored at open) when I also need to see manual exits of "my" positions.
`OnTradeTransaction` does not replay deals that happened while the EA was not running. On `OnInit` I still walk history from the last stored deal ticket and reconcile, otherwise a restart in the middle of a close is invisible.
I am interested in whether anyone has a clean pattern for the "closed while the EA was down" case, and whether you trust `DEAL_REASON_EXPERT` to mean "my EA" when several experts are running on the same account.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
I needed a reliable way to tell whether a position left the account because of Stop Loss, Take Profit, Stop Out, or an EA/manual close, including partial closes. Polling `PositionsTotal()` only tells you that something disappeared. The deal in history has the reason.
The hook is `OnTradeTransaction`, looking at `TRADE_TRANSACTION_DEAL_ADD`, then `HistoryDealSelect`, then `DEAL_ENTRY` and `DEAL_REASON`.
`DEAL_REASON_SL`, `DEAL_REASON_TP` and `DEAL_REASON_SO` are the three that matter for a guard. `DEAL_ENTRY_OUT` with a volume smaller than the position is a partial close. `DEAL_FEE` is a documented deal property and should be included with commission and swap if you are summing money.
Two traps:
I am interested in whether anyone has a clean pattern for the "closed while the EA was down" case, and whether you trust `DEAL_REASON_EXPERT` to mean "my EA" when several experts are running on the same account.