Loading…

Lockout Policy Objects

"Stop trading after 3 losses in a row" and "stop trading once today's loss hits a limit" are two of the most commonly re-implemented risk rules in EA code, and they are usually re-derived from scratch inside each EA's own OnTick, with the day-rollover logic and the lockout- expiry bookkeeping quietly duplicated (and quietly slightly different) every time. This library packages three such rules - consecutive-loss lockout, daily-loss-limit lockout and a flat post-trade cooldown - into one policy object with a single entry point. LockoutPolicyRecordTradeClose is called once per closed trade; it updates the consecutive-loss streak and the current day's accumulated loss (rolling the day counter over automatically the first time a trade closes on a new calendar day), and arms a time-boxed lockout the moment any configured threshold is crossed. LockoutPolicyIsLockedOut answers "am I locked out right now" and clears an expired lockout as a side effect of being asked, so nothing needs a separate timer tick just to notice the clock ran out. A lockout is never silently shortened: if a cooldown is already active and a bigger trigger (say, the daily loss limit) fires on the same trade close, the longer of the two expiries wins. The daily-loss rule locks for a flat 24 hours from the triggering close rather than trying to compute "until midnight server time" - the day-key rollover on the next RecordTradeClose call resets the loss accumulator independently regardless, so the 24-hour lockout and the day-key rollover do not need to agree on where the day boundary sits. Notes and limitations - Day-rollover is measured on whatever datetime the caller passes as closeTime - normally TimeCurrent(), which is the trade server's time, not necessarily the trader's own local day. Passing a different time source changes what "a new day" means to this policy. - The daily-loss lockout is a flat 24 hours from the triggering trade, not "until the next server-day boundary" - see FULL DESCRIPTION for why that is intentional rather than an oversight. - A lockout can only be extended, never shortened, by a later RecordTradeClose call in the same active lockout window - a 5-minute cooldown trade closing while a 60-minute streak lockout is already active will not cut the streak lockout short. - This library only tracks state and answers yes/no; it does not read the order pool, does not close any position, and does not prevent an EA from calling OrderSend anyway - the caller must actually check LockoutPolicyIsLockedOut before trading. - No day-of-week or session-calendar awareness: weekends and holidays are not treated specially, they simply have no trade closes recorded against them.

Script · MT5 · v1.0 · Free

About Lockout Policy Objects

Lockout Policy Objects is a script for MetaTrader 5. It has 4 settings, such as Max consecutive losses, Consecutive loss lockout minutes and Daily loss limit money. MQL source code included.

Files in the download: LockoutPolicyObjects.mq5.mqh, LockoutPolicyObjects.txt, LockoutPolicyObjectsDemo.ex5, LockoutPolicyObjectsDemo.mq5.

Settings

SettingDefaultWhat it does
Max consecutive losses3consecutive losses before a cooldown lockout
Consecutive loss lockout minutes60lockout length once the streak limit is hit
Daily loss limit money200.0absolute daily loss limit, in account currency
Post trade cooldown minutes5cooldown after every single trade close

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 → Scripts.
  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 script onto a chart to run it once.

Download the PDF guide

Tags: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria