A step-by-step guide to build 3 optimized portfolios with a few...

@pyquantnews
PyQuant News 🐍@pyquantnews
30 views Feb 15, 2024 ~2 min read
Advertisement
1
A step-by-step guide to build 3 optimized portfolios with a few lines of Python:
Media image
2
Here’s how skfolio helps:

• Unified library for building optimized portfolios
• Built in Python code to take care of the maths
• Built on top of scikit-learn to leverage ML

This thread will walk you through how to use it:
3
We’ll use scikit-learn for creating data splits, skfolio for optimizing the portfolios, and OpenBB for data.

We’ll do our analysis on a list of sector-based ETFs.
Media image
4
Download the historic price data, manipulate the DataFrame to use with skfolio, and split the data into training and testing sets.
Media image
5
The next step is to fit different models to the data.

We’ll use skfolio to create three separate portfolios: maximum diversification, equal weighted, and random weighted.
Media image
6
We can use skfolio to predict the portfolio weights for each of the weighting methods.
Media image
7
The result is a visualization of the weights of the sector ETFs for each of the portfolios.
Media image
8
Generate the cumulative returns of each strategy to visualize how they performed over the analysis period.

This is a quick way to easily assess the performance of the portfolios.
Media image
9
The portfolio with the maximum diversification underperforms both the equally weighted and randomly weighted portfolios.

You might conclude that being heavily weighted in XLU (utilities) was a drag on the overall performance of the strategy.
Media image
10
While skfolio presents a the cumulative returns of each portfolio, we need to apply periodic rebalancing to better represent a real investment strategy.

As a next step, plug skfolio into your favorite backtesting library and rebalance every month.

How do the returns change?
11
To keep this thread handy, just click the link and repost the first tweet.

Then follow me @pyquantnews to get content for algo trading and quant finance 4x daily.
@pyquantnews
PyQuant News 🐍@pyquantnews
A step-by-step guide to build 3 optimized portfolios with a few lines of Python:
Media image
12
If you want real Python code for algo trading and market analytics, you might consider The PyQuant Newsletter:

• Real code
• No jargon
• 5 minutes to read

Join 22,000+ subscribers who are taking action with Python.

pyquantnews.com/the-pyquant-ne…
Actions
What You Can Do
  • Export as PDF or Markdown
  • Batch Export to Notion
  • Bookmark & Highlight
  • Screenshot Tweet
Create Free Account

Includes 7-day Premium trial

Advertisement