Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Historical Simple Moving Averages

Hello, this seems like it will be fairly straightforward so sorry in advance if this is trivial. I want to get the X-day Simple Moving Average of yesterday, the day before, etc. How do I do this?

Thanks in advance!

3 responses

When I'm faced with a problem like this, I get a big chunk of history, and take segments of it. Then manipulate that segment however I need.

hist = history(11, '1d', 'price')  
yesterdays_ten_day_avgs = hist[:-1].mean()  
todays_ten_day_avgs = hist[1:].mean()  

The brackets get segments of data, or slices, then we take the mean of that data. Hope this helps.

Calvin

Another option is the rolling_mean function in Pandas.

The rolling_mean is a lot more elegant than my method, thanks.