
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
dtrader-level2-client
Advanced tools
一个用于连接 DTrader Level2 WebSocket API 的 Python 客户端库。
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())
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
市场数据对象包含以下字段:
class MarketData:
stock_code: str # 股票代码
data_type: int # 数据类型
timestamp: int # 时间戳
data: dict # 具体数据内容
4
: 逐笔成交数据8
: 逐笔大单数据14
: 逐笔委托数据查看 examples/basic_example.py
了解完整的使用流程。
客户端会自动处理以下错误:
通过设置 on_error
回调函数可以接收错误通知。
close()
方法清理资源await
关键字本项目遵循 MIT 许可证。
FAQs
DTrader Level2 WebSocket API Python客户端
We found that dtrader-level2-client demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.