• Stars
    star
    321
  • Rank 129,967 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created over 4 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A python packaged used to interact with the Interactive Brokers REST API.

UPDATE

This repo will soon be updated to include the new https://github.com/areed1192/interactive-brokers-api library. The master branch will be overwritten and the old code will need to be updated to reflect the new changes. If you would like to start rewriting your old code, please refer to the new branch merge-new-repo or go the link above.

Unofficial Interactive Brokers API

Table of Contents

Overview

The unofficial Python API client library for Interactive Broker Client Portal Web API allows individuals with Interactive Broker accounts to manage trades, pull historical and real-time data, manage their accounts, create and modify orders all using the Python programming language.

Interactive Broker offers multiple APIs for their clients. If you would like to learn more about their API offerings click on the links below:

What's in the API

  • Authentication
  • Account Endpoints
  • Market Data Endpoints
  • Trade Endpoints
  • Portfolio Endpoints
  • Scanner Endpoints
  • Portfolio Analysis Endpoints
  • Web Streaming

Setup Requirements

The following requirements must be met to use this API:

  • A Interactive Broker account, you'll need your account password and account number to use the API.
  • Java 8 update 192 or higher installed (gateway is compatible with higher Java versions including OpenJDK 11).
  • Download the Beta Client Portal Gateway

Setup Client Portal

Once you've downloaded the latest client portal or if you chose to use the one provided by the repo. You need to unzip the folder and place it in the repo where this code is stored.

Setup API Key and Credentials

The API does not require any API keys to use it, all of the authentication is handled by the Client Portal Gateway. Everytime a user starts a new session with the API they will need to proivde their login credentials for the account they wish to use. The Interactive Broker Web API does offer the ability to use the API using a paper account.

Important: Your account number and account password should be kept secret.

Setup Installation

pip install interactive-broker-python-web-api

Setup Writing Account Information

The Client needs specific account information to create a and validate a new session. Where you choose to store this information is up to you, but I'll layout some options here.

Write a Config File:

It's common in Python to have a config file that contains information you need to use during the setup of a script. Additionally, you can make this file in a standard way so that way it's easy to read everytime. In Python, there is a module called configparser which can be used to create config files that mimic that of Windows INI files.

To create a config file using hte configparser module, run the script below in a separate file or go to the Resources Folder and run the write_config.py file.

import pathlib
from configparser import ConfigParser

# Initialize a new instance of the `ConfigParser` object.
config = ConfigParser()

# Define a new section called `main`.
config.add_section('main')

# Set the values for the `main` section.
config.set('main', 'REGULAR_ACCOUNT', 'YOUR_ACCOUNT_NUMBER')
config.set('main', 'REGULAR_USERNAME', 'YOUR_ACCOUNT_USERNAME')

config.set('main', 'PAPER_ACCOUNT', 'YOUR_ACCOUNT_NUMBER')
config.set('main', 'PAPER_USERNAME', 'YOUR_ACCOUNT_USERNAME')

# Make the `config` folder for the user.
new_directory = pathlib.Path("config/").mkdir(parents=True, exist_ok=True)

# Write the contents of the `ConfigParser` object to the `config.ini` file.
with open('config/config.ini', 'w+') as f:
    config.write(f)

Store the Variables in the Script:

If you plan to not share the script with anyone else, then you can store the account info inside the script itself. However, please make sure that you do not make the file public to individuals you don't know.

Usage

This example demonstrates how to login to the API and demonstrates sending a request using the market_data_history endpoint, using your API key.

from ibw.client import IBClient

REGULAR_ACCOUNT = 'MY_ACCOUNT_NUMBER'
REGULAR_USERNAME = 'MY_ACCOUNT_USERNAME'

# Create a new session of the IB Web API.
ib_client = IBClient(
    username=REGULAR_USERNAME,
    account=REGULAR_ACCOUNT,
    is_server_running=True
)

# create a new session.
ib_client.create_session()

# grab the account data.
account_data = ib_client.portfolio_accounts()

# print the data.
print(account_data)

# Grab historical prices.
aapl_prices = ib_client.market_data_history(conid=['265598'], period='1d', bar='5min')

# print the prices.
print(aapl_prices)

# close the current session.
ib_client.close_session()

Features

Request Validation

For certain requests, in a limited fashion, it will help validate your request when possible. For example, when grabbing real-time quotes using the market_data endpoint, it will validate the fields you request to ensure they're valid fields for that endpoint.

Endpoint Verification

To use certain endpoints, you must call other endpoints before you use it. To help limit the amount of confusion for users, the library will call those endpoints for you behind the scenes so that way you don't need to worry about it.

Client Portal Download

If the user doesn't have the clientportal gateway downloaded, then the library will download a copy it, unzip it for you, and quickly allow you to get up and running with your scripts.

