question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`IB.client.getReqId()` returns already used ID

See original GitHub issue

I am using IB.client.getReqId() to place my version of bracket orders and set parentId in target and stop loss orders.

But I have stumbled upon an error:

Traceback (most recent call last):
  (...)
  File "_", line 53, in bracket_order
    parent_trade: Trade = ib_client.placeOrder(contract, parent)
  File "/venv/lib/python3.6/site-packages/ib_insync/ib.py", line 528, in placeOrder
    assert trade.orderStatus.status not in OrderStatus.DoneStates
AssertionError

Which means that trade with such ID was found.

Parent order is created like that:

parent = MarketOrder(
    action, quantity,
    orderId=ib_client.client.getReqId(),
    transmit=False,
    conditionsIgnoreRth=False,
    outsideRth=False
)

Entire bracket_order function is copy of ib_insync.ib.IB.bracketOrder source with parent placed as MarketOrder instead of LimitOrder. So all but stopLoss are transmit=False. Orders are placed in order:

  • parent,
  • target,
  • stop.

Is there a problem with IB that it start IB.client on wrong ID or does not update it properly?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:23 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
nemoznycommented, Jun 14, 2021

I can confirm the issue seems to be fixed with 0.9.66. I started tracking for orderId collisions, but since the update I haven’t seen any. In my previous comment I was NOT on 0.9.66.

2reactions
erdewitcommented, Apr 16, 2020

In the upcoming version of ib_insync the maximum orderId of open/completed orders is used to set the reqId sequence (see this code).

It seems the “nextValidId” from IB can’t be relied upon when TWS/gateway gets restarted.

Read more comments on GitHub >

github_iconTop Results From Across the Web

insync@groups.io | Wrong orderId
I have run into this issue with order id's being reused in ib_sync when orders get canceled for some reason - not sure...
Read more >
Source code for ib_insync.ib
isConnected() else 'not connected') return f'<{self.__class__. ... clientId: ID number to use for this client; must be unique per connection.
Read more >
Dmitry's TWS API FAQ - Trader's Notes
[Q] Client can not re-connect to TWS using the same client ID after ... Here's an example when IB API returned $2.45, while...
Read more >
ibapi - Go Packages
Interactive Brokers API 9.79 pure golang, Unofficial version, smilar to the ... func (ic *IbClient) GetReqID() int64; func (ic *IbClient) HandShake() error ...
Read more >
IB_insync - Sanic error after one successful order preventing ...
when I try ib.qualifyContracts() I'm seeing the same error. I swear I used to use it no problem... – needfulmeerkat41954. Apr ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found