Overview of 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.
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
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']
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
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')
<matplotlib.legend.Legend at 0x7fcba010cbe0>
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')
<matplotlib.legend.Legend at 0x7fcb974f6fd0>
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')
<matplotlib.legend.Legend at 0x7fcb976846d8>
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')
<matplotlib.legend.Legend at 0x7fcb971ec160>
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')
<matplotlib.legend.Legend at 0x7fcb975c3fd0>
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')
<matplotlib.legend.Legend at 0x7fcb973aed68>
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')
<matplotlib.legend.Legend at 0x7fcb972c4be0>
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')
<matplotlib.legend.Legend at 0x7fcb9741b908>
We have performed moving average on different intervals such as 9-12,10-20,12-22,50-200.