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
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.