Overnight Window Loss Guard
A prop-firm daily loss limit is usually sized to survive a normal trading session. That same allowance is often far too generous overnight, when nobody is watching and a losing gap can run for hours before anyone notices. This EA layers a second, smaller budget on top: while the clock is inside the configured overnight window, it tracks P/L against a fresh balance snapshot taken the moment the window opened, and flattens the watched orders outright as soon as that smaller limit is reached - it does not wait for morning, and it does not wait for a wider daily rule to also trip. The baseline balance for the window is stored in a GlobalVariable, keyed to the window's own calendar label (which correctly stays "last night's window" even a few hours past midnight for a window that wraps overnight). A terminal restart in the middle of the night does not quietly reset the allowance - the same baseline is picked back up rather than resnapshotted. The window can be scoped to every order on the account or to a single magic number, so it can run either as a whole-account overnight guard or alongside StrategyLossFirewall as a magic-specific overnight layer. Notes and limitations - Once tripped inside a window, it stays tripped for the rest of that window - it does not repeatedly re-flatten an account that is already flat. - A zero-width window (start hour equals end hour) is treated as "always on", which is a deliberate fallback rather than a special case to avoid. - Flattening requires AutoTrading to be enabled; if it is off when the limit is breached, the EA alerts loudly but cannot act, and says so. - Window P/L is measured as current balance plus current floating P/L minus the window's opening balance, so it already reflects every trade closed since the baseline was taken without a separate history scan.
Expert advisor · MT4 · v1.0 · Free
About Overnight Window Loss Guard
Overnight Window Loss Guard is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 9 settings, such as Overnight start hour, Overnight end hour and Limit is percent. MQL source code included.
Files in the download: OvernightWindowLossGuard.ex4, OvernightWindowLossGuard.mq4, OvernightWindowLossGuard.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Overnight start hour | 22 | Overnight window start, broker/server time, 0-23 |
| Overnight end hour | 6 | Overnight window end, broker/server time, 0-23 (wraps past midnight if less than start) |
| Limit is percent | true | true = InpOvernightLossLimit is % of window-open balance, false = account currency |
| Overnight loss limit | 1.0 | Loss allowed inside the window before flattening (percent or currency, per InpLimitIsPercent) |
| Magic filter | 0 | 0 = watch every order on the account, otherwise only this magic number |
| Dry run | false | true = alert only when the window limit is breached, never actually flattens |
| Timer interval sec | 15 | How often the window and the loss are checked |
| Max retries | 5 | Attempts per order action before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Trading Sessions · Alerts · MT4 Expert Advisor · MQL4 Source Code