Cross Hedge Size Calculator
Hedging one symbol's exposure with a correlated but different symbol - crude oil against an energy index, one gold-related pair against another, two correlated currency crosses - is common when the exact same instrument is not available or not liquid enough to trade in size. The naive approach matches lot size one-to-one between the two symbols, which only actually cancels P&L swings by coincidence: two symbols with different point values, different contract sizes or different volatility need a different lot ratio to genuinely offset each other. This script measures that ratio directly from recent bars rather than assuming it. It builds a bar-to-bar price-change series for both symbols over a configurable lookback window and computes the minimum-variance hedge ratio - the regression coefficient of the primary symbol's price changes against the hedge symbol's - along with the correlation coefficient as a reliability check. The recommended hedge trade's lot size and direction are derived from that ratio and the exposure being hedged, normalised to the hedge symbol's own lot step. A low correlation over the measured window triggers an explicit warning in the log, because a hedge ratio measured on a weak relationship is not a reliable number to size a real trade against. Notes and limitations - The hedge ratio is measured on raw price differences, not percentage returns, and does not convert for any difference in quote currency between the two symbols. Hedging across two symbols with different quote currencies needs that conversion applied by hand to the result. - A correlation below 0.30 in absolute value over the measured window triggers a warning - the hedge ratio is still computed and printed, but should not be trusted for sizing a real trade at that reliability. - The ratio is a rolling historical measurement, not a forward-looking guarantee. Correlation between two symbols can and does change; a hedge sized today is not guaranteed to still offset correctly next week without re-running the script. - This script reports a recommendation only - it never sends an order.
Script · MT4 · v1.0 · Free
About Cross Hedge Size Calculator
Cross Hedge Size Calculator is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 6 settings, such as Primary symbol, Hedge symbol and Primary lots. MQL source code included.
Files in the download: CrossHedgeSizeCalculator.mq4, CrossHedgeSizeCalculator.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Primary symbol | Symbol carrying the exposure ("" = this chart's symbol) | |
| Hedge symbol | Correlated symbol to hedge with (must be set) | |
| Primary lots | 0.0 | Lots to hedge (0 = auto-sum this chart account's open lots on the primary) |
| Lookback bars | 100 | Bars used to measure the rolling hedge ratio |
| Timeframe | PERIOD_H1 | Timeframe the hedge ratio is measured on |
| Magic filter | -1 | Magic filter when auto-summing primary lots (-1 = every magic) |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Gold (XAUUSD) · Hedging · Risk & Lot Size · Oil · Alerts · MT4 Script · MQL4 Source Code