Skip to main content

tokease

灵鲸算力指数平台的 Python SDK。可以在本地查看查询参数,也可以用 API Key 把行情读成 pandas.DataFrame

服务地址固定为 https://whale.tokease.cn。平台上的字段说明见 用户手册 · SDK

安装

Python 3.10 及以上。pandas 随包安装,没有额外的可选依赖。

pip install tokease

API Key

查询必须带 Key。按下面的顺序取值:

  1. Client(api_key=...)
  2. 环境变量 TOKEASE_API_KEY
  3. 环境变量 WHALEINDEX_API_KEY

没有 Key 时抛出 TokeaseError,不会发请求。Key 无效或权限不足时抛出 TokeaseAPIError

import tokease

client = tokease.Client(api_key="tokease_xxxxxxxxxxxx_xxxxxxxx", timeout=30, max_retries=5)

timeout 默认 30 秒。遇到分钟限流(HTTP 429、业务码 42901)时最多再重试 max_retries 次,默认 5 次,退避为 2^次数 秒加少量随机抖动。每日额度用尽(42902)不会自动重试。

查看参数

这些函数不访问网络。

import tokease

tokease.list_datasets()
tokease.dataset_params("index_daily")
tokease.dataset_fields("index_daily")
tokease.describe("index_quote")
tokease.builtin_indices()
tokease.hardware_base_codes()

list_datasets() 只有 index_basicindex_dailyindex_latestindex_quotetrade_calendar。未知数据集、未知参数、未知字段会在本地拒绝。builtin_indices() 是内置默认目录;线上名称和排序以 index_basicsort_no 为准。

查询

五个方法都返回 DataFrame。日期可以写 YYYYMMDDYYYY-MM-DD,或传入 datetime.datetrade_date 不能和 start_date / end_date 一起用。

basic = client.index_basic()
latest = client.index_latest()
daily = client.index_daily(
    index_code="Server_Index",
    start_date="20260801",
    end_date="20260831",
    fields=["trade_date", "index_code", "index_value", "change_rate"],
)
quotes = client.index_quote(
    index_code="Server_Index",
    specification="B300",
    start_date="20260801",
    end_date="20260831",
)
calendar = client.trade_calendar(start_date="20260801", end_date="20260831")
方法 scope 参数
index_basic index:read index_code
index_daily index:read index_code,以及 trade_date 或日期区间
index_latest index:read index_code。省略时每个可见指数一条最新日线
index_quote quote:read 同日线,另加精确匹配的 specification
trade_calendar calendar:read trade_date 或日期区间

通用入口是 client.query(dataset, params, fields, limit=1000, offset=0, paginate=True)

fields 省略时返回该数据集的全部列。paginate=True 时按 has_more 自动取完。limit 必须在 1 到 5000 之间。省略 index_code 只返回该 Key 有权查看的指数。

DataFrame

  • trade_datebase_datedatetime64
  • calculated_at 是带 UTC 时区的时间。
  • 点位、价格、权重、涨跌是 float64change_rate0.0123 时表示上涨 1.23%。
  • sort_no 是可空整数,enabledis_workday 是可空布尔。
  • frame.attrs["dataset"] 是数据集名,frame.attrs["request_id"] 是最后一页的请求编号。

权限

SDK 使用 API Key,返回该 Key 允许看到的完整字段。网站上标成「仅公开点位」的指数,Key 具备对应 scope 时仍包含加权价格和规格报价。

  • 免费 Key 默认延迟 7 个交易日,且不能读取 Hardware_Index
  • 会员 Key 可以读取最新数据和会员指数。Hardware_Index 没有规格报价。
  • 新 Key 默认同时拥有 index:readquote:readcalendar:read
from tokease import TokeaseAPIError, TokeaseError

try:
    frame = client.index_daily(index_code="Server_Index", trade_date="20260820")
except TokeaseError as exc:
    print("参数或 Key 缺失:", exc)
except TokeaseAPIError as exc:
    print(exc.code, exc.status, exc.request_id)

发布

包版本等于仓库根目录 package.jsonversion。推送标签 v<version> 时,GitHub Actions 用 secret PYPI_API_TOKEN 发布到 PyPI。

Release files for tokease 1.14.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tokease 1.14.2
File Size Uploaded
tokease-1.14.2.tar.gz 17.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tokease 1.14.2
File Interpreter ABI Platform
tokease-1.14.2-py3-none-any.whl Python 3 none any Details

Total release size: 32.7 kB

Release files / tokease-1.14.2.tar.gz

Download URL tokease-1.14.2.tar.gz
Size 17.2 kB
Tags Source
SHA-256 checksum
How to use checksums
de87002325bb7ec11f7532c6b20c6e3bb2bbbf18c437a5656892dfbe37f2027a
BLAKE2b-256 checksum
How to use checksums
67bdb8ebb6d684a5b6010cfb8a7f7241a41a02b0ece35681ebca9d62d6804acd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / tokease-1.14.2-py3-none-any.whl

Download URL tokease-1.14.2-py3-none-any.whl
Size 15.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
047db9181a411ff8e9dd3cc1ba7f9dae882e4dc7b0e4c96ef86bb7dd7272a83b
BLAKE2b-256 checksum
How to use checksums
4bce15b4f0e5f26f4d07eadc76c7f3bf8e43c55383720f0dccb8c7654d9efa84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

1.14.2 This release

2 release files

1.14.1

2 release files

1.14.0

2 release files

1.13.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page