Hi All -
I've been playing with the sample algorithm and testing out some of the features of the API. I tried to design a rather simple algorithm, where you essentially buy into SPY when its 15 day MA crosses above its 126 day and sell when 15 crosses below 126. Here is what it looks like:
#Buy and sell SPY when 15 day MA is above 126 day MA
def initialize(context):
#Add SPDR SPY
context.SPY = sid(8554)
#Set min/max notional
context.max_notional = 10000.0
context.min_notional = -10000.0
def handle_data(context, data):
notional = context.portfolio.positions[context.SPY].amount * data[context.SPY].price
if MARatio_SPY(15,126)>1:
order(context.SPY,+100)
elif MARatio_SPY(15,126)<1:
order(context.SPY,-100)
def MARatio_SPY(smallday,bigday):
#Return the ratio of the small day MA to long day MA for SPY
#NOTE if ratio is >1 then small day MA is above long day MA (bullish indicator)
return ( data[context.SPY].vwap(15)/data[context.SPY].vwap(126) )
and here are my build errors:
14 Warning Local variable 'notional' is assigned to but never used
26 Warning Undefined name 'data'
26 Warning Undefined name 'context'
26 Warning Undefined name 'data'
26 Warning Undefined name 'context'
26 Error Runtime exception: NameError: global name 'data' is not defined
Any ideas?
p.s. - What is the benchmark in the backtest and can it be changed?