Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Is there a way to prevent orders from being cancelled at the end of the day?

It would be quite useful to be able to keep orders over-night for live trading.

Can we set some option in initialize to prevent this default behavior?

3 responses

If I recall, in live trading, all orders get cancelled at the end of day. Quantopian backtester is just replicating what IB does. If you want this behavior, you can reorder them yourself at open the next morning.

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.factors import AverageDollarVolume  
def initialize(context):  
    schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_close(hours=1))  
    # cancel the open orders ourselves so we can reorder tomorrow.  
    schedule_function(func=cancel,  
                      time_rule=time_rules.market_close(minutes=5),  
                      date_rule=date_rules.every_day())  
    schedule_function(func=reorder,  
                      time_rule=time_rules.market_open(minutes=5),  
                      date_rule=date_rules.every_day())

    dollar_volume = AverageDollarVolume(window_length=1)  
    pipe = Pipeline(columns={'dollar_volume': dollar_volume},  
                    screen=dollar_volume.percentile_between(5, 10))  
    attach_pipeline(pipe, 'pipe')  
    context.reorder_dict = {}  
def before_trading_start(context, data):  
    context.output = pipeline_output('pipe').head(5)  
    context.security_list = context.output.index  
def my_rebalance(context, data):  
    # buy a lot of some thinly traded stocks  
    for stock in context.security_list:  
        order_target_percent(stock, 0.2)  
        log.debug("Buy: {ticker}".format(ticker=stock))  
def cancel(context, data):  
    open_orders = get_all_open_orders()  
    for order in open_orders:  
        log.info("X CANCELED {0:s} with {1:,d} / {2:,d} filled"\  
            .format(order.sid.symbol,  
                    order.filled,  
                    order.amount))  
        cancel_order(order)  
        context.reorder_dict[order.sid] = order.amount - order.filled

def get_all_open_orders():  
    from itertools import chain  
    orders = chain.from_iterable(get_open_orders().values())  
    return list(orders)  
def reorder(context, data):  
    for stock, amount in context.reorder_dict.iteritems():  
        order_target(stock, amount)  
        log.info("Reorder {stock} {amount}".format(stock=stock, amount=amount))  
    context.reorder_dict = {}

@Tar Xvzf

Thanks, I am doing something similar at the moment (am using RH). However, I believe NYSE is FIFO, so I lose my position in the line to get filled at a certian price each morning when I replace the order (i.e. each morning I am sent to the back of the line).

@Ann

Thanks a lot for that elegant solution. As a newcomer to Quantopian, I was wrapping my head around the problem for hours without finding a solution myself.
However, I found a tiny mistake in the above solution:
in the reorder function one should use "order(stock, amount)" instead of "order_target(stock, amount)". With the later the reorder function changes the target amount of equities in the portfolio of a stock to the amount of stocks missing to fill, while we actually just want to order/sell the outstanding amount of equities.