yingfeng64 Claude Sonnet 4.6 commited on
Commit
98c3a25
·
1 Parent(s): 47bccb9

Rewrite README with proper Markdown formatting

Browse files

Fix broken code fences, replace ASCII box tables with GFM tables,
add ts_code filter docs for cache endpoint, add volume response example.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. README.md +256 -56
README.md CHANGED
@@ -9,89 +9,256 @@ pinned: false
9
 
10
  # Kronos Stock Predictor API
11
 
12
- Monte-Carlo probabilistic stock forecasting powered by
13
- [Kronos](https://arxiv.org/abs/2508.02739) — Tsinghua University's open-source
14
- financial K-line foundation model.
15
 
16
- Data source: **Tushare Pro** (front-adjusted / qfq).
 
 
17
 
18
  ---
19
 
20
- ## Endpoints
21
 
22
- ### `POST /api/v1/predict`
 
 
 
 
 
 
 
23
 
24
- Submit a prediction job. Returns a `task_id` immediately.
25
 
26
- **Request body**
27
 
28
- | Field | Type | Default | Description |
29
- |-------|------|---------|-------------|
30
- | `ts_code` | string | — | Tushare stock code, e.g. `"600900.SH"` |
31
- | `lookback` | int | 512 | Historical bars to feed the model (1–512) |
32
- | `pred_len` | int | 5 | Future trading days to predict (1–60) |
33
- | `sample_count` | int | 30 | MC sampling iterations (1–100) |
34
- | `mode` | string | `"simple"` | `"simple"` or `"advanced"` |
35
- | `include_volume` | bool | false | Include volume CI in `advanced` mode |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  ```json
38
  {
39
- "ts_code": "600900.SH",
40
- "lookback": 512,
41
  "pred_len": 5,
42
- "sample_count": 30,
43
- "mode": "simple"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  }
45
  ```
46
 
47
- **Response**
 
 
 
 
 
 
 
48
 
49
  ```json
50
- { "task_id": "550e8400-e29b-41d4-a716-446655440000" }
 
 
 
 
 
 
 
51
  ```
52
 
53
  ---
54
 
55
- ### `GET /api/v1/predict/{task_id}`
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
- Poll for results.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  ```json
60
  {
 
61
  "status": "done",
62
- "error": null,
63
- "result": {
64
- "ts_code": "600900.SH",
65
- "base_date": "2026-03-13",
66
- "pred_len": 5,
67
- "confidence": 95,
68
- "confidence_warning": false,
69
- "direction": { "signal": "bullish", "probability": 0.73 },
70
- "summary": {
71
- "mean_close": 27.05,
72
- "range_low": 25.80,
73
- "range_high": 28.30,
74
- "range_width": 2.50
75
- },
76
- "bands": [
77
- {
78
- "date": "2026-03-14",
79
- "step": 1,
80
- "mean_close": 26.88,
81
- "trading_low": 26.20,
82
- "trading_high": 27.55,
83
- "uncertainty": 0.0504
84
- }
85
- ]
86
- }
87
  }
88
  ```
89
 
90
- `status` is one of `"pending"` / `"done"` / `"failed"`.
 
 
 
 
 
91
 
92
  ---
93
 
94
- ### `GET /health`
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  ```json
97
  { "status": "ok" }
@@ -99,9 +266,42 @@ Poll for results.
99
 
100
  ---
101
 
102
- ## Notes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
- - **Direction signal**: based on the last predicted close vs. the last historical close across all MC samples.
105
- - **95 % trading band**: `trading_low` = q2.5 of daily predicted lows; `trading_high` = q97.5 of daily predicted highs.
106
- - **`confidence_warning: true`** when `pred_len > 30` (model uncertainty grows significantly beyond ~30 days).
107
- - CPU inference: ~3–5 s/sample → 30 samples ≈ 2–5 min. Consider selecting GPU hardware for production use.
 
9
 
10
  # Kronos Stock Predictor API
11
 
12
+ 基于[清华大学 Kronos 金融 K 线基础大模型](https://arxiv.org/abs/2508.02739)的 A 股概率预测 REST API。
 
 
13
 
14
+ - **数据源**:Tushare Pro,前复权(qfq
15
+ - **推理方式**:蒙特卡洛多次采样,输出预测方向、置信度及 95% 交易区间
16
+ - **异步任务**:POST 提交 → 返回 `task_id` → GET 轮询结果
17
 
18
  ---
19
 
20
+ ## 模型信息
21
 
22
+ | 项目 | 值 |
23
+ |---|---|
24
+ | 模型 | [NeoQuasar/Kronos-base](https://huggingface.co/NeoQuasar/Kronos-base) |
25
+ | 参数量 | 102.3M |
26
+ | 架构 | Decoder-only Transformer |
27
+ | 最大上下文 | 512 根 K 线 |
28
+ | 预训练数据 | 全球 45 个交易所,120 亿根 K 线 |
29
+ | 推理设备 | CUDA(若可用)/ CPU fallback |
30
 
31
+ ---
32
 
33
+ ## 快速开始
34
 
35
+ ### 1. 提交预测任务
36
+
37
+ ```bash
38
+ curl -X POST "https://yingfeng64-kronos-api.hf.space/api/v1/predict" \
39
+ -H "Content-Type: application/json" \
40
+ -d '{
41
+ "ts_code": "000063.SZ",
42
+ "lookback": 512,
43
+ "pred_len": 5,
44
+ "sample_count": 30,
45
+ "mode": "simple"
46
+ }'
47
+ ```
48
+
49
+ ```json
50
+ { "task_id": "550e8400-e29b-41d4-a716-446655440000" }
51
+ ```
52
+
53
+ ### 2. 轮询结果
54
+
55
+ ```bash
56
+ curl "https://yingfeng64-kronos-api.hf.space/api/v1/predict/550e8400-e29b-41d4-a716-446655440000"
57
+ ```
58
+
59
+ `status` 为 `"pending"` 时继续等待,变为 `"done"` 或 `"failed"` 后读取结果。
60
+
61
+ ### Python 一键轮询版
62
+
63
+ ```python
64
+ import time, requests
65
+
66
+ BASE = "https://yingfeng64-kronos-api.hf.space"
67
+
68
+ resp = requests.post(f"{BASE}/api/v1/predict", json={
69
+ "ts_code": "000063.SZ",
70
+ "lookback": 512,
71
+ "pred_len": 5,
72
+ "sample_count": 30,
73
+ })
74
+ task_id = resp.json()["task_id"]
75
+
76
+ while True:
77
+ r = requests.get(f"{BASE}/api/v1/predict/{task_id}").json()
78
+ if r["status"] in ("done", "failed"):
79
+ break
80
+ time.sleep(10)
81
+
82
+ print(r["result"])
83
+ ```
84
+
85
+ ---
86
+
87
+ ## API 文档
88
+
89
+ ### `POST /api/v1/predict` — 提交单个预测任务
90
+
91
+ **请求体**
92
+
93
+ | 字段 | 类型 | 默认值 | 范围 | 说明 |
94
+ |---|---|---|---|---|
95
+ | `ts_code` | string | — | — | Tushare 股票代码,如 `"000063.SZ"` |
96
+ | `lookback` | int | 512 | 20–512 | 回看历史 K 线根数 |
97
+ | `pred_len` | int | 5 | 1–60 | 预测未来交易日数(建议 ≤ 30) |
98
+ | `sample_count` | int | 30 | 1–100 | MC 蒙特卡洛采样次数 |
99
+ | `mode` | string | `"simple"` | `simple` / `advanced` | 返回字段详细程度 |
100
+ | `include_volume` | bool | false | — | `advanced` 模式下是否返回成交量预测 |
101
+
102
+ **响应(`mode=simple`)**
103
 
104
  ```json
