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
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:
Build the optimal portfolio and visualize it with a dendogram chart
Optimize the portfolio and visualize the optimal weights
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.
Want exclusive access to our FULL codebase for this Quant Science tutorial plus dozens more?
Join thousands of aspiring Python quants here 👉
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:
Find edge
Analyze risk
Backtest trading strategies
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...
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.
In the first step, we’ll use the excellent RiskFolio-Lib to build our HRP portfolio and yfinance
for market data.
Run this code:
Next, grab historic data and compute returns.
Sign up for our Newsletter to get all of the code you see today
We can plot the dendrogram to visualize which ETFs are clustered together.
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.
Building the optimal portfolio based on the hierarchy is one line of 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.
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.
We can also visualize the risk contribution of each asset.
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.
Risk contribution is important for identifying assets that disproportionately increase portfolio risk.
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.
Alex was waiting 9 years for a course like this:
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?
👉 Join 10,700+ future Quant Scientists on our Python for Algorithmic Trading Course Waitlist: https://learn.quantscience.io/python-algorithmic-trading-course-waitlist
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