Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Scheduled Functions For Market Open and Close Run At The Same Time, Throwing Off Backtests

In the backtest, my return is 0% because the function that is supposed to be run at market open, and the function that is supposed to run at market close, seem to be run at the same time.

If you look at the log output in the endOfTradingDayFunction, it looks like this:

2015-08-03endOfTradingDayFunction:52INFO2015-08-03 00:00:00+00:00: Canceling order for 72 of DWTI created on 2015-08-03 00:00:00+00:00  

Which, looks to me, that they are running at the same time, and not allowing that day's activity to happen.

Any help is greatly appreciated. Thanks!

from pytz import timezone

# This is what interactive brokers charges us  
set_commission(commission.PerShare(cost=0.0075, min_trade_cost=1.00))

# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
def initialize(context):  
    # I want this to run every trading day, a couple minutes after we start trading  
    schedule_function(  
        func=myDailyTradingFunction,  
        date_rule=date_rules.every_day(),  
        time_rule=time_rules.market_open(minutes=30),  
        half_days=False  
    )  
    # IF we can't sell our stock on that day, just sell it  
    # General cleanup (cancle outstanding orders)  
    schedule_function(  
        func=endOfTradingDayFunction,  
        date_rule=date_rules.every_day(),  
        time_rule=time_rules.market_close(minutes=30),  
        half_days=False  
    )  
    return True

def myDailyTradingFunction(context,data):  
    # These are the stocks I want to trade between  
    oilUp = symbol('UWTI')  
    oilDown = symbol('DWTI')  
    # Figure out which stock to buy  
    # By default, we we figure oil is going down in price  
    stockToBuy = oilDown  
    if data[oilUp].price*1.01 > data[oilUp].open_price:  
        if data[oilUp].high == data[oilUp].price:  
            stockToBuy = oilUp  
    # Place the Buy order  
    order_target_percent(stockToBuy, 1)  
    # Place the Sell order  
    limitPrice = data[stockToBuy].price*1.01  
    stopPrice = data[stockToBuy].price*0.99  
    order_target_percent(stockToBuy, 0,  style=StopLimitOrder(limit_price=limitPrice, stop_price=stopPrice))  
    return True  
def endOfTradingDayFunction(context,data):  
    all_open_orders = get_open_orders()  
    if all_open_orders:  
        for security, oo_for_sid in all_open_orders.iteritems():  
            for order_obj in oo_for_sid:  
                log.info("%s: Cancelling order for %s of %s created on %s" %  
                         (get_datetime(), order_obj.amount,  
                          security.symbol, order_obj.created))  
                cancel_order(order_obj)  
    for stock in context.portfolio.positions:  
        order_target_percent(stock, 0)  
        log.debug(stock)  
    return True

# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    pass
2 responses

Are you running in Minutely or Daily mode? Your datetime is 00:00 which looks like Daily mode... hence why your functions are running at the same time

That fixed it. Thanks!