IB API - orderID? new orderID for every order?

Discussion in 'Order Execution' started by bluematrix, May 21, 2013.

  1. Hi

    I was playing around with IB api - it seems every time you have a new order you must have a new orderID? if you use the same orderID you get "duplicate orderid"!?

    even after restarting you get the same error unless you change the orderid.

    does the api allow you to easily get the last orderID in the system? so you can easily increment that, or do you have to handle it yourself?

  2. vicirek


    On succesful connection NextValidId event/callback returns next valid order id you can use. Each subsequent order has to be incremented by one (or different from other orders according to your own program logic).

    You can reset order id's in TWS but I cannot give you more information because I never used it.
  3. ok great - so on a new connection I can get the next valid orderID - but from that point on I have to increment it myself, unless I restart. correct?


  4. I got it, works, thanks for the help! :)
  5. Bob111


    how else you identify the orders? it has to be unique number every time. kinda like a primary key in the table
  6. oh sure, it was that every time you restart it, it doesn't reset - you need to pick up the last order you had used.
  7. Bob111


    that's right.just a random example- imagine holding large position on same stock with multiple entries and maybe open target\stops orders. then your internet dies or Pc crashed or whatever. you have to restart TWS and now you stuck,if you don't have your ID's and rest of the info about orders stored or they reset because of this restart. what you gonna do?
    it's the only way-assign new ID in whatever increments you like every time upon successful connection. TWS keeps track of them on your side and fire next available every time
  8. yup that's it. thanks for the help. :D
  9. TraDaToR


    I prefer to reset the order id at the beginning of every session( there's a button in the API settings configuration ). It also helps protect me against sending unwanted orders while connecting( I use excel and if an order sending DDE link remains in my spreadsheet, it is not sent since ids haven't be reset yet ).
  10. So you can definitely use the callback function in the IB api for the next valid order Id. However, I have mine implemented a little different.

    When my system is turned on, it connects to a database which has all my trades and executions stored in there and pulls out the most recent orderId, then increments from there. Each time the strategy fires both the signal and execution are dumped into a queue which is processes by a worker thread pushing them into my database.

    I think this makes things a little more detached from IB, so if you decide to change brokers there will be less headache.
    #10     Jun 6, 2013