uSMART OpenAPI Python SDK
Project description
uSMART OpenAPI Python SDK
盈立智投 OpenAPI Python 封装库,提供简洁的API调用方式,支持股票交易、行情查询和实时数据推送。
作者: wuboyuan
联系方式: wuboyuan92@126.com
免责声明: 本库参考 uSMART 官方 API 文档 开发,仅供学习交流使用。使用本库进行交易产生的任何风险和损失由使用者自行承担,作者不承担任何责任。
Features
- ✅ 用户认证 - 登录、交易解锁
- ✅ 持仓资产 - 查询持仓、资产信息
- ✅ 订单管理 - 下单、撤单、改单、查询
- ✅ 行情数据 - 实时行情、K线、买卖盘、逐笔成交
- ✅ WebSocket推送 - 实时行情推送(实时行情、买卖盘、逐笔成交)
- ✅ 标准库设计 - 所有配置通过参数传入,无需配置文件
Installation
pip install usmart-api
Or install from source:
git clone https://github.com/usmart/usmart-api-python.git
cd usmart-api-python
pip install -e .
Quick Start
1. 基础用法
from usmart_api import USmartClient
# 创建客户端
client = USmartClient(
X_Channel='your_channel',
phoneNumber='your_phone',
login_password='your_password',
trade_passwrod='your_trade_password',
public_key='your_public_key',
private_key='your_private_key'
)
# 登录(自动获取token并解锁交易)
result = client.login()
if result['success']:
print("登录成功!")
# 查询持仓
holdings = client.get_holdings(exchange_type='0') # 0=港股
print(holdings)
# 查询资产
assets = client.query_asset(money_type=2) # 2=港币
print(assets)
else:
print(f"登录失败: {result.get('error')}")
2. 交易操作
# 买入股票
result = client.buy(
stock_code='00700',
price='400.0',
quantity='100',
exchange_type=0 # 港股
)
# 卖出股票
result = client.sell(
stock_code='00700',
price='410.0',
quantity='100',
exchange_type=0
)
# 撤单
result = client.cancel_order(entrust_id='123456')
# 查询今日订单
orders = client.get_today_orders(exchange_type='0')
3. 行情查询
# 实时行情
quote = client.get_quote(['hk00700', 'usTSLA'])
# K线数据
kline = client.get_kline(
secu_id='hk00700',
kline_type=5, # 日K
count=100
)
# 买卖盘
orderbook = client.get_orderbook('hk00700')
# 逐笔成交
ticks = client.get_tick('hk00700', count=20)
4. WebSocket实时行情推送
import time
# 定义数据回调函数
def my_handler(topic: str, data: str):
"""
处理收到的行情数据
Args:
topic: 主题,如 "rt.us.TSLA"
data: JSON格式的行情数据
"""
import json
data_obj = json.loads(data)
if topic.startswith("rt."):
# 实时行情
price = data_obj.get('latestPrice')
print(f"[实时行情] {topic}: 最新价={price}")
elif topic.startswith("ob."):
# 买卖盘
bids = data_obj.get('data', [])
if bids:
print(f"[买卖盘] {topic}: 买一={bids[0].get('bidPrice')}, 卖一={bids[0].get('askPrice')}")
# 启动WebSocket推送
client.start_quote_push(handler=my_handler)
time.sleep(3) # 等待连接建立
# 订阅行情
client.subscribe_quote(["rt.us.TSLA", "ob.us.TSLA"])
# 保持运行...
time.sleep(60)
# 取消订阅并停止
client.unsubscribe_quote(["rt.us.TSLA", "ob.us.TSLA"])
client.stop_quote_push()
API Reference
USmartClient
主客户端类,整合所有功能。
初始化参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| X_Channel | str | ✅ | 渠道标识 |
| phoneNumber | str | ✅ | 手机号 |
| login_password | str | ✅ | 登录密码 |
| trade_passwrod | str | ✅ | 交易密码 |
| public_key | str | ✅ | RSA公钥 |
| private_key | str | ✅ | RSA私钥 |
| areaCode | str | ❌ | 区号,默认'86' |
| X_Lang | str | ❌ | 语言,默认'1'(简体) |
主要方法
| 方法 | 说明 |
|---|---|
login() |
登录并解锁交易 |
get_holdings() |
查询持仓 |
query_asset() |
查询资产 |
buy() / sell() |
买入/卖出 |
cancel_order() |
撤单 |
get_quote() |
查询实时行情 |
get_kline() |
查询K线 |
start_quote_push() |
启动WebSocket推送 |
subscribe_quote() |
订阅行情主题 |
stop_quote_push() |
停止推送 |
主题格式
WebSocket订阅主题格式:<类型>.<市场>.<代码>
| 类型 | 说明 |
|---|---|
| rt | 实时行情 (realtime) |
| ob | 买卖盘 (orderbook) |
| tk | 逐笔成交 (tick) |
| 市场 | 说明 |
|---|---|
| hk | 港股 |
| us | 美股 |
| sh | 上海 |
| sz | 深圳 |
示例:
rt.hk.00700- 腾讯实时行情ob.us.TSLA- 特斯拉买卖盘tk.hk.00700- 腾讯逐笔成交
Examples
See examples/ directory for more usage examples.
See API_DOCUMENTATION.md for complete API reference with real-world examples.
作者信息
- 作者: wuboyuan
- 邮箱: wuboyuan92@126.com
- GitHub: https://github.com/wuboyuan
免责声明
本库参考 uSMART 官方 API 文档 开发,仅供学习交流使用。
使用本库进行交易产生的任何风险和损失由使用者自行承担,作者不承担任何责任。
- 投资有风险,入市需谨慎
- 请确保您了解所使用接口的风险
- 建议在实盘交易前充分测试
License
MIT License - see LICENSE file for details.
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 usmart_api-2.0.3.tar.gz.
File metadata
- Download URL: usmart_api-2.0.3.tar.gz
- Upload date:
- Size: 30.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71bf2d846c7896dc7cca863747ae810d7f8c4055773b2ac100c3f115ba91de32
|
|
| MD5 |
becb036a81f3550d2f46ffbd3af35810
|
|
| BLAKE2b-256 |
8e16d4559fa4b1bf8ef5a6038a111edd470930ef420f66ed2fbbaa52618bb735
|
File details
Details for the file usmart_api-2.0.3-py3-none-any.whl.
File metadata
- Download URL: usmart_api-2.0.3-py3-none-any.whl
- Upload date:
- Size: 26.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4996762aeeb11178574c059b18a3335cd91a32caf6a06e612a6acc0515f0d76
|
|
| MD5 |
41a15afb6e38c8bd06da2a0f3feae62e
|
|
| BLAKE2b-256 |
01e57d7c8849f8c8317520092b12d200c771532d6f52c266bb8b8b91307451d4
|