Back to Community
Anybody have a solid code framework for tracking/confirming when multiple orders are completed when using Schedule_Function??

During algorithm testing I've noticed when using 'Schedule_Function' and 'order_target_percent' that if the orders aren't filled within the specified day, the open order carries over until the next 'Schedule_Function' call. For example, a monthly scheduled buy order 15 minutes before close, if not filled that day appears to stay open until the next month's 'Schedule_Function' call in which case it tries to fill the order out of sequence which really mucks up the strategy.
I could use a simple cancel all orders code block but then portfolio allocation is affected.

Within the community I've only seen this issue addressed using a 'cancel all orders' code block when integrated with a 'Schedule Function' or the orders are handled within the 'handle_data' call using some combination of 'get_datetime()' and datetime tracking without the use of 'Schedule Function', which goes against the recently published "Tips for Writing Robust Algorithms for the Hedge Fund"

Any help is appreciated.

3 responses

Maybe try duplicating any of your schedule_function calls to call track_orders immediately after them.

Cancel orders, where 'who' is a string you could use to identify where the call came from (since it could be from more than one place):

def cancel_open_orders(who):  
    for security, oo_for_sid in get_open_orders().iteritems():  
        for o in oo_for_sid:  
            if o.amount != 0 and o.filled != o.amount:  
                shrs = o.amount - o.filled  
      '{}  _ c a n c e l _  {}/{} {}'.format(who, shrs, o.amount, o.sid.symbol))  

@garyha I'll test the track_orders code block later tonight; it def looks applicable/useful on first glance. To clarify I don't want to cancel open orders. Ideally I'm trying to confirm that any Buy/Sell orders sent by my Schedule_Function are completed before the next Schedule_Function call. Essentially I don't want my algorithm to send new orders before the previous Buy/Sell orders are completed.

This for that ...

for stock in data:  
    if get_open_orders(stock):  

        < your strategy ... >