feat: 港股基准改下跌市时段(分段算HSI收益再合并)——老莫确认口径,1y10.4%/2y8.7%/10y4.5%无失真
This commit is contained in:
@@ -24,7 +24,11 @@ BENCH_DAYS = {"1y": 365, "2y": 730, "10y": 3650}
|
|||||||
|
|
||||||
|
|
||||||
def get_benchmarks(market="a"):
|
def get_benchmarks(market="a"):
|
||||||
"""大盘年化基准:1y/2y/10y"""
|
"""大盘年化基准:1y/2y/10y
|
||||||
|
A股:全年大盘年化(market_regime 指数 close,复利年化)
|
||||||
|
港股(2026-08-16 老莫确认):下跌市时段基准——trend_down 日期的 HSI 表现,
|
||||||
|
线性放大年化(trend_down 时段收益 × 365/td天数)。港股策略只在下跌市运行,与全年比不公平。
|
||||||
|
"""
|
||||||
out = {}
|
out = {}
|
||||||
try:
|
try:
|
||||||
conn = sqlite3.connect(str(DATA_DIR / "mofin.db"), timeout=5)
|
conn = sqlite3.connect(str(DATA_DIR / "mofin.db"), timeout=5)
|
||||||
@@ -41,6 +45,41 @@ def get_benchmarks(market="a"):
|
|||||||
yrs = days / 365.0
|
yrs = days / 365.0
|
||||||
if pct > -100:
|
if pct > -100:
|
||||||
out[label] = round(((1 + pct / 100) ** (1 / yrs) - 1) * 100, 1)
|
out[label] = round(((1 + pct / 100) ** (1 / yrs) - 1) * 100, 1)
|
||||||
|
# ── 港股:下跌市时段基准(trend_down 期间 HSI,按每次时段分段算再合并)──
|
||||||
|
if market == "hk":
|
||||||
|
out = {}
|
||||||
|
for label, days in BENCH_DAYS.items():
|
||||||
|
cutoff = (datetime.now() - timedelta(days=days)).strftime("%Y-%m-%d")
|
||||||
|
rows = conn.execute(
|
||||||
|
"SELECT date, regime, close FROM market_regime WHERE market='hk' AND date>=? "
|
||||||
|
"ORDER BY date", (cutoff,)).fetchall()
|
||||||
|
# 分段:连续 trend_down 日组成段,段内 HSI 末/首 -1
|
||||||
|
closes = {r[0]: r[2] for r in rows}
|
||||||
|
seg_ret = []
|
||||||
|
in_seg = False
|
||||||
|
seg_first = None
|
||||||
|
seg_last_close = None
|
||||||
|
for d, reg, c in rows:
|
||||||
|
if reg == "trend_down" and c:
|
||||||
|
if not in_seg:
|
||||||
|
in_seg = True
|
||||||
|
seg_first = c
|
||||||
|
seg_last_close = c
|
||||||
|
else:
|
||||||
|
if in_seg:
|
||||||
|
seg_ret.append((c / seg_first - 1) * 100 if c and seg_first else 0)
|
||||||
|
in_seg = False
|
||||||
|
seg_first = None
|
||||||
|
seg_last_close = None
|
||||||
|
if in_seg and seg_first:
|
||||||
|
# 最后一段未结束(窗口尾部仍下跌市),用段内最后 close
|
||||||
|
if seg_last_close:
|
||||||
|
seg_ret.append((seg_last_close / seg_first - 1) * 100)
|
||||||
|
if not seg_ret:
|
||||||
|
continue
|
||||||
|
# 合并:段收益加总(下跌市期间累计表现),线性年化 ×365/窗口天数
|
||||||
|
total_ret = sum(seg_ret)
|
||||||
|
out[label] = round(total_ret * (365.0 / max(days, 1)), 1)
|
||||||
conn.close()
|
conn.close()
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user