Perhaps someone can provide a code snippet showing how to cancel all open orders? Must involve cancel_order(order) and get_open_orders(sid=sid), but I haven't seen an example yet. Thanks, Grant
Perhaps someone can provide a code snippet showing how to cancel all open orders? Must involve cancel_order(order) and get_open_orders(sid=sid), but I haven't seen an example yet. Thanks, Grant
Hello Garnt,
I don't know if the attached has any merit - for me it raises more questions.
I think I gave feedback to Quantopian that get_open_orders() isn't actually empty when it's empty but that could be by design, I suppose.
And I'm worried now that Stop and Limit orders are open i.e. have they been sent to IB? I'm sure to many these are the most stupid of questions but I'm learning.
P.
I use this.
def closeAnyOpenOrders(stock):
orders = get_open_orders(stock)
if orders:
for order in orders:
message = 'Canceling order for {amount} shares in {stock}'
message = message.format(amount=order.amount, stock=stock)
#log.debug(message)
cancel_order(order)
Hi Grant, Peter,
The attached backtest has a couple helper methods that should cancel everything, either globally or for a given sid. Hope that helps!
Peter, you're correct that 'get_open_orders()' is returning a truthy value even when there are no open orders. Looks like a bug to me - I'll make sure a ticket is filed internally about it.
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.
Hello Brian/John,
Thanks. I'm actually more interested in "what is an open order?" and what it means to say "open orders are cancelled overnight" when clearly (or not) they can't be (or can be).
P.
I can't answer your question but I can tell you what I do to avoid it. I don't allow any orders to be carried over to the next day. If they haven't been executed by 3:55 EST I rid myself of them and do not allow my algorithm to issue any new orders.
# End of Day
if get_datetime().hour == 20 and get_datetime().minute == 55:
for stock in data.keys():
closeAnyOpenOrders(stock)
A brief note in case anyone has started to implement this. Sometimes the market closes early, and Quantopian maintains a database of the early closes. The helper functions are in:
https://github.com/quantopian/zipline/blob/master/zipline/utils/tradingcalendar.py
Grant
Here's my first attempt at a function to cancel all open orders at EOD, when running backtests. Note that it assumes EOD to be normally at 4 pm and at 1 pm for an early close, which is consistent with get_open_and_close in tradingcalendar.py:
def get_open_and_close(day, early_closes):
market_open = pd.Timestamp(
datetime(
year=day.year,
month=day.month,
day=day.day,
hour=9,
minute=31),
tz='US/Eastern').tz_convert('UTC')
# 1 PM if early close, 4 PM otherwise
close_hour = 13 if day in early_closes else 16
market_close = pd.Timestamp(
datetime(
year=day.year,
month=day.month,
day=day.day,
hour=close_hour),
tz='US/Eastern').tz_convert('UTC')
return market_open, market_close
Looking at the backtest Transaction Details, the first fill is timestamped at 9:32 am, so I think that this shows that the orders submitted at 4 pm (after EOD) are cancelled:
2014-02-06 16:00:00 SPY BUY 100 $177.45 $17,745.00
2014-02-07 09:32:00 SPY BUY 100 $178.28 $17,828.00
As a side note, this code may not be quite correct, since it is noted in get_early_closes:
# Not included here are early closes prior to 1993
# or unplanned early close
If I am reading things correctly, if over the span of the backtest, the early market close was not planned, the code will fail to cancel open orders at EOD (since it will never reach the 4 pm normal close condition). So the question for the Quantopian folks is why not update tradingcalendar.py to reflect actual early closings?
Grant
Hello Grant,
Isn't EOD cancelling in the algo irrelevant in live trading and when paper trading an IB account as all orders are cancelled overnight by Quantopian? And at some point this behaviour will be replicated in non-IB paper trading and the backtester?
P.
Hey Peter,
That's my understanding, although per the help page:
All open orders are cancelled at end-of-day
Presumably, this means that for a normal day, at 4 pm, all open orders are cancelled automatically by IB, per standing instructions (or Quantopian sends out a bulk cancellation instruction to IB, to be executed ASAP).
I'm not holding my breath for the behavior to be replicated outside of paper/live trading w/ IB. It's not backwards compatible and will complicate "back-of-the-envelope" backtesting (particularly for thinly traded securities). Also, Alisa points out on https://www.quantopian.com/posts/why-cancel-orders-at-end-of-day ("We're flexible and there is room to change this behavior, depending on the feedback from live traders and how they like the experience") that it isn't set in stone that Quantopian will maintain the automatic EOD cancellation.
As a side note, for full compatibility, Quantopian would need to emulate in the backtester the re-launching of the algorithm every day under paper/live trading w/ IB. In the end, they are absolutely doing the right thing by getting the entire end-to-end system up and running, but there is some "sausage making" going on that doesn't look pretty.
Grant