ClawKit Logo
ClawKitReliability Toolkit
Back to Registry
Official Verified

tqsdk

天勤量化TqSdk - 开源Python期货/期权交易SDK,提供实时行情、回测和实盘交易功能。

skill-install — Terminal

Install via CLI (Recommended)

clawhub install openclaw/skills/skills/coderwpf/tqsdk
Or

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
交易所代码示例
上海期货交易所SHFESHFE.cu2401(铜)
大连商品交易所DCEDCE.m2405(豆粕)
郑州商品交易所CZCECZCE.CF405(棉花)
中国金融期货交易所CFFEXCFFEX.IF2401(沪深300期货)
上海国际能源交易中心INEINE.sc2407(原油)
广州期货交易所GFEXGFEX.si2407(工业硅)
上交所期权SSESSE.10004816(50ETF期权)
深交所期权SZSESZSE.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

Author@coderwpf
Stars3409
Views0
Updated2026-03-25
View Author Profile
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.