Portfolio Beta To Anchor Symbol
"My book feels tied to gold" or "everything I hold seems to move with the dollar index" is a hunch until it is measured. Measuring it properly needs a regression: take the book's value change bar by bar, take the anchor's price change over the same bars, and fit a slope. Doing that by hand means exporting history for every symbol, aligning it, and running the regression in a spreadsheet. This script does it directly inside the terminal. It takes every currently open position, and for each historical bar over the chosen lookback replays what that position's value would have been if the book had been held exactly as it is now - same symbols, same lot sizes, same directions - back through that history. It computes the anchor's price change and the simulated basket's value change for each consecutive bar pair, then fits an ordinary least squares slope: beta. A beta of 2.0 means the book's value has moved, on average, about two account-currency units for every one-unit move in the anchor; R-squared says how much of that movement the anchor actually explains, versus everything else going on in the book. This is a REPLAY of the current book against history, not a record of what the book actually earned while positions were opened and closed at different times - it answers "if I had been holding exactly this book throughout this window, how would its value have tracked the anchor", which is the right question for a diversification check. Notes and limitations - Currency conversion for each position's simulated value uses TODAY's conversion rate held constant across the whole historical window, not the actual historical cross rate on each bar. This is a simplification documented here deliberately: MT4 does not expose historical rates for every needed cross without loading and aligning many additional series, and for a diversification check the distortion is usually small next to the anchor's own price swings. Treat the beta as directionally informative, not accounting-grade precise. - Each position's replay uses its own symbol's actual historical close aligned to the anchor's bar times via iBarShift; a bar with no aligned history for a given symbol is skipped for that one sample rather than aborting the whole regression, which can leave a shorter effective sample count than InpLookbackBars for a thinly-traded symbol. - This is a replay of the CURRENT book held constant through history, not a backtest of what actually happened while positions were opened and closed at different times and sizes. - A low R-squared means the fitted beta is not a trustworthy summary of the relationship - always read both numbers together. - Read-only: the script never opens, closes or modifies an order.
Script · MT4 · v1.0 · Free
About Portfolio Beta To Anchor Symbol
Portfolio Beta To Anchor Symbol is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 4 settings, such as Anchor symbol, Timeframe and Lookback bars. MQL source code included.
Files in the download: PortfolioBetaReport.mq4, PortfolioBetaReport.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Anchor symbol | Anchor symbol ("" = the chart's own symbol) | |
| Timeframe | PERIOD_H1 | Bar period used for both the anchor and the basket replay |
| Lookback bars | 200 | Number of historical bar-to-bar changes to regress over |
| Magic filter | -1 | Magic number filter for positions considered (-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) · Alerts · MT4 Script · MQL4 Source Code