Loading…

Portfolio Value At Risk

A single VaR number invites false confidence, because each way of computing it lies in a different direction. This library computes three and prints them side by side, on purpose. Parametric VaR fits a normal distribution to the portfolio's historical daily P&L and reads the confidence-level quantile off that curve. It is fast and smooth, and it is blind to fat tails - real market returns blow through the normal-implied VaR far more often than the model concedes, which is exactly the failure mode that has embarrassed more than one risk desk. Historical VaR takes the actual worst days from the sample and reads the quantile straight off them, no distribution assumed. It is honest about the shape of history it was given, and correspondingly blind to any shock that history never contained. Simulated VaR bootstrap-resamples single-day P&L observations from the same history, with replacement, to build a larger synthetic distribution before reading the quantile. It widens the effective sample the quantile is read from, but it is still built entirely out of the same historical days - it is not an independent model and should not be mistaken for one. Expected Shortfall is computed alongside each VaR: the average loss on days at least as bad as the VaR threshold. VaR answers "how bad is the edge of the tail"; Expected Shortfall answers "how bad is the tail on average", which is the number that actually matters once the threshold is breached. Every position's money P&L per unit of price move is derived from the broker's own tick value and tick size (MODE_TICKVALUE/MODE_TICKSIZE on MQL4, SYMBOL_TRADE_TICK_VALUE/SYMBOL_TRADE_TICK_SIZE on MQL5), read per symbol, never from an assumed contract size. A VaR library that gets this wrong on a metal, an index CFD or a JPY cross is worse than no VaR library, because it looks precise while being wrong. Notes and limitations - Needs at least 30 usable daily samples across every symbol in the basket to produce a result; the demo says plainly when it does not have that. - Bars are clipped to the shortest available history among the holdings - one recently listed symbol in the basket shortens the whole sample. - All three methods are single-period (one day ahead). Scaling to a longer horizon with the square-root-of-time rule assumes independent daily returns, which real markets do not fully honour, especially in a crisis. - The simulated method resamples the same historical days it was given; it does not synthesise scenarios outside that history. Treat it as a smoother read of the historical method, not an independent check on it. - Correlation between symbols in the basket is captured implicitly, because the P&L series is summed across holdings on the same calendar days, not computed from a separately estimated correlation matrix. - Pure computation only. Nothing here sends an order or reads AccountEquity for you - the caller decides what to do with the numbers.

Script · MT5 · v1.0 · Free

About Portfolio Value At Risk

Portfolio Value At Risk is a script for MetaTrader 5. It has 4 settings, such as Confidence, History bars and Simulations. MQL source code included.

Files in the download: PortfolioValueAtRisk.mq5.mqh, PortfolioValueAtRisk.txt, PortfolioValueAtRiskDemo.ex5, PortfolioValueAtRiskDemo.mq5.

Settings

SettingDefaultWhat it does
Confidence0.95Confidence level, e.g. 0.95 = 95%
History bars120Daily bars of history to sample
Simulations2000Bootstrap draws for the simulated method
Magic filter-1Magic number filter, -1 = every open position

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria