Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Why this Mebane Faber strategy algo does not trade?

I am new on Quantopian and not very experienced in Python. The algo is running in backtest but does not produce trades. Any odeas why? pls do not refer to specifics of trade rules as opposed to other people implementation of Faber strategies because it is a simple rule (buy when previous day close is above 200 SMA) just to start with. Some silly thing I guess why does not work but cannot spot it for now. Thanks.

def initialize(context):  
    # Instantiate the object context which contain data and overall needed  
    # Reference to SPY as porxy for broad market  
    context.spy = sid(8554) # only SPY  
    schedule_function(rebalance, date_rule = date_rules.every_day(),         time_rule = time_rules.market_open())  
    # I also need to record values for statistics using schedule_function and record_vars. Update daily  
    schedule_function(record_vars, date_rules.every_day(), time_rules.market_close())  

def rebalance(context, data):  
    # Create 200 day  trailing window.  
    prices_200d = data.history(context.spy, 'close', 200, '1d')  


    # 200 day simple moving average (SMA)  
    sma_200d = prices_200d.mean()  
    # last_day_close  
    # you have to introduce this varbl cause panda series  
    # need a.all() or a.any() method for boolen comparison  
    # just data.history(context.spy, 'close', 1, '1d') > sma_200d brings error - an ambigious comparison  
    last_day_close = data.history(context.spy, 'close', 1, '1d')  
    if (last_day_close.any() > sma_200d):  
        order_target_percent(context.spy, 1.00)  
    else:  
        order_target_percent(context.spy, 0.00)  




def record_vars(context, data):  
    """  
    Record variables at the end of each day.  
    """  
    longs = shorts = 0  
    for position in context.portfolio.positions.itervalues():  
        if position.amount > 0:  
            longs += 1  
        elif position.amount < 0:  
            shorts += 1

    # Record our variables.  
    record(leverage=context.account.leverage, long_count=longs, short_count=shorts)  
1 response

Dmitry.

This should trade:

def initialize(context):  
    schedule_function(rebalance, date_rule = date_rules.every_day(), time_rule = time_rules.market_open())  
    schedule_function(record_vars, date_rules.every_day(), time_rules.market_close()) 

def rebalance(context, data):  
    stock = symbol('SPY')

    # To calculate today previous_day_sma_200d  you need 1 more bar of data:  
    prices = data.history(stock, 'close', 200 + 1, '1d')  
    previous_day_sma_200d = prices[:-2].mean()  
    previous_day_close = prices[-2] 

    if previous_day_close > previous_day_sma_200d:  
        order_target_percent(stock, 1.00)  
    else:  
        order_target_percent(stock, 0.00) 

def record_vars(context, data):  
    record(leverage = context.account.leverage)