IB Midprice and the need Limit price

Discussion in 'Order Execution' started by Collagen, Dec 21, 2021.

  1. Collagen

    Collagen

    I have been trying to get a midprice order type written. The IB docs (https://interactivebrokers.github.io/tws-api/ibalgos.html) suggest that the limit price is Optional:

    Order order = new Order();
    order.Action = action;
    order.OrderType = "MIDPRICE";
    order.TotalQuantity = quantity;
    ///order.LmtPrice = priceCap; OPTIONAL according to IB doc.
    client.placeOrder(nextOrderId++, ContractSamples.USStockAtSmart(), OrderSamples.Midprice("BUY", 1, 150));

    However when I submit the order via the TWS connection, the order appears in the pending orders panel but the LMT Price field in the order entry panel shows <ERROR>. Can someone who has used a midprice order type confirm what should be entered in the limit price field to emulate the setting "NONE" ?
     
  2. ValeryN

    ValeryN

    I can confirm - MIDPRICE should not require you to set LmtPrice.

    Sounds like a problem is somewhere else. Perhaps try ditching their sample code and just create your own order. I just looked at OrderSamples.Midprice code, your 3rd argument is priceCap, maybe your value it's way off for that particular stock, IB has limits on how that price can be from last traded price.
     
  3. Collagen

    Collagen

    Thank You for your response.

    I don't enter the price limit at all since according to IB documentation is optional. I would think the omission equal to "none". Strange thing is that when I submit my orders, which are all sent at the same time in batch of dozen, some go through without issue while some others get the error reported.

    The idea is to use the midprice order type as a synthetic market order so I wouldn't want to enter any limit price. I believe in the limit field something should be entered to emulate "none", I just don't know what I should put. Talked with IB chat which they opened a case last week related to requirements but I didn't hear them back.
     
  4. ValeryN

    ValeryN

    This is very strange. I'm not sure what language bindings you're using but in Java you can't set this to "none". It's marked as a double type. I'm using this order type in live trading without any issues and I only set transmit/action/orderType/totalQuantity.
     
  5. Collagen

    Collagen

    I don't set the limit price field as well. (I believe by default the value used is lmt=1.797693135e+308, same like aux price) Interestingly I get in return the following error:

    bad message length: <API-error: you must enter a valid price cap. code=507: "This can occur if there is an attempt to connect to TWS with a client ID that is already in use, or if TWS is locked, closes, or breaks the connection. It should be handled by the client application and used to indicate that the socket connection is not valid."

    But I don't know what to make of it since all the other stuff I am running is working properly. I am just having problem with MIDPRICE, without limit price field entered. Another odd thing is that in demo works..
     
  6. d08

    d08

    MIDPRICE doesn't require any limit. Print out the order object and paste it here, otherwise we are just guessing.
     
  7. Collagen

    Collagen



    def midpoint_buy(ticker_to_buy, num_shares):
    ib = IB()
    ib.connect('127.0.0.1', ib_acct_port, clientId = random.randint(0, 9999))

    # Create a contract and an order object:
    contract = Stock(symbol=ticker_to_buy,
    exchange = 'SMART',
    currency = 'USD',
    tif = 'DAY',
    hidden = 'TRUE',
    rth = 'TRUE')
    ib.qualifyContracts(contract)

    order = Order(action='BUY',
    totalQuantity=num_shares,
    orderType="MIDPRICE")

    trade = ib.placeOrder(contract, order)
    trade.filledEvent+= order_status

    # disconnect the session
    ib.disconnect()
     
  8. Collagen

    Collagen

    So I am still testing... If I continue to submit orders (one every second) with the limit field filled with a price then I never get any error. However If I don't submit the limit field, most of the time, the px cap error appear.


    I am using TWS 981.3
     
    • 1.png
      1.png
      File size:
      59.3 KB
      Views:
      25
  9. d08

    d08

    This isn't the native API is it?
    Why are order parameters (tif, hidden, rth) with the contract object? Makes no sense.
    I send MIDPRICE orders with action, tif, orderType and totalQuantity fields regularly without issues.
     
  10. Collagen

    Collagen

    mmmhh. But MIDPRICE is not an order type? I treat midprice as order type. For instance the adaptive algo is used in a different field. Maybe is that?
     
    #10     Dec 22, 2021