Financial Analysis With Python: Single Stock Analysis

Moses Agbelese
4 min readJan 13, 2021

Python and Machine Learning is being widely used in different industries. The revolution it has caused in these industries has been positive and satisfying.

Thus, Python is also being adapted to various activities in the finance industry. Financial analysis is made easier with various functions in Python and with Machine Learning skills.

In this article, we would be exploring together how to analyze a single stock.

The first major step is to install the important libraries needed to perform this analysis in Python.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

To be able to read in the stock data from online, it would require that we use the Pandas Datareader function.

from pandas_datareader import data as wb

The stock we are going to be analyzing in this article is ‘SNP’. We hereby read in the stock data using the pandas datareader function that we already imported as ‘wb’.

sp=wb.DataReader('SNP', data_source= 'yahoo', start='2000-1-1')sp.head()

The source of our data is Yahoo Finance and is stipulated as ‘yahoo’ above. Yahoo finance has all historic data of publicly traded stocks. But for the purpose of analysis, one would have to put in the start date and/or end date of the data. It all depends on the specific task you have to do. For the purpose of this article, we are interested in analyzing stock data of SNP from the first day of year 2000 till date, that is why there is no ‘end’ date in the bracket above.

It is necessary to note that trading only occurs five working days of the week and days that are not public holidays. But, automatically, ‘wb’ will only give you the information for the days that the stock was being traded without having to know the exact dates. For instance, if ‘2000–1–1’ falls on the weekend, the data that will be read in will start from the first working day nearest to the keyed in date or when the stock was initially listed on the exchange market.

One could also check the tail of the data to get the latest stock information. With this, one can crosscheck to ensure the information is accurate and there are no mistakes.

sp.tail()

From the historical data, it is easy to derive the rate of return for the specific stock under analysis. According to experts in the field of finance and economics, the best method of calculating the rate of return for a single stock is to use the Logarithmic Rate of Return.

Again, Python has this function built in so no need for panic. Log returns simply is to find the log of the adjusted close price (Adj close) divided by the adjusted close price of the previous day.

sp['Log_return %']=np.log(sp['Adj Close']/sp['Adj Close'].shift(1)) * 100

To accomplish getting the adj close price of the previous day, the shift method is used. Since, the difference is by a day, 1 is put in bracket (.shift(1)).

sp['Log_return %'].tail()
sp.head()

Since, the create new column instance was used (sp[“Log_return %”]), “Log_returns %” has been added as a new column to the data.

There are 250 trading days in a year. The mean/average log return is multiplied by 250 days to get the annual rate of return, and is also rounded up to 2 figures.

print(str(round(sp['Log_return %'].mean()*250,2)) + ' %')

In order to take note of the stock trend, it would be nice visualize it. Doing so will help to highlight the outlier adj close price and to note most frequent adj close prices.

sp['Log_return %'].plot(figsize=(12,8))

In conclusion, from this simple methods, it would to easy to analyze a stock and to build further on it for the purpose of making best decisions.

Disclaimer: This article is an independent one and is just for tutorial purposes. Though data used is real, it is however not a pointer as to making investments in it or not.

Your Claps and comments are welcome and you can also reach me on LinkedIn and Twitter.

--

--