Back to Registry
View Author Profile
Official Verified
tqsdk
天勤量化TqSdk - 开源Python期货/期权交易SDK,提供实时行情、回测和实盘交易功能。
skill-install — Terminal
Install via CLI (Recommended)
clawhub install openclaw/skills/skills/coderwpf/tqsdkOr
TqSdk(天勤量化SDK)
TqSdk 是信易科技开发的开源Python期货/期权量化交易SDK。通过统一的异步API提供实时行情、历史数据、回测和实盘交易功能。
文档:https://doc.shinnytech.com/tqsdk/latest/ 免费版提供延时行情(15分钟延迟),专业版支持实时数据。
安装
pip install tqsdk
快速入门
from tqsdk import TqApi, TqAuth, TqBacktest
from datetime import date
# 实盘模式(免费账户提供延时行情)
api = TqApi(auth=TqAuth("your_username", "your_password"))
# 获取实时行情
quote = api.get_quote("SHFE.cu2401") # Shanghai copper futures
print(f"最新价: {quote.last_price}, 成交量: {quote.volume}")
# 获取K线数据
klines = api.get_kline_serial("SHFE.cu2401", duration_seconds=60) # 1-min bars
print(klines.tail())
# 关闭API
api.close()
代码格式
EXCHANGE.CONTRACT
| 交易所 | 代码 | 示例 |
|---|---|---|
| 上海期货交易所 | SHFE | SHFE.cu2401(铜) |
| 大连商品交易所 | DCE | DCE.m2405(豆粕) |
| 郑州商品交易所 | CZCE | CZCE.CF405(棉花) |
| 中国金融期货交易所 | CFFEX | CFFEX.IF2401(沪深300期货) |
| 上海国际能源交易中心 | INE | INE.sc2407(原油) |
| 广州期货交易所 | GFEX | GFEX.si2407(工业硅) |
| 上交所期权 | SSE | SSE.10004816(50ETF期权) |
| 深交所期权 | SZSE | SZSE.90000001(300ETF期权) |
行情数据
实时行情
from tqsdk import TqApi, TqAuth
api = TqApi(auth=TqAuth("user", "pass"))
quote = api.get_quote("CFFEX.IF2401")
# 关键字段:
# quote.last_price — 最新价
# quote.bid_price1 — 买一价
# quote.ask_price1 — 卖一价
# quote.bid_volume1 — 买一量
# quote.ask_volume1 — 卖一量
# quote.highest — 日最高价
# quote.lowest — 日最低价
# quote.open — 开盘价
# quote.close — 昨收价
# quote.volume — 总成交量
# quote.amount — 总成交额
# quote.open_interest — 持仓量
# quote.upper_limit — 涨停价
# quote.lower_limit — 跌停价
# quote.pre_settlement — 昨结算价
# quote.settlement — 今结算价
# 等待行情更新
while True:
api.wait_update()
if api.is_changing(quote, "last_price"):
print(f"Price update: {quote.last_price}")
K线数据
# 获取K线序列(返回pandas DataFrame)
klines = api.get_kline_serial(
"SHFE.cu2401",
duration_seconds=60, # K线周期:60=1分钟, 300=5分钟, 3600=1小时, 86400=日线
data_length=200 # 获取K线数量
)
# 列:datetime, open, high, low, close, volume, open_oi, close_oi
# 多周期
klines_1m = api.get_kline_serial("SHFE.cu2401", 60)
klines_5m = api.get_kline_serial("SHFE.cu2401", 300)
klines_1d = api.get_kline_serial("SHFE.cu2401", 86400)
Tick数据
ticks = api.get_tick_serial("SHFE.cu2401", data_length=500)
# 列:datetime, last_price, highest, lowest, bid_price1, ask_price1,
# bid_volume1, ask_volume1, volume, amount, open_interest
交易
下单
from tqsdk import TqApi, TqAuth
api = TqApi(auth=TqAuth("user", "pass"))
# 限价单 — buy open 2 lots
order = api.insert_order(
symbol="SHFE.cu2401",
direction="BUY", # "BUY" or "SELL"
offset="OPEN", # "OPEN", "CLOSE", "CLOSETODAY"
volume=2, # Number of lots
limit_price=68000.0 # Limit price (None for market order)
)
Metadata
AI Skill Finder
Not sure this is the right skill?
Describe what you want to build — we'll match you to the best skill from 16,000+ options.
Find the right skill Add to Configuration
Paste this into your clawhub.json to enable this plugin.
{
"plugins": {
"official-coderwpf-tqsdk": {
"enabled": true,
"auto_update": true
}
}
}Safety NoteClawKit audits metadata but not runtime behavior. Use with caution.
Related Skills
backtrader
Backtrader 开源量化回测框架 - 支持多数据源、多策略、多周期回测与实盘交易,纯Python实现。
coderwpf 3409
pywencai
同花顺问财自然语言数据查询工具 - 使用中文自然语言查询A股、指数、基金、港美股、可转债等市场数据。
coderwpf 3409
rqalpha
RQAlpha 米筐开源事件驱动回测框架 - 支持A股和期货,模块化架构,可自由扩展。
coderwpf 3409
tushare
Tushare Pro 金融大数据平台 - 提供A股、指数、基金、期货、债券、宏观数据,Token认证方式访问。
coderwpf 3409
baostock
BaoStock 免费A股数据平台 - 支持K线、财务数据、行业分类查询,无需注册即可使用。
coderwpf 3409