Help Change Simple Moving Average to an Exponential Moving Average

Hello all,

I'm a serious newbie at this but..

I think this is the 5-day SMA in my code (which someone helped me build because I'm still in coding school):

# compute the diff
vix_to_realized_vol_diff = vix_data-vol_data
# average for last 5 days
vix_to_realized_vol_diff_5 = sum(vix_to_realized_vol_diff[-5:])/5


I need that changed to a 5-day exponential moving average.

I have found this example function for an EMA online that uses numpy:

def ExpMovingAverage(values, window):
weights = np.exp(np.linspace(-1.,0.,window))
weights /= weights.sum()

a = np.convolve(values, weights) [:len(values)]
a[:window]=a[window]
return a


I am not certain on precisely how to place the values of my old SMA into the new example EMA. Could anyone out there give me a hand with that?

HUGELY APPRECIATED if anyone can... or IM me about it. Thanks!

3 responses

Hi Keith, I honestly don't fully understand what you're trying to do. So I'm not going to try and help you with the specific code. But, you might consider searching community messages for exponential moving average and variants thereof. Here for instance is a post that might be able to help you get going:

https://www.quantopian.com/posts/exponential-moving-average-but-when-do-we-get-ta-lib

There are quite a few others that relate to this computation. Good luck!

Sunil

Can you not just use pandas.rolling_mean() and pandas.ewma() since zipline provides pandas objects?

Hi Keith,

You can use the talib.EMA(params)