Python Financial Stock Analysis (Risk vs Performance)
Tags: Algorithmic Trading, Trading, Investing, Stock Market, Finance

Before we get started I would like to mention that this is part 2 in my series of Python Financial Stock Analysis arcticles. As a result of this, we will not dive into how the Python environment is setup or the details on how to fetch stock data. Instead have a look at part 1 in this series:

Python Financial Stock analysis (Algo Trading) https://medium.com/vinsloev-academy/python-financial-stock-analysis-algo-trading-4d5304d07416

So without further introduction lets get into the actual implementation. If you get stuck at some point or just want to view this tutorial in another format, the implementation can be seen on YouTube: https://youtu.be/SPav9VWDruM

The following analysis will be based on the three tech companies:

Google (GOOGL)

Amazon (AMZN)

Tesla (TSLA)

The Risk measure in this analysis will be based on the standard devitation value(STD). STD measures the variation of a value set which means that a high STD indicates that the value is spread over a wide range, whereas a low value indicates that the values tend to be close to the MEAN. In relation to stocks a high STD indicates that the stock is making large jumps in both a positive and negative direction. This raises the risk of owning the stock as what is earned one week or month could be lost twice the following, due to regular high jumps and drops.

The Performance measure in this analysis is based on the MEAN value, which is the sum of stock price changes over a given time period divided by the number of values.

The code block below defines the libraries used and the fetching of stocks(Read part1 to get a better understand of this part)

import pandas as pd

import yfinance as yf

import matplotlib.pyplot as plt

import seaborn

import numpy as np stock = [“GOOGL”, “AMZN”, “TSLA”] stocks = yf.download(stock, start = “2015–01–01”, end = “2020–12–08”) data = stocks.loc[:, “Close”].copy() data.pct_change()

The last line will print the stocks and their percentage change for a given trading day:

To ensure that you dont have any null values in your data set you can use the .dropna() function. We will do so on our data variable which contains our set of percentage. The new value set will be assigned to the variable com.

com = data.pct_change().dropna()

com.describe()

Using the describe() function we can print the following values for our data set. (count, mean, std, min, 25%, 50%, 75% and max).

As mentioned above our analysis will focus on the MEAN and STD values. To isolate these values we will use the loc command, and define the rows we which to assign to our new sum variable.

sum = com.describe().T.loc[:, [“mean”, “std”]]

sum

This will leave us with the following data set.

The above is the MEAN and STD values based on all trading days in the period defined when the stock data was fetched. To get a a better understand of the value over time we will multiple the values with 252 (The average trading days in a year).

sum[“mean”] = sum[“mean”]*252

sum[“std”] = sum[“std”] * np.sqrt(252)

With the values multipled it’s time to define our plot chart. Our x axis will be the STD value whereas our Y axis will be our MEAN. The figsize and fontsize is purly cosmetics and can be changed.

sum.plot.scatter(x = “std”, y = “mean”, figsize = (12,8), s = 50, fontsize = 15)

With the above code we will get a plot chart with three dot’s but there is currently no annotation attached to the dots, making our plot hard to read. The annotations can be added using our little for loop below.

for i in sum.index:

plt.annotate(i, xy=(sum.loc[i, “std”]+0.002, sum.loc[i, “mean”]+0.002), size = 15)

Below is our final plot chart with company annotations.

This concludes the analysis of our risk vs performance. We can conclude based on our data, that the Tesla stock is more risky to own than Amazon and Google, however the expected return value is also very high for the Tesla stock compared to both Amazon and Google. Amazon on the other hand have a medium return value without being equally risky making it a favorable stock to own based on our analysis of the three tech companies.