Interactive Brokers JAVA API Delay for placeOrder

Discussion in 'Automated Trading' started by jjs235, Jan 26, 2012.

  1. jjs235


    I am experiencing seemingly high latency when using the
    eClientSocket.placeOrder() function in the JAVA API of Interactive Brokers. It
    seems to take 30 milliseconds for this socket connection to take place outside
    of my JAVA program. This seems quite high, as I would think it should be around
    1 millisecond. Here is my isolated code to test this latency (all variables have
    been declared and code executes):

    //////start code///////////////////////////////////////////////////////////
    contract = createContract("SPY", "STK", "SMART", "USD");
    order = createOrder("Buy", 1, "LMT", 1.00);
    requestId = (int) (System.currentTimeMillis()/1000);

    startTime = System.nanoTime();

    eClientSocket.placeOrder(requestId, contract, order);
    eClientSocket.placeOrder(requestId+1, contract, order);

    estimatedTime = System.nanoTime()-startTime;

    //Print nanosecond time to complete place order code//
    System.out.println("Time to complete in code: " + estimatedTime);
    ////end code///////////////////////////////////////////////////////////////

    The code after running posts a software latency of 3,172,000 ns of latency or
    around 3.2 milliseconds. This would mean 1.6 milliseconds per placeOrder. Yet
    the submit times Interactive Brokers gives me are 30 milliseconds apart. Which
    means that each function takes 30 milliseconds to develop before actually being
    sent to Interactive brokers. I know this delay exists for both orders, and will
    break it down as such.

    At 0 milliseconds my posted code starts

    At 1.6 milliseconds the first placeOrder is being developed by my computer.

    At 3.2 milliseconds the second placeOrder is being developed

    At 31.6 milliseconds the first placeOrder is sent

    At 33.2 milliseconds the second placeOrder is sent

    At 39.6 milliseconds IB receives my first order, 8 ms to IB (from ping)

    At 41.2 milliseconds IB receives my second order, 8 ms to IB

    Could someone please help me remove this 30 milliseconds latency in the
    eClientSocket.placeOrder() function?

    I believe I've isolated the latency, and I believe it's not due to any risk
    checking on IB's end nor internet latency nor my java code. It seems to be
    entirely within the eClientSocket java class.

  2. jjs235


    I believe this thread would be helpful to anyone trying to operate a low latency JAVA program tied to Interactive Brokers. At least as low a latency as possible with JAVA and not utilizing FIX protocol.
  3. slumdog


    The source code for this class is provided.
  4. rosy2


    you have one socket connection when you connect. each placeorder() does not create a new connection. :D
  5. jjs235


    Thanks for the response Rosy2. So where does this 30 millisecond delay come from? Can you tell from either the code I posted or the eClientSocket class?
  6. Sounds about right I see the same in my app.

    Thats probably not bad when you think of the comms to/from and then the msg que response times on their end. Time will vary depending on time of day I'm sure the first 5mins of the day things are slower.
  7. rosy2


    I could take a look but getting more professional help would be a better approach.
  8. jjs235


    Could someone at Interactive Brokers please help?
  9. Might be worth sending a PM to 'dcraig' and asking if he'll have a look at the thread...very knowledgeable.
  10. Have you tried sending a message to ""
    Typically they will get back to you in a few days.
    You need to be very specific in your question, though as they tend to be quite literal.
    Also, do you get the same response when you connect to the demo account? Are you connecting via TWS or the Gateway?
    #10     Jan 30, 2012