Interactive Brokers (IB) allows to connect with their trading software Trader Workstation (TWS) through their TWS API.
You first need to start TWS on your computer and within the settings menue you have to enable TWS API access for your "localhost" network interface (127.0.0.1) on port 7496. For paper trading (demo/test account) this is port 7497 per default. This allows to run scripts on the same machine you run TWS on.
Instead of IB TWS, you can also use IB Gateway. This uses port 4002 per default for paper trading (demo/test account) and 4001 for an active/real/live account.
Check this: https://interactivebrokers.github.io/tws-api/initial_setup.html.
Once you start scripts connecting to your TWS, you can see also a new tab "API" on your TWS.
Official TWS API software from Interactive Brokers can be found on https://www.interactivebrokers.com/en/trading/ib-api.php.
Discussions around the TWS API are best on https://groups.io/g/twsapi.
Please also check the FAQ at: https://dimon.ca/dmitrys-tws-api-faq/.
ib_async is another python API to connect to your TWS with docu at https://ib-api-reloaded.github.io/ib_async/api.html and discussions at https://groups.io/g/insync. (Original project at https://github.com/erdewit/ib_insync.)
To install ib_async, first install python3 and then run:
pip3 install ib_async
To update ib_async later on, run:
pip3 install --upgrade ib_async
pandas is a useful additional python library for data analysis and manipulation.
Install on Debian or Ubuntu Linux with:
sudo apt-get install python3-pandas
Or you can install via pip3:
pip3 install pandas
For updates run:
pip3 install --upgrade pandas
Example script which downloads historic stock data for all companies of the DOW, SP500 and Nasdaq100 indices.
The data is stored into the subdirectory "data" per default, so please create this directory before calling this script.
See stock-hist-data-download.py.
How to update the index list of the SP500 and Nasdaq100:
python3 stock-hist-data-download.py --list-index > TMPFILE diff -u stock-hist-data-download.py TMPFILE
Also check out https://github.com/deltaray-io/ib_dl.
A bit different to the TWS API are flex queries and downloading/parsing of them.
Here some projects around this: