Skip to main content

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 回调函数可以接收错误通知。

注意事项

  1. 确保在使用前先连接和认证
  2. 使用完毕后记得调用 close() 方法清理资源
  3. 所有操作都是异步的,需要使用 await 关键字
  4. 建议在生产环境中添加适当的错误处理和重连逻辑

许可证

本项目遵循 MIT 许可证。

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

dtrader_level2_client-1.0.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

dtrader_level2_client-1.0.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file dtrader_level2_client-1.0.0.tar.gz.

File metadata

  • Download URL: dtrader_level2_client-1.0.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for dtrader_level2_client-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c91b784bf67238c3b5ff07138bd4244eb8a768ef3703f26a0ec9d5993ea7a454
MD5 c83b0942b35e7e3694ec038d4b417c5f
BLAKE2b-256 dca26a169a73af98aa60499a638b15da29df732259b50e3bd47a4a94bae3a70d

See more details on using hashes here.

File details

Details for the file dtrader_level2_client-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dtrader_level2_client-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b4c7b190be1ba8760fefa8ff70d0a037b2c70335f962b8456da049f62e744cae
MD5 439ca63359ed3bf3692e65fe78539fbd
BLAKE2b-256 8d16f6cb091fdf6cee404bf28cf688e0886b73ecbd900cdbe673d4b756623a79

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