庫存分析的三維框架:
- 方向(Direction):庫存是上升還是下降
- 速度(Speed):每週流出量
drain_rate(t) = -Δ1(t)
- 加速度(Acceleration):流出速度的變化
Δ2(t) = drain_rate(t) - drain_rate(t-1)
當 drain_rate > 0 且 Δ2 > 0 時,表示「庫存正在流出,且流出速度在加快」——這是晚期供給訊號的核心特徵。
使用歷史視窗(建議 3~5 年)計算 Z 分數:
z_drain(t) = (drain_rate(t) - mean) / std
z_accel(t) = (Δ2(t) - mean) / std
門檻判定:
| 指標 |
門檻 |
意義 |
| z_drain |
≤ -1.5 |
流出速度顯著大於常態 |
| z_accel |
≥ +1.0 |
流出正在加速 |
| level_percentile |
≤ 0.20 |
庫存處於歷史低檔 |
|
|
|
把推文敘事轉為可執行規則:
| 條件 |
描述 |
單獨成立 |
組合效果 |
| A. Level |
庫存水位 < 20% 歷史分位 |
WATCH |
- |
| B. Speed |
z_drain ≤ -1.5 |
WATCH |
B+C → MEDIUM |
| C. Acceleration |
z_accel ≥ +1.0 |
WATCH |
A+B+C → HIGH |
訊號分級:
HIGH_LATE_STAGE_SUPPLY_SIGNAL:A+B+C 同時成立
MEDIUM_SUPPLY_TIGHTENING:(B+C) 或 (A+B) 成立
WATCH:任一條件成立
NO_SIGNAL:無異常
主要數據來源:
- CEIC Data:上海期貨交易所白銀倉單數據
- URL:
https://www.ceicdata.com/zh-hans/china/shanghai-futures-exchange-commodity-futures-stock/cn-warehouse-stock-shanghai-future-exchange-silver
- 數據範圍:2012-07-02 至今(約 3,300+ 觀測值)
- 更新頻率:每日
- 歷史最高:3,091 噸 (2021-01-12)
重要提醒:
- 這是「交易所可交割/倉單」口徑,不等於全中國社會庫存
- 單週跳動可能反映倉儲規則變動或搬倉,需平滑處理
- 使用 Selenium 模擬人類瀏覽器抓取 SVG 圖表,遵循反偵測策略
- 數據採集:抓取 SGE/SHFE 週報庫存數據
- 三維量化:計算方向、速度、加速度
- 標準化判斷:使用 Z 分數判定異常
- 訊號生成:輸出晚期供給訊號分級
- 市場交叉驗證(選配):COMEX、ETF、現貨溢價
輸出:庫存水位、耗盡速度、加速度、Z 分數、訊號分級、敘事解讀。
最快的方式:檢查上海白銀庫存耗盡狀態
cd skills/detect-shanghai-silver-stock-drain
# 首次使用:安裝依賴
pip install pandas numpy selenium webdriver-manager matplotlib
# 1. 抓取最新數據(5 年歷史,約 200+ 週)
python scripts/fetch_shfe_stock.py --force-update
# 2. 執行快速檢查
python scripts/drain_detector.py --quick
輸出範例:
{
"as_of": "2026-01-16",
"signal": "MEDIUM_SUPPLY_TIGHTENING",
"latest_combined_stock_tonnes": 1133.3,
"level_percentile": 0.12,
"z_drain_rate": -2.1,
"z_acceleration": 1.4
}
完整分析 + 視覺化報告:
# 1. 執行完整分析
python scripts/drain_detector.py \
--start 2020-01-01 \
--end 2026-01-16 \
--output result.json
# 2. 生成視覺化報告
python scripts/visualize_drain.py \
--result result.json \
--output ../../../output/
- 快速檢查 - 查看目前上海白銀庫存耗盡狀態與訊號
- 完整分析 - 執行完整的歷史庫存分析與趨勢計算
- 數據更新 - 抓取最新的 SGE/SHFE 庫存數據
- 交叉驗證 - 使用 COMEX、ETF 等指標交叉驗證
- 方法論學習 - 了解三維度量化與訊號判定邏輯
請選擇或直接提供分析參數。
路由後,閱讀對應文件並執行。
方法論: references/methodology.md
- 三維度量化邏輯(方向、速度、加速度)
- Z 分數標準化
- 三段式訊號判定
資料來源: references/data-sources.md
- SGE 行情周報 PDF 抓取
- SHFE 倉單/庫存周報抓取
- Selenium 反偵測策略
輸入參數: references/input-schema.md
| Script |
Command |
Purpose |
| drain_detector.py |
--quick |
快速檢查耗盡狀態 |
| drain_detector.py |
--start DATE --end DATE --output FILE |
完整歷史分析 |
| fetch_sge_stock.py |
--output sge_stock.csv |
抓取 SGE 庫存 |
| fetch_shfe_stock.py |
--output shfe_stock.csv |
抓取 SHFE 庫存 |
| visualize_drain.py |
--result result.json --output DIR |
生成視覺化報告 |
|
|
|
核心參數
| 參數 |
類型 |
預設值 |
說明 |
| start_date |
string |
3Y 前 |
分析起始日 (YYYY-MM-DD) |
| end_date |
string |
today |
分析結束日 (YYYY-MM-DD) |
| frequency |
string |
weekly |
數據頻率 (weekly/daily) |
| include_sources |
array |
["SGE","SHFE"] |
納入的庫存來源 |
| unit |
string |
tonnes |
單位 (tonnes/kg/troy_oz) |
分析參數
| 參數 |
類型 |
預設值 |
說明 |
| smoothing_window_weeks |
int |
4 |
平滑視窗(週) |
| drain_threshold_z |
float |
-1.5 |
異常耗盡 Z 分數門檻 |
| accel_threshold_z |
float |
+1.0 |
耗盡加速 Z 分數門檻 |
| confirm_with_markets |
bool |
true |
是否做市場側交叉驗證 |
完整參數定義見 references/input-schema.md。
完整輸出結構見 templates/output-json.md。
1---2name: detect-shanghai-silver-stock-drain3description: 以公開交易所庫存資料為核心,量化上海白銀庫存耗盡的方向、速度與加速度,並將其轉成可交易的供給緊縮訊號。4---5
6<essential_principles>
7
8<principle name="drain_metrics_core">
9**庫存耗盡三維度量化**
10
11庫存分析的三維框架:
12- **方向(Direction)**:庫存是上升還是下降
13- **速度(Speed)**:每週流出量 `drain_rate(t) = -Δ1(t)`
14- **加速度(Acceleration)**:流出速度的變化 `Δ2(t) = drain_rate(t) - drain_rate(t-1)`
15
16當 `drain_rate > 0` 且 `Δ2 > 0` 時,表示「庫存正在流出,且流出速度在加快」——這是晚期供給訊號的核心特徵。
17</principle>
18
19<principle name="z_score_standardization">
20**Z 分數標準化判斷**
21
22使用歷史視窗(建議 3~5 年)計算 Z 分數:
23- `z_drain(t) = (drain_rate(t) - mean) / std`
24- `z_accel(t) = (Δ2(t) - mean) / std`
25
26門檻判定:
27| 指標 | 門檻 | 意義 |
28|------------------|--------|----------------------|
29| z_drain | ≤ -1.5 | 流出速度顯著大於常態 |
30| z_accel | ≥ +1.0 | 流出正在加速 |
31| level_percentile | ≤ 0.20 | 庫存處於歷史低檔 |
32</principle>
33
34<principle name="three_stage_signal">
35**三段式晚期訊號判定**
36
37把推文敘事轉為可執行規則:
38
39| 條件 | 描述 | 單獨成立 | 組合效果 |
40|-----------------|-------------------------|----------|--------------|
41| A. Level | 庫存水位 < 20% 歷史分位 | WATCH | - |
42| B. Speed | z_drain ≤ -1.5 | WATCH | B+C → MEDIUM |
43| C. Acceleration | z_accel ≥ +1.0 | WATCH | A+B+C → HIGH |
44
45**訊號分級**:
46- `HIGH_LATE_STAGE_SUPPLY_SIGNAL`:A+B+C 同時成立
47- `MEDIUM_SUPPLY_TIGHTENING`:(B+C) 或 (A+B) 成立
48- `WATCH`:任一條件成立
49- `NO_SIGNAL`:無異常
50</principle>
51
52<principle name="data_sources">
53**資料來源與口徑**
54
55主要數據來源:
56- **CEIC Data**:上海期貨交易所白銀倉單數據
57 - URL: `https://www.ceicdata.com/zh-hans/china/shanghai-futures-exchange-commodity-futures-stock/cn-warehouse-stock-shanghai-future-exchange-silver`
58 - 數據範圍:2012-07-02 至今(約 3,300+ 觀測值)
59 - 更新頻率:每日
60 - 歷史最高:3,091 噸 (2021-01-12)
61
62**重要提醒**:
63- 這是「交易所可交割/倉單」口徑,不等於全中國社會庫存
64- 單週跳動可能反映倉儲規則變動或搬倉,需平滑處理
65- 使用 Selenium 模擬人類瀏覽器抓取 SVG 圖表,遵循反偵測策略
66</principle>
67
68</essential_principles>
69
70<objective>
71監控上海白銀庫存(SGE + SHFE)的耗盡狀態:
72
731. **數據採集**:抓取 SGE/SHFE 週報庫存數據
742. **三維量化**:計算方向、速度、加速度
753. **標準化判斷**:使用 Z 分數判定異常
764. **訊號生成**:輸出晚期供給訊號分級
775. **市場交叉驗證**(選配):COMEX、ETF、現貨溢價
78
79輸出:庫存水位、耗盡速度、加速度、Z 分數、訊號分級、敘事解讀。
80</objective>
81
82<quick_start>
83
84**最快的方式:檢查上海白銀庫存耗盡狀態**
85
86```bash
87cd skills/detect-shanghai-silver-stock-drain
88
89# 首次使用:安裝依賴
90pip install pandas numpy selenium webdriver-manager matplotlib
91
92# 1. 抓取最新數據(5 年歷史,約 200+ 週)
93python scripts/fetch_shfe_stock.py --force-update
94
95# 2. 執行快速檢查
96python scripts/drain_detector.py --quick
97```
98
99輸出範例:
100```json
101{
102 "as_of": "2026-01-16",
103 "signal": "MEDIUM_SUPPLY_TIGHTENING",
104 "latest_combined_stock_tonnes": 1133.3,
105 "level_percentile": 0.12,
106 "z_drain_rate": -2.1,
107 "z_acceleration": 1.4
108}
109```
110
111**完整分析 + 視覺化報告**:
112```bash
113# 1. 執行完整分析
114python scripts/drain_detector.py \
115 --start 2020-01-01 \
116 --end 2026-01-16 \
117 --output result.json
118
119# 2. 生成視覺化報告
120python scripts/visualize_drain.py \
121 --result result.json \
122 --output ../../../output/
123```
124
125</quick_start>
126
127<intake>
128需要進行什麼操作?
129
1301. **快速檢查** - 查看目前上海白銀庫存耗盡狀態與訊號
1312. **完整分析** - 執行完整的歷史庫存分析與趨勢計算
1323. **數據更新** - 抓取最新的 SGE/SHFE 庫存數據
1334. **交叉驗證** - 使用 COMEX、ETF 等指標交叉驗證
1345. **方法論學習** - 了解三維度量化與訊號判定邏輯
135
136**請選擇或直接提供分析參數。**
137</intake>
138
139<routing>
140| Response | Action |
141|--------------------------------|--------------------------------------------|
142| 1, "快速", "quick", "check" | 執行 `scripts/drain_detector.py --quick` |
143| 2, "分析", "analyze", "full" | 閱讀 `workflows/analyze.md` 並執行 |
144| 3, "更新", "fetch", "data" | 閱讀 `workflows/fetch-data.md` 並執行 |
145| 4, "驗證", "validate", "cross" | 閱讀 `workflows/cross-validate.md` 並執行 |
146| 5, "學習", "方法論", "why" | 閱讀 `references/methodology.md` |
147| 提供日期參數 | 閱讀 `workflows/analyze.md` 並使用參數執行 |
148
149**路由後,閱讀對應文件並執行。**
150</routing>
151
152<directory_structure>
153```
154detect-shanghai-silver-stock-drain/
155├── SKILL.md # 本文件(路由器)
156├── skill.yaml # 前端展示元數據
157├── manifest.json # 技能元數據
158├── workflows/
159│ ├── analyze.md # 完整庫存分析工作流
160│ ├── fetch-data.md # 數據抓取工作流
161│ └── cross-validate.md # 交叉驗證工作流
162├── references/
163│ ├── data-sources.md # SGE/SHFE 資料來源說明
164│ ├── methodology.md # 三維度量化方法論
165│ └── input-schema.md # 完整輸入參數定義
166├── templates/
167│ ├── output-json.md # JSON 輸出模板
168│ └── output-markdown.md # Markdown 報告模板
169└── scripts/
170 ├── drain_detector.py # 主偵測腳本
171 ├── fetch_sge_stock.py # SGE 庫存抓取(PDF)
172 ├── fetch_shfe_stock.py # SHFE 庫存抓取
173 └── visualize_drain.py # 視覺化報告生成
174```
175</directory_structure>
176
177<reference_index>
178
179**方法論**: references/methodology.md
180- 三維度量化邏輯(方向、速度、加速度)
181- Z 分數標準化
182- 三段式訊號判定
183
184**資料來源**: references/data-sources.md
185- SGE 行情周報 PDF 抓取
186- SHFE 倉單/庫存周報抓取
187- Selenium 反偵測策略
188
189**輸入參數**: references/input-schema.md
190- 完整參數定義
191- 預設值與建議範圍
192
193</reference_index>
194
195<workflows_index>
196| Workflow | Purpose | 使用時機 |
197|-------------------|--------------|--------------------------|
198| analyze.md | 完整庫存分析 | 需要完整歷史分析時 |
199| fetch-data.md | 數據抓取 | 更新 SGE/SHFE 庫存數據 |
200| cross-validate.md | 交叉驗證訊號 | 確認供給緊縮訊號真實性時 |
201</workflows_index>
202
203<templates_index>
204| Template | Purpose |
205|--------------------|-------------------|
206| output-json.md | JSON 輸出結構定義 |
207| output-markdown.md | Markdown 報告模板 |
208</templates_index>
209
210<scripts_index>
211| Script | Command | Purpose |
212|---------------------|-----------------------------------------|------------------|
213| drain_detector.py | `--quick` | 快速檢查耗盡狀態 |
214| drain_detector.py | `--start DATE --end DATE --output FILE` | 完整歷史分析 |
215| fetch_sge_stock.py | `--output sge_stock.csv` | 抓取 SGE 庫存 |
216| fetch_shfe_stock.py | `--output shfe_stock.csv` | 抓取 SHFE 庫存 |
217| visualize_drain.py | `--result result.json --output DIR` | 生成視覺化報告 |
218</scripts_index>
219
220<input_schema_summary>
221
222**核心參數**
223
224| 參數 | 類型 | 預設值 | 說明 |
225|-----------------|--------|----------------|--------------------------|
226| start_date | string | 3Y 前 | 分析起始日 (YYYY-MM-DD) |
227| end_date | string | today | 分析結束日 (YYYY-MM-DD) |
228| frequency | string | weekly | 數據頻率 (weekly/daily) |
229| include_sources | array | ["SGE","SHFE"] | 納入的庫存來源 |
230| unit | string | tonnes | 單位 (tonnes/kg/troy_oz) |
231
232**分析參數**
233
234| 參數 | 類型 | 預設值 | 說明 |
235|------------------------|-------|--------|----------------------|
236| smoothing_window_weeks | int | 4 | 平滑視窗(週) |
237| drain_threshold_z | float | -1.5 | 異常耗盡 Z 分數門檻 |
238| accel_threshold_z | float | +1.0 | 耗盡加速 Z 分數門檻 |
239| confirm_with_markets | bool | true | 是否做市場側交叉驗證 |
240
241完整參數定義見 `references/input-schema.md`。
242
243</input_schema_summary>
244
245<output_schema_summary>
246```json
247{
248 "skill": "detect_shanghai_silver_stock_drain",
249 "as_of": "2026-01-16",
250 "unit": "tonnes",
251 "sources": ["SGE", "SHFE"],
252 "latest_combined_stock": 1133.3,
253 "level_percentile": 0.12,
254 "recent_4w_avg_drawdown": 58.4,
255 "drawdown_acceleration": 9.7,
256 "z_scores": {
257 "z_drain_rate": -2.1,
258 "z_acceleration": 1.4
259 },
260 "signal": "HIGH_LATE_STAGE_SUPPLY_SIGNAL",
261 "narrative": [...],
262 "caveats": [...]
263}
264```
265
266完整輸出結構見 `templates/output-json.md`。
267</output_schema_summary>
268
269<success_criteria>
270執行成功時應產出:
271
272- [ ] 上海合併庫存水位(SGE + SHFE)
273- [ ] 庫存水位歷史分位數
274- [ ] 近 N 週平均流出速度
275- [ ] 流出加速度
276- [ ] z_drain_rate 與 z_acceleration
277- [ ] 訊號分級(HIGH/MEDIUM/WATCH/NO_SIGNAL)
278- [ ] 敘事解讀(中文)
279- [ ] 數據口徑與限制說明
280</success_criteria>