How to Code a Prop Firm Daily Loss Limit in an EA
A prop firm daily loss limit in an EA is only as reliable as its arithmetic: the EA must measure the loss exactly as the firm does, from the same reference and reset time, and stop with room to spare. This note covers how the guard is coded in MQL5, how its buffer is sized and how to prove it acts in time.
QuantFX7 min read
Balance, equity and the reference level
A daily loss limit, which some rulebooks call the daily drawdown rule, is a floor for one trading day: a reference fixed at the reset, minus an allowed loss set as a share of the initial balance or of that day's reference. Rulebooks use one of three references, and an EA built for one is wrong for the others:
- Start-of-day balance. Closed results only; positions carried over the reset do not move the floor.
- Start-of-day equity. Balance plus the floating result of open positions, so a trade carried overnight at a loss lowers the floor.
- The higher of the two. The strictest form: a floating gain at the reset raises the floor and can vanish before anything closes.
Most firms then compare the floor with equity, so a floating loss counts the moment it appears, and a position that touches the floor and recovers has still breached. In MT5 the guard reads AccountInfoDouble(ACCOUNT_EQUITY), the platform's own figure from the broker's prices, rather than rebuilding equity itself.
Server time and the daily reset
The reset is a clock time in a named time zone, usually midnight, and often neither the broker's server time nor the terminal computer's clock. In MQL5, TimeCurrent() is the time of the last quote, TimeTradeServer() the terminal's estimate of server time and TimeGMT() a value derived from the local clock, so the guard converts the firm's reset into server time explicitly.
Europe and the United States change their clocks on different Sundays: the United States on the second Sunday of March and the first Sunday of November, the EU on the last Sunday of March and the last Sunday of October. For two to three weeks in March and for one week around the turn of October and November, the gap between New York time and a European server moves by an hour, and a hard-coded offset resets an hour early or late.
Detect the reset with a timer, since a quiet market delays the first tick of the day, and expect equity to dip around the rollover, when spreads widen and swaps are booked, just as the reference is taken.
How the EA enforces a prop firm daily loss limit
We build the guard as a separate layer above the strategy: the strategy proposes trades, the guard can refuse, shrink or close them, and the strategy cannot overrule it. It has four parts.
Check before every entry
Before an order goes out, the guard adds the loss each open position would take at its stop, the new trade's loss at its stop and a slippage allowance per position. If the total breaks the internal limit, the trade is refused or reduced: for an equity rule, reacting after the loss is too late.
Close out and lock
Past a soft limit the EA stops opening trades. Past the hard limit it deletes pending orders, closes every position at market and locks until the next reset, retrying requotes, logging refusals such as TRADE_RETCODE_MARKET_CLOSED and keeping the lock even when a close fails.
Size the buffer
The internal limit sits above the firm's floor by a buffer covering everything between decision and last fill: the spread at the worst hour, slippage on every close, exit commission and the seconds between the EA's snapshot and the firm's. Size it per instrument and broker; gold at the New York open needs more than EURUSD at midday.
Survive a restart
A terminal restart wipes memory. The guard writes its reference, its day and its lock flag to a state file on every change, rebuilds the day's closed result from deal history with HistorySelect() on start-up and will not trade until the two agree.
Trailing drawdown in an EA
A static maximum loss is a fixed floor: the initial balance minus the allowed loss. A trailing limit rises with the highest level reached, so the room is measured from the peak; some firms stop trailing at the starting balance, others move the peak only at each day's close.
For a trailing drawdown EA the question is which peak counts. If the firm trails equity, a trade that runs up and falls back lifts the floor on the way up and spends the room on the way down, with nothing closed. The EA tracks that peak on every update and stores it. Why drawdown deserves this care is covered in why we rank drawdown first.
A worked example
An example 100,000 USD account, not any firm's actual rules: a daily limit of 5% of the initial balance (5,000), measured on equity against the higher of balance and equity at a midnight server-time reset. At the reset the balance is 101,200 and an open position shows a floating gain of 600, so the reference is 101,800 and the firm's floor 96,800. The EA stops new entries at a 3,500 loss and closes everything at 4,250, keeping 750 for spread, slippage and commission.
| Guard measures from | Hard stop | Equity after close-out | Against 96,800 |
|---|---|---|---|
| Higher of the two: 101,800 | 97,550 | 97,370 | 570 above |
| Balance only: 101,200 | 96,950 | 96,770 | 30 below: breach |
Both close-outs assume the same 180 of spread and slippage.
Same buffer, same strategy: the second EA breaches because it measured from the balance, which put its own floor 600 too low. No buffer rescues a guard measuring from the wrong level, so our specification states how each limit is measured and you confirm it before any code is written.
Testing the daily drawdown rule in MT5
Ordinary backtests rarely reach the limit, so breach tests push each rule over it on purpose, in three layers above unit tests of the guard's arithmetic:
- Strategy tester runs on every tick based on real ticks, with an execution delay set, where a harness opens the maximum exposure just before the reset and drives it into loss.
- Custom symbols built from edited tick data, to stage a gap through the floor, a spread spike at the rollover or a market that closes with positions open.
- A demo forward test on the firm's server time, comparing the EA's daily floor with the firm's dashboard every day.
Each test states its expected outcome before it runs; a guard that acts late goes back to the code, not to a wider buffer.
The audit log
Every risk decision should leave a log line with server time and UTC, balance, equity, reference, floor, action and reason. The EAs we build log each decision with its time and reason, so the question of why an EA stood aside at 14:32 has a one-line answer.
What to ask a developer
Whoever builds the guard, ask:
- Which reference does the guard use, and where is it written down?
- Which time zone is the reset in, and what happens when clocks change?
- Is risk checked at the stops before each entry, or only after a loss?
- Can I see a test report that breaches each rule on purpose?
In practice at QuantFX
We build prop firm EAs with the firm's rules in one specification alongside the strategy, each rule mapped to the code that enforces it and tested with scenarios built to breach it before a demo forward test. You receive the source code, the compiled EA, the test reports and the forward-test log, as with every custom MT5 Expert Advisor we build.
Builds are priced by class, from £6,000 with a 30% deposit: see the prices and start. The guards are built to stop the EA before a rule is breached; no code stops a market gapping through the floor. We are not affiliated with any prop firm.
Prop firm daily loss limits: questions we are asked
Is a prop firm daily loss limit measured on balance or equity?
Most firms compare the floor with equity, so floating losses count as they happen. The reference varies: start-of-day balance, start-of-day equity or the higher of the two, so the firm's exact definition belongs in the written specification.
Does a daily loss guard mean the account cannot breach the rule?
No. A weekend gap or a news spike can move the price through the floor before any order fills. Pre-trade checks and a buffer make a breach much less likely, not impossible.
What is the difference between static and trailing drawdown?
A static limit is a fixed floor set from the initial balance. A trailing limit rises with the highest balance or equity reached, so the room shrinks after a run-up; if the firm trails equity, open gains raise it too.
Can you add a daily loss guard to an EA I already have?
Yes, if you have the source code; we cannot work from a compiled .ex5 or .ex4 file. The guard is added as a separate layer that overrules the strategy, and the work is quoted after we read the code.
Do commissions and swaps count towards the daily loss?
Usually, yes: they reduce balance and equity like any other loss. Swaps are booked at the rollover, often near the daily reset, so the buffer must allow for them.
Trading leveraged products carries a high risk of loss. This article is general engineering information, not advice on whether or how to trade. QuantFX is not affiliated with any prop firm.