Trim Book To Target Exposure
A book built from several EURUSD, GBPUSD and EURGBP tickets carries USD, EUR and GBP exposure that does not equal any single ticket's lot size - buys and sells on the same currency partly cancel, and a currency can appear as both the base leg of one symbol and the quote leg of another. Deciding "which ticket to close" to bring one currency's exposure down answers the wrong question, because closing a whole ticket also changes exposure to every other currency that ticket touched. This script works out net notional per currency across the whole book, converted into account currency using whatever conversion pair is available in Market Watch, then for any currency over its configured ceiling computes the fraction the book needs to shrink by and applies that same fraction to every position touching that currency with a partial close. A position that touches two over-ceiling currencies at once is trimmed by whichever fraction is tighter, so both ceilings are respected in one pass rather than two conflicting ones. Pro-rata trimming keeps the book's relative shape - every contributing setup stays open, just smaller - rather than the alternative of closing tickets outright in some order until the number happens to fit, which changes which trades are still running, not just how large they are. A trim that would leave a ticket below the broker's minimum lot size is closed in full instead of leaving a dust position neither the broker nor the trader can do anything useful with, and this is stated in the journal when it happens. Notes and limitations - Currency conversion relies on a symbol for that currency pair being visible in Market Watch (either direction). A currency with no conversion path to the account currency cannot be priced and its positions are skipped from the notional calculation with nothing reported - check the journal for skipped symbols if a total looks low. - Symbol names are parsed as the first three and last three characters, which covers standard names and the common broker suffix schemes (EURUSDm, EURUSD.a, EURUSD_i). An unusual naming scheme may parse the currency legs incorrectly; verify the printed base/quote split for an unfamiliar symbol before relying on this for a live account. - Only currencies in InpCurrencyList are policed. A currency not listed is invisible to this script no matter how large its exposure. - This is a one-time trim, not a running exposure manager. Run it again after new positions are opened if the ceiling needs re-checking. - Broker lot step rounds every partial close down, never up, so the resulting exposure after a trim is always at or under the target fraction, never slightly over it from rounding.
Script · MT5 · v1.0 · Free
About Trim Book To Target Exposure
Trim Book To Target Exposure is an expert advisor (trading robot) for MetaTrader 5. It has 6 settings, such as Currency list, Exposure ceiling and Magic filter. MQL source code included.
Files in the download: TrimBookToExposure.ex5, TrimBookToExposure.mq5, TrimBookToExposure.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 |
|---|---|---|
| Currency list | USD,EUR,GBP,JPY,CHF,AUD,CAD,NZD | Currencies to police, comma separated |
| Exposure ceiling | 100000.0 | Ceiling per currency, in account currency notional |
| Magic filter | -1 | Magic number (-1 covers every magic) |
| Confirm first | true | Ask for confirmation before trimming |
| Max retries | 5 | Attempts per position before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
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 · EURUSD · GBPUSD · MT5 Script · MQL5 Source Code