Calculate the Volatility of Historic Stock Prices with Pandas and Python

What will we cover in this tutorial?

We will calculate the volatility of historic stock prices with Python library Pandas.

Step 1: Read Historic Stock Prices with Pandas Datareader

We will use Pandas Datareader to read some historic stock prices. See this tutorial for details.

import pandas_datareader as pdr
import datetime as dt

ticker = "AAPL"
start = dt.datetime(2019, 1, 1)
end = dt.datetime(2020, 12, 31)

data = pdr.get_data_yahoo(ticker, start, end)

print(data.head())

Resulting in this.

                 High        Low       Open      Close       Volume  Adj Close
Date                                                                          
2019-01-02  39.712502  38.557499  38.722500  39.480000  148158800.0  38.505024
2019-01-03  36.430000  35.500000  35.994999  35.547501  365248800.0  34.669640
2019-01-04  37.137501  35.950001  36.132500  37.064999  234428400.0  36.149662
2019-01-07  37.207500  36.474998  37.174999  36.982498  219111200.0  36.069202
2019-01-08  37.955002  37.130001  37.389999  37.687500  164101200.0  36.756794

Step 2: Calculate the Volatility of an Asset

Let’s explore the difference between daily simple returns and daily log returns. Shortly explained, the log returns have the advantage that you can add them together, while this is not the case for simple returns. Therefore the log returns are used in most financial analysis.

To calculate the daily log returns we need the NumPy library. For the purpose here, we will not explore the depths of NumPy, all we need is to apply the log-function on a full column in our DataFrame (see my other FREE course for more details on NumPy).

import numpy as np

data['Log returns'] = np.log(data['Close']/data['Close'].shift())

This creates a column called Log returns with the daily log return of the Close price.

We need the standard deviation for the volatility of the stock.

This can be calculated from our Log returns as follows.

data['Log returns'].std()

The above gives the daily standard deviation. The volatility is defined as the annualized standard deviation. Using the above formula we can calculate it as follows.

volatility = data['Log returns'].std()*252**.5

Notice that square root is the same as **.5, which is the power of 1/2.

Step 3: Visualize the Volatility of Historic Stock Prices

This can be visualized with Matplotlib.

str_vol = str(round(volatility, 4)*100)

fig, ax = plt.subplots()
data[‘Log returns’].hist(ax=ax, bins=50, alpha=0.6, color=’b’)
ax.set_xlabel(“Log return”)
ax.set_ylabel(“Freq of log return”)
ax.set_title(“AAPL volatility: ” + str_vol + “%”)

Resulting in the following output.

Next steps?

Want to learn more?

This is part of the FREE online course on my page. No signup required and 2 hours of free video content with code and Jupyter Notebooks available on GitHub.

Follow the link and read more.

Simple and Exponential Moving Average with Python and Pandas

What will we cover in this tutorial?

In this tutorial we will cover how to calculate the Simple Moving Average (MA) and the Exponential Moving Average (EMA) of a Time Series using the Pandas library in Python.

Step 1: Read some Financial Historic Time Series Stock Prices

We will use Pandas Datareader to read some historic stock prices. See this tutorial for details.

import pandas_datareader as pdr
import datetime as dt

ticker = "AAPL"
start = dt.datetime(2019, 1, 1)
end = dt.datetime(2020, 12, 31)

data = pdr.get_data_yahoo(ticker, start, end)

print(data.head())

Resulting in this.

                 High        Low       Open      Close       Volume  Adj Close
Date                                                                          
2019-01-02  39.712502  38.557499  38.722500  39.480000  148158800.0  38.505024
2019-01-03  36.430000  35.500000  35.994999  35.547501  365248800.0  34.669640
2019-01-04  37.137501  35.950001  36.132500  37.064999  234428400.0  36.149662
2019-01-07  37.207500  36.474998  37.174999  36.982498  219111200.0  36.069202
2019-01-08  37.955002  37.130001  37.389999  37.687500  164101200.0  36.756794

Step 2: Calculate the Simple Moving Average with Python and Pandas

To calculate the Simple Moving Average (MA) of the data can be done using the rolling and mean methods.

data['MA10'] = data['Close'].rolling(10).mean()

Where here we calculate the Simple Moving Average of 10 days. You can change it to fit your needs.

Step 3: Calculate the Exponential Moving Average with Python and Pandas

