Loading…

Exposure Limits Rulebook

Most risk gates in published EAs are a handful of hardcoded if-statements buried in OnTick - "if lots > 1.0 return", "if OrdersTotal() > 5 return". That holds up until the trader wants a tighter cap on JPY pairs during the Asian session, or a different total-exposure limit for one strategy's magic number than another's, and the if-statements start multiplying and contradicting each other. This header turns those checks into a small declarative rulebook instead. Each rule names the scope it applies to - currency, asset class, session, strategy magic number, any combination of which can be wildcarded to "any" - and a limit it enforces: maximum net lots, maximum approximate notional, or maximum number of open positions. AddRule() builds the table once at startup; Evaluate() is called before every proposed order and walks every rule whose scope matches, aggregating the relevant open exposure plus the proposed order on top, and returns the single most restrictive verdict found across every rule that fired - BLOCK beats WARN beats ALLOW - along with the name of the rule responsible and a human-readable reason. Session scoping resolves from server hour through simple, explicit bands (Asian, London, New York) rather than a broker-timezone lookup table. London and New York deliberately overlap (13:00-16:00 server time), because both desks are genuinely active in that window and an order there should be checked against both sessions' rules, not forced to pick one label. This library is bookkeeping and judgement, not enforcement. Evaluate() never touches OrderSend or any broker call - it only tells the caller what it thinks should happen and why. The calling EA remains responsible for actually refusing to send an order when the verdict is BLOCK. Notes and limitations - Notional is approximate: lots * MarketInfo(symbol, MODE_LOTSIZE), in the contract's own units, not converted through a cross rate into account currency. Treat ELR_LIMIT_MAX_NOTIONAL as a same-quote-currency comparison tool, not an exact account-currency risk figure. - Session bands are fixed, explicit hour ranges (Asian 00-08, London 08-16, New York 13-21, server time). Adjust ElrHourInSession/ElrSessionFromHour directly if your broker's server time offset puts these bands somewhere you don't want them. - The rulebook does not read OrderSelect or any broker state itself - the caller supplies the open-position snapshot on every Evaluate() call. This is deliberate (keeps the header testable and broker-agnostic) but means a stale snapshot gives a stale verdict; refresh it every time before calling. - This is advisory only. Nothing here blocks OrderSend. A BLOCK verdict is only as effective as the calling code that checks for it and refuses to trade.

Script · MT4 · v1.0 · Free

About Exposure Limits Rulebook

Exposure Limits Rulebook is a script for MetaTrader 4. It shows several symbols in one panel. It has 1 setting, such as Server hour. MQL source code included.

Files in the download: ExposureLimitsRulebook.mqh, ExposureLimitsRulebook.txt, ExposureLimitsRulebookDemo.mq4.

Settings

SettingDefaultWhat it does
Server hour10simulated server hour (0-23) used for session scoping in this demo

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: Trading Sessions · Dashboards & Scanners · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria