spike(s1): vnpy.alpha A 股支撑度 PASS(AlphaLab 存取 + ts_mean 因子计算)
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
"""S1: 验证 vnpy.alpha 能否处理 A 股数据(Phase 1 真实数据 → AlphaLab → 因子计算)。
|
||||
|
||||
验证链:
|
||||
1. AlphaLab init
|
||||
2. read_db_daily 读 A 股 bars(Phase 1)
|
||||
3. save_bar_data + load_bar_data(存取兼容性)
|
||||
4. 构造 panel df(datetime, vt_symbol, OHLCV)
|
||||
5. AlphaDataset + add_feature(ma5) + prepare_data(spawn pool 因子计算)
|
||||
6. fetch_raw(验证因子列生成)
|
||||
|
||||
A 股特性支撑度(T+1/涨跌停/停牌)在因子层层面:alpha 模块本身不强制这些约束,
|
||||
由策略/回测层处理;因子计算只需 OHLCV,A 股数据格式无差异。
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, "/app/vnpy_v4.4.0")
|
||||
sys.path.insert(0, "/app")
|
||||
|
||||
|
||||
def main():
|
||||
from vnpy.alpha.lab import AlphaLab
|
||||
from vnpy.alpha.dataset import AlphaDataset, Segment
|
||||
from vnpy.trader.constant import Interval
|
||||
import polars as pl
|
||||
|
||||
results = {}
|
||||
|
||||
# 1. AlphaLab init
|
||||
try:
|
||||
lab = AlphaLab("/tmp/alpha_lab_spike")
|
||||
results["alphalab_init"] = "OK"
|
||||
except Exception as e:
|
||||
results["alphalab_init"] = f"FAIL: {e}"
|
||||
return _print(results)
|
||||
|
||||
# 2. read A 股
|
||||
try:
|
||||
from sanguo_data.datareader import read_db_daily
|
||||
from sanguo_data.config import load_config
|
||||
cfg = load_config("/app/config/data_platform.yaml")
|
||||
bars = read_db_daily("600000", "2024-01-01", "2024-06-30", cfg)
|
||||
results["read_bars"] = f"OK ({len(bars)} bars)" if bars else "FAIL: 0 bars"
|
||||
if not bars:
|
||||
return _print(results)
|
||||
except Exception as e:
|
||||
import traceback; traceback.print_exc()
|
||||
results["read_bars"] = f"FAIL: {e}"
|
||||
return _print(results)
|
||||
|
||||
# 3. save + load
|
||||
try:
|
||||
lab.save_bar_data(bars)
|
||||
vt = bars[0].vt_symbol
|
||||
loaded = lab.load_bar_data(vt, Interval.DAILY, "2024-01-01", "2024-06-30")
|
||||
results["save_load"] = f"OK (vt={vt}, loaded={len(loaded)})"
|
||||
except Exception as e:
|
||||
import traceback; traceback.print_exc()
|
||||
results["save_load"] = f"FAIL: {e}"
|
||||
return _print(results)
|
||||
|
||||
# 4. panel df + AlphaDataset + add_feature + prepare + fetch
|
||||
try:
|
||||
df = pl.DataFrame({
|
||||
"datetime": [b.datetime.replace(tzinfo=None) for b in bars],
|
||||
"vt_symbol": [b.vt_symbol for b in bars],
|
||||
"open": [b.open_price for b in bars],
|
||||
"high": [b.high_price for b in bars],
|
||||
"low": [b.low_price for b in bars],
|
||||
"close": [b.close_price for b in bars],
|
||||
"volume": [b.volume for b in bars],
|
||||
"turnover": [b.turnover for b in bars],
|
||||
"open_interest": [b.open_interest for b in bars],
|
||||
})
|
||||
ds = AlphaDataset(df, ("2024-01-01", "2024-04-30"),
|
||||
("2024-05-01", "2024-05-15"),
|
||||
("2024-05-16", "2024-06-30"))
|
||||
ds.add_feature("ma5", "ts_mean(close, 5)")
|
||||
ds.prepare_data(max_workers=1)
|
||||
raw = ds.fetch_raw(Segment.TEST)
|
||||
has_ma5 = "ma5" in raw.columns
|
||||
results["factor_calc"] = f"OK (cols={raw.columns}, rows={raw.height}, ma5={has_ma5})"
|
||||
except Exception as e:
|
||||
import traceback; traceback.print_exc()
|
||||
results["factor_calc"] = f"FAIL: {e}"
|
||||
|
||||
_print(results)
|
||||
|
||||
|
||||
def _print(results):
|
||||
print("=== S1 Spike Result ===")
|
||||
for k, v in results.items():
|
||||
print(f" {k}: {v}")
|
||||
ok = all("OK" in v for v in results.values())
|
||||
print(f"\nS1 VERDICT: {'PASS' if ok else 'PARTIAL/FAIL'}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,27 @@
|
||||
"""S1 spike:vnpy.alpha A 股支撑度。
|
||||
|
||||
验证在 scripts/spike_s1_alpha_ashare.py(NAS 容器内运行,用 Phase 1 真实数据)。
|
||||
VERDICT: PASS —— vnpy.alpha 完整处理 A 股数据。
|
||||
|
||||
关键发现(修正后续 task):
|
||||
- AlphaLab 存的列名是 open/high/low/close(非 _price)→ data_adapter.py
|
||||
- 表达式语法是 ts_/cs_/math 算子 + 列名 DataProxy(非 pl.col)→ library.py
|
||||
- prepare_data 用 spawn pool(~11s/因子,spawn 开销)→ 编排层性能注意
|
||||
"""
|
||||
|
||||
|
||||
def test_s1_a_share_support_recorded():
|
||||
findings = {
|
||||
"alphalab_init": "OK",
|
||||
"data_convert_from_phase1": "OK (117 bars 600000.SSE 2024-01~06)",
|
||||
"save_load_roundtrip": "OK (vt=600000.SSE, 117 loaded)",
|
||||
"expression_factor_calc": "OK (ts_mean(close,5) → ma5 列, 31 rows in TEST segment)",
|
||||
"t_plus_1_support": "N/A (因子层不强制,由策略/回测层处理)",
|
||||
"price_limit_support": "N/A",
|
||||
"suspend_support": "N/A",
|
||||
}
|
||||
# S1 VERDICT: PASS
|
||||
assert findings["alphalab_init"] == "OK"
|
||||
assert findings["data_convert_from_phase1"].startswith("OK")
|
||||
assert findings["save_load_roundtrip"].startswith("OK")
|
||||
assert findings["expression_factor_calc"].startswith("OK")
|
||||
Reference in New Issue
Block a user