Portfolio Optimization
Overview
Portfolio optimization is a capability in Merlin that helps allocate capital across multiple trading strategies. Using Modern Portfolio Theory Merlin determines optimal strategy weights to achieve specific objectives such as minimizing volatility, maximizing returns, or maximizing Sharpe.
Merlin uses portfolio optimization methods provided by Q-API, including Mean-Variance Optimization and Kelly Criterion based allocation.
Optimization Process
The portfolio optimization workflow in Merlin consists of these steps:
- Data Collection: Gather performance data for all component strategies using MesoSim APIs
- Strategy Optimization (optional): Apply SETS models to improve individual strategies when model is present.
- Walkforward Testing: Run walkforward optimization to obtain Out Of Sample results
- Performance Evaluation: Analyze the results of the optimized portfolio using tearsheets and performance metrics
The optimization process is parallelized for efficiency, with the degree of parallelism controlled by the BACKTESTS_IN_FLIGHT and MODEL_CALLS_IN_FLIGHT environment variables.
Walkforward Analysis
Similar to strategy optimization, portfolio optimization uses walkforward optimization to avoid overfitting.
Optimization Methods
Merlin supports several optimization targets:
Mean-Variance Optimization (MVO)
Based on Modern Portfolio Theory, MVO seeks to maximize expected returns for a given level of risk or minimize risk for a given level of expected return.
Merlin implements two MVO variants:
- MinVolatility: Minimize portfolio volatility
- MaxSharpe: Maximize the Sharpe ratio
MVO works best with strategies that have relatively stable statistical properties and follows a quadratic optimization approach.
The MinVolatility target often results higher Out Of Sample Sharpe ratio than MaxSharpe target.
The optimization is done with PyPortfolioOpt. You can find the optimizer Source Code here.
Kelly Criterion
The Kelly Criterion calculates the optimal position sizes to maximize the long-term growth rate of capital, considering both the edge (expected return) and risk of each strategy.
This implementation scales the allocation fractions as such that the overall allocation will be 100%. You can find the optimizer Source Code here
Configuration Parameters
Portfolio optimization is configured through a portfolio config JSON file:
Basic Parameters
| Parameter | Description |
|---|---|
| Target | Optimization method (MeanVarianceMinVolatility, MeanVarianceMaxSharpe, KellyCriterion) |
| Window | Number of trading days used for optimizing the portfolio |
| TopAlloc | Maximum number of strategies to include in the portfolio |
| ReweightPeriod | Number of trading days between rebalancing |
| InitialCash | Initial capital for the portfolio |
Strategy Configuration
The Strategies section defines component strategies:
| Parameter | Description |
|---|---|
| StrategyDefinition | Path to strategy definition file |
| ModelCfg | Path to model configuration for strategy optimization (optional) |
| DataCollectionJobId | MesoSim backtest ID for strategy data. If not provided, a new data-collection backtest is ran. (optional) |