Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Pardon my newbness, question on algorithm - SPY volatility

Very new to this and got stuck. Any help/insight would be appreciated.

what i'm trying to backtest

All in on something (using SPY , for exmple). In the event that SMA drops x% (10%, for example) - OR volatility spikes (we'll also say 10%) ,over 30 day period, go into something less risky (TLT, for example). until sma ticks back up (using 10%, again) AND (not or) volatility also drops 10% at which point we'll go all in on SPY again. I hope that made sense.

What I've got

Import VIX from Yahoo

url ='http://www.quandl.com/api/v1/datasets/YAHOO/INDEX_VIX.csv?request_source=python&request_version=2&sort_order=asc&trim_start=2002-01-01'

Define symbols, etc

def initialize(context):
context.spy = symbol('SPY')
context.tlt = symbol('TLT')
context.date = None
fetch_csv(url,
symbol='VIX',
date_column='Date',
pre_func=add_fields)

get 30 day SMA

def handle_data(context, data):
sma = data[context.spy].mavg(30)
price = data[context.spy].price

Here's where I get stuck. I don't know what combination of if/elif/else sequences will calculate the +/- 10% scenarios AND trigger the moving between SPY & TLT.

Thanks in advance for reading this far and for any guidance.

2 responses

I'm not an expert either but it seems to me you can just use python's OR and AND operators, so:

if smachange > -10% or vixchange > 10%:  
    do something  
elif  
   do something else

if smachange > 10% and vixchange > -10%  
   do something  
elif  
   do something else  

where you need to qualify smachange etc of course.

I'm not an expert either but it seems to me you can just use python's OR and AND operators, so:

if smachange > -10% or vixchange > 10% :  
   do something  
elif ...  
   do something else

if smachange > 10% and vixchange > -10% :  
   do something  
elif ...  
   do something else  

where you need to qualify smachange etc of course.