Risk Parity Weight Allocator
An equal-money basket is not an equal-risk basket. A metal or an index CFD can carry several times the daily volatility of a major FX pair, so splitting a risk budget evenly across symbol names quietly hands most of the actual risk to whichever instrument moves the most - while every ticket in the log looks like an equally sized, equally reasonable trade. This library weights each symbol by the inverse of its measured volatility. That weighting is the correct equal-risk-contribution solution under the common simplifying assumption that pairwise correlations across the basket are roughly uniform - a reasonable working assumption for a basket of related instruments, and a wrong one for a basket that mixes genuinely independent and genuinely correlated symbols. Rather than pretend that assumption always holds, the library also computes each asset's actual marginal contribution to total portfolio variance from the full covariance structure - volatility and the real pairwise correlation matrix, not the uniform-correlation shortcut. A perfectly risk-parity basket shows every entry at 1/n of total risk; the gap between that ideal and what the diagnostic actually reports is exactly how much the correlation structure has moved the real risk split away from the inverse-volatility approximation. Full iterative equal-risk-contribution optimisation - solving the nonlinear system that makes every marginal contribution exactly equal - is out of scope here on purpose; it belongs in a dedicated optimiser, not a header meant to be read start to finish, and this one is honest about the trade-off instead of hiding it. Turning a weight into a lot size still needs a stop distance, because "risk" is not defined by weight alone - it is money at risk to a stop. The sizing function takes a per-symbol stop in pips and converts through the broker's own tick value/tick size (MODE_TICKVALUE/MODE_TICKSIZE on MQL4, SYMBOL_TRADE_TICK_ VALUE/SYMBOL_TRADE_TICK_SIZE on MQL5), never an assumed contract size, and rounds down to the broker's lot step - a risk-derived size that rounds up spends more risk than the budget authorised. Notes and limitations - Inverse-volatility weighting is an approximation, not exact equal-risk- contribution allocation. The marginal risk contribution diagnostic exists specifically to show how far off that approximation is for the current basket - read it, do not skip straight to the weights. - Volatility and correlation are both estimated from a finite historical sample and are not stable; both can shift sharply exactly when a portfolio risk control matters most. - RpaWeightToLots assumes one fixed stop per symbol. A basket with materially different stop philosophies per instrument needs those stops supplied correctly - the function has no way to infer them. - Needs at least 3 overlapping return samples between any two symbols to measure correlation; fewer than that is treated as zero correlation for that pair, not as missing data. - Pure computation only. Nothing here sends an order; the caller decides how to act on the weights and lot sizes.
Script · MT5 · v1.0 · Free
About Risk Parity Weight Allocator
Risk Parity Weight Allocator is a script for MetaTrader 5. It has 4 settings, such as History bars, Max basket size and Total risk money. MQL source code included.
Files in the download: RiskParityAllocator.mq5.mqh, RiskParityAllocator.txt, RiskParityAllocatorDemo.ex5, RiskParityAllocatorDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| History bars | 200 | Bars of history per symbol |
| Max basket size | 5 | How many Market Watch symbols to sample |
| Total risk money | 500.0 | Total risk budget, account currency |
| Stop loss pips | 30.0 | Uniform stop distance used to size every symbol |
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 → Scripts.
- 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 script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code