Back to Community
Time Management for Orders

A common inconsistency with backtesting vs. live trading is the length of time that orders remain valid. IB's default "time in force" model is a DAY order, which means that IB cancels all open orders at the end of the day unless explicitly told not to do so. However, in backtesting all orders persist until they are filled or explicitly cancelled.

I wrote an "OrderWrapper" class and some time-in-force models to more accurately model the time-in-force options IB offers. I implemented the following ones.

  • DAY: this is the default time in force. It is cancelled at the end of the day
  • GTC Good-Till-Cancelled: This order remains valid until the end of the following business quarter. IB does the same thing except the order is also cancelled if there are any corporate actions on the stock.
  • IOC Immediate-Or-Cancel: this order is given one minute to fill, any unfilled portion is cancelled.
  • GBD Good-Between-Dates: The order only becomes valid between two dates. If an end date is specified with no start, the order is valid right away. Allows orders to be scheduled in the future which is pretty cool.

The algo just blindly buys 5000 shares of two illiquid biotechs all the time to demo what's going on. You will see that the full 5000 shares rarely gets through due to slippage. I used the IOC and default DAY orders here.

I hope you find this useful.


Clone Algorithm
Backtest from to with initial capital
Total Returns
Max Drawdown
Benchmark Returns
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 559a866ab94da412592f0a91
There was a runtime error.
2 responses

This is nice, but why not just implement proper order types in the IB interface as well as zipline? IOC is quite different from allowing an order a whole minute to execute.

It would be nice to have "one cancels another" pairs of orders.
For example, a strategy that exits with either a stop-loss or stop-profit.
When either order is triggered it cancels the other order.

[If this feature exists please comment here]