Working through algo bugs

Ok,

I am getting somewhere with my 1st algorithm. Just need to work through some bugs.

First, what does the algo do?

Captures the opening price and then waits until the current_price is between .10 and .20 above the open price.

The sell conditions are as follows:

If current_price falls below opening price by .10.
If current_price is > $1.10 above opening price. If EOD is approaching. I cannot figure out why the first two transactions are BUYS, I only want one open position at a time, so there should not be two BUYS in a row in the transaction table. I also cannot figure out why the first SELL was for 100 shares, there were many more shares that should have been sold. And finally, the log shows multiple "below open price sales" for a similar amount right in a row. 13 Loading... Total Returns -- Alpha -- Beta -- Sharpe -- Sortino -- Max Drawdown -- Benchmark Returns -- Volatility --  Returns 1 Month 3 Month 6 Month 12 Month  Alpha 1 Month 3 Month 6 Month 12 Month  Beta 1 Month 3 Month 6 Month 12 Month  Sharpe 1 Month 3 Month 6 Month 12 Month  Sortino 1 Month 3 Month 6 Month 12 Month  Volatility 1 Month 3 Month 6 Month 12 Month  Max Drawdown 1 Month 3 Month 6 Month 12 Month # these modules are used in the example from collections import deque import pytz EST = pytz.timezone('US/Eastern') import numpy as np # these modules are optional but useful from scipy import stats import math import random import datetime def initialize(context): context.nflx = sid(23709) #NFLX context.initialize = True context.event_day = 0 context.first_day = False context.new_day = False context.day_counter = 0 pass def handle_data(context, data): nflx = data[context.nflx] date = get_datetime().astimezone(EST) event_datetime = data[context.nflx].datetime event_day = data[context.nflx].datetime.day if context.initialize: context.event_day = event_day context.initialize = False context.first_day = True if event_day != context.event_day: context.openp = data[context.nflx].price context.new_day = True log.info("New day, open price: {p} ".format(p=context.openp)) else: context.new_day = False if context.first_day or context.new_day: context.day_counter = context.day_counter + 1 print event_datetime context.openp = data[context.nflx].price log.info("New day, open price: {p} ".format(p=context.openp)) else: # If our stock is in the buyzone, check if there is an order, if not #place an order if context.openp + .20 >= data[context.nflx].price >= context.openp + .10: if(context.portfolio.positions[context.nflx].amount == 0): context.shares = context.portfolio.cash/data[context.nflx].price order(context.nflx,context.shares) log.info("Stock purchase, {s} shares purchased at {p}.".format(s=context.shares, p=data[context.nflx].price)) # if stock is$1.00 higher than open, if we own the stock, sell the entire position
if data[context.nflx].price >= context.openp +1.10:

if(context.portfolio.positions[context.nflx].amount > 0):

order(context.nflx,-context.portfolio.positions[context.nflx].amount)
log.info("$1-Stock sale, {s} shares sold at {p}.".format(s=context.shares, p=data[context.nflx].price)) # if stock is below the buyzone, if we own the stock, sell the entire position if data[context.nflx].price <= context.openp -.10: if(context.portfolio.positions[context.nflx].amount > 0): order(context.nflx,-context.portfolio.positions[context.nflx].amount) log.info("Below Open-Stock sale, {s} shares sold at {p}.".format(s=context.shares, p=data[context.nflx].price)) # check if closing rally (3:46pm - 4:01pm) # (NOTE: some days the market closes before this!) #Sell our position at the end of the day if(date.hour == 15 and date.minute >= 46) or date.hour == 16: if(context.portfolio.positions[context.nflx].amount > 0): order(context.nflx, -context.portfolio.positions[context.nflx].amount) log.info("EOD-Stock sale, {s} shares sold at {p}.".format(s=context.shares, p=data[context.nflx].price)) context.first_day = False context.event_day = event_day This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes. There was a runtime error. 2 responses Hi Chuck, Let's see if I can understand what you want to do: 1. Trade in one security only (e.g. Netflix). 2. Each day, record the opening price. 3. If the price rises and lands between$0.10 & $0.20 above the opening price, then execute a single buy for the day, shifting all cash into the market. 4. Presumably, if it gets too late in the day, you will no longer buy. 5. If you have bought into the stock, then sell all shares: If current_price falls below opening price by .10. If current_price is >$1.10 above opening price.
If EOD is approaching.

Did I miss anything?

Grant

Grant that is exactly what i want to do.

I think I have some issues with the if statements. Not sure why my buy and sells are not "matched" up.