Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    live trading : ibbroker.py error when order from another clientid occur

    General Code/Help
    2
    3
    118
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • E
      EMR last edited by

      Hi backtraders,

      I tested on an IB paper account the following :

      • running a strategy with clientid, let's say 8888.
      • Execute manually an order.
        The result was an error :
      08-Oct-20 17:47:24 ERROR     Exception in message dispatch.  Handler 'commissionReport' for 'commissionReport'
      Traceback (most recent call last):
        File "x/backtrader-J2SaRgr0/lib/python3.7/site-packages/ib/opt/dispatcher.py", line 44, in __call__
          results.append(listener(message))
        File "x/backtrader/backtrader/backtrader/stores/ibstore.py", line 1323, in commissionReport
          self.broker.push_commissionreport(msg.commissionReport)
        File x/labs/backtrader/backtrader/backtrader/brokers/ibbroker.py", line 482, in push_commissionreport
          ex = self.executions.pop(cr.m_execId)
      KeyError: '0000e215.5f7e7752.01.01
      

      Tested also the following :

      • running a strategy with clientid : 8888.
      • running another strategy with a different client id : 9999

      I encounter the same error.

      As far as I investigated, my understanding is that IB notify the order, that BT receive the notification and do not find it its own list of orders, hence the error. There is no filtering by clientid.

      Is it correct or did I missed something ? Any help would be much appreciated.

      1 Reply Last reply Reply Quote 0
      • E
        EMR last edited by

        So, in case it is of some interest for people who like me were ignorant about this and are willing to know more : BT cannot filter by ClientID when receiving commissionReport, as IB do not send a ClientID in the commissionReport message.
        Feel free to correct me if I am wrong :

        • IB do send the ClientID and the OrderId with the openorder callback but not the ExecId (Which is normal as the order may not have been executed)
        • IB send the ExecId, the ClientID and the OrderId with the execDetails messages.
        • IB do not send the ClientID or OrderId with the commissionReport messages, the only thing that can be linked to an order that is then sent is the ExecId

        So a running strategy will receive commissionReport messages for all order executed on TWS : its own orders, orders placed manually and from another clientId, but not the openorder and execDetails messages.
        There are 2 exception for this :

        • ClientID 0 will receive openorders, execDetails and commissionReports for itself and for orders placed manually
        • If a ClientID is set as "Master API client ID" (in Global configuration / API / Settings) it will receive openorder, execDetails and commissionReport for all clients.
        1 Reply Last reply Reply Quote 0
        • vladisld
          vladisld last edited by

          Thanks for sharing. Although I'm not sure how critical this scenario is - it is worth to open an issue for this: https://github.com/backtrader2/backtrader/issues

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors