Stamp Interactive Brokers Portfolio
This sample illustrates how to retrieve, save, and stamp an Interactive Brokers (IB) portfolio.
The sample can be run from the command line interactively or as a script if your environment is set appropriately.
The sample will run the Interactive Brokers (IB) Client Portal Gateway on your Windows computer and make a request to it using the Web API from Python.
You can find the implementation in stamp_interactive_brokers_portfolio.py
.
1. Prerequisites
Interactive Brokers Account: You must have an IB account (live or paper trading).
Python: Ensure Python is installed on your Windows machine.
vBase: Follow the vBase Windows Guide to set up Windows environment to run vBase samples.
2. Download and Install Client Portal Gateway
Interactive Brokers provides the Client Portal Gateway as a lightweight API gateway for accessing account data via a Web API. Follow these steps to download and run the gateway:
Download the Client Portal Gateway:
Visit the official Client Portal Gateway page and download the latest Client Portal Gateway for Windows.
Alternatively, you can access the latest Client Portal Gateway directly from the IBKR GitHub Repository.
Extract the ZIP File:
After downloading the Client Portal Gateway ZIP file, extract it to a folder on your machine (e.g.,
C:\IBClientPortalGateway
).
Run the Gateway:
At the Command Line, run:
Authenticate to the Gateway:
Open the client portal in a browser:
You may need to ignore security warnings and accept the self-signed certificate in your browser.
Login to the client portal with your IB credentials.
You should see the “Client login succeeds” message.
Once the gateway is running, it provides a Web API that requires authentication via the session token returned during the login process. The gateway will keep running in the background.
3. Set Environment Variables
Set the following environment variables for your IB and vBase configuration:
IB Configuration:
IB_ACCOUNT_ID
- The IB account id.
AWS S3 Configuration (Optional if you save the portfolio data elsewhere):
AWS_ACCESS_KEY_ID
- The Access Key used to connect to the S3 service.AWS_SECRET_ACCESS_KEY
- The Secret Key used to connect to the S3 service.AWS_S3_BUCKET_NAME
- The AWS S3 bucket name used to store the portfolio data.
vBase Configuration:
VBASE_FORWARDER_URL
- vBase Forwarder Service URL:https://dev.api.vbase.com/forwarder/ for development/testnet.
https://api.vbase.com/forwarder/ for production.
VBASE_API_KEY
- The vBase API Key used to access the Forwarder service.VBASE_COMMITMENT_SERVICE_PRIVATE_KEY
- The private key used to sign portfolio stamps.VBASE_DATASET_NAME
- The name of the vBase dataset that will hold the portfolio history.
4. Run the Sample
Run the sample from the command line:
or walk through the sample in an interactive window.
Last updated