It is a bit more involved to calculate the Exponential Moving Average.

data['EMA10'] = data['Close'].ewm(span=10, adjust=False).mean()

There you need to set the span and adjust to False. This is needed to get the same numbers as on Yahoo! Finance.

Next steps?

Want to learn more?

This is part of the FREE online course on my page. No signup required and 2 hours of free video content with code and Jupyter Notebooks available on GitHub.

Follow the link and read more.

Read Historical Prices from Yahoo! Finance with Python

What will we cover in this tutorial?

In this tutorial we will cover the following.

  • How to use Pandas Datareader to read historical stock prices from Yahoo! Finance.
  • Learn how to read weekly and monthly data.
  • Also how to read multiple tickers at once.

Step 1: What is Pandas Datareader?

Pandas-Datareader is an up to date remote data access for pandas.

This leads to the next question. What is pandas?

Pandas is a data analysis and manipulation tool containing a great data structure for the purpose.

Shortly said, pandas can be thought of as a data structure in Python, which is similar to working with data in a spreadsheet.

Pandas-datareader reads data from various sources and puts the data into a pandas data structures.

Pandas-datareader has a call to return historic stock price data from Yahoo! Finance.

To use Pandas-datareader you need to import the library.

Step 2: Example reading data from Yahoo! Finance with Pandas-Datareader

Let’s break the following example down.

import pandas_datareader as pdr
import datetime as dt
 
ticker = "AAPL"
start = dt.datetime(2019, 1, 1)
end = dt.datetime(2020, 12, 31)
 
data = pdr.get_data_yahoo(ticker, start, end)

print(data)

Where we first import two libraries.

  • pandas_datareader The Pandas Datareader. If you do not have it installed already in your Jupyter Notebook you can do that by entering this in a cell !pip install pandas_datareader and execute it.
  • datetime This is a default library and represents a date and time. We only use it for the date aspects.

The the following lines.

  • ticker = “AAPL” The ticker we want data from. You can use any ticker you want. In this course we have used the ticker for Apple (AAPL).
  • start = dt.datetime(2019, 1, 1) Is the starting day we want historic stock price data.
  • end = dt.datetime(2020, 12, 31) The end day.
  • data = pdr.get_data_yahoo(ticker, start, end) This is the magic that uses Pandas Datareader (pdr) to get data from the Yahoo! Finance API. It returns a DataFrame as we know it from previous lessons.

The output of the code is as follows.

                  High         Low  ...       Volume   Adj Close
Date                                ...                         
2019-01-02   39.712502   38.557499  ...  148158800.0   38.505024
2019-01-03   36.430000   35.500000  ...  365248800.0   34.669640
2019-01-04   37.137501   35.950001  ...  234428400.0   36.149662
2019-01-07   37.207500   36.474998  ...  219111200.0   36.069202
2019-01-08   37.955002   37.130001  ...  164101200.0   36.756794
...                ...         ...  ...          ...         ...
2020-12-24  133.460007  131.100006  ...   54930100.0  131.773087
2020-12-28  137.339996  133.509995  ...  124486200.0  136.486053
2020-12-29  138.789993  134.339996  ...  121047300.0  134.668762
2020-12-30  135.990005  133.399994  ...   96452100.0  133.520477
2020-12-31  134.740005  131.720001  ...   99116600.0  132.492020

[505 rows x 6 columns]

Step 3: A few parameters to set

You can get multiple tickers at once by parsing a list of them.

import pandas_datareader as pdr
import datetime as dt

ticker = ["AAPL", "IBM", "TSLA"]
start = dt.datetime(2019, 1, 1)
end = dt.datetime(2020, 12, 31)

data = pdr.get_data_yahoo(ticker, start, end)

print(data)

You can get the weekly or monthly data by using the argument as follows.

import datetime as dt

ticker = ["AAPL", "IBM", "TSLA"]
start = dt.datetime(2019, 1, 1)
end = dt.datetime(2020, 12, 31)

data = pdr.get_data_yahoo(ticker, start, end, interval='w')

print(data)

Set interval=’m’ to get monthly data instead of weekly with ‘w’.

Next steps?

Want to learn more?

This is part of the FREE online course on my page. No signup required and 2 hours of free video content with code and Jupyter Notebooks available on GitHub.

Follow the link and read more.