import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import talib
ma_length = 200
# Get some daily pricing to demo.
day_pricing = get_pricing('SPY', start_date='2019-01-18', end_date='2020-01-02')
day_pricing['close_price'][-10:]
Wilder EMA formula = price today K + EMA yesterday (1-K)
(where K =1/N and N = the number of periods)
def WildersMA(closes, length):
wma = np.zeros(len(closes))
k = 1.0/length
for i in range(0, len(closes)):
if i < 1:
wma[i] = closes[i]
else:
wma[i] = closes[i]*k + np.roll(wma, 1)[i]*(1-k)
return wma
wma = WildersMA(day_pricing['close_price'], ma_length)
wma = pd.Series(wma)
wma.index = day_pricing.index
plt.figure(1) # the first figure
### upper window ###
plt.subplot(311)
day_pricing['close_price'].plot()
wma.plot()