I am having two problems at the moment.
1) I decided to write an algorithm that would buy when the SPY 15 day MA crosses over its 126 day MA and sell when it crosses below. The code is below. For the entire backtest period, the ratio of 15 day to 126 day MA was 1, exactly. So I'm not sure why this isn't working correctly.
2) I like to use "print" statements when debugging my algorithms to ensure they are producing the correct results. The log of the backtest (when I'm doing a build) seems to print the print statements but if I do more than 1 build I see print statements from the previous build intermixed with the print statements from the new build. Is it possible to clear the log files from one build to the next to avoid this problem?
#Buy and sell SPY when 15 day MA is above 126 day MA
def initialize(context):
#initiate the global i
# global i
# i=0
#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):
# global i
# i=i+1
# print(str(i))
#compute the notional portfolio value
notional = context.portfolio.positions[context.SPY].amount * data[context.SPY].price
#compute a notional value where short positions require 2x cash
#notional =
#test the market MA
MAR_SPY = data[context.SPY].vwap(15) / data[context.SPY].vwap(126)
print(str(data[context.SPY].vwap(15))+" "+str(data[context.SPY].vwap(126))+" "+str(MAR_SPY))
if MAR_SPY > 1 and notional < context.max_notional:
order(context.SPY,+100)
print("Notional="+str(notional))
elif MAR_SPY < 1 and notional > context.min_notional:
order(context.SPY,-100)
print("Notional="+str(notional))