Member-only story

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. Finally, we will backtest the strategy and visualize the results using various plots.

Prerequisites

Before we begin, make sure you have the following Python libraries installed:

pip install yfinance pandas numpy matplotlib cvxpy plotly

Step 1: Fetch NASDAQ-100 Components

First, let’s fetch the list of NASDAQ-100 components from Wikipedia and store them in a DataFrame.

import pandas as pd

# Fetch NASDAQ-100 components from Wikipedia
url = "https://en.wikipedia.org/wiki/NASDAQ-100#Components"
tables = pd.read_html(url)
nasdaq_100_df = tables[4] # The table with NASDAQ-100 components
nasdaq_100_df = nasdaq_100_df[['Ticker']]
nasdaq_100_df.head()
Ticker
0 AAPL
1 MSFT
2 AMZN
3 TSLA
4 GOOG

Step 2: Download Historical Data from Yahoo…

--

--

Janelle Turing
Janelle Turing

Written by 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! 🚀🔍😄

Responses (2)