105
  {
106
+ "ts_code": "000063.SZ",
107
+ "base_date": "2026-03-13",
108
  "pred_len": 5,
109
+ "confidence": 95,
110
+ "confidence_warning": false,
111
+ "cached": false,
112
+ "cache_expires_at": "2026-03-16 15:00:00 UTC+08:00",
113
+ "direction": {
114
+ "signal": "bearish",
115
+ "probability": 0.4333
116
+ },
117
+ "summary": {
118
+ "mean_close": 36.35,
119
+ "range_low": 30.60,
120
+ "range_high": 40.01,
121
+ "range_width": 9.41
122
+ },
123
+ "bands": [
124
+ {
125
+ "date": "2026-03-16",
126
+ "step": 1,
127
+ "mean_close": 36.10,
128
+ "trading_low": 34.46,
129
+ "trading_high": 37.32,
130
+ "uncertainty": 0.0785
131
+ }
132
+ ]
133
  }
134
  ```
135
 
136
+ **`mode=advanced` 在每个 `bands` 条目中追加**
137
+
138
+ | 字段 | 说明 |
139
+ |---|---|
140
+ | `mean_open` / `mean_high` / `mean_low` | 预测开/高/低价均值 |
141
+ | `close_ci_low` / `close_ci_high` | 收盘价 95% 置信区间 |
142
+
143
+ 当 `include_volume: true` 时额外返回顶层 `volume` 数组:
144
 
145
  ```json
