Alım-satım robotlarını ücretsiz olarak nasıl indirebileceğinizi izleyin
Bizi Telegram üzerinde bulun!
Fan sayfamıza katılın
Kodu ilginç mi buldunuz?
Öyleyse bir link gönderin -
başkalarının da faydalanmasını sağlayın
Kodu beğendiniz mi? MetaTrader 5 terminalinde deneyin
Kütüphaneler

PortfolioWeights: risk parity, HRP, minimum variance and maximum diversification - MetaTrader 5 için kütüphane

Mohamed-amine Baddir
Mohamed-amine Baddir
MetaTrader 5 için Expert Advisor'lar geliştiriyorum ve gerçek işlemlerde kullanmadan önce bunları düzgün şekilde test ediyorum.
Yaptığım işler:
- Stratejinizi, TradingView scriptinizi veya yazılı işlem kurallarınızı temiz ve profesyonel bir MT5 Expert Advisor'a dönüştürmek
Görüntülemeler:
51
Yayınlandı:
\MQL5\Include\PortfolioWeights\ \MQL5\Scripts\PortfolioWeights\
MQL5 Freelance Bu koda dayalı bir robota veya göstergeye mi ihtiyacınız var? Freelance üzerinden sipariş edin Freelance'e git

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.

Usage

#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);

Methods

  • InverseVolatility: each part gets a weight inverse to its volatility.
  • RiskParity: equal risk contribution, every part brings the same share of the variance of the mix (cyclical coordinate descent, Griveau-Billion, Richard and Roncalli, 2013).
  • HierarchicalRiskParity: the parts are ordered by a single-linkage tree of the distance sqrt((1 - correlation) / 2), and the weight is split down the ordered list in inverse proportion to the variance of each half (Lopez de Prado, 2016). order[] returns the order of the tree.
  • MinimumVariance: the lowest variance of the mix, with an optional cap on the weight of one part.
  • MaximumDiversification: the highest ratio of the weighted volatilities of the parts to the volatility of the mix (Choueifaty and Coignard, 2008).
  • LedoitWolf: the sample covariance with its correlations shrunk towards zero by the optimal intensity of Ledoit and Wolf (2004), computed on the returns scaled to unit variance. With few periods and many parts the sample correlations are noisy; the shrunk covariance gives steadier weights.

The two optimizations are solved exactly with an active-set quadratic programme (bounds and one equality), not by search.

Example

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).

Output of PortfolioWeightsExample.mq5

Checked

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.

Files

PortfolioWeights.mqh goes to MQL5\Include\PortfolioWeights, PortfolioWeightsExample.mq5 to MQL5\Scripts\PortfolioWeights.

TesterReport: read a saved Strategy Tester report into deals and trades 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 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 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 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.