Python Financial Stock analysis (Algo Trading)
Tags: Data Science, Python, Finance, Stock Market, Algorithmic Trading

In this article we will dive into Financial Stock Analysis using the Python programming language and the Yahoo Finance Python library. This tutorial covers fetching of stock data, creation of Stock charts and stock analysis using stock data normalization. The implementation will take place within the Jupyter Notebook which we will install using the Anaconda data science platform. So without further introduction lets get into the actual implementation.

The following implementation is divided into three parts. If you get stuck at some point or just want to view this tutorial in another format, a Youtube link is attached to each section, where you can see the actual implementation being executed.

Installing Anaconda and Yahoo Finance

YouTube: https://youtu.be/8gGV6eGp9IQ

If you already have Anaconda installed on your system then you can skip this part.

Navigate to https://www.anaconda.com/products/individual and download the latest version to your Operation System (Windows, Linux or Mac OS).

Follow the installation tutorial — requires around 500mb of free space.

Once installed, open Anaconda navigator and ensure you can access Jupyter.

Anaconda Navigator

Next open the Anaconda Prompt

Anaconda Prompt

Execute the following to install the Yahoo Finance library.

pip install yfinance

To ensure that you have installed Yahoo Finance you can execute the following command.

show yfinance

Fetch Stock data

YouTube: https://youtu.be/8gGV6eGp9IQ

Once Anaconda and Yahoo Finance has been installed we’re ready to create our first Notebook. It’s in our Notebook that we will write and execute our Python code. To create a Notebook Navigate to Jupyter and click New to create a new one.

In a Jupyter Notebook its possible to write Python code directly in between plain text. You execute a block of Python code by clicking the following button.

Jupyter Run Button

Next we will Import the following libraries, these is to be used throughout this guide.

import pandas as pd

import yfinance as yf

import matplotlib.pyplot as plt

Following the import of libraries a list of companies is defined based on the ones that we wish to fetch stock data from. For this guide we will fetch data from Apple and Google.

stocks = [“AAPL”, “GOOGL”]

You can stick with these or select stocks from other companies. This is done by navigating to Yahoo Finance or Google and search for the company. Here you will see their stock initials e.g.

Apple Stock code

Once you have defined a list of companies, the Yahoo Finance library will be used to fetch data for a given period, this is done using the following line:

data = yf.download(stocks, start = “2020–01–01”, end = “2020–11–28”)

When using the yf.download you need to provide the following three parameters Stock list, Start of period and End of period.

If you wish to look at the data fetched you can write:

data.head()

Fetched Stock data displayed using data.head()

Creating Stock Charts

YouTube: https://youtu.be/MwBtuEdFeBI

Now that we have fetched stock data based on a list of companies in a given period, it’s time to visualize the data by creating a chart as illustrated below:

Stock Chart

The first step in the creation of a chart is to define the parameters that the chart is to include. If you’ve tried to print the fetched stock data using data.head() you will notice that our dataset consist of the following: Adj Close, Close, High, Low, Open prices. In this series we will focus solely on the closing price on a given trading day. To do such we need to create a new variable where we copy the closing data of our original fetched data. This is done using the following line:

closedStocks = data.loc[:, “Close”].copy()

With our closed price dataset in place, its time to specify the parameters of the chart that is to be used. We will define the size of the Chart, the font size and the style. Feel free to adjust these parameters as they are purely cosmetics.

closedStocks.plot(figsize = (18, 8), fontsize = 15)

plt.style.use(“seaborn”)

With the styling parameters set, we’re left with the last and final line that will present our closed price stock data visually.

plt.show()

Normalize Stock Data

YouTube: https://youtu.be/COwS2JcZ2tM

If we’re to look solely on our stock Chart it might seems that Apple is doing far worse than Google in regards to performance, but the stock chart can be misleading as it does not account for different price ranges in the period e.g. Apple is starting at 7.6 US dollar and Google at 313 US dollar. What we want to do is to normalize the data in order to get a better overview of the price performance over time.

To Normalize the data we divide the series by the first element then multiply by 100, this will make all stocks in our chart start at 100 and display the percentage change each day from the start to the end of our chosen period.

The normalization is achieved using the following line:

norm = closedStocks.div(closedStocks.iloc[0]).mul(100)

Following this we can either choose to reuse our previous defined chart or create a new one to illustrate the difference. For the sake of this example we will define a new plot chart.

norm.plot(figsize = (18, 8), fontsize = 15)

plt.style.use(“seaborn”)

plt.show()

Normalized Stock Chart

If we compare the two charts its clear to see that the Apple stock is actually performing much better than the Google stock when looking a price increase over time.

Using this approach we can start compare and analyze the performance of different stocks.