• For any query, contact us at
  • +91-9872993883
  • +91-8283824812
  • info@ris-ai.com

Moving Average

Moving Average (MA) is a technical indicator that smooths out price trends by filtering the "noise" from random short-term price fluctuations.It is used for forecasting goods with constant demand, where there is a slight trend or seasonality and it can help you identify areas of support and resistance.

In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from alpha_vantage.timeseries import TimeSeries
from alpha_vantage.techindicators import TechIndicators
import requests
import datetime
from sklearn import tree
import mplfinance as fplt
%matplotlib inline
In [2]:
url = 'https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY_ADJUSTED&symbol=IBM&apikey=3S58CRIJGD7I0I5Z'
r = requests.get(url)
ts = TimeSeries(url,output_format='pandas')

data, meta = ts.get_intraday('EURUSD=X', outputsize='compact')
data['4. close']
Out[2]:
date
2021-07-30 19:15:00    140.98
2021-07-30 17:30:00    141.19
2021-07-30 16:45:00    141.00
2021-07-30 16:30:00    141.00
2021-07-30 16:15:00    140.96
                        ...  
2021-07-28 13:00:00    142.09
2021-07-28 12:45:00    141.94
2021-07-28 12:30:00    142.13
2021-07-28 12:15:00    142.35
2021-07-28 12:00:00    142.43
Name: 4. close, Length: 100, dtype: float64
In [3]:
data, meta = ts.get_intraday('EURUSD=X', interval='1min', outputsize='compact')
data['4. close'].plot(figsize=(12,6))
data['4. close'].rolling(window =9).mean().plot(grid=True, label='sma 9')
data['4. close'].rolling(window =12).mean().plot(grid=True, label='sma 12')
plt.legend(loc = 'upper right')
Out[3]:
<matplotlib.legend.Legend at 0x7fcba010cbe0>
Moving Average
In [4]:
data, meta = ts.get_intraday('EURUSD=X', interval='5min', outputsize='compact')
data['4. close'].plot(figsize=(12,6))
data['4. close'].rolling(window =10).mean().plot(grid=True, label='sma 10')
data['4. close'].rolling(window =20).mean().plot(grid=True, label='sma 20')
plt.legend(loc = 'upper right')
Out[4]:
<matplotlib.legend.Legend at 0x7fcb974f6fd0>
Moving Average
In [5]:
data, meta = ts.get_intraday('EURUSD=X', interval='15min', outputsize='compact')
data['4. close'].plot(figsize=(12,6))
data['4. close'].rolling(window =12).mean().plot(grid=True, label='sma 12')
data['4. close'].rolling(window =22).mean().plot(grid=True, label='sma 22')
plt.legend(loc = 'upper right')
Out[5]:
<matplotlib.legend.Legend at 0x7fcb976846d8>
Moving Average
In [12]:
data, meta = ts.get_intraday('EURUSD=X', interval='15min', outputsize='compact')
data['4. close'].plot(figsize=(12,6))
data['4. close'].ewm(span=12).mean().plot(grid=True, label='ema 12')
data['4. close'].ewm(span =22).mean().plot(grid=True, label='ema 22')
plt.legend(loc = 'upper right')
Out[12]:
<matplotlib.legend.Legend at 0x7fcb971ec160>
Moving Average
In [6]:
data, meta = ts.get_intraday('EURUSD=X', outputsize='compact')
data['4. close'].plot(figsize=(12,6))
data['4. close'].rolling(window =50).mean().plot(grid=True, label='sma 50')
data['4. close'].rolling(window =200).mean().plot(grid=True, label='sma 200')
plt.legend(loc = 'upper right')
Out[6]:
<matplotlib.legend.Legend at 0x7fcb975c3fd0>
Moving Average
In [10]:
data, meta = ts.get_intraday('EURUSD=X', outputsize='compact')
data['4. close'].plot(figsize=(12,6))
data['4. close'].rolling(window =44).mean().plot(grid=True, label='sma 44')
plt.legend(loc = 'upper right')
Out[10]:
<matplotlib.legend.Legend at 0x7fcb973aed68>
Moving Average
In [11]:
data, meta = ts.get_intraday('EURUSD=X', outputsize='compact')
data['4. close'].plot(figsize=(12,6))
data['4. close'].ewm(span=44).mean().plot(grid=True, label='ema 44')
plt.legend(loc = 'upper right')
Out[11]:
<matplotlib.legend.Legend at 0x7fcb972c4be0>
Moving Average
In [9]:
data, meta = ts.get_intraday('EURUSD=X', outputsize='compact')
data['4. close'].plot(figsize=(12,6))
data['4. close'].ewm(span=9).mean().plot(grid=True, label='ema 9')
data['4. close'].ewm(span=20).mean().plot(grid=True, label='ema 20')
plt.legend(loc = 'upper right')
Out[9]:
<matplotlib.legend.Legend at 0x7fcb9741b908>
Moving Average

Conclusion

We have performed moving average on different intervals such as 9-12,10-20,12-22,50-200.

Resources You Will Ever Need