For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
2019-10-02: The community is currently in read-only mode

Bracket order persistence in live trading (IB) across sessions



  • I have built trading and analysis platform using backtrader framework. So thank you for all the time and effort on the project.

    I use bracket orders when placing (live) in IB to have profit taking and stop-loss orders all in one.

    The issue I currently have once a bracket order is placed in the current application session (or one single trading day) and stop the app overnight for maintenance and then restart it following morning for the 2nd session the bracket order information doesn't get loaded into the strategy, as a result the application doesn't have a sense of bracket order that could be in the live platform.

    The adverse effect of this issue is that my strategy does have other logic which initiates a SELL based on indicator signals, the 2nd app session doesn't have a sense of currently open bracket orders at the broker, app initiates a "SHORT" position.

    So far I see two solutions

    1. Some way to resync broker (IB) bracket orders back in strategy.
    2. Save strategy / application state

    I don't know if IB bracket order sync capability is there, if someone could explain how to use this that would solve my issue. I tried application save using pickle, dill and other serialisation methods but always break due to socket connections and other serialisation issues (which seems like a bad way to solve this issue anyway so I have given up on this approach).

    Keen to know other approaches or a potential solution for this.


  • administrators

    The IB store implementation isn't designed for persistence across session.

    @purinda-gunasekara said in Bracket order persistence in live trading (IB) across sessions:

    Keen to know other approaches or a potential solution for this.

    You need to implement a custom IB Store/Broker.



  • Hi @backtrader, over the weekend I got some time to implement what you advised which was to add a custom broker/store. but bit stuck at the moment and have TWO questions.

    Q1: So far this is what I have done, and need to know if I am on the right path?

    I have ...

    1. Extended the backtrader IBBroker class with a custom to implementation which makes reqAllOpenOrders call via IBStore to load open/active orders (bracket, etc).
    2. When my application backfill is done, I make a reqAllOpenOrders to load all active orders via TWS.
    3. Once request is done, I get IBPy ib.ext.Order instance passed via notify_store method (within strategy) with active orders (BUT these are obviously ib.ext.Order objects)

    .. I cannot seem to figure out an easier way to instantiate IBOrder objects based on the ib.ext.Order data (which consists of lot of m_ attributes) I get. I mean I can try to map all the m_ attributes such as m_lmtPrice to pricelimit (in IBOrder) and so on, but I would like to know if there is a better approach or get a sanity check of what I have done before I dig this hole deeper..

    Q2: Would it be possible to get an explanation on what variables/attributes within ibstore, broker or basestrategy layers need to be repopulated when a new session is created? for backtrader to seamlessly use the retrieved orders. Since I have only focussed on extending IBBroker and making a reqAllOpenOrders to load active orders.

    Your time is much appreciated.


  • administrators

    You have to copy the attributes.

    Strategies don't need any kind of repopulation. As for the broker, you will have to add the orders without triggering spurious notifications which would be a kind of false positives given to the strategies


Log in to reply
 

});