IB API Order ID question

Discussion in 'Automated Trading' started by bidask, Jul 3, 2008.

  1. bidask


    If I create my own trade IDs for api trades, is there a chance that those IDs will conflict with the order IDs from manual trades? Can I just keep incrementing the order ID by 1 with every new API trade?

    Do manual trades have order IDs?

  2. Don't assign your own orderId's. Use the nextValidId callback function tell you what it is.

    Yes, manually entered orders (into TWS) have order IDs too.
  3. bidask


    is there a way to stay comfortable out of range of the orderIDs generated by manual orders?

    for example, if manual orderIDs start from 1 and i know that i won't have 100 manual orders, I'll start the API orderIDs at 500 and start incrementing up from there.

    the problem with nextvalid id is that it doesn't generate ids fast enough. it seems like if i request the id right before the order, the order will get sent before it retrieved the id from nextvalidid.

    reqIds(1) // then the id is saved into a variable
    placeOrder(...) //this order gets sent before the new id comes in

    but if i just increment the orderID by 1 right before sending the order, it works fine.

  4. I use the Java API and I have always used:


    as the start value and the incremented by one from there..

    I have been using the API before they added the nextID callback, and havent bothered to change my code to use that method.

    Seems to work fine for me..
  5. When you first connect through the API, nextValidId is called, so you remember that number and use it on the next transaction. Each time you want a new ID, just increment it.

    I never enter manual transactions when using my custom app so I don't know what happens to the ID numbers then.
  6. bidask


    IB's User's Guide says this

    id - The next available order ID received from TWS upon connection. Increment all successive orders by one based on this ID.

    It seems like IB wants you to just increment by1 too. I'm just worried about it conflicting with manual orders. Wish they can address this.
  7. vikana

    vikana Moderator

    My guess is that you don't need to worry about conflicting with manual TWS as TWS itself has clientID of 0 which is different that anything you have.
  8. rosy2


    not sure about IB's API but usually each model would request the next 100 or so orderIDs from somewhere and hold them in itself. that way you dont need to constantly request nextID()
  9. bidask


    that's right! i feel more assured now, but placeOrder() doesn't ask for a client ID.

  10. vikana

    vikana Moderator

    sounds like you need to study the documentation :)

    placeOrder is a method if the EClientSocket class, which is the class making the connection with the clientID as one of it's parameters.

    You're safe.
    #10     Jul 3, 2008