Basic API Example Not Working

Discussion in 'Interactive Brokers' started by burgermeister, Dec 18, 2020.

  1. I'm trying to just start working with the TWS API with Python. I've copied the instructional video exactly, but it doesn't seem to be working. The code below should just get market data.

    Code:
    from ibapi.client import EClient
    from ibapi.wrapper import EWrapper
    from ibapi.contract import Contract
    from ibapi.ticktype import TickTypeEnum
    
    class TestApp(EWrapper, EClient):
        def __init__(self):
            EClient.__init__(self, self)
    
        def error(self, reqId, errorCode, errorString):
            print("Error: ", reqId, " ", errorCode, " ", errorString)
    
        def tickPrice(self, reqId, tickType, price, attrib):
            print("Tick Price. Ticker Id ", reqId, " tickType: ", TickTypeEnum.to_str(tickType), end=' ')
    
        def tickSize(self, reqId, tickType, size):
            print("test")
            print("Tick Size. Ticker Id ", reqId, " tickType: ", TickTypeEnum.to_str(tickType), "Size: ", size)
    
    
    def main():
        app = TestApp()
    
        app.connect("127.0.0.1", 7496, 0)
    
        contract = Contract()
        contract.symbol = "AAPL"
        contract.secType = "STK"
        contract.exchange = "SMART"
        contract.currency = "USD"
        contract.primaryExchange = "NASDAQ"
    
        app.reqMarketDataType(4)
        app.reqMktData(1, contract, "", False, False, [])
        app.run()
    
    
    if __name__ == "__main__":
        main()
    
    When I run that, nothing prints. I've also added a print statement to the tickPrice and tickSize function in the wrapper.py file, so it seems the tickPrice and tickSize functions are never called, what are reasons this might be?
     
    Last edited: Dec 18, 2020
  2. DaveV

    DaveV

  3. You don't mention it, but you must have the appropriate market data subscription.
     
  4. DaveV

    DaveV

    The OP had set reqMarketDataType( 4) which will get you delayed quotes even if you don't have a subscription.
     
  5. BAT31

    BAT31

    I am not sure how the API works. However, as I look at the Python code, I don't see an explicit function call to tickPrice and tickSize. I see that an object is created from the class (i.e. app = TestApp()) that holds the functions. But I don't see you accessing the methods of app (i.e. app.tickSize() and app.tickPrice).

    Again, I don't know the API, so my reasoning might be incorrect.
     
  6. Are you sure about this? My experience has been that without a data subscription I couldn't even get historical data (historical bars). Anyway, that may have changed meanwhile.

    Edit: there are 3 requirements for getting data through the API for both live and historical data:
    http://interactivebrokers.github.io/tws-api/market_data.html
    (1) have trading permission for the instrument, (2) have a funded account, (3) have market data subscription for the instrument
     
    Kust likes this.
  7. DaveV

    DaveV

    Hmmm. It works for me to get delayed quotes for foreign stocks where I have no subscriptions. However, I am not sure about US stocks since I have subscripton for all US exchanges.
     
  8. The API communicates with TWS (or Gateway) on your computer. TWS then communicates with IB's servers. The API sends a request to TWS, in this case about price data. Once TWS receives data from the servers it calls the API through tickPrice() and tickSize(). That is why the API does not call these methods, but you do need to code what these methods should do when they are being called by TWS. In the given example is the received value printed to screen.
     
  9. BAT31

    BAT31

    Understood...