Skip to main content

Interactive Brokers Data SDK

Project description

IB Data SDK

A Python SDK for retrieving historical market data from Interactive Brokers with a clean, extensible interface.

Features

  • 🚀 Easy to use: Simple, intuitive API for data retrieval
  • 🔧 Extensible: Plugin architecture for custom data processors
  • 📊 Pandas integration: Built-in DataFrame support
  • 🛡️ Type safe: Full type hints and Pydantic validation
  • 📱 CLI included: Command-line interface for quick data retrieval
  • 🏗️ Production ready: Proper error handling, logging, and testing

Installation

pip install ib-data-sdk

For development:

pip install ib-data-sdk[dev]

Quick Start

Environment Setup

Set up your IB connection parameters:

export TWS_HOST=127.0.0.1
export TWS_PORT=7497  # or 7496 for live trading
export TWS_CLIENT_ID=1

Basic Usage

from ib_data_sdk import IBDataClient, HistoricalDataRequest
from ib_data_sdk.enums import Duration, BarSize, DataType

# Create client
client = IBDataClient()

# Create request
request = HistoricalDataRequest(
    symbol="AAPL",
    duration=Duration.DAY_1, 
    bar_size=BarSize.MIN_5,
    data_type=DataType.TRADES
)

# Get data as DataFrame
df = client.get_historical_data_as_dataframe(request)
print(df.head())

# Clean up
client.disconnect_from_ib()

Using the CLI

# Get daily AAPL data
ib-data-sdk get-data AAPL --duration DAY_1 --bar-size MIN_5

# Save to file
ib-data-sdk get-data AAPL --output aapl_data.csv

# List available options
ib-data-sdk list-enums

Advanced Usage

Custom Data Processors

from ib_data_sdk import DataProcessor, DataType

class VolumeWeightedDataProcessor(DataProcessor):
    def process_bar_data(self, price):
        return {
            "date": price.date,
            "vwap": (price.open + price.close) / 2 * price.volume,
            "volume": price.volume,
        }
    
    def get_data_type(self):
        return DataType.TRADES

# Register the processor
from ib_data_sdk.processors import DataProcessorFactory
DataProcessorFactory.register_processor(DataType.TRADES, VolumeWeightedDataProcessor)

Options Data

request = HistoricalDataRequest(
    symbol="AAPL",
    asset_type=AssetType.OPT,
    expiry="20240119",
    strike=150.0,
    right="C"  # Call option
)

df = client.get_historical_data_as_dataframe(request)

Error Handling

from ib_data_sdk.exceptions import ConnectionError, DataRequestError, TimeoutError

try:
    df = client.get_historical_data_as_dataframe(request)
except ConnectionError:
    print("Failed to connect to IB")
except DataRequestError as e:
    print(f"Data request failed: {e}")
except TimeoutError:
    print("Request timed out")

API Reference

Classes

  • IBDataClient: Main client class for data retrieval
  • HistoricalDataRequest: Request model with validation
  • HistoricalDataResponse: Response model with metadata
  • DataProcessor: Abstract base for custom processors

Enums

  • AssetType: STK, OPT, FUT, CASH, IND
  • BarSize: 1 sec to 1 month intervals
  • Duration: 1 D to 2 Y periods
  • DataType: TRADES, BID_ASK, MIDPOINT, etc.

Development

Setup

git clone https://github.com/quantphantom/ib-data-sdk
cd ib-data-sdk
pip install -e .[dev]
pre-commit install

Testing

pytest tests/ -v --cov=src/ib_data_sdk

Contributing

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

License

MIT License - see LICENSE file for details.

Support

Disclaimer

This project is not affiliated with Interactive Brokers. Use at your own risk. Trading

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

ib_data_sdk-0.1.6.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

ib_data_sdk-0.1.6-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file ib_data_sdk-0.1.6.tar.gz.

File metadata

  • Download URL: ib_data_sdk-0.1.6.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for ib_data_sdk-0.1.6.tar.gz
Algorithm Hash digest
SHA256 64a0fa146c5fd6161f7323c2b117c3dd4789b942ba43ae430ea8f54ea8eee759
MD5 2e48c0e429443e5578bd9baf26b8dfb9
BLAKE2b-256 7aa12353c6d4515d667ed345dddbb534124c66c4b1073a7a1df6dba14f36c159

See more details on using hashes here.

File details

Details for the file ib_data_sdk-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: ib_data_sdk-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for ib_data_sdk-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 d8f9a87e0e9c80059b0b6fdddbf450d3b8c0295f3ae8f691099b1d12f7080e5a
MD5 00984c0579626c50f156473acf77797d
BLAKE2b-256 6f011288800f23aab509183466b18d8f29d407bb862b434cd01f951d19dc33b1

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