Portfolio optimization is a widely studied topic, especially in academia. The main idea is to maximize a portfolio’s value by finding the most productive combination of assets to yield the highest return. In this article, I will show you how to create your own Python program to optimize your portfolio, find the most productive asset weights, and test it out on the sample. All of the code in this article can be found on my Github.
Step I: Initialize Data Feed
To start we will set up a data feed using Quandl’s free API to receive real historical market data. Head on over and get your free API key and we will set up a class in Python to manage the JSON response. We will need the quandl library, so run the command pip install quandl in your command prompt if you haven’t already.
Now that we have a class dedicated to connecting to our data source, we can use it to start making requests.
Step II: Data Requests
To receive data we will use the class that we just created above. This next class is dedicated to receiving the historical data for a set of assets, and converting it to a format for portfolio optimization.
The field table in the PortfolioDataRequests class is dedicated to housing our asset data in a format for portfolio optimization.
Step III: Portfolio Optimization
Pretty easy so far huh? We created a connection for the data feed, a class for the requests themselves, all that is left is to optimize what was returned from the requests. To do this we will use pypfopt, go ahead and run pip install pypfopt in your command prompt if you don’t already have the library.
Step IV: Calculating Returns
We almost have all the pieces to the puzzle, the last thing we need is to determine the value of the portfolio for any discrete allocation. I wrote a class to help with this, it's very straightforward.
Step V: Putting it All Together
Now we have everything we need to create a “train” and optimize a portfolio, and “test” it against unseen data. Just place the symbols in the stocks string and it will parse and request historical data for each one. Afterward, it will optimize the portfolio from 2010–2017 (or any specified date range), then it will use those optimized weights to get the returns for 2017–2018 and we can see how the optimization did!