It’s hard to compare the returns on the given securities just by looking at their price history in absolute terms so we need to find a better way of comparing historical returns. One of the possible solutions is to adjust the whole data series in such a way that the first data point would be equal to some predefined number. We are going to adjust the price history of both stocks so they will always start with the same value, let’s say 100. This method allows us to see what would happen with $100 invested in 2 given stocks on the date D.
This is the third part of the “Plotting Financial Data With Python” series and it’s better if you read it in chronological order:
- Part 1 - History
- Part 2 - Variance
- Part 3 - Comparing Returns (you are here)
- Part 4 - Efficient Frontier (2 Assets)
- Part 5 - Efficient Frontier (N Assets)
You can get the full source code here: https://github.com/bubelov/market-plots
You should already have a function for retrieving stock price history, if you don’t have it - go to the first post and implement it. It should return all of the data we need but it has 2 major issues:
- The values can have different date ranges, we need to use the same range for both datasets
- The values are in absolute terms so we need to adjust them to start with the same value but keep the original proportions
Adjusting The Range
The most obvious way to solve the range mismatch is to cut out all of the dates that are not present in both data sets. Here is the possible solution:
We just filter each dataset and keep only the dates that are present in the other dataset. Now we can be sure that we’re comparing the same date range.
Adjusting The Values
Different stocks can have different prices at any given time and if some stock is selling for $1000 per share it does not mean that it’s better than the stock trading for $1 per share. It’s all relative so we have to scale the data before we compare it.
Here’s how we can do that:
Plotting The Data
Let’s create a new file and name it compare.py:
This code combines the previous steps and also draws the resulting datasets on the screen.
Let’s test this module by comparing the Tesla Inc cumulative returns using the S&P 500 index as a benchmark:
python compare.py TSLA SPX
You should see the chart similar to this one:
It seems like the TSLA stock made much more money for it’s shareholders than the SPX index but it also looks more volatile and jittery.
Now we have a tool for comparing stock performance and it also gives us a hint about the stock volatility. We can use the script from the Part 2 - Variance to measure the volatility more accurately.
In the next post we’ll create a tool for measuring the “compatibility” of 2 given stocks. Expected returns might be a good hint on what to buy but it’s also important to understand the relationship between different stocks in a portfolio.