This is a factor optimization strategy, written for Quantconnect LEAN (Python). The attached code demonstrates how to combine technical and fundamental factors in an optimized portfolio, focusing on the most heavily-weighted stocks in SPY.
- Universe Selection
The strategy targets the top 5 weighted stocks in the S&P 500 ETF (SPY). This approach focuses on the most liquid, large-cap names in the market, providing stability and reliability for factor testing.
- Factor Definitions
Two complementary factors are used to evaluate stocks: - Kaufman Efficiency Ratio (KER): A technical factor that identifies stocks exhibiting smooth, directional price movement. Values closer to 1 indicate strong trend efficiency, while values closer to 0 indicate choppy price action.
- Free Cash Flow Yield (FCF Yield): A fundamental factor that highlights companies generating strong cash flows relative to their market value. Higher values indicate potentially undervalued companies with solid cash generation.
- Live Optimization with Nelder-Mead
At each monthly rebalance, the algorithm:- Collects raw factor values for each security
- Normalizes them to create factor z-scores for cross-sectional comparison
- Uses the Nelder-Mead method to optimize factor weights, maximizing correlation with recent price performance
- This direct search optimization method is effective for this low-dimensional problem without requiring derivatives
- Portfolio Construction
After optimizing factor weights, the algorithm:- Calculates combined portfolio scores by applying optimized weights to factor z-scores
- Filters out negative-scored securities to maintain a long-only portfolio
- Normalizes weights to ensure 100% exposure
- Monthly Rebalancing
The strategy rebalances at the start of each month, incorporating both technical efficiency and fundamental strength in its weighting scheme. This monthly cadence balances trading costs with the need to adapt to changing market conditions. This approach combines momentum and value factors in a systematic, optimized framework while maintaining focus on the market’s most liquid securities.