Creating a Trading Strategy with Portfolio Optimization Using CVXPY

Janelle Turing
5 min readJun 16, 2024

In this tutorial, we will walk through the process of creating a trading strategy that maximizes the Sharpe Ratio using portfolio optimization. We will use the NASDAQ-100 components, source historical data from Yahoo Finance and implement a backtesting strategy with monthly rebalancing. The goal is to allocate weights to the portfolio components in a way that maximizes the Sharpe Ratio while ensuring the weights sum to 100% and are not overly concentrated in a single ticker.

Photo by m. on Unsplash

Introduction

Portfolio optimization is a crucial aspect of modern finance, allowing investors to allocate their assets in a way that maximizes returns while minimizing risk. One popular metric for this purpose is the Sharpe Ratio, which measures the risk-adjusted return of a portfolio. In this tutorial, we will use the CVXPY library to optimize the portfolio weights of NASDAQ-100 components to maximize the Sharpe Ratio. We will also implement a backtesting strategy to evaluate the performance of our optimized portfolio with monthly rebalancing.

We will start by fetching the list of NASDAQ-100 components from Wikipedia and then use the yfinance library to download historical price data for these components. Next, we will calculate the monthly returns and use CVXPY to optimize the portfolio weights…

--

--

Janelle Turing

Your AI & Python guide on Medium. 🚀📈 | Discover the Power of AI, ML, and Deep Learning | Check out my articles for a fun tech journey – see you there! 🚀🔍😄