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.
David