Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
New to quantopian - please help

Hi everyone,

Apologies in advance for the dumb questions - this is my first Quantopian algorithm and I am new to Python as well.

The strategy that I am trying to test:

  • we are using Vanguard S&P500 ETF all the way (VOO);
  • we are in a buy and hold position all the time, EXCEPT when two conditions are met:
    -VOO is below its 12-month moving average
    AND
    - unemployment rate is above its 12-month moving average.

This is the code that I ended up having:

def initialize (context):  
    context.voo = sid(40107)  
    date_rule=date_rules.month_start(days_offset=5)  
    time_rules.market_open(hours=1)  
def handle_data (context, data):  
    hist = data.history(context.voo, 'price', 400, '1d')  
    monthly_voo = hist.resample('1M').last()  
    sma_12_voo=hist[-12:].mean()  
    from quantopian.pipeline.data.quandl import fred_unrate  
    sma_12_unempl=hist[-12].mean()  


if monthly_voo>sma_12_voo or fred_unrate<sma_12_unempl:  
    order_target_percent(context.voo,1,0,style=MarketOrder)  
else:  
    order_target_percent(context.voo,0,0,style=MarketOrder)  

The error that I am getting:

Runtime exception: NameError: name 'monthly_voo' is not defined

I would really appreciate is someone could help me debugging this.

Also - if you see any other errors, please let me know. Many thanks in advance for all your help!

2 responses

@ Aleksei,
something very similar from my library can help.

# MAC of a stock and fred_unrate  
from quantopian.pipeline.factors import SimpleMovingAverage as SMA  
from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline.data.quandl import fred_unrate  
from quantopian.pipeline import Pipeline  
# ------------------------------------------------------------------------  
STOCK = symbol('SPY'); BOND = symbol('TLT'); MA = 200; LEV = 1.0; wt = {};  
# ------------------------------------------------------------------------  
def initialize(context):  
    schedule_function(trade, date_rules.month_start(5), time_rules.market_open(minutes = 30))  
    unrate_latest = SMA(inputs = [fred_unrate.value], window_length = 1)  
    unrate_sma = SMA(inputs = [fred_unrate.value], window_length = MA)  
    attach_pipeline(Pipeline({'unrate': unrate_latest, 'unrate_sma': unrate_sma,}), 'pipeline')

def trade(context, data):  
    output = pipeline_output('pipeline')  
    unrate = output[:1].unrate  
    unrate_sma = output[:1].unrate_sma    

    price_sma = data.history(STOCK, 'price', MA,'1d').mean()  
    price = data.current(STOCK, 'price') 

    if all(data.can_trade([STOCK,BOND])):  
        if unrate[-1] < unrate_sma[-1] and price > price_sma:  
            wt[STOCK] = LEV; wt[BOND] = 0

        elif unrate[-1] > unrate_sma[-1] or price < price_sma:  
            wt[STOCK] = 0; wt[BOND] = LEV 

    for sec, weights in wt.items():  
        order_target_percent(sec, weights)  

    record(unrate = unrate, unrate_sma = unrate_sma)  

@Vladimir,

Thank you so much for the prompt response and for sharing your code!

Being a newbie, some pieces of your code I don't fully understand, but will take time on Monday to figure out how it works.

Still not sure why I am receiving an error - 'monthly_voo' seems to be perfectly defined to me. Perhaps will figure out once I better understand your code.

Thank you very much again and have a great weekend!

Alexey