## What will we cover in this tutorial?

In this tutorial we will cover the following

- Read historic time series data from
**Yahoo! Finance**using**Pandas-Datareader**. - Calculate the
**Average True Range (ATR)**. - Visualize it on a chart.

## Step 1: Read historic stock prices from Yahoo! Finance API

To read data from Yahoo! Finance API we use Pandas-Datareader, which has a direct method.

This requires that we give a start date on how old data we want to retrieve.

import pandas_datareader as pdr import datetime as dt start = dt.datetime(2020, 1, 1) data = pdr.get_data_yahoo("NFLX", start) print(data.tail())

This we result in similar output.

High Low Open Close Volume Adj Close Date 2021-02-12 561.250000 550.849976 556.940002 556.520020 2195900 556.520020 2021-02-16 563.630005 552.729980 557.289978 557.280029 2622400 557.280029 2021-02-17 555.250000 543.030029 550.989990 551.340027 2069600 551.340027 2021-02-18 550.000000 538.229980 549.000000 548.219971 2456200 548.219971 2021-02-19 548.989990 538.809998 548.000000 540.219971 2838600 540.219971

## Calculate the Average True Range (ATR)

The Average True Range (ATR) is calculated as follows, as **investopedia.org** defines it.

This can be calculated as follows.

import numpy as np import pandas_datareader as pdr import datetime as dt start = dt.datetime(2020, 1, 1) data = pdr.get_data_yahoo("NFLX", start) high_low = data['High'] - data['Low'] high_close = np.abs(data['High'] - data['Close'].shift()) low_close = np.abs(data['Low'] - data['Close'].shift()) ranges = pd.concat([high_low, high_close, low_close], axis=1) true_range = np.max(ranges, axis=1) atr = true_range.rolling(14).sum()/14

Where we use the 14 days standard.

## Visualize the ATR and the stock price

We will use **Matplotlib** to visualize it as it integrates well with DataFrames from Pandas.

import matplotlib.pyplot as plt fig, ax = plt.subplots() atr.plot(ax=ax) data['Close'].plot(ax=ax, secondary_y=True, alpha=0.3) plt.show()

This will result in a chart similar to this one.

Hello Rune…why are you still dividing the value for the ATR by 14 when you already have the rolling mean of 14

Hi Ricky,

Yes, it could have been done by using rolling mean.

Notice I did it with sum instead. Just to follow the formula from Investopedia.org.

`true_range.rolling(14).sum()/14`

Cheers,

Rune