Back to Community
Cancel_orders is not working as expected

I have a universe of stocks that are not so liquid, but I am executing trades every 30 mins.
Before I execute each trade, I will make sure I have cancelled all the open orders, using:

open_orders = get_open_orders()
cancel_order(order for order in open_orders)

However, whenever I check the open_orders after the above code is executed, there are still orders that are not cancelled.
Actually I did this:
open_orders = get_open_orders()
cancel_order(order for order in open_orders)
open_orders = get_open_orders()

Strangely, N1=N2 all the time. It is not supposed to be this way.

Please help.

import numpy as np  
import pandas as pd

def initialize(context):  
    #initiate symbols we want to trade.  
    #should read from master.csv file when available.  
    #below are only randomly picked stocks from asian ETF markets

    context.tradeFreq=30 #only make a trade every 60 minutes  
    #define the trading time as how many minutes after market open  
    set_commission(commission.PerShare(cost=0.02, min_trade_cost=0.1))  
    for minute in context.tradingTime:  
def handle_data(context, data):  
    mins = get_datetime().minute  
    #print mins  
    if mins in context.tradingTime:  
        if nstocks<(context.longNum+context.shortNum):  
            for i in xrange(1,longNum):  

        #Cancel any orders that are still open  
        open_orders = get_open_orders()  
        cancel_order(order for order in open_orders)  
        #start trading  
        for stock in context.stocks:  
            if stock in longStocks:  
            elif stock in shortStocks:  
        record(total_position_value=context.account.total_positions_value) "longs: "+", ".join([long_.symbol for long_ in longStocks])) "shorts: "  +", ".join([short_.symbol for short_ in shortStocks]))  

def sort_returns(context):  
    #rank stock returns by the ascending order  
    return ranks  
6 responses

Like the order* functions, cancel_order probably just sends aa cancel request to the Quantopian engine and returns, and you will only see a change in your outstanding orders a minute lapass

Edited to add:
Another thing is that you are passing a sequence of orders. According to the documentation, you can only oass a single order id or a string. So you should instead do this:

    open_orders = get_open_orders()  
    for order in open_orders:  
        cancel_order(order) "Cancelling order {}".format(order))  

André beat me to it, and he's exactly right.

All order commands are asynchronous. When your code executes an order (or cancel order), that information is sent to the brokerage essentially immediately. However, your code is not waiting for that order to be acknowledged - your code continues.

In situations like this you have two options. The safe way is to wait another minute and verify that the orders were closed, then submit new orders. The aggressive way is to fire off the cancels, hope they work, and press on with new orders.


The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

With the "aggressive way", I would recommend you make sure you don't order the same assets you have cancelled.

The cancel_order() code above doesn't actually cancel the order, it silently does nothing (a bug in my opinion).

Use this to cancel orders instead, it works:

def cancels(context, data):    # Cancel all open orders  
    for o_list in get_open_orders().values():   # List of lists of all orders  
        for order in o_list:                    # Each order in the list  
            cancel_order(              # The order id  

There's something weird about that, maybe can be improved. This works also and equally odd, it's the use of values() that does that I guess:

def cancels(context, data):    # Cancel all open orders  
    o_list = get_open_orders().values()   # List of lists of all orders  
    for order in o_list[0]:               # Each order in the list  
        cancel_order(            # The order id  

This might be more clear:

def cancels(context, data):  # Cancel all open orders  
    oo = get_open_orders()             # Dict of security objects with open orders  
    for sec in oo:                     # Each security object has a list  
        for order in oo[sec]:          # Each order in the list  
            cancel_order(     # The order id  


def cancels(context, data):  # Cancel all open orders  
    for sec in get_open_orders():  
        for order in get_open_orders(sec):  

Those can all be just cancel_order(order), I like the .id for certainty, as it would generate an error if no id present, the erroneous one posted above would have been caught for example.

@garyha - You're right, my code of Aug. 27, 2015 is incorrect. Quantopian help documentation for get_open_orders correctly states that, when called with no argument, it "returns all open orders", which I took to mean "returns a list of all open orders".

Instead, as the documentation clarifies two paragraphs later, it "returns a dictionary keyed by security id", each entry of which "contains a list of orders for each sid". Thus, when used as an iterator, get_open_orders() yields Equity objects representing securities for which there are unfilled orders.

I would recommend your code under "This might be more clear" above, or this, more Pythonic and perhaps slightly faster:

def cancel_open_orders(context, data):  # Cancel all open orders. AP 2016-01-22  
    for sec, oo_sec in get_open_orders().items():  
        for order in oo_sec:  
  "Cancelling order {}: {} {}/{}".format(, sec.symbol, \  
                     order.amount-order.filled, order.amount)) # n shares unfilled / ordered  

where oo_sec is a list of open orders for security sec, such as oo[sec] or the result of get_open_orders(sec).

Good to see the pertinent order details in your log line (since order objects can otherwise be really tough to parse with the eye).

My "This might be more clear" surprisingly is a couple hundredths faster than yours on average.

Timing code