Loading…

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 · MT5 · v1.0 · Free

About Overnight Window Loss Guard

Overnight Window Loss Guard is an expert advisor (trading robot) for MetaTrader 5. It trades only in the hours you set (time filter) and can size trades from your balance and risk percent. 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.ex5, OvernightWindowLossGuard.mq5, OvernightWindowLossGuard.txt.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
Overnight start hour22Overnight window start, broker/server time, 0-23
Overnight end hour6Overnight window end, broker/server time, 0-23 (wraps past midnight if less than start)
Limit is percenttruetrue = InpOvernightLossLimit is % of window-open balance, false = account currency
Overnight loss limit1.0Loss allowed inside the window before flattening (percent or currency, per InpLimitIsPercent)
Magic filter00 = watch every position/order on the account, otherwise only this magic number
Dry runfalsetrue = alert only when the window limit is breached, never actually flattens
Timer interval sec15How often the window and the loss are checked
Max retries5Attempts per order action 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 5 open File → Open Data Folder, then go to MQL5 → Experts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: Trading Sessions · Time Filter · Risk & Lot Size · MT5 Expert Advisor · MQL5 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria