DTrader Level2 WebSocket API Python客户端
Project description
DTrader Level2 Python WebSocket 客户端
一个用于连接 DTrader Level2 WebSocket API 的 Python 客户端库。
功能特性
- 异步 WebSocket 连接
- JWT 认证支持
- 股票数据订阅/取消订阅
- 批量订阅操作
- 重置订阅功能
- 自动心跳保持连接
- 完整的错误处理
- 事件回调机制
安装依赖
pip install dtrader-level2-client
快速开始
基本使用
import asyncio
from dtrader_level2_client import DTraderHQClient, MarketData
async def on_data(data: MarketData):
print(f"收到数据: {data.stock_code} - 类型: {data.data_type}")
async def main():
client = DTraderHQClient("ws://localhost:8080/ws")
client.on_data = on_data
await client.connect()
await client.authenticate("your_jwt_token")
await client.subscribe("000001", [4, 8]) # 订阅逐笔成交和逐笔大单
# 保持连接
await asyncio.sleep(60)
await client.close()
asyncio.run(main())
API 参考
DTraderHQClient
构造函数
client = DTraderHQClient(url, ping_interval=30)
url: WebSocket 服务器地址ping_interval: 心跳间隔(秒),默认 30 秒
主要方法
连接和认证
# 连接到服务器
await client.connect()
# 用户认证
await client.authenticate(token)
# 关闭连接
await client.close()
订阅管理
# 单个订阅
await client.subscribe(stock_code, data_types)
# 批量订阅
await client.batch_subscribe(subscriptions)
# 取消订阅
await client.unsubscribe(stock_code)
# 批量取消订阅
await client.batch_unsubscribe(stock_codes)
# 重置订阅
await client.reset_subscriptions(subscriptions)
# 获取当前订阅
subscriptions = client.get_subscriptions()
状态检查
# 检查连接状态
if client.is_connected:
print("已连接")
# 检查认证状态
if client.is_authenticated:
print("已认证")
事件回调
# 设置数据回调
client.on_data = async def(data: MarketData):
# 处理市场数据
pass
# 设置错误回调
client.on_error = async def(error: str):
# 处理错误
pass
# 设置成功回调
client.on_success = async def(msg_type: str, data: dict):
# 处理成功消息
pass
# 设置连接回调
client.on_connected = async def():
# 连接成功
pass
# 设置认证回调
client.on_authenticated = async def():
# 认证成功
pass
MarketData
市场数据对象包含以下字段:
class MarketData:
stock_code: str # 股票代码
data_type: int # 数据类型
timestamp: int # 时间戳
data: dict # 具体数据内容
数据类型
4: 逐笔成交数据8: 逐笔大单数据14: 逐笔委托数据
示例代码
基本示例
查看 examples/basic_example.py 了解完整的使用流程。
错误处理
客户端会自动处理以下错误:
- 连接断开自动重连
- 认证失败
- 订阅错误
- 网络异常
通过设置 on_error 回调函数可以接收错误通知。
注意事项
- 确保在使用前先连接和认证
- 使用完毕后记得调用
close()方法清理资源 - 所有操作都是异步的,需要使用
await关键字 - 建议在生产环境中添加适当的错误处理和重连逻辑
许可证
本项目遵循 MIT 许可证。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dtrader_level2_client-1.0.1.tar.gz.
File metadata
- Download URL: dtrader_level2_client-1.0.1.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c8d02eed3bd981833bf02dae2637ca7d558f89c9dfcf4622c019396e5a77778
|
|
| MD5 |
9a0494f80fbfaa1cd779132d5b120a77
|
|
| BLAKE2b-256 |
538ed02740c77239745175ca7a99ea5fe8c38ce321507630e365134930c2f6e8
|
File details
Details for the file dtrader_level2_client-1.0.1-py3-none-any.whl.
File metadata
- Download URL: dtrader_level2_client-1.0.1-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1aca6f3834541833584dc849f8eb38d2d25a1a5c5b2122fe2e6a6086853e961c
|
|
| MD5 |
d7259fd416888756d8d9af96fd675634
|
|
| BLAKE2b-256 |
c0ffa6201a62f1c6c58ea18801c45841d26aa65306afb89f07f4bbbac7b7f6e4
|