Strategy Capacity Governor
Every strategy has a size at which its own order flow starts to move the price against itself. Below that size the fill is clean; above it, the average slippage creeps up trade after trade. Most sizing tools ignore this entirely and let a strategy scale until the broker or the account balance stops it, by which point the strategy has already been quietly paying for its own size for a while. This governor measures the relationship directly from history instead of assuming a fixed capacity figure. For each magic number it walks its most recent closed trades, estimates each one's entry slippage as the distance between the actual fill and the M1 close nearest that order's open time - a proxy for "what the market was printing right before this order landed" - and buckets the results by lot size. The capacity cap for that magic is the largest lot size reached while the running average slippage across sizes up to that point still sits within the configured budget. The cap is published as a GlobalVariable per magic number (STRATEGYCAP_MAGIC_<n>) so any other EA sharing that magic can check it before opening. This governor itself never opens or fully closes a trade - its only direct action, when enabled, is trimming an open position that is already bigger than the current cap back down to size via a partial close, which is the same enforcement a human risk manager would apply by hand after noticing a strategy has outgrown its own edge. Notes and limitations - Slippage is an estimate against the nearest M1 close, not a broker- reported figure - MQL4 does not expose the requested price separately from the filled one, so this is the closest honest proxy available. - A magic with fewer than InpMinSamplesPerBucket trades in history is treated as unrestricted (cap = 0, meaning no limit) rather than guessed. - Enforcement only ever trims volume down via a partial close; it never closes a position outright and never opens one. - The published GlobalVariable convention (STRATEGYCAP_MAGIC_<n>) is only useful if the strategies sharing that magic are written to check it before sizing - this EA cannot force a foreign strategy to respect it.
Expert advisor · MT5 · v1.0 · Free
About Strategy Capacity Governor
Strategy Capacity Governor is an expert advisor (trading robot) for MetaTrader 5. It has 9 settings, such as Magic list, History lookback trades and Max slippage points. MQL source code included.
Files in the download: StrategyCapacityGovernor.ex5, StrategyCapacityGovernor.mq5, StrategyCapacityGovernor.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Magic list | Comma-separated magic numbers to govern; empty = every magic seen | |
| History lookback trades | 30 | Closed trades per magic used to measure the slippage-vs-size curve |
| Max slippage points | 15.0 | Ceiling on acceptable average slippage, in points, at the capacity size |
| Min samples per bucket | 3 | Minimum trades a size bucket needs before it counts toward the curve |
| Enforce on open positions | true | Partially close open positions that already exceed the published cap |
| Timer interval sec | 60 | How often history is rescanned and the cap republished |
| Max retries | 5 | Attempts per partial-close before giving up |
| Retry wait ms | 500 | Pause between retry attempts, milliseconds |
| Dry run | false | true = compute and log caps, never actually closes any volume |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
- Test it first on a demo account or in the Strategy Tester.
Tags: Risk & Lot Size · MT5 Expert Advisor · MQL5 Source Code