## 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.

## Want to learn more?

This is part of a 2-hour full video course in 8 parts about Technical Analysis with Python.

If you are serious about learning **Python for Finance check out this course**.

- Learn
**Python for Finance**with pandas and NumPy. **21 hours**of video in over**180 lectures**.*“Excellent course for anyone trying to learn to code and invest.”*–**Lorenzo B**.

## 12% Investment Solution

Would you like to get 12% in return of your investments?

D. A. Carter promises and shows how his simple investment strategy will deliver that in the book **The 12% Solution**. The book shows how to test this statement by using backtesting.

**Did Carter find a strategy that will consistently beat the market?**

Actually, it is not that hard to use Python to validate his calculations. But we can do better than that. If you want to work smarter than traditional investors then continue to read here.

## Learn Python

**Learn Python A BEGINNERS GUIDE TO PYTHON**

- 70 pages to get you started on your journey to
**master Python**. - How to install your setup with
**Anaconda**. - Written
**description**and introduction to all concepts. **Jupyter Notebooks**prepared for 17 projects.

**Python 101: A CRASH COURSE**

- How to
**get started**with this 8 hours.**Python 101: A CRASH COURSE** **Best practices**for learning Python.- How to download the
**material**to follow along and create projects. - A chapter for each lesson with a
**description**,**code****snippets**for easy reference, and links to a**lesson video**.

## Expert Data Science Blueprint

**Expert Data Science Blueprint**

- Master the Data Science Workflow for actionable data insights.
- How to download the material to follow along and create projects.
- A chapter to each lesson with a Description, Learning Objective, and link to the lesson video.

## Machine Learning

**Machine Learning – The Simple Path to Mastery**

- How to get started with Machine Learning.
- How to download the material to follow along and make the projects.
- One chapter for each lesson with a Description, Learning Objectives, and link to the lesson video.

If you face some errors when reading data from Yahoo, try this:

!pip install –upgrade pandas-datareader

!pip install –upgrade pandas

Helped me a lot.

Great.