Documentation and Resources

Support these Projects

Patreon: Help support this project and future projects by donating to my Patreon Page. I'm always looking to add more content for individuals like yourself, unfortuantely some of the APIs I would require me to pay monthly fees.

YouTube: If you'd like to watch more of my content, feel free to visit my YouTube channel Sigma Coding.

Hire Me: If you have a project, you think I can help you with feel free to reach out at [email protected] or fill out the contract request form

More Repositories

1

sigma_coding_youtube

This is a collection of all the code that can be found on my YouTube channel Sigma Coding.
Jupyter Notebook
963
star
2

td-ameritrade-python-api

Unofficial Python API client library for TD Ameritrade. This library allows for easy access of the Standard API and allows users to build data pipelines for the Streaming API.
Python
684
star
3

python-trading-robot

A trading robot, that can submit basic orders in an automated fashion using the TD API.
Python
447
star
4

python-sec

A simple python library that allows for easy access of the SEC website so that someone can parse filings, collect data, and query documents.
Python
116
star
5

td-ameritrade-api

The unofficial Python API client library for TD Ameritrade. This library allows for easy access of the Standard REST API and Streaming API.
Python
109
star
6

tradestation-python-api

A Python Client library for the TradeStation API.
Python
88
star
7

finance-news-aggregator

A news aggregator in python, that focuses primarily on business and market news sources.
Python
68
star
8

interactive-brokers-api

A python application used to interact with the Interactive Brokers REST API.
Python
57
star
9

ms-graph-python-client

A Python Client Application that allows users to interact with the Microsoft Graph API.
Python
50
star
10

portfolio-optimization

A python application, that demonstrates optimizing a portfolio using machine learning.
Python
40
star
11

sigma-coding

This is a repository used for educational purposes to demonstrate how to upload a Python package to PyPi.
Python
39
star
12

power-bi-python-api

The Unofficial Python API wrapper for the Microsoft Power BI REST API.
Python
38
star
13

azure-sql-data-project

A python application that is used to create instances of SQL Server instances on Microsoft Azure, and insert data into those databases.
Python
30
star
14

excel-custom-function-add-in

A tutorial on how to build an Excel Custom Function Add-In using the new Office JavaScript API.
TypeScript
17
star
15

python-bureau-economic-analysis-api-client

A Python API client used to pull and retrieve data from the US Bureau of Economic Analysis
Python
13
star
16

azure-functions-http

A tutorial that covers how to use Azure functions using the python programming language.
Python
10
star
17

trading-system

A python application that leverages Microsoft Azure to build a fully functional trading system.
Python
10
star
18

us-federal-treasury-python-api

A Python application which is used to pull data from the United States Federal Treasury API.
Python
7
star
19

sigma-template

Serves as a template folder for other repos I create.
Python
7
star
20

us-federal-register-python-api

A python API client for the United States Federal Register.
Python
5
star
21

trading-factory

My Azure Data Factory for managing my trading system.
Python
5
star
22

youtube-python-client

A Python client library for the YouTube API.
Python
5
star
23

united-states-congress-python-api

A Python API client for the United States Congressional data found GovInfo.com.
Python
5
star
24

trade-robot

A python package that allows you to place automated trades using the TD Ameritrade API.
Python
4
star
25

us-federal-reserve-python-api

A Python application which is used to pull data from the US Federal Reserve.
Python
4
star
26

amzn-associates-api

A python API client library used to interact with the Amazon Associates Marketing API.
Python
3
star
27

sigma-utilities

Random Code I used to help with different projects.
Python
3
star
28

epa-scraper-project

This is a python project that collects and stores data from the EPA website.
3
star
29

tap-federal-reserve

A singer Tap for extracting data from the United States Federal Reserver API (FRED).
Python
3
star
30

python-for-starters

A guide on how to get up and running with Python, VSCode, Git, & GitHub on Windows.
Python
2
star
31

new-office-add-in

TypeScript
2
star
32

energy-scraper

A python program that will go and scrape EPA data.
2
star
33

sigma-azure-management

A Python project that demonstrates managing different Azure Services.
TSQL
2
star
34

gmail-api-python

A Python project that uses the Gmail API in Python.
Python
2
star
35

trading-data-factory

A Python Application that leverages the Azure Data Factory to store trading data.
Python
2
star
36

azure-and-terraform

Terraform modules used to create Azure resources for other projects.
HCL
1
star
37

interview-items

A collection of items I use for interview purposes.
Jupyter Notebook
1
star
38

logistic-regression-model-stocks

Python
1
star
39

docker-trading-data-workflow

A docker application used to collect price data using a docker image.
Python
1
star
40

excel-python-com-server

Demonstrates how to create a Python COM Server using the Win32COM library.
Python
1
star