PortfolioWeights.mqh computes the weights of a portfolio from the returns of its parts, which can be symbols or the daily results of several Expert Advisors. It gives five classic long-only methods, a covariance with Ledoit-Wolf shrinkage, and the risk contribution of each part. It uses the matrix and vector types of MQL5 and needs no DLL.
#include <PortfolioWeights\PortfolioWeights.mqh> matrix returns(days, assets); // one row per period, one column per asset // ... fill the returns ... matrix cov; double shrink = CPortfolioWeights::LedoitWolf(returns, cov); // or Covariance(returns, cov) int order[]; vector w = CPortfolioWeights::RiskParity(cov); // the weights add up to 1 vector h = CPortfolioWeights::HierarchicalRiskParity(cov, order); vector rc = CPortfolioWeights::RiskContributions(cov, w);
The two optimizations are solved exactly with an active-set quadratic programme (bounds and one equality), not by search.
The script PortfolioWeightsExample.mq5 takes a list of symbols (or the first 8 of the Market Watch), computes their daily log returns over the chosen number of days and prints the weights of every method with its diversification ratio, and the risk contributions of the risk parity mix (picture 1).

The weights of the five methods from a later run of the same example (300 days to 22 October 2026), so the numbers are not those of the log above. Every method puts its largest weight on USDCAD, from 24% (inverse volatility) to 57% (HRP), and almost nothing on gold and bitcoin.

Why: USDCAD is the least volatile symbol (4.1% a year) and moves against EURUSD, GBPUSD and AUDUSD (correlations near -0.6), so it lowers the risk of the whole portfolio. Gold (26.7%) and bitcoin (37.1%) are three to nine times more volatile than the currencies. These are the volatilities and the shrunk correlations computed by the library in the same run.

On 750 periods of 8 correlated series, the shrinkage intensity was equal to scikit-learn's LedoitWolf, the tree order of the hierarchical method to scipy's single linkage, its weights to a Python version of the bisection, the risk parity contributions were equal to 1e-15, and the minimum variance (with and without a cap) and maximum diversification weights matched scipy's SLSQP optimizer within 2e-8.
PortfolioWeights.mqh goes to MQL5\Include\PortfolioWeights, PortfolioWeightsExample.mq5 to MQL5\Scripts\PortfolioWeights.
TesterReport: read a saved Strategy Tester report into deals and trades
Reads a Strategy Tester report saved as HTML into its deals, closed trades, net profit and balance drawdowns, for comparing, combining or exporting backtests.
Live Test Bench: automated checks of an EA on a demo account
Automated checks of an Expert Advisor on a live demo chart (restart, second copy, server history, chart button) with a PASS/FAIL report.
DrawdownBootstrap: Monte Carlo of the maximal drawdown
Block bootstrap of daily results: how deep the drawdown of an account or strategy can get over the next months, with percentiles and a seeded generator for repeatable runs.
BacktestOverfitting: probability of backtest overfitting (PBO) of an optimization
Probability of Backtest Overfitting (PBO) of an MT5 optimization by combinatorially symmetric cross-validation. Every pass sends its daily results as a frame, and the PBO is computed when the optimization ends.