146
+ "volume": [
147
+ {
148
+ "date": "2026-03-16",
149
+ "mean_volume": 1050000,
150
+ "volume_ci_low": 820000,
151
+ "volume_ci_high": 1280000
152
+ }
153
+ ]
154
  ```
155
 
156
  ---
157
 
158
+ ### `GET /api/v1/predict/{task_id}` — 查询单个任务结果
159
+
160
+ **响应**
161
+
162
+ | `status` | 含义 |
163
+ |---|---|
164
+ | `pending` | 正在计算 |
165
+ | `done` | 完成,`result` 字段包含预测数据 |
166
+ | `failed` | 失败,`error` 字段包含错误信息 |
167
+
168
+ ---
169
+
170
+ ### `POST /api/v1/predict/batch` — 批量提交
171
+
172
+ 一次提交最多 20 个预测请求,所有子任务并发入队。每个��任务参数与单个预测相同。
173
 
174
+ ```bash
175
+ curl -X POST "https://yingfeng64-kronos-api.hf.space/api/v1/predict/batch" \
176
+ -H "Content-Type: application/json" \
177
+ -d '{
178
+ "requests": [
179
+ {"ts_code": "000063.SZ", "pred_len": 5, "sample_count": 30},
180
+ {"ts_code": "600900.SH", "pred_len": 5, "sample_count": 30},
181
+ {"ts_code": "000001.SZ", "pred_len": 5, "sample_count": 30}
182
+ ]
183
+ }'
184
+ ```
185
+
186
+ ```json
187
+ {
188
+ "batch_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
189
+ "task_ids": ["aaa...", "bbb...", "ccc..."]
190
+ }
191
+ ```
192
+
193
+ ---
194
+
195
+ ### `GET /api/v1/predict/batch/{batch_id}` — 查询批量进度
196
 
197
  ```json
