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

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