The strategy is more just a personal test on what happens to the market during lunch hours. For some reason I am getting negative returns on both when I go long spy from 12-2 and when I short spy from 12-2. Can someone take a look at my code and explain why
import pandas as pd
Put any initialization logic here. The context object will be passed to
the other methods in your algorithm.
def initialize(context):
context.sids = [sid(8554), sid(2174), sid(19920)]
Will be called on every trade event for the securities you specify.
def handle_data(context, data):
#Get the current exchange time in EST
exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')
#At the open (as close as we can execute at) close out an existing positions
if exchange_time.hour == 13 and exchange_time.minute ==59:
log.info('closing any existing positions at %s' % str(exchange_time))
for sid in context.sids:
order_target_percent(sid, 0)
#At the close (as close as we can execute) open positions in the stocks
#in our universe.
if exchange_time.hour == 11 and exchange_time.minute == 59:
for sid in context.sids:
order_target_percent(sid, -0.33)
log.info('buying position in %s at %s' % (str(sid),str(exchange_time)))