Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Strange output from 'Record' function in full backtesting

Hi everyone,

Sorry if it sounds like a noob question. I'm a college student and I was trying to compare the performance of a sample EWMA strategy on WTI futures to the oil price. Given we cannot use set_benchmark for continuous futures, I tried to record the spot futures price in the same backtesting chart.

import talib  
import datetime

def initialize(context):  
    fetch_csv('https://docs.google.com/spreadsheets/d/e/2PACX-1vRAaHlpWPQdaPvh-XFgjUKZ3WYp7iVzjOvhd5G2RkmBZrqady13tOKVY1FlcBJg_LIU8Efz4TJ8YxcU/pub?output=csv', date_column = 'Date', date_format = 'dd/mm/yyyy', symbol='Oil', pre_func = preview, post_func = date_correct)  
    #set_benchmark('Oil')  
    context.futures = continuous_future('CL', offset=0, roll='volume', adjustment=None)  
    #set_benchmark(context.futures)  
    # Create a variable to track the date change  
    context.yesterday = None  
    schedule_function(rebalance, date_rules.every_day(), time_rules.market_open())  


def preview(df):  
    log.info(df.head())  
    return df

def date_correct(df):  
    df = df.tshift(-1, freq=datetime.timedelta(1))  
    return df

def rebalance(context, data):  
    spot = data.current(context.futures, 'contract')  
    # added for comparison the standard ma  
    mavg_standard = data.history(context.futures, 'price', 11, '1d').mean()  
    record(position=context.portfolio.positions[spot].amount)  
    # Check for start of a new day  
    if get_datetime().date() == context.yesterday:  
        return  
    context.yesterday = get_datetime().date()  
    price_history = data.history(context.futures, 'price', 11, '1d')  
    maseries = price_history  
    maverage = talib.EMA(maseries, timeperiod=11)[-1]  
    current_price = data.current(spot, 'price')  
    position = context.portfolio.positions[spot].amount

    if current_price < maverage and position >= 0:  
        order_target_percent(spot, -1)  
       # log.info("SHORT %s" % (context.security.symbol))  
    # elif current_price > maverage and position <= 0:  
        # order_target_percent(context.security, 0)  
        # # log.info("COVER SHORT %s" % (context.security.symbol))  
    if current_price > maverage and position <= 0:  
        order_target_percent(spot, 1)  
       # log.info("LONG %s" % (context.security.symbol))  
    # elif current_price < maverage and position >= 0:  
        # order_target_percent(context.security, 0)  
        # # log.info("COVER LONG %s" % (context.security.symbol)  
    oilprice = data.current('Oil', 'price')  
    #record(Price=current_price)  
    #record(Ema=maverage)  
    #record(MaStandard=mavg_standard)  
    record(Oil=oilprice)  

While the output looks normal under 'build algorithm', when I switched to full backtesting view the 'Oil' line shows fluctuations that do not exist in the source data. Any idea how this can be fixed ?

Many thanks.