Page 1 of 1

Counting stops in an MT5 EA with OnTradeTransaction: DEAL_REASON_SL is not always a loss

Posted: Sat Oct 24, 2026 7:55 pm
by Fairman
Counting stops in an MT5 EA with OnTradeTransaction: DEAL_REASON_SL is not always a loss

A common request is an EA that pauses after three stop-outs in a row. OnTradeTransaction is the place to detect them, because it fires for each deal as it happens, including closes made by the server when a stop or target triggers.

The skeleton:
void OnTradeTransaction(const MqlTradeTransaction &t, const MqlTradeRequest &r, const MqlTradeResult &res)
{
if(t.type != TRADE_TRANSACTION_DEAL_ADD) return;
if(!HistoryDealSelect(t.deal)) return;
if(HistoryDealGetInteger(t.deal, DEAL_ENTRY) != DEAL_ENTRY_OUT) return;
if(HistoryDealGetInteger(t.deal, DEAL_MAGIC) != MyMagic) return;
double net = HistoryDealGetDouble(t.deal, DEAL_PROFIT) + HistoryDealGetDouble(t.deal, DEAL_COMMISSION) + HistoryDealGetDouble(t.deal, DEAL_SWAP);
if(net < 0) lossStreak++; else lossStreak = 0;
}

I count the sign of the net result and not DEAL_REASON_SL, because the reason only says what closed the deal. A trailing stop that has moved to +12 pips and then gets hit still has the reason DEAL_REASON_SL, and it is a winner. Counting those as stops would put you in a pause after three winning trails. Check the reason when you want to know how trades end, and the net amount when you want to know whether they lost.

Re: Counting stops in an MT5 EA with OnTradeTransaction: DEAL_REASON_SL is not always a loss

Posted: Sat Oct 31, 2026 5:38 am
by Fairman
Filter by symbol as well as magic number if the EA runs on several charts with one magic. Add a check on HistoryDealGetString(t.deal, DEAL_SYMBOL) against the chart's symbol, or keep a separate streak per symbol in an array. Without it, a loss on GBPUSD can pause the EURUSD instance, which may be what you want or not, so decide on purpose. Also decide what counts as a streak. Three losses across a Friday close and a Monday open might not be the same situation as three in an hour. A timestamp stored with each loss lets you reset the count after, say, twelve hours without a trade, instead of carrying a stale streak into a new week.

Re: Counting stops in an MT5 EA with OnTradeTransaction: DEAL_REASON_SL is not always a loss

Posted: Sat Oct 31, 2026 6:18 am
by Fairman
Closes come from more than stops and targets. DEAL_REASON_SO marks a stop out from low margin, DEAL_REASON_CLIENT a manual close from the terminal, and others cover expert, mobile and web closes. For the streak, the net sign treats them all alike, which is usually right. For reporting, count them apart. A manual close at -4 pips is not the strategy failing, it is you intervening, and it should not trigger an automatic pause meant for the system's own losses. Read the reason with HistoryDealGetInteger(t.deal, DEAL_REASON) and keep one counter for system exits, one for manual, and one for stop outs, which should always be zero on a properly sized account.

Re: Counting stops in an MT5 EA with OnTradeTransaction: DEAL_REASON_SL is not always a loss

Posted: Sat Oct 31, 2026 6:48 am
by Fairman
The counter lives in memory, so a terminal restart or EA reload wipes it. Rebuild it at start from history instead of persisting a number. In OnInit call HistorySelect with the start of the trading day or week, loop over the deals from newest to oldest, skip entries that are not DEAL_ENTRY_OUT or do not match the magic, and count losses until you meet a win. For a three loss pause that is at most a few deals to read. This is more reliable than a stored value, since it matches what the account actually did even when trades were closed while the EA was off, and it cannot go out of sync with the real history.

Re: Counting stops in an MT5 EA with OnTradeTransaction: DEAL_REASON_SL is not always a loss

Posted: Sat Oct 31, 2026 7:33 am
by Fairman
Test it on a demo with a short stop. Place the EA on a fast pair with a 3 pip stop and force three entries in a row, or place three manual trades with a 3 pip stop, setting the EA's magic input to 0 for the test so manual deals count, and let them stop out, which happens within minutes in a volatile hour. The Experts tab should show the streak going 1, 2, 3 and the pause message after the third. Then close one trade at a small profit by hand and check that the streak resets to zero. Repeat once with a trailing stop that closes at a profit. If the streak rises on that one, you are counting the reason instead of the net, and it is better to find that on a demo than three losses into a live week.

Re: Counting stops in an MT5 EA with OnTradeTransaction: DEAL_REASON_SL is not always a loss

Posted: Sat Oct 31, 2026 11:58 am
by FXS HongKong Scalper
Good point about DEAL_REASON_SL. A trailing stop that has moved into profit closes with the same reason, so counting it as a loss gives the wrong picture. Checking DEAL_PROFIT plus commission and swap, and counting only those below zero, fixes it. It is also worth handling partial closes, since each creates its own deal.