Ways to repopulate IB trades/orders in strategy after shutdown
scottz1 last edited by
Hey folks, can anyone share thoughts on this? Suppose you have a daily strategy running live in Backtrader with IB, with an active trade and a pending stop order, when your machine shuts off, accidentally or intentionally to save energy for the weekend. Two solutions came to mind, but I'd love to hear others chime in.
- When submitting an order or opening a trade, save the object to disk. Read it back in strategy init. Pro: easy to code. Con: could get out of sync with broker, e.g. if broker closes a position or order expires.
- In strategy init, call IB Client Portal Web API from to get current state and create a new position and order from it. Pro: accurate broker state. Con: more coding effort and need to make sure all the appropriate fields are populated.
Requirement: Use a specific
clientId, ALWAYS the same. You will then only request orders which were issued by your client and not others.
Spend your time in extending the broker to retrieve open orders upon connecting to the server.
And then match the open orders and current position against your record of open orders and current position.
You need to take the open position into account, because orders are tied to the specific
clientId, but a position is general.