简体   繁体   中英

Basic Interactive Brokers API with Python Example Not Working

I'm trying to use the Interactive Brokers API with Python. I've tried to implement one of their basic examples by doing everything exactly how they have in their instructional video but it doesn't seem to work. Below is the code that should return some market data but when I run it nothing prints.

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()

I'm not sure what could be happening, but I'm guessing it has something to do with the TestApp functions not getting properly overwritten, or something along those lines. Thanks for any help or info you could provide.

  1. reMarketDataType()

I agree with what Brian said, reqMarketDataType(4) will only work outside ordinary trading hours. It states on IB API's documentation as below:

"2 - Frozen Data: Frozen market data is the last data recorded at market close. In TWS, Frozen data is displayed in gray numbers. When you set the market data type to Frozen, you are asking TWS to send the last available quote when there is not one currently available. "

"3- Delayed Data: Free, delayed data is 15 - 20 minutes delayed. "

"4 - Delayed Frozen Data: Requests delayed "frozen" data for a user without market data subscriptions."

[Market Data Types IB API Python documentation]

As I understand "Delayed Frozen Data" is both delayed 15-20mins and is a market close price, so it can only be requested after the market closed.

  1. Contract.primaryExchange

When initializing a contract class, if exchange property is defined as "SMART", then NASDAQ is always defined as "ISLAND" in the primaryExchange field:

contract.primaryExchange = "ISLAND"
  1. New thread to handle received data

Agree with MatthewScarpino, to avoid interruption between sending requests and receiving data: always start a new thread in the background to run EWrapper to handle the returned data, then call this function in the main() after the API connection is started.

def web_socket_thread(app: TestApp):
    websocket_thread = threading.Thread(target=app.run, daemon=True)
    websocket_thread.start()

def main():
    ...
    web_socket_thread(app)
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM