Loading…

Daily Loss Limit Guardian

A daily loss limit is one of the simplest risk rules to state and one of the easiest to break in the moment, because enforcing it by hand means correctly adding up realised and floating loss under pressure and then actually clicking close on everything. This EA does the arithmetic and the flattening. The total tracked is realised loss (closed trade history since server midnight) plus floating loss (open positions right now), never realised alone. A trader who has banked a loss on one position and is sitting on a large floating loss on another has the same account risk as one who has banked both losses; a guard that only watches closed history would miss exactly the situation it exists to catch. The limit can be a fixed money amount or a percentage of the balance recorded at the start of the server day - deliberately the start-of-day balance, not the live balance. A percent limit measured against a balance that is falling throughout the day would shrink as losses accrue, tightening the guard's own threshold mid-day in a way that has nothing to do with the trader's original intent. The start-of-day balance is captured once, at the first tick after attach or at server-day rollover, and held fixed until the next rollover. Server-day rollover is tracked with a plain boolean flag and a stored midnight timestamp, checked every tick. The comparison is a couple of integer operations, so checking it constantly costs nothing measurable, and a risk guard that only samples occasionally is a guard that can let losses run past the limit between samples. Once triggered, the EA closes every open position it manages and sets the blocked flag for the remainder of the server day. It keeps checking every tick after that and will close any position that appears afterwards too - useful if a manual trade or another EA opens something after the trip - but it cannot reach into another EA's OrderSend call to prevent that trade in the first place; "blocks new ones" means the chart comment and journal state plainly that new trades are not safe to place, and any position that does appear is flattened again on the next tick. Notes and limitations - This EA cannot prevent another program's trade request directly - neither platform has a cross-EA trade veto. The block is communicated through the chart comment and the journal, and enforced by immediately closing anything that does appear while blocked. Treat the comment as the source of truth for "should I be trading right now." - Server-day rollover uses TimeCurrent(), the trade server's clock, not the local machine's. This matches how the broker itself defines "today" for swap and history purposes. - Only one instance's blocked state exists at a time, in that instance's memory. Removing the EA, recompiling it, or restarting the terminal clears the flag; on the next tick after restart it is recalculated from scratch from realised history and current floating loss, which in practice reproduces the same trigger if the underlying loss is still there. - The forced close uses a wider slippage tolerance than a normal exit, because at that point the priority is getting flat, not getting the best price. - If a position cannot be closed (market closed, broker rejects every retry), it is reported by ticket in the journal so it can be closed by hand - the EA does not pretend the flatten succeeded. - A percentage limit is computed once per day from the start-of-day balance; deposits or withdrawals during the day change the live balance but not that day's already-fixed limit. - The MQL5 version's entry-commission lookup assumes each position closes in one exit deal (no partial closes) when attributing that commission to "today's" realised loss - see MQL4 VERSUS MQL5 DIFFERENCES above.

Expert advisor · MT4 · v1.0 · Free

About Daily Loss Limit Guardian

Daily Loss Limit Guardian is a script for MetaTrader 4. It has 7 settings, such as Limit type, Limit money and Limit percent. MQL source code included.

Files in the download: DailyLossGuard.ex4, DailyLossGuard.mq4, DailyLossGuard.txt.

Settings

SettingDefaultWhat it does
Limit typeLIMIT_PERCENTLimit type
Limit money100.0Loss limit, account currency (LIMIT_MONEY)
Limit percent3.0Loss limit, percent of start-of-day balance (LIMIT_PERCENT)
Magic filter-1Magic number filter for positions this EA closes (-1 = every magic)
Current symbol onlyfalseRestrict closing to this chart's symbol
Max retries5Attempts per close before giving up
Retry wait ms500Pause between attempts, milliseconds

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT4 Expert Advisor · MQL4 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria