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 许可证。
Metadata
Release files for dtrader-level2-client 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dtrader_level2_client-1.0.1.tar.gz | 10.4 kB | Without provenance | Details for dtrader_level2_client-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dtrader_level2_client-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for dtrader_level2_client-1.0.1-py3-none-any.whl |
Total release size: 18.6 kB
Release files / dtrader_level2_client-1.0.1.tar.gz
| Download URL | dtrader_level2_client-1.0.1.tar.gz |
|---|---|
| Size | 10.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9c8d02eed3bd981833bf02dae2637ca7d558f89c9dfcf4622c019396e5a77778
|
|
BLAKE2b-256 checksum How to use checksums |
538ed02740c77239745175ca7a99ea5fe8c38ce321507630e365134930c2f6e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.0
|
Release files / dtrader_level2_client-1.0.1-py3-none-any.whl
| Download URL | dtrader_level2_client-1.0.1-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1aca6f3834541833584dc849f8eb38d2d25a1a5c5b2122fe2e6a6086853e961c
|
|
BLAKE2b-256 checksum How to use checksums |
c0ffa6201a62f1c6c58ea18801c45841d26aa65306afb89f07f4bbbac7b7f6e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.0
|
Security for dtrader-level2-client 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.