Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help getting started with futures contracts and an error trying to backtest the moving average with an order.

I am trying to figure out how to get started using this data in automation. I am getting an error that I have not specified the contract when I am working this code. Can anyone help me understand what I should be doing to have the 'YM' buy or sell a contract when the moving averages crossover?

def initialize(context):  
    # Dow Jones E-Mini Continuous Future  
    context.future = continuous_future('YM')  
    schedule_function(daily_func, date_rules.every_day(), time_rules.market_open())

def daily_func(context, data):  
    ym_active = data.history(context.future, 'price', 50, '1m')  
    log.info(ym_active)  
    sma_50 = ym_active.mean()  
    sma_20 = ym_active[-20:].mean()  
    if sma_20 > sma_50:  
        #I am running into a problem where it says that I have to specify the contract.  
        order(context.future, 1, style=MarketOrder)  
    elif sma_20 < sma_50:  
        #I am running into a problem here too.  
        order(context.future, -1, style=MarketOrder)