Many traders want their EA to stop after a run of losing trades, cool off, and resume later. A daily loss limit covers money, but a streak breaker covers behaviour: if the market regime changed, three or four losses in a row is often the first sign.
This post shows a small, restart safe way to do it using the deal history instead of a counter in memory.
Why not a simple counter
A global variable like int losses = 0; resets every time the EA is reloaded, the terminal restarts, or you change an input. The history does not. Counting from history means the breaker survives restarts.
Counting the current streak
We walk closed deals from newest to oldest, look only at exits for our magic number, and stop at the first winner.
input long InpMagic = 12345;
input int InpMaxLosses = 4;
input int InpCoolOffSecs = 43200; // 12 hours
int CurrentLossStreak(datetime &lastLossTime)
{
lastLossTime = 0;
if(!HistorySelect(TimeCurrent() - 2592000, TimeCurrent()))
return 0;
int streak = 0;
for(int i = HistoryDealsTotal() - 1; i >= 0; i = i - 1)
{
ulong deal = HistoryDealGetTicket(i);
if(deal == 0) continue;
if(HistoryDealGetInteger(deal, DEAL_MAGIC) != InpMagic) continue;
if(HistoryDealGetString(deal, DEAL_SYMBOL) != _Symbol) continue;
long entry = HistoryDealGetInteger(deal, DEAL_ENTRY);
if(entry != DEAL_ENTRY_OUT && entry != DEAL_ENTRY_OUT_BY) continue;
double net = HistoryDealGetDouble(deal, DEAL_PROFIT)
+ HistoryDealGetDouble(deal, DEAL_SWAP)
+ HistoryDealGetDouble(deal, DEAL_COMMISSION);
if(net >= 0.0) break; // first winner ends the streak
if(streak == 0)
lastLossTime = (datetime)HistoryDealGetInteger(deal, DEAL_TIME);
streak = streak + 1;
}
return streak;
}
Note the net calculation includes swap and commission. A trade that closed slightly green on price but red after costs is a loss for this purpose.
The gate
bool StreakAllowsTrading()
{
datetime lastLoss;
int streak = CurrentLossStreak(lastLoss);
if(streak < InpMaxLosses) return true;
datetime resumeAt = lastLoss + InpCoolOffSecs;
if(TimeCurrent() >= resumeAt) return true;
static datetime lastLog = 0;
if(TimeCurrent() - lastLog > 3600)
{
PrintFormat("Loss streak %d, paused until %s", streak, TimeToString(resumeAt));
lastLog = TimeCurrent();
}
return false;
}
Call StreakAllowsTrading() before you look for new entries. It does not touch open positions, so existing trades keep their stops and targets.
Things to watch
- Partial closes create several exit deals for one position. If you scale out, group deals by DEAL_POSITION_ID first, or one losing trade can count as two.
- The 30 day window (2592000 seconds) is a performance choice. If your EA trades rarely, widen it.
- After the cool off ends, the streak is still four. The breaker only lets the EA trade again because time has passed. If the next trade also loses, the pause starts over, which is usually what you want.
- Test it in the Strategy Tester with a deliberately bad parameter set so you can see the pause in the journal.
A breaker like this will not make a bad strategy good. It just limits the damage while you find out that it is bad.
Top comments (0)