IB API HOWTOs and Guidelines - For Beginners

Discussion in 'App Development' started by Butterfly, Nov 8, 2013.

  1. 2rosy, you are correct. I do not know why there isn't just a reqOrderStatus() function. But I thought the client ID needed to unique. Now that I look at it, it looks like clientID should be 0 for the API orders with a unique orderId (incremented) and for TWS orders the orderId is 0 and clientId is any number other than zero. From IB:

    reqAutoOpenOrders()

    Call this method to request that newly created TWS orders be implicitly associated with the client. When a new TWS order is created, the order will be associated with the client and automatically fed back through the openOrder() and orderStatus() methods on the EWrapper.
    Note: TWS orders can only be bound to clients with a clientId of 0.
    void reqAutoOpenOrders(boolean bAutoBind)
     
    #71     May 6, 2014
  2. junkone

    junkone

    I am trying to download historical data via rs43/ib-csharp
    For example. Until 8 am EST today, I could get hist data using daily bars only till 28th. after 8 am, i can get till 29th.
    Would the historical data skip a day?
    Not sure where i was doing it wrong.
     
    #72     May 30, 2014
  3. Below, I read something about timer based vs event driven based algo trading. In my opinion, the event driven algo is the way to go. Simply put, if nothing changes that may influence the algo, then why bother to re-evaluate what action to take? Of course, the algo should watch price changes but changes in bid/ask levels are just as important because those really determine what the traded security is worth.

    From my experience, the (Java) IB API is meant to be used in an event driven way. Waiting loops with delays is bad practice in my view. If you really want to do something every certain time interval, you could add some sort of 'internal event' to the general event stream to wake up the algo.
     
    #73     May 31, 2014
  4. Great thread and content. Had fun reading it. Joined ET new. I use another broker api now, but reminds me of the days of using the IB api. I will chime in where I can of help. Hello all
     
    #74     Jun 5, 2014
  5. Butterfly

    Butterfly

    glad you liked it, the code is really for demonstration, but as you can see it's quite simple

    there is the issue of "OOP" vs "Process" programming. Many developers are only taught OOP these days, so they are lost when they have to deal with flow of information and process flow type of programming. OOP makes sense for "events" and managing objects visually. With data feeds and trader order flows, OOP is actually counter-productive and against the idea of Object Oriented Programming. Only HFT programming should be done in OOP, because of the events nature of the trade.

    I keep being amazed by the level of incompetence from developers these days, they seem to be quite clueless and very "narrow" minded. They can do one trick and that's it.
     
    #75     Sep 4, 2014
  6. Butterfly

    Butterfly

    Hi there,

    sorry for the slow response, for some reasons ET was no longer sending me alerts when this thread was updated or when I received PM. Must be the new forum software.

    To answer your question, you need to re-submit the same order object with the modified price or shares with the exact same order_id you created for submission to IB Queue Server. This means that you need to keep track of your orderID attached to the trade order. In case you don't track those order_id locally in your code, you can always ask IB Server for the "OpenOrders" and it will send the securities with price and shares attached to each order_id. I hope it sounds clear, it not let me know.

    See below for an example to change the current price for an existing limit order:

    Code:
    # Create a contract object
    # Update the parameters to be sent to the Market Order request
    order_ticker = Contract()
    order_ticker.m_symbol = 'GTAT'
    order_ticker.m_secType = 'STK'
    order_ticker.m_exchange = 'SMART'
    order_ticker.m_primaryExch = 'SMART'
    order_ticker.m_currency = 'USD'
    order_ticker.m_localSymbol = 'GTAT'
    
    # Create a new order
    # Update the parameters to be sent to the Market Order request
    newOrderID=8
    order_desc = Order()
    order_desc.m_minQty = 100
    order_desc.m_lmtPrice = 11.00
    order_desc.m_orderType = 'LMT'
    order_desc.m_totalQuantity = 100
    order_desc.m_action = 'SELL'
    
    # Send the Market Order request
    ibgw_conTradeChannel.placeOrder(newOrderID, order_ticker, order_desc)
    
    # Now, change the price of the trade order and re-submit
    order_desc.m_lmtPrice = 11.50
    ibgw_conTradeChannel.placeOrder(newOrderID, order_ticker, order_desc)
    
    # Here, we change the shares of the trade order and re-submit
    order_desc.m_totalQuantity = 150
    ibgw_conTradeChannel.placeOrder(newOrderID, order_ticker, order_desc)
    
    
     
    #76     Sep 4, 2014
  7. Butterfly

    Butterfly

    not bad in principle and theory, but won't work on a real server, I can see an issue with the nextOrderID and your implementation of the code for IB connection to the MQ server.

    Nice try though, but needs a lot of improvement and testing first.
     
    #77     Sep 4, 2014
  8. Butterfly

    Butterfly

    to answer your second question, you need to catch the OpenOrder complex objects and assign the "object" for OrderStatus within the complex object to a new "variable" so you can extract the AvgFill or Filled attribute.

    I actually did that somewhere on my IB server but didn't publish it here yet. Will find it and try to post it here.
     
    #78     Sep 4, 2014
  9. Butterfly

    Butterfly

    here this is how it is done...

    Code:
    # Define a function to catch "OrderStatus" and "OrderState" type messages from IB Server
    def status_orders(msg):
      global ft
      #dump(msg)
      print "Status: ", msg.typeName, msg
      #ft.write("%s [%s] %s\n" % (datetime.datetime.now().strftime("%Y-%m-%d|%T"), msg.typeName, msg))
      ft.write("%s:%s:%s:%s:%s:%s:%s\n" % (msg.typeName, msg.orderId, msg.status, msg.whyHeld, msg.avgFillPrice, msg.filled, msg.remaining))
    
    # Map server replies to "status_orders" function client requests
    ibgw_conChannel.register(status_orders, 'OrderStatus','OrderState')
    
    #print "Requesting All Open Orders..."
    ibgw_conChannel.reqAllOpenOrders()
    
     
    #79     Sep 5, 2014
    GunsnMoney likes this.
  10. JTrades

    JTrades

    A quick question:

    I have a working Java example using reqMktData that retrieves a quote for a single stock.

    I am not yet familiar with the API. What is the best practice for requesting quotes for many contracts, say 100 or 500?

    J
     
    #80     Sep 5, 2014