Hi there. It's my first day here working on learning how to write scripts to back-test ideas and I'm hitting a bit of a wall trying to merge a few concepts I've picked up from the tutorials.
This is super basic and I'm trying to start small and learn as I go how to write scripts. I'm not much of a developer. I'm starting out with wanting to write something that will show the effect of placing one trade per day at 3:55pm ET. (Five mins before the market closes)
The logic for that trade is simple. If the current price at 3:55pm is greater than the prior day close, buy $500 worth of SPY for each point greater. If it is less than the prior day close, do the opposite and sell $500 worth for each point lower.
I'm not saying this is some super strategy, just using something simple to get my feet wet so I can work from there and learn. My code is below and I am encountering errors that indicate to me I am not declaring variables and/or using them correctly in my code. Any help would be greatly appreciated.
def initialize(context):
context.spy = sid(8554)
set_commission(commission.PerTrade(9.99))
schedule_function(daily_trade, time_rules.market_close(minutes=5), half_days=False)
def handle_data(context, data):
price_history = data.history(context.spy, "price", bar_count=2, frequency="1d")
for s in context.spy:
prev_bar = price_history[s][-2]
curr_bar = price_history[s][-1]
delta = curr_bar-prev_bar
tde_amt = delta*500
def daily_trade(context, data):
order_value(s, tde_amt)
** Runtime error** happens saying the following:
TypeError: 'zipline.assets._assets.Equity' object is not iterable
...
USER ALGORITHM:9, in handle_data
for s in context.spy:
The Build Errors are as follows:
13 Warning Local variable 'tde_amt' is assigned to but never used
16 Warning Undefined name 's'
16 Warning Undefined name 'tde_amt'