Loading…

Terminal Risk Kernel

Running several EAs on one terminal usually means each one computes its own idea of "how much risk is left" independently, and they disagree - one EA can keep opening new positions while another has already decided the day's risk budget is exhausted. This kernel is meant to be the one place that question gets answered: attach it once per terminal, and it publishes a small, documented set of GlobalVariables (prefix TRK_) that any other EA can read before sizing or sending an order. It publishes a kill-state flag (an operator override, a breached daily loss limit, or an unbounded-risk condition from a stopless open position all set it), the day's remaining loss budget in both money and percent, a heartbeat timestamp so a consumer can detect a kernel that has stopped running, and a per-symbol maximum new lot size for a configurable watch list, each derived from a configurable risk percent and this same remaining budget. Consistent with the rest of this catalogue's advisory publishing tools (see Daily Loss Guarantee Sizer), nothing in MQL4 lets one EA intercept or resize another EA's own OrderSend call - the contract here is a convention, not an enforcement mechanism, and this document says so plainly. This catalogue's Libraries\KillSwitchProtocol\ and Libraries\StructuredGvSharedState\ folders were checked for an existing GlobalVariable naming convention to align with, since a second, incompatible naming scheme in the same catalogue would confuse downloaders trying to wire tools together; at the time this project was written both folders existed but contained no header file yet, so this kernel defines its own convention (prefix TRK_) rather than guessing at one that does not exist. It deliberately does not reuse Daily Loss Guarantee Sizer's DLGS_ prefix, since that project is a narrower, single- symbol sizing helper and this kernel is a wider-scope, dedicated publisher. Notes and limitations - Advisory only. It cannot force any EA to read or respect the published GlobalVariables, and it cannot intercept or resize an order another program sends. - If any open position anywhere on the account has no stop-loss, the day's worst case is unbounded and TRK_Valid is published as 0; every per-symbol max lot is also zeroed in that state rather than publishing a number that understates the real risk. - On OnDeinit (detaching the kernel, or the terminal closing), TRK_ KillState is set to 1 and TRK_Valid to 0 so a consumer checking the heartbeat cannot mistake a frozen, stale "all clear" for a live one - check TRK_HeartbeatTime against TimeCurrent() for staleness too. - Day-start balance is recorded the first time the kernel sees a given server-side calendar day after attaching - if attached mid-day, that day's start balance is balance at attach time, not balance at actual midnight. - The per-symbol max lot figure assumes InpAssumedStopLossPips as the stop distance; a consuming EA using a materially different stop should treat it as a rough cap, not a precise risk-equivalent size.

Expert advisor · MT4 · v1.0 · Free

About Terminal Risk Kernel

Terminal Risk Kernel is a script for MetaTrader 4. It is set up for gold (XAUUSD). It has 7 settings, such as Daily loss limit percent, Max account risk percent and Watched symbols. MQL source code included.

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

Settings

SettingDefaultWhat it does
Daily loss limit percent4.0Maximum acceptable account loss today, as a percent of the day's starting balance
Max account risk percent10.0Hard kill threshold - total floating loss across the account, as a percent of equity
Watched symbolsEURUSD,GBPUSD,USDJPY,XAUUSDComma-separated symbols to publish a per-symbol max lot for
Per symbol risk percent1.0Risk percent used to derive each watched symbol's published max new lot size
Assumed stop loss pips30Template stop distance used for the per-symbol max lot calculation
Scan interval sec5Seconds between recalculations
Manual kill switchfalseOperator override - set true to force TRK_KillState = 1 regardless of computed risk

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: Risk & Lot Size · Gold (XAUUSD) · MT4 Expert Advisor · MQL4 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria