I'm trying to learn how to use the platform and could use some help. I created the below algorithm, but it isn't working how I expected. I came across a paper that basically invested in the S&P above its 200SMA and went to cash when below. I wanted to try and mimic those results as a way to learn a basic Quantopian algorithm. Any advice on what I'm doing wrong here?
import numpy as np
# This function is run once at the beginning of the algorithm, REQUIRED
def initialize(context):
# SPY stock
context.spy = sid(8554)
# This function is run once per day before any calls to handle_data, OPTIONAL
def before_trading_start(context, data):
pass
# This function is run once per bar, REQUIRED
def handle_data(context, data):
twohund_sma = history(201, '1d', 'close_price')
current_price = data[context.spy].price
for spy in data:
if current_price > np.mean(twohund_sma[spy][:-1]):
order_target_percent(context.spy, 1.0, style=MarketOrder())
else:
order_target_percent(context.spy, 0.0, style=MarketOrder())
pass