In this article, I’ll explain about how we can perform risk analysis for certain publicly listed stocks. To perform this, we will look at the data of certain time frames and will try to analyze which stock is riskier than the other one based on the price deviation and the average price.
Throughout this
exercise, I’ll be using Python as a programming language.
Required
Packages
Here is the list of
packages we need to be installed before proceeding:
- pandas
- numpy
- yfinance
- matplotlib
Once packages are
installed, we are good to go.
Importing Libraries
import pandas as pd import numpy as np import yfinance as yf import matplotlib.pyplot as pyplot
Select Stocks and Duration
Next, we need to select the list of stocks we want to analyze and the duration. Here, I’m going with Microsoft and Tesla stocks, and the duration I’m selecting is from 2018–01–01 to 2022–02–01.
stocks = yf.download([‘MSFT’,’TSLA’], start=”2018–01–01", end=”2022–02–01") stocks.head(5)
Passing 5 will show you the initial 5 rows of data as shown below:
stocksData = stocks.loc[:,”Close”].copy()
Plot Closing Price for Initial Analysis
Let's plot the closing price of both the stocks to get high level idea of where each of this stand and this can be done by below lines of code:
stocksData.plot(figsize= (20,10), fontsize=15) pyplot.style.use(“classic”) pyplot.show()
Executing the above lines, will give you the plot as shown below:
Analyze How Much Price Has Changed
Next, we will check how much price has changed on a daily basis and will remove all the rows with missing values. Here you will get missing values for non-trading days.
data = stocksData.pct_change().dropna()
Now we need to find deviation and average value for each of these days. This we can do it using below line:
data = data.describe().T.loc[:,[“mean”, “std”]]
Standard deviation will tell us how much is the deviation between historical figures of any given stock and mean value will tell us the average stock price for the duration.
The above table shows data for a single day. So, we need to calculate it for the entire year, which comes to approximate 251 trading days.
data[“mean”] = data[“mean”] * 251 data[“std”] = data[“std”] * np.sqrt(251)
Plot Mean and Standard Deviation
Time to plot mean and standard deviation, which can be done by below lines:
data.plot.scatter(figsize= (20,10), fontsize=15, x=”std”, y=”mean”) for idx in data.index: pyplot.annotate(idx,xy=(data.loc[idx,”std”]+0.005,data.loc[idx,”mean”]+0.005)
Analysis
By looking at Graph 1 and Graph 2, you can see that TSLA is very high in terms of both standard deviation and mean, which means it has been very volatile. This volatility puts TSLA into high price change(increase) and risk as compared to MSFT. Along with this, Graph 1 clearly displays the trend depicting the closing price in 2018 and the closing price in 2022.
Hope you got an idea about risk analysis and enjoyed reading this article. You can also watch the video tutorial of this article on my YouTube channel named Shweta Lodha.
Disclaimer
This article is just to
give you a gist of how you can programmatically perform risk analysis on stocks
and has nothing to do with any particular stock or financial advice.
Comments
Post a Comment