IB API - Using twice the same Id

Discussion in 'Automated Trading' started by Krass, Apr 19, 2011.

  1. Krass

    Krass

    Hi everybody. I'm new to the forums. Google landed me on elitetrader.com quite a couple of time.

    I'm quite new to the IB API and as I could notice, there are many users around here.

    I was able to use the API (within my vb app!) to retrieve live data feed for my watchlist (stocks + options). I've ended up understanding that each of those "mkt data requests" must be done under their own Id (and it's my job to remember which id corresponds to which conid in, example, my watchlist).

    I've then started today to test placing orders. It works good but there's one thing I think I do not understand with the IB API.

    Ex.:
    a) I've submitted an order and it was refused (done intentionally)
    b) I'm trying to resubmit the same order and it says "Duplicate order with id=x"

    My question is: when an order is placed/executed/refused/whatever, is the Id "banned" for the rest of this api lifecycle? Why can't I reuse it right now? "Cancelling the order" won't help as it was already refused anyway. Must I jump to the next Id?

    My INITIAL plan was to use Ids 2 to xxx for my watchlist-to-update, and always use id=1 for placing orders, or querying contract data or whatever is done once, as a snapshot, if you will.

    Hope I was clear enough for someone to hopefully share his 2 cents.. any info greatly appreciated.
     
  2. rosy2

    rosy2

    yes

    your plan was to use the same orderid for every order? how can you keep track of your orders that way? just do this id++
     
  3. It's been a few months since I've played with my code, so hopefully the cobwebs aren't too thick. The IB API requires orderIDs for orders as well as requests (market data, portfolio info, etc) but they are independent things. Once you use an ID# on an order, you can't re-use it on another order (except to modify that existing order), but you can use it on a request (and vice-versa)

    I use two functions: one for each type of request: orderID() and reqID() and auto-increment whenever it is called. I also keep two separate lists which allow me to look up orders and market data by both ticker symbol (convenient for me) and orderID (which IB returns in its events).

    IB raises the nextValidID() event when you connect, so you can start there if you want. I think it may persist from session to session (possibly to reset at end of day), but you'll have to do some digging to see if that's true.
     
  4. Krass

    Krass

    Thanks for the replies!

    I figured out this Id thing so I'm now incrementing on every "mkr data reqs" and orders. I don't think I'll be reusing an id, ever. Even tho that makes more id to handle, I'd rather eliminate the risk of error or confusion.

    Right now, when I load up my application, ids 1 to 17 are automatically used and filled. So I have a total of 17 options or stocks in my watchlist that gets updated perfectly.

    The weird thing is that when I then try to submit an order, it says:
    id: 18, Error - Duplicate order id, .. I try again
    id: 19, Error - Duplicate order id, .. I try again
    id: 20, Error - Duplicate order id, .. I try again
    id #21 ends up being unused and the order goes through.

    This is strange because I really am using id 1 to 17 only. Perhaps have I gone over 17 on this TWS's lifecycle, now showing this little problem. (I just tried closing my app+tws, then reopen all - same behaviour)

    Anyhow, I thought that maybe this could easily be solved by using that "Req Next Id" button I saw in the IB API DEMO. Unfortunately, clicking it doesn't return anything. Was that option supposed to return me the next id available? (in my case, id #21). That would be useful.

    Any tips greatly appreciated!
     
  5. dtcreate

    dtcreate

    You need to use the API to request the next orderID at startup which will give you the starting number to use and then you can increment from there.

    I use this in the API all the time, so I know it works. I am not sure about their demo application.
     
  6. TraDaToR

    TraDaToR

    Otherwise, there is a "reset API order id sequence" in API configuration. You can click on it before every start.