Skip to content
This repository was archived by the owner on Jun 18, 2025. It is now read-only.

Commit b25814f

Browse files
Initial commit
0 parents  commit b25814f

16 files changed

+1812
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/.idea
2+
# Coverage output
3+
.coverage
4+
/.nox/
5+
/build/
6+
/coverage.xml
7+
/.zip/
8+
/venv/

README.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# IBEvent
2+
3+
A lightweight, event-driven framework for Interactive Brokers API that makes it easy to handle real-time market data.
4+
5+
[中文文档](README_zh.md)
6+
7+
## Features
8+
9+
- Simple and intuitive event-based API
10+
- Support for both synchronous and asynchronous handlers
11+
- Priority-based event handling
12+
- Automatic data source binding
13+
- Built on top of [ib_async](https://github.com/erdewit/ib_async)
14+
15+
## Installation
16+
17+
```bash
18+
pip install ibevent
19+
```
20+
21+
## Quick Start
22+
23+
Here's a simple example that subscribes to USD/JPY forex data:
24+
25+
```python
26+
from ib_async import IB
27+
from ibevent.events import IBEventType
28+
from ib_async.contract import Forex
29+
30+
# Create IB connection
31+
ib = IB()
32+
ib.connect('127.0.0.1', 7497, clientId=1)
33+
34+
# Create USD/JPY contract
35+
usd_jpy = Forex('USDJPY')
36+
37+
# Register bar update handler
38+
@ib.events.register(
39+
IBEventType.BAR_UPDATE,
40+
bind_to=ib.reqRealTimeBars(
41+
usd_jpy,
42+
barSize=5,
43+
whatToShow='MIDPOINT'
44+
)
45+
)
46+
def handle_bar(ib, bars, has_new_bar):
47+
if has_new_bar:
48+
print(bars[-1])
49+
50+
# Run until interrupted
51+
try:
52+
ib.run()
53+
except KeyboardInterrupt:
54+
ib.disconnect()
55+
print("\nShutting down...")
56+
```
57+
58+
## Event Types
59+
60+
IBEvent supports all IB API events through the `IBEventType` enum:
61+
62+
- `BAR_UPDATE`: Real-time bar data updates
63+
- `ERROR`: Error events from IB API
64+
- `CONNECTED`: Connection established events
65+
- `DISCONNECTED`: Connection lost events
66+
67+
## Event Handlers
68+
69+
### Basic Handler
70+
71+
```python
72+
@ib.events.register(IBEventType.BAR_UPDATE)
73+
def handle_bar(ib, bars, has_new_bar):
74+
if has_new_bar:
75+
print(bars[-1])
76+
```
77+
78+
### Priority-based Handler
79+
80+
```python
81+
@ib.events.register(IBEventType.BAR_UPDATE, priority=10)
82+
def high_priority_handler(ib, bars, has_new_bar):
83+
# This handler runs first
84+
pass
85+
```
86+
87+
### Async Handler
88+
89+
```python
90+
@ib.events.register(IBEventType.BAR_UPDATE)
91+
async def handle_bar(ib, bars, has_new_bar):
92+
if has_new_bar:
93+
await process_bar(bars[-1])
94+
```
95+
96+
### Data Source Binding
97+
98+
```python
99+
@ib.events.register(
100+
IBEventType.BAR_UPDATE,
101+
bind_to=ib.reqRealTimeBars(contract)
102+
)
103+
def handle_bar(ib, bars, has_new_bar):
104+
pass
105+
```
106+
107+
## Examples
108+
109+
Check out the [examples](examples/) directory for more:
110+
111+
- [Basic Usage](examples/basic_usage.py): Simple real-time bar data subscription
112+
- [Multiple Streams](examples/multiple_streams.py): Handling multiple data streams with priorities
113+
- [Async Handlers](examples/async_handlers.py): Using async/await with event handlers
114+
115+
## Requirements
116+
117+
- Python 3.7+
118+
- ib_async
119+
- Interactive Brokers TWS or IB Gateway
120+
121+
## Contributing
122+
123+
Contributions are welcome! Please feel free to submit a Pull Request.
124+
125+
## License
126+
127+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

README_zh.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# IBEvent
2+
3+
IBEvent 是一个轻量级的事件驱动框架,专为 Interactive Brokers API 设计,让实时市场数据处理变得简单直观。
4+
5+
[English](README.md)
6+
7+
## 特性
8+
9+
- 简单直观的事件驱动 API
10+
- 同时支持同步和异步处理器
11+
- 基于优先级的事件处理机制
12+
- 自动的数据源绑定
13+
- 基于 [ib_async](https://github.com/erdewit/ib_async) 构建
14+
15+
## 安装
16+
17+
```bash
18+
pip install ibevent
19+
```
20+
21+
## 快速开始
22+
23+
这是一个订阅 USD/JPY 外汇数据的简单示例:
24+
25+
```python
26+
from ib_async import IB
27+
from ibevent.events import IBEventType
28+
from ib_async.contract import Forex
29+
30+
# 创建 IB 连接
31+
ib = IB()
32+
ib.connect('127.0.0.1', 7497, clientId=1)
33+
34+
# 创建 USD/JPY 合约
35+
usd_jpy = Forex('USDJPY')
36+
37+
# 注册 bar 数据更新处理器
38+
@ib.events.register(
39+
IBEventType.BAR_UPDATE,
40+
bind_to=ib.reqRealTimeBars(
41+
usd_jpy,
42+
barSize=5,
43+
whatToShow='MIDPOINT'
44+
)
45+
)
46+
def handle_bar(ib, bars, has_new_bar):
47+
if has_new_bar:
48+
print(bars[-1])
49+
50+
# 运行直到被中断
51+
try:
52+
ib.run()
53+
except KeyboardInterrupt:
54+
ib.disconnect()
55+
print("\n正在关闭...")
56+
```
57+
58+
## 事件类型
59+
60+
IBEvent 通过 `IBEventType` 枚举支持所有 IB API 事件:
61+
62+
- `BAR_UPDATE`:实时 bar 数据更新
63+
- `ERROR`:来自 IB API 的错误事件
64+
- `CONNECTED`:连接建立事件
65+
- `DISCONNECTED`:连接断开事件
66+
67+
## 事件处理器
68+
69+
### 基础处理器
70+
71+
```python
72+
@ib.events.register(IBEventType.BAR_UPDATE)
73+
def handle_bar(ib, bars, has_new_bar):
74+
if has_new_bar:
75+
print(bars[-1])
76+
```
77+
78+
### 优先级处理器
79+
80+
```python
81+
@ib.events.register(IBEventType.BAR_UPDATE, priority=10)
82+
def high_priority_handler(ib, bars, has_new_bar):
83+
# 这个处理器会首先运行
84+
pass
85+
```
86+
87+
### 异步处理器
88+
89+
```python
90+
@ib.events.register(IBEventType.BAR_UPDATE)
91+
async def handle_bar(ib, bars, has_new_bar):
92+
if has_new_bar:
93+
await process_bar(bars[-1])
94+
```
95+
96+
### 数据源绑定
97+
98+
```python
99+
@ib.events.register(
100+
IBEventType.BAR_UPDATE,
101+
bind_to=ib.reqRealTimeBars(contract)
102+
)
103+
def handle_bar(ib, bars, has_new_bar):
104+
pass
105+
```
106+
107+
## 示例
108+
109+
查看 [examples](examples/) 目录获取更多示例:
110+
111+
- [基础用法](examples/basic_usage.py):简单的实时 bar 数据订阅
112+
- [多数据流](examples/multiple_streams.py):使用优先级处理多个数据流
113+
- [异步处理器](examples/async_handlers.py):在事件处理器中使用 async/await
114+
115+
## 系统要求
116+
117+
- Python 3.7+
118+
- ib_async
119+
- Interactive Brokers TWS 或 IB Gateway
120+
121+
## 贡献
122+
123+
欢迎提交 Pull Request 来改进这个项目!
124+
125+
## 许可证
126+
127+
本项目采用 MIT 许可证 - 详见 [LICENSE](LICENSE) 文件

__init__.py

Whitespace-only changes.

examples/async_handlers.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""Async handler example with IBEvent.
2+
3+
This example demonstrates:
4+
1. How to use async event handlers
5+
2. How to handle real-time bar data asynchronously
6+
3. Basic usage of asyncio with IBEvent
7+
"""
8+
9+
from ib_async import IB
10+
from ibevent.events import IBEventType
11+
from ib_async.contract import Forex
12+
import asyncio
13+
14+
15+
# Create IB connection
16+
ib = IB()
17+
ib.connect('127.0.0.1', 7497, clientId=1)
18+
19+
# Create USD/JPY contract
20+
usd_jpy = Forex('USDJPY')
21+
22+
# Register bar update handler
23+
@ib.events.register(
24+
IBEventType.BAR_UPDATE,
25+
bind_to=ib.reqRealTimeBars(
26+
usd_jpy,
27+
barSize=5,
28+
whatToShow='MIDPOINT',
29+
useRTH=True
30+
)
31+
)
32+
async def handle_bar(ib, bars, has_new_bar):
33+
"""Handle real-time bar updates asynchronously.
34+
35+
This handler demonstrates how to use async/await with IBEvent.
36+
You can add your own async processing logic here.
37+
"""
38+
if has_new_bar:
39+
# Simulate some async processing
40+
await asyncio.sleep(0.1)
41+
print(bars[-1])
42+
43+
# Run until interrupted
44+
try:
45+
ib.run()
46+
except KeyboardInterrupt:
47+
ib.disconnect()
48+
print("\nShutting down...")

examples/basic_usage.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""Basic usage example of IBEvent.
2+
3+
This example demonstrates:
4+
1. How to connect to Interactive Brokers
5+
2. How to subscribe to real-time bar data for USD/JPY
6+
3. How to handle bar updates using a simple event handler
7+
"""
8+
9+
from ib_async import IB
10+
from ibevent.events import IBEventType
11+
from ib_async.contract import Forex
12+
13+
14+
# Create IB connection
15+
ib = IB()
16+
ib.connect('127.0.0.1', 7497, clientId=1)
17+
18+
# Create USD/JPY contract
19+
usd_jpy = Forex('USDJPY')
20+
21+
# Register bar update handler
22+
@ib.events.register(
23+
IBEventType.BAR_UPDATE,
24+
bind_to=ib.reqRealTimeBars(
25+
usd_jpy,
26+
barSize=5,
27+
whatToShow='MIDPOINT',
28+
useRTH=True
29+
)
30+
)
31+
def handle_bar(ib, bars, has_new_bar):
32+
"""Handle real-time bar updates for USD/JPY."""
33+
if has_new_bar:
34+
print(bars[-1])
35+
36+
# Run until interrupted
37+
try:
38+
ib.run()
39+
except KeyboardInterrupt:
40+
ib.disconnect()
41+
print("\nShutting down...")

0 commit comments

Comments
 (0)