198
  {
199
+ "batch_id": "xxxxxxxx-...",
200
  "status": "done",
201
+ "total": 3,
202
+ "done": 3,
203
+ "failed": 0,
204
+ "tasks": [
205
+ {
206
+ "task_id": "aaa...",
207
+ "status": "done",
208
+ "result": { ... },
209
+ "error": null
210
+ }
211
+ ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212
  }
213
  ```
214
 
215
+ | `status` | 含义 |
216
+ |---|---|
217
+ | `pending` | 仍有子任务在计算 |
218
+ | `done` | 全部成功 |
219
+ | `partial` | 部分成功、部分失败 |
220
+ | `failed` | 全部失败 |
221
 
222
  ---
223
 
224
+ ### `GET /api/v1/cache` — 查看缓存状态
225
+
226
+ 列出有效缓存条目及剩余 TTL。
227
+
228
+ | 参数 | 说明 |
229
+ |---|---|
230
+ | `ts_code`(可选) | 只返回该股票的缓存,不传则返回全部 |
231
+
232
+ ```bash
233
+ # 查某只股票
234
+ curl "https://yingfeng64-kronos-api.hf.space/api/v1/cache?ts_code=000063.SZ"
235
+
236
+ # 查全部
237
+ curl "https://yingfeng64-kronos-api.hf.space/api/v1/cache"
238
+ ```
239
+
240
+ ```json
241
+ {
242
+ "count": 1,
243
+ "entries": [
244
+ {
245
+ "ts_code": "000063.SZ",
246
+ "lookback": 512,
247
+ "pred_len": 5,
248
+ "sample_count": 30,
249
+ "mode": "simple",
250
+ "include_volume": false,
251
+ "cached_at": "2026-03-16 10:23:00 UTC+08:00",
252
+ "expires_at": "2026-03-16 15:00:00 UTC+08:00",
253
+ "ttl_seconds": 17220
254
+ }
255
+ ]
256
+ }
257
+ ```
258
+
259
+ ---
260
+
261
+ ### `GET /health` — 健康检查
262
 
263
  ```json
264
  { "status": "ok" }
 
266
 
267
  ---
268
 
269
+ ## 响应字段说明
270
+
271
+ | 字段 | 含义 |
272
+ |---|---|
273
+ | `base_date` | 预测所基于的最后一个历史 K 线日期 |
274
+ | `direction.signal` | `"bullish"` / `"bearish"`,MC 样本中末日收盘价 > 当前收盘的比例决定 |
275
+ | `direction.probability` | 看涨概率(0–1) |
276
+ | `trading_low` | 该日预测最低价的 q2.5 分位数(95% 交易区间下沿) |
277
+ | `trading_high` | 该日预测最高价的 q97.5 分位数(95% 交易区间上沿) |
278
+ | `uncertainty` | `(trading_high − trading_low) / last_close`,无量纲不确定性 |
279
+ | `confidence_warning` | `pred_len > 30` 时为 `true`,表示预测不确定性较大 |
280
+ | `cached` | 结果是否来自缓存 |
281
+ | `cache_expires_at` | 缓存失效时间(北京时间) |
282
+
283
+ ---
284
+
285
+ ## 缓存机制
286
+
287
+ 缓存 key 由 `(ts_code, lookback, pred_len, sample_count, mode, include_volume)` 六元组构成,失效时机为下一个 A 股交易日收盘(15:00 CST)。
288
+
289
+ | 请求时间(CST) | 缓存过期时间 |
290
+ |---|---|
291
+ | 工作日 00:00–15:00 | 当日 15:00 CST |
292
+ | 工作日 15:00 后 | 次个工作日 15:00 CST |
293
+ | 周末 | 下周一 15:00 CST |
294
+ | 法定节假日 | 不特殊处理(节假日无新数据,缓存命中结果正确) |
295
+
296
+ 缓存命中时响应时间 < 100ms,无需等待模型推理。
297
+
298
+ ---
299
+
300
+ ## 性能参考
301
+
302
+ | 环境 | 单次采样 | 30 次 MC 总耗时 |
303
+ |---|---|---|
304
+ | RTX 3090 (CUDA) | ~230ms | ~7s |
305
+ | CPU only | ~3–5s | ~2–5min |
306
 
307
+ > 推荐选用 GPU 硬件以获得实用响应速度。首次冷启动需从 HuggingFace Hub 下载模型权重,约需 1–2 分钟。