Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
how can I convert unfilled limit orders to market orders?

I have an algo that places limit orders through out the day. To ensure 100% fill rate I would like to use the last 10 minutes to replace these limit orders with market orders? Is there a replace order functionality in quantopian?

8 responses

Pavy, I think your only option would be to use get_open_orders and cancel the original limit order and replace with a new market order. something like:

#get a dictionary of sid->order object  
oo = get_open_orders()  
for stock, o in oo:  
   cancel_order(o)  
   order(stock, o.amount)  

You might want to have a filter to check that the order is a limit order you are canceling. I'd breakpoint in the loop to make sure the behavior is what you want.

thanks,
fawce

Disclaimer

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.

Thanks John. I will try to see if there is a property on order to tell if its a limit or market.

Pavy,

You could also confirm that the limit orders were cancelled prior to submitting the market orders. This way, there would be no risk of having both a limit order and a market order in place for a given security.

Grant

Thanks Grant. I could not find any way in API to check if a cancel instruction was successful i.e. the order was cancelled. Also what if a limit order is partially filled. Can I still cancel it?

Pavy,

If you want to check if there are any open orders, you can use:

    # skip bar if any orders are open  
    for stock in context.stocks:  
        if bool(get_open_orders(stock)):  
            return  

This assumes you are wanting to cancel all orders. If you just want to check that there are no limit orders, the logic would be different.

Regarding cancelling a partially filled limit order, I'd assume that this is possible, but I'd test it.

If you're just backtesting, the order cancellation process should be instantaneous, hence Fawce's use of the cancellation and submission in one minutely bar. Under live trading at IB, I figure that it'd be prudent to confirm cancellation prior to submission.

Grant

Curious no, how as one fleshes out a truly robust trading algorithm, one must delve into more and more complex details. The Pareto rule is most apropos here: 80% of trades flow through the central logic pipe of the strategy which represents just 20% of the strategy's code. But the other 20% of trades end up flowing through the complex, exception handling side of the strategy's logic which is built from the remaining 80% of the code. And often it's worse; 90:10, or 95:5. Imagine spending 95% of your time writing and debugging code for just 5% of your trades. It sucks, I know, done it too many times to count (not here though). But robustness is one of the first tenents of strategy dev.

oo = get_open_orders()
for stock, o in oo:
cancel_order(o)
order(stock, o.amount)

for the code suggestion above:
the order could be partial fill, then would my order not be:
order(stock, o.amount - o.filled)

You're on the right track.

    for stock, orders in get_open_orders().iteritems():  
        for o in orders:  
            if o.limit:  
                cancel_order(o)  
                order(stock, o.amount - o.filled)  
                log.info('{}  amt {}  filled {}  market {}'.format(o.sid.symbol, o.amount, o.filled, o.amount - o.filled))  

Also a quick easy way to verify one's limit orders are doing good work. Replace limit with stop for stop orders.