Join the Quant Scientist Newsletter

Gain access to exclusive tools that Wall Street's Elite don't want you to have. Don't miss the next issue...

Join 11,500+ Quant Scientists learning one article at a time

Join 11,500+ Quant Scientists learning one article at a time

Use machine learning to build portfolios

Use machine learning to build portfolios

October 12, 20244 min read

Matt's a machine learning expert.

Jason's a quant.

Spoiler alert: That's why we teamed up to build Quant Science.

In today's issue, we're going to combine Matt and Jason's experience and build a state of the art stock portfolio using an advanced technique.

The good news?

You don't need a Ph.D. to do it.

In today's issue of the QS Newsletter (get the code), we are going to use machine learning to build an advanced portfolio using the riskfolio package.

What You’ll Learn:

  1. Build the optimal portfolio and visualize it with a dendogram chart

  2. Optimize the portfolio and visualize the optimal weights

  3. Understand how each asset contributes to the risk of the portfolio

BONUS: Get the Python Code for EVERYTHING you see in this post

Disclaimer:

The information and educational material provided by Quant Science, LLC are for educational purposes only and should not be considered as financial advice or recommendations to purchase, hold, or sell any securities or other financial instruments. Before you proceed, please review our full disclaimer here.

Use machine learning to build portfolios

Join the Quant Scientist Newsletter (and Get the Code)

Want exclusive access to our FULL codebase for this Quant Science tutorial plus dozens more?

Join thousands of aspiring Python quants here 👉

NEW: Free 5-Day Algorithmic Trading Course

5 Day Algorithmic Trading Course

Since you're here, you probably want to learn how to get started developing (profitable) algorithmic trading strategies and reinvest those profits.

Here are the steps:

  1. Find edge

  2. Analyze risk

  3. Backtest trading strategies

  4. Execute trades automatically

Easy right? Well, not exactly... Avoid the 5 biggest mistakes beginners make with our free, 5-day email course:

Click here to join our free 5-Day Algorithmic Trading Course 👉

Now on to the show...

Use machine learning to build portfolios

A cutting edge technique called Hierarchical Risk Parity (HRP) uses graph theory and machine learning to build a hierarchical structure of the investments.

First, make sure to sign up for our Newsletter to get all of the code you see today.

Imports and set up

In the first step, we’ll use the excellent RiskFolio-Lib to build our HRP portfolio and yfinance for market data.

Run this code:

Code

Next, grab historic data and compute returns.

Code

Sign up for our Newsletter to get all of the code you see today

Build the optimal portfolio

We can plot the dendrogram to visualize which ETFs are clustered together.

Code

The plot visualizes the hierarchical clustering of assets based on their historical return correlations. It illustrates how clusters of assets are merged at each hierarchical level and can give us insight into the correlation structure within a portfolio.

Chart

Building the optimal portfolio based on the hierarchy is one line of code.

Code

Additional parameters like linkage, max_k, and leaf_order are specified to fine-tune the clustering and dendrogram construction process.

The result is a pandas Series with the optimal weight for each of the assets.

Visualize the results

RiskFolio-Lib makes it easy to visualize the results of the optimization. The result of running the last few lines of code is the portfolio weights.

Chart

We can also visualize the risk contribution of each asset.

Code

The risk contribution of each asset in a portfolio quantifies how much individual assets contribute to the total risk, considering both their own volatility and their correlation with other assets.

We can see the highest risk contribution is from OIH which is an oil ETF.

Code

Risk contribution is important for identifying assets that disproportionately increase portfolio risk.

Congratulations!

You just learned how to use machine learning to build an optimal stock portfolio.

But, there's more to learn in algorithmic trading:

  • Backtesting your portfolio construction algorithm to make sure the strategy will work in the future

  • Executing the trades automatically

  • Monthly rebalancing

  • Tracking your actual Profit and Loss

  • Incorporating Trading Fees

Are you interested in learning algorithmic trading strategies that maximize returns responsibly, help you manage risk, and grow your investments?

We implement 3 core trading strategies including portfolio, momentum, and spread trades that have worked in our favor in the past and continue to produce results for our students.

Join 400+ of us that are learning to apply python to algorithmic trading to grow investments.

Leo was up 11.5% in just 13 trading days.

Leo up 13pct

Alex was waiting 9 years for a course like this:

testimonial

Ready to make Algorithmic Trading Strategies that actually work?

There's nothing worse than going at this alone--

Learning Python is tough.

❌ Learning Trading is tough.

Learning Math & Stats is tough.

It's no wonder why it's easy to feel lost, make bad decisions, and lose money.

Want help?

Python for Algorithmic Trading Course

👉 Join 10,700+ future Quant Scientists on our Python for Algorithmic Trading Course Waitlist: https://learn.quantscience.io/python-algorithmic-trading-course-waitlist

button course waitlist

investingstockspythonalgorithmic tradingsoftwareffn
Matt is a Data Science expert with over 18 years working in business and 10+ years as a Data Scientist, Consultant, and Trainer. Matt has built Business Science, a successful educational platform with similar goals to Quant Science, but focused on developing Data Scientists in business, marketing, and finance disciplines.

Matt Dancho

Matt is a Data Science expert with over 18 years working in business and 10+ years as a Data Scientist, Consultant, and Trainer. Matt has built Business Science, a successful educational platform with similar goals to Quant Science, but focused on developing Data Scientists in business, marketing, and finance disciplines.

Back to Blog

Start Your Journey To Becoming A Quant Today!

Join the Quant Scientist Newsletter

Gain access to exclusive tools that Wall Street's Elite don't want you to have. Don't miss the next issue...

Join 11,500+ Quant Scientists learning one article at a time

Join 11,500+ Quant Scientists learning one article at a time

© 2024 Quant Science - All Rights Reserved

Next Cohort Launch: Wednesday, January 15th at 10AM EST