Interactive brokers offer one of the lost latest Trading desktop application and best part is that it offers API access to most of the worlds leading stock exchanges. For India, Interactive brokers provide free access to NSE data (monthly account maintenance charges of INR 200) which is nothing if you compare pricing by main competitor zerodha ( Streaming INR 2000 + Historic INR 2000).
In this article, let us have a look at how to get started with Interactive Brokers API access.
Step#1 Open an account
First and foremost, you need to have funded account with Interactive Brokers. They are opening account virtually provided you have all relevant documents scanned copy.
If you need an access to market data you need to keep $500 or its equivalent in your account. in INR, you can transfer ~40,000/- to be on safer side.
Step#2 Activate Market data.
Its takes about a week for your documents to be verified and account activation. Once your account is activated, you need to suscribe to market data using following path.
Login to web portal –> User Settings –> Market Data Suscription (Click on gear sign>–> select required exchanges and market data type–> confirm.
And you are all set.
Please note for NSE, Equity and Futures and Options market data is separate, so you need to sele
Step#3 Download TWS
You can download TWS (Traders Workstation) from here. This is one of the most advanced if you compare it with other competitors.
You might ask, I need API access, why do I need TWS ? Well, Interactive Brokers API need TWS to be installed on local machine. In a way, API piggy backs on the TWS. More on this later
Step#4 Download API Libraries
You can download API libraries from here. You can choose stable version.
Step#5 Basic Setup
Version that I downloaded is “twsapi_macunix.979.01.zip”. This might be different for you based on latest version released by IBKR
$cd ~/Downloads $sudo unzip twsapi_macunix.979.01.zip -d $HOME/ $cd ~/IBJts ~/IBJts$ ls -la total 68 drwxr-xr-x 5 userid root 4096 May 29 09:39 . drwxr-xr-x 36 userid userid 4096 May 29 11:16 .. -rw-r--r-- 1 userid root 21 Feb 6 01:04 API_VersionNum.txt drwxr-xr-x 2 userid userid 4096 May 28 22:38 log drwxr-xr-x 5 userid root 4096 Feb 6 01:07 samples drwxr-xr-x 5 userid root 4096 Feb 6 01:07 source
You can find code inside source and sample folder.
Before you start using this package, you need to install it. Go to folder twsapi_macunix.981.01–>IBJts–>source–>pythonclient
$cd Downloads/twsapi_macunix.981.01/IBJts/source/pythonclient $ sudo python3 setup.py install
Step#6 Configure TWS
As mentioned in step#3, you need TWS. You need to enable API access in TWS by making following changes.
Go to Edit -> Global Configuration -> API -> Settings and make sure the “Enable ActiveX and Socket Clients” option is activated as shown below:
Step#7 Sample Code
Go to source–>pythonclient–>tests
Here you will find many sample code for various tasks
Whenever you run API, you need to make sure TWS is running. else you will get error like below
~/IBJts$ /usr/bin/python3 "/home/conquistadorjd/IBJts/source/pythonclient/working/check_connection v 4.0.py" ERROR -1 502 Couldn't connect to TWS. Confirm that "Enable ActiveX and Socket EClients" is enabled and connection port is the same as "Socket Port" on the TWS "Edit->Global Configuration...->API->Settings" menu. Live Trading ports: TWS: 7496; IB Gateway: 4001. Simulated Trading ports for new installations of version 954.1 or newer: TWS: 7497; IB Gateway: 4002
Step#8 Sample code for you first program
from ibapi.client import EClient from ibapi.wrapper import EWrapper class IBapi(EWrapper, EClient): def __init__(self): EClient.__init__(self, self) app = IBapi() app.connect('127.0.0.1', 7496, 1) app.run() ''' #Uncomment this section if unable to connect #and to prevent errors on a reconnect import time time.sleep(2) app.disconnect() '''
You might get following error
traceback (most recent call last): File "/home/userid/Downloads/twsapi_macunix.981.01/IBJts/source/pythonclient/tests/check_connection.py", line 1, in <module> from ibapi.client import EClient ModuleNotFoundError: No module named 'ibapi'
If you are seeing this error, you might have forgotten to manually install downloaded ibapi. Go to step#5 and refer to last instruction of manually installing ibapi