Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Starting out and having a little trouble with my first script.

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'

2 responses

Hello, welcome to Quantopian.

When you are looking for help in code, it's often best to share the full backtest. That makes it much easier to debug because we get the full code, formatting, dates, capital, etc.

I was able to get your code to run. There were a bunch of blockers. You were trying to iterate over a single equity in for s in context.spy. To fix that I put your equity in brackets so it iterates over the items in brackets. You also were using local variables in handle_data() and then trying to use them in daily_trade() - you need to put your variables on the context object so they can be passed around from function to function.

The code now runs, but I don't think it makes a lot of sense. You calculate the tde_amt every minute because it's in handle_data, but you only use the tde_amt once per day in handle_data(). So I think you have some logical questions you need to work out for yourself, depending on what you're trying to do.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Hi, yes thank you. I knew I was handling the variables wrong, just wasn't sure how.

I'm only trying to look at two numbers per day, not per minute. Yesterday's close, and the current price at 3:55pm. I'm then calculating the delta of those and either buying or selling the delta times $500 depending on whether SPY is up or down on the day as of that time. One trade per day.

I'm assuming it just takes forever to run because I'm calculating everything in handle_data() every minute.