Interactive Brokers Web API plugin for bt_api, supporting stock and futures trading via IB Web API.
English | 中文
This package provides an Interactive Brokers Web API gateway adapter for the bt_api framework. It connects to IB Web API and exposes a unified interface for trading stocks and futures.
- IB WebSocket connection via
websocket-client - Real-time market data via WebSocket push notifications
- Account & position tracking — balance, positions, orders, trades
- Order management — place and cancel orders
- Stock & futures support via
asset_typekwarg (STKorFUT) - Symbol subscription — batch subscribe by conId or symbol alias
- Python 3.9+
bt_api_base >= 0.15requests >= 2.31websocket-client >= 1.7
pip install bt_api_ib_webOr install from source:
git clone https://github.com/cloudQuant/bt_api_ib_web
cd bt_api_ib_web
pip install -e .frombt_api_ib_webimportIbWebGatewayAdapter# Initialize the adapter (STK for stocks, FUT for futures)adapter=IbWebGatewayAdapter(
base_url="https://localhost:5000", # IB Web API endpointasset_type="STK", # "STK" (stock) or "FUT" (future)
)
# Connect to IB Web APIadapter.connect()
# Subscribe to symbols (by conId or alias)adapter.subscribe_symbols(["265598", "AAPL"])
# Get account balancebalance=adapter.get_balance()
print(balance)
# Get open positionspositions=adapter.get_positions()
print(positions)
# Place a limit orderorder_result=adapter.place_order({
"symbol": "AAPL",
"size": 10,
"price": 150.0,
"order_type": "limit",
"side": "buy",
})
print(order_result)
# Cancel an ordercancel_result=adapter.cancel_order({"order_id": "ABC123"})
print(cancel_result)
# Disconnectadapter.disconnect()| Operation | Method | Status |
|---|---|---|
| Connect | connect() | ✅ |
| Disconnect | disconnect() | ✅ |
| Subscribe symbols | subscribe_symbols(symbols) | ✅ |
| Account balance | get_balance() | ✅ |
| Open positions | get_positions() | ✅ |
| Place order | place_order(payload) | ✅ |
| Cancel order | cancel_order(payload) | ✅ |
{
"symbol": "AAPL", # Symbol or conId"size": 10, # Order size (volume)"price": 150.0, # Limit price (omit for market orders)"order_type": "limit", # "market", "limit", etc."side": "buy", # "buy" or "sell""client_order_id": "my_ref", # Optional: client order reference"extra_data": {}, # Optional: extra IB parameters
}bt_api_ib_web/
├── src/bt_api_ib_web/
│ ├── __init__.py # Exports IbWebGatewayAdapter
│ ├── plugin.py # bt_api plugin registration
│ ├── gateway/
│ │ └── adapter.py # IbWebGatewayAdapter implementation
│ ├── runtime/
│ │ ├── feed.py # IbWebRequestDataStock / Future
│ │ └── stream.py # IbWebDataStream / IbWebAccountStream
│ └── containers/
├── tests/
└── docs/
└── index.md # Documentation
| Resource | Link |
|---|---|
| English Docs | https://bt-api-ib-web.readthedocs.io/ |
| Chinese Docs | https://bt-api-ib-web.readthedocs.io/zh/latest/ |
| GitHub Repository | https://github.com/cloudQuant/bt_api_ib_web |
| Issue Tracker | https://github.com/cloudQuant/bt_api_ib_web/issues |
MIT License - see LICENSE for details.
- Report bugs via GitHub Issues
- Email: yunjinqi@gmail.com
本包为 bt_api 框架提供 Interactive Brokers Web API 网关适配器。连接 IB Web API,提供股票和期货交易的统一接口。
- IB WebSocket 连接 — 通过
websocket-client库 - 实时行情推送 — WebSocket 订阅行情数据
- 账户与持仓跟踪 — 余额、持仓、订单、成交
- 订单管理 — 下单、撤单
- 股票与期货支持 — 通过
asset_type参数(STK或FUT) - 品种订阅 — 按 conId 或品种别名批量订阅
- Python 3.9+
bt_api_base >= 0.15requests >= 2.31websocket-client >= 1.7
pip install bt_api_ib_web或从源码安装:
git clone https://github.com/cloudQuant/bt_api_ib_web
cd bt_api_ib_web
pip install -e .frombt_api_ib_webimportIbWebGatewayAdapter# 初始化适配器(STK 为股票,FUT 为期货)adapter=IbWebGatewayAdapter(
base_url="https://localhost:5000", # IB Web API 端点asset_type="STK", # "STK"(股票)或 "FUT"(期货)
)
# 连接 IB Web APIadapter.connect()
# 订阅品种(按 conId 或别名)adapter.subscribe_symbols(["265598", "AAPL"])
# 查询账户余额balance=adapter.get_balance()
print(balance)
# 查询持仓positions=adapter.get_positions()
print(positions)
# 下限价单order_result=adapter.place_order({
"symbol": "AAPL",
"size": 10,
"price": 150.0,
"order_type": "limit",
"side": "buy",
})
print(order_result)
# 撤单cancel_result=adapter.cancel_order({"order_id": "ABC123"})
print(cancel_result)
# 断开连接adapter.disconnect()| 操作 | 方法 | 状态 |
|---|---|---|
| 连接 | connect() | ✅ |
| 断开 | disconnect() | ✅ |
| 订阅品种 | subscribe_symbols(symbols) | ✅ |
| 账户余额 | get_balance() | ✅ |
| 持仓查询 | get_positions() | ✅ |
| 下单 | place_order(payload) | ✅ |
| 撤单 | cancel_order(payload) | ✅ |
{
"symbol": "AAPL", # 品种名或 conId"size": 10, # 委托数量"price": 150.0, # 限价(市场价单不填)"order_type": "limit", # "market", "limit" 等"side": "buy", # "buy" 或 "sell""client_order_id": "my_ref", # 可选:客户端订单引用"extra_data": {}, # 可选:额外 IB 参数
}bt_api_ib_web/
├── src/bt_api_ib_web/
│ ├── __init__.py # 导出 IbWebGatewayAdapter
│ ├── plugin.py # bt_api 插件注册
│ ├── gateway/
│ │ └── adapter.py # IbWebGatewayAdapter 实现
│ ├── runtime/
│ │ ├── feed.py # IbWebRequestDataStock / Future
│ │ └── stream.py # IbWebDataStream / IbWebAccountStream
│ └── containers/
├── tests/
└── docs/
└── index.md # 文档
MIT 许可证 - 详见 LICENSE。
- 通过 GitHub Issues 反馈问题
- 邮箱: yunjinqi@gmail.com