Back to Community
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)]  
    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:

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


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)