Skip to main content

Python client library for Interactive Brokers Gateway running in Kubernetes (K3s) environments

Project description

IB Gateway API Client

A Python client library for connecting to Interactive Brokers Gateway running in Kubernetes (K3s) environments for both paper and live trading.

Features

  • 🚀 Easy Integration: Simple Python API for connecting to IB Gateway
  • 🔄 Async Support: Built on ib-insync with full async/await support
  • 🏗️ K3s Ready: Automatically detects K3s node IPs and service endpoints
  • 📊 Account Management: Get account information, positions, and trading data
  • 🔒 Secure: Random client ID generation to prevent connection conflicts
  • 📦 PyPI Ready: Installable via pip from PyPI

Installation

pip install ibgateway-api-client

Quick Start

Paper Trading

import asyncio
from ibgateway_api_client import K3sPaperClient

async def main():
    client = K3sPaperClient(host="192.168.1.222", port=32002)
    
    if await client.connect():
        await client.get_account_info()
        await client.get_positions()
        await client.disconnect()

asyncio.run(main())

Live Trading

import asyncio
from ibgateway_api_client import K3sLiveClient

async def main():
    client = K3sLiveClient(host="192.168.1.222", port=32001)
    
    if await client.connect():
        await client.get_account_info()
        await client.get_positions()
        await client.disconnect()

asyncio.run(main())

Auto-Detection (K3s)

import asyncio
from ibgateway_api_client import K3sPaperClient, get_k3s_node_ip

async def main():
    # Automatically detect K3s node IP
    host = get_k3s_node_ip()
    client = K3sPaperClient(host=host, port=32002)
    
    if await client.connect():
        await client.get_account_info()
        await client.disconnect()

asyncio.run(main())

Configuration

Environment Variables

Create a .env file or set environment variables:

# Paper Trading
K3S_PAPER_HOST=192.168.1.222
K3S_PAPER_PORT=32002
K3S_PAPER_CLIENT_ID=1001

# Live Trading
K3S_LIVE_HOST=192.168.1.222
K3S_LIVE_PORT=32001
K3S_LIVE_CLIENT_ID=1002

Configuration File

# config.json
{
    "connection": {
        "host": "192.168.1.222",
        "port": 32002,
        "client_id": 1001,
        "timeout": 10
    }
}

# Usage
client = K3sPaperClient(config_file="config.json")

API Reference

K3sPaperClient

Paper trading client for K3s environments.

client = K3sPaperClient(host=None, port=32002, client_id=None, config_file=None)

Parameters:

  • host (str, optional): Gateway host IP. Defaults to auto-detection.
  • port (int, optional): Gateway port. Defaults to 32002 for paper trading.
  • client_id (int, optional): Client ID. Defaults to random (1000-9999).
  • config_file (str, optional): Path to JSON configuration file.

Methods:

  • async connect(): Connect to the gateway
  • async get_account_info(): Get account summary information
  • async get_positions(): Get current positions
  • async disconnect(): Disconnect from the gateway

K3sLiveClient

Live trading client for K3s environments.

client = K3sLiveClient(host=None, port=32001, client_id=None, config_file=None)

Parameters:

  • host (str, optional): Gateway host IP. Defaults to auto-detection.
  • port (int, optional): Gateway port. Defaults to 32001 for live trading.
  • client_id (int, optional): Client ID. Defaults to random (1000-9999).
  • config_file (str, optional): Path to JSON configuration file.

Methods:

  • async connect(): Connect to the gateway
  • async get_account_info(): Get account summary information
  • async get_positions(): Get current positions
  • async disconnect(): Disconnect from the gateway

Utility Functions

from ibgateway_api_client import get_k3s_node_ip, test_connection

# Get K3s node IP automatically
host = get_k3s_node_ip()

# Test connection
success = await test_connection(host, 32002, "Paper Trading")

Command Line Tools

The package includes command-line tools for testing connections:

# Test paper trading connection
ibgateway-test-paper

# Test live trading connection
ibgateway-test-live

# Test both connections
python -m ibgateway_api_client.test_connection

Requirements

  • Python 3.8+
  • Interactive Brokers Gateway running in K3s
  • ib-insync library
  • kubectl configured for K3s cluster access

Development

Setup

git clone https://github.com/logycon/ibgateway-api-client
cd ibgateway-api-client
pip install -e ".[dev]"

Testing

pytest

Building

python -m build

Publishing

python -m twine upload dist/*

License

MIT License - see LICENSE file for details.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

Support

Disclaimer

This library is for educational and development purposes. Always test thoroughly in paper trading before using with live accounts. The authors are not responsible for any financial losses.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ibgateway_api_client-0.1.1.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ibgateway_api_client-0.1.1-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file ibgateway_api_client-0.1.1.tar.gz.

File metadata

  • Download URL: ibgateway_api_client-0.1.1.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for ibgateway_api_client-0.1.1.tar.gz
Algorithm Hash digest
SHA256 577aab55c92232f723d9fb797c202d0c413f4e1b95dbb886a71d15fb33df457a
MD5 7b06d34eae09757f94715c614eaf647e
BLAKE2b-256 f0b8870bd52fd7d04325a95f3a0c3b41f074570a24d4a603417f284db47f5fb8

See more details on using hashes here.

File details

Details for the file ibgateway_api_client-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ibgateway_api_client-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 62ec565ecb2758e23d7e8f91b8d39cbe55fef180b7474da0aa78f5043ca97916
MD5 fceeadf1e73101465fa09fee01e6a027
BLAKE2b-256 6c1d29196de4984c6fec5c2925c72037dd55ac3e7185bd600bf9bc1bbc7bebb9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page