Someone help an amateur figure this out?
I'm not sure why my if statement isn't working, towards the bottom:
def initialize(context):
context.aapl = sid(24)
#can only go long - no shorting
set_long_only()
#portfolio can only have 1 share of AAPL at a time
set_max_position_size(symbol('AAPL'), max_shares=1)
def handle_data(context, data):
#the sume of the prior 11 minutes volume, excluding the most recent minute
hist = data.history(sid(24), 'volume', 10, '1m')
vol_10 = hist[0:10].sum()
#volume of the most recent minute
vol_1 = data.current(sid(24), 'volume')
#define current trading day low price
day_low = data.history(sid(24), 'low', 1, '1d')
#define current price
current_price = data.current(sid(24), 'price')
#define cost basis
cost_basis = context.portfolio.positions[context.aapl].cost_basis
#define current position
current_position = context.portfolio.positions[context.aapl].amount
#if most recent minute volume is > 25% of the prior 10 minutes total volume
#and the current price is within 5 minutes of the low price
#for the day, buy 1 share of AAPL
if vol_1 > vol_10 *.25 and current_price == day_low:
order(('AAPL'), 1)
#if position opened, simultaneously open stop loss order at .999% of cost basis
if current_position > 0:
order(('AAPL'), -1, style=StopOrder(cost_basis *.999))
It's basically placing an order when current minute volume is greater than past 10 minutes volume *.25 and price is at the low of the day. Also, simultaneously placing a stop loss order at .001% below cost basis.
Another: I wanted to place an order if the volume spike is within 5 minutes of the day's low price, not just at the low price, but couldn't figure out how.