Order IDs In Interactive Brokers API

Discussion in 'Automated Trading' started by Oliver West, Oct 20, 2006.

  1. I’m having a bit of an issue with order IDs using the C++ API. nextValidId() is called when you connect giving you the order ID to use for your first order. When I send an order I call reqIds() so as to make an new order ID available for my next order. This works fine if orders aren’t sent in quick succession but problems can arise if you want to send a new order before nextValidId() has been called with the next order ID.

    The obvious solution would be to request a number of IDs and put them in a queue so you would always have IDs on hand ready for the next order. Unfortunately though no matter what value you pass to reqIds() it only ever results in one call to nextValidId() and thus only one ID being sent.

    As nextValidId() is called from the same thread you can’t simply suspend execution until nextValidId() has been called and the new ID received. The only solution I can see would be to put the API code in its own thread and when suspend the main thread when a new ID is not available until nextValidId() has been called in the API thread with the next ID to use. However that’s an absurd solution to deal with something as simple as generating a unique order ID and there must be an easier way. IB’s client2 project provides no answers sine it just gets the order ID from the value entered in the dialog box so I’m unsure how this issue should be handled.

    Do IB have a recommended way of handling this situation? If not, how did other people manage the issue?

    Thanks for the help
  2. You question has been covered in depth on the IB technical BBS at:


    Log in and choose TWS API, then search for "orderID".

    The short answer is to keep a "global" orderID variable that you get once at startup an increment yourself whenever you create a new order. It must be global in that it must be accessible by all the threads that create orders.
  3. I'm up to 17,800 and it's only October! :D

    17567 SNDK
    17568 STLD
    17563 LLTC
    17620 UTHR
    17638 HWAY
    17647 ANSS
    17650 HLEX
    17651 QCOM
    17683 AZPN
    17671 CRZO
    17666 GNCMA
    17667 HYDL
    17677 LNCE
    17676 SMTC
    17669 VPHM
    17670 WIRE
    17712 ASTSF
    17719 GLBL
    17705 HWAY
    17706 HYDL
    17707 LIFC
    17708 PLLL
    17717 SSRI
    17710 XMSR
    17711 XMSR
    17759 SIRF
    17766 PWAV
    17775 CRME
    17773 HWAY
    17780 CRME
    17790 RHAT
    17801 CNET
    17797 LLTC
    17798 RHAT
    17817 NETL
    17813 SAFM
    17819 SGMS
    17816 ATYT
    17814 JDSUD
  4. Bragging about the size of your orderID??? That's a new one!
  5. lol
  6. Thanks for the reply. I didn’t know Interactive Brokers had a forum so that’s useful to know about. I also hadn’t realised that each client had its own set of orderIDs so that order 1 in client 0 would be different to order 1 from client 1. I therefore thought that incrementing it yourself would create problems as it would more than likely result in two clients using the same order ID, but I now know this isn’t an issue.

    That forum is really good to know about. There were a few other things I wasn’t sure about such as whether to use eTradeOnly and firmQuoteOnly but they all seem to be answered in that forum.

    Thanks again for the help.