YON CISE

IBKR Web API

IBKR’s documentation and organization of their API documents leave much to be desired. The documentation exists in several official places, yet many of them are outdated. After spending some time testing, I finally succeeded in using their API to retrieve historical market data.

Essentially, IBKR offers two types of APIs: the TWS API and the Web API. The TWS API is based on raw sockets, while the Web API relies on the HTTP protocol. The Web API is easier to integrate and is more suitable for automated trading because, although both the TWS API and Web API require a gateway app to establish an authorized session, the Web API’s gateway app can operate without a GUI environment.

To initiate the Web API gateway, you must first download it from here. Once downloaded and unzipped, you can start the gateway with this command:

bin/run.sh root/conf.yaml

Note that the gateway defaults to listening on port 5000, which may conflict with the AirPlay Receiver on a Mac. You can either disable the AirPlay Receiver or modify the gateway’s listening port in the root/conf.yaml file.

After launching the gateway, you should navigate to https://localhost:5000/ and log in with your IBKR account. Once logged in, retrieving data with the Web API is straightforward. To fetch Apple’s historical market data, you can visit the following URL:

https://localhost:5000/v1/api/iserver/marketdata/history?conid=265598&exchange=SMART&period=1d&bar=1h&startTime=20230821-13:30:00&outsideRth=true