Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Going long and short are both giving negative returns

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)))  
2 responses

commission and slippage..

What is happening is that Quantopian's default commission and slippage models are working. This simulates the amount paid for each trade and the amount above/below price your order is filled at. If you add to your code:

    set_commission(commission.PerShare(cost=0.00, min_trade_cost=0))  
    set_slippage(slippage.VolumeShareSlippage(volume_limit=0.0, price_impact=0.10))

You should get no profit or loss. If not, there is something else wrong with your code, which there does not look to be.

Hope that helps! :)
Max