Calculate Annualized Expected Stock Returns Using Python

How I calculated the average annual expected returns of stocks using Python

In this article, I will show you how you can calculate annualized returns using python! Be sure to check out the video for more details.

What Are Annualized Returns?

An annualized return is the average amount of money earned by an asset each year over some period of time (e.g. 10 years, 5years, 1 year, etc.). It is calculated by adding 1 to the cumulative return and raising the result to the power of 365 divided by the number of days held minus 1.

Formula For Annualized Returns

Why Use Annualized Returns?

According to Investopedia, the annualized return is used because the amount of investment lost or gained in a given year is interdependent with the amount from the other years under consideration because of compounding.

If you prefer not to read this article and would like a video representation of it, you can check out the YouTube Video. It goes through everything in this article with a little more detail and will help make it easy for you to start programming the code even if you don't have the programming language Python installed on your computer. Or you can use both the article and the video as supplementary materials for learning! The entire code and the logic behind the code are written or talked about in the video.

Programming

The first thing that I needed to do to start calculating annualized returns with Python was to import the libraries that I planned on using throughout the program.

``````#Import the libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
``````

``````#Load the data
``````

The stock data contained the price of the stock from different companies. All of which I planned to get the annualized prices.

Next, I calculated the daily simple returns and multiplied them by 252 (roughly the number of trading days in a year) to get the annualized returns. Once I had the annualized returns, I sorted the data in descending order.

``````#Show the data
sorted_annualized_returns
``````

Next, I wanted to see the data more visually, so I created a bar chart to display the information.

``````#Plot the sorted annualized returns
plt.bar(sorted_annualized_returns.index, sorted_annualized_returns)
plt.ylabel('Annualized Returns')
plt.xlabel('Assets')
plt.title('Annualized Returns of Assets')
plt.xticks(rotation = 90)
``````

From the bar chart plot, I could easily see that Tesla (TSLA) gave more than an 80% annualized return which was also the highest return within this data set and Coca-Cola (KO) gave the least annualized returns at a little under 20%. Now, with this program, I have an easy way to calculate and see annualized returns for different assets and can compare them.