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

mavg(n) appears to be generated starting from the beginning of the backtest period. Is there a way to get the n-Day Moving Average starting from Day 1 of the test?

2 responses

Here's an example using history (which is the only way to have data "warmed up" prior to the backtest start):

import pandas as pd  
from pytz import timezone

def initialize(context):  
    context.stocks = [sid(8554),sid(33652)]

def handle_data(context, data):  
    prices = history(31,'1d','price')[:-1]  
    print get_datetime().astimezone(timezone('US/Eastern'))  
    for stock in context.stocks:  
        print stock.symbol + ': ' + str(prices.mean()[stock])  

Make sense?

Grant

Ah... Hadn't delved into history or pandas yet.

Thanks :-)