How to Get Setup
How to Fire Off Individual Trades
How to Receive Data
Download Python here: https://www.python.org/downloads/
Download PyCharm IDE here: https://www.jetbrains.com/pycharm/download/#section=windows
Download IBKR TWS here: https://www1.interactivebrokers.com/en/index.php?f=16040
To get setup with the IBKR API, follow the steps listed below.
- Visit the url: https://interactivebrokers.github.io/
- Click on "I Agree". A new frame slides into view.
- The first row lists stable API, for Windows on the Left, and for Linux/Mac on the right.
- Select your OS and download the API.
- For Linux/Mac open a command terminal and navigate to the download location where you've downloaded the API.
- Type in the command
sudo unzip twsapi_macunix.n.m.zip -d $HOME/. The n.m.zip part will actually have your version number. - After unzip'ing the files, navigatge to the API location for python. Use the command:
cd ~/IBJts/source/pythonclient. - Then type in the following commands.
python setup.py build
python setup.py install
Note: for Ubuntu and derivates you'll need to use
python3instead ofpython. For Ubuntupythonrefers to python 2.x version, andpython3refers to python 3.x version.
- Launch your TWS client and enable API connectivity by following the instructions at: https://interactivebrokers.github.io/tws-api/initial_setup.html
- Open a new file using your favorite IDE and type in the following code:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import *
from threading import Timer
class IBapi(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
app = IBapi()
app.connect('127.0.0.1', 7497, 123) # TWS PaperTrading
app.connect('127.0.0.1', 4002, 123) # IB Gateway PaperTrading
app.run()
time.sleep(2)
app.disconnect