108 lines
3.6 KiB
Python
108 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
||
"""refresh_macro_context.py — 刷新macro_context_log
|
||
|
||
读取最新market_snapshots + 腾讯实时指数, 写入macro_context_log
|
||
让load_macro_context()拿到最新市场偏向, 而不是12天前的数据
|
||
|
||
每30分钟跑一次(交易日)
|
||
"""
|
||
import json, sqlite3, sys
|
||
from pathlib import Path
|
||
from datetime import datetime
|
||
from mo_data import get_price
|
||
|
||
# ── 消息通道统一路由(broadcast/xmpp by delivery) ──
|
||
try:
|
||
from messenger import install_stdio_hook as _msh
|
||
_msh()
|
||
except Exception:
|
||
pass
|
||
|
||
DB = Path("/home/hmo/MoFin/data/mofin.db")
|
||
|
||
def fetch_index(code, name):
|
||
"""从统一入口获取指数行情"""
|
||
try:
|
||
price, change_pct = get_price(code)
|
||
if price is not None:
|
||
return {
|
||
"price": price,
|
||
"change_pct": round(change_pct or 0, 2),
|
||
"high": price,
|
||
"low": price,
|
||
}
|
||
return None
|
||
except:
|
||
return None
|
||
|
||
def main():
|
||
# 采集各指数
|
||
indices = {
|
||
"上证指数": fetch_index("sh000001", "上证指数"),
|
||
"深证成指": fetch_index("sz399001", "深证成指"),
|
||
"创业板指": fetch_index("sz399006", "创业板指"),
|
||
"恒生指数": fetch_index("hkHSI", "恒生指数"),
|
||
"国企指数": fetch_index("hkHSCEI", "国企指数"),
|
||
}
|
||
indices = {k: v for k, v in indices.items() if v}
|
||
|
||
# 计算偏向
|
||
sh = indices.get("上证指数", {})
|
||
sh_change = sh.get("change_pct", 0) if sh else 0
|
||
# 2026-08-19 修复:大盘描述用 market_regime 真实温区(不写死"震荡")
|
||
# 原逻辑:涨跌±1.5%内→永远"大盘震荡"(每30min一条无意义通知,老莫)
|
||
# 温区映射:trend_up=强势 / trend_down=弱势 / choppy=震荡(真实ADX判定)
|
||
_regime = "unknown"
|
||
try:
|
||
import sqlite3 as _sq3
|
||
_c3 = _sq3.connect(str(DB), timeout=5)
|
||
_r3 = _c3.execute("SELECT regime FROM market_regime WHERE market='a' ORDER BY date DESC LIMIT 1").fetchone()
|
||
_c3.close()
|
||
if _r3:
|
||
_regime = _r3[0] or "unknown"
|
||
except Exception:
|
||
pass
|
||
if _regime == "trend_up":
|
||
overall = "bullish"
|
||
desc = "大盘强势(趋势市)"
|
||
elif _regime == "trend_down":
|
||
overall = "bearish"
|
||
desc = "大盘弱势(下跌趋势)"
|
||
elif _regime == "choppy":
|
||
overall = "neutral"
|
||
desc = "大盘震荡(ADX<20)"
|
||
else:
|
||
# 温区不可用 → 退回涨跌幅度判断
|
||
if sh_change < -1.5:
|
||
overall = "bearish"
|
||
desc = "大盘偏弱"
|
||
elif sh_change > 1.0:
|
||
overall = "bullish"
|
||
desc = "大盘偏强"
|
||
else:
|
||
overall = "neutral"
|
||
desc = "大盘震荡"
|
||
|
||
structure = json.dumps({"overall": overall, "description": desc}, ensure_ascii=False)
|
||
indices_json = json.dumps(indices, ensure_ascii=False)
|
||
|
||
# 写入macro_context_log
|
||
now = datetime.now()
|
||
session = "midday" if now.hour >= 12 else "morning"
|
||
ts = now.strftime("%Y-%m-%d %H:%M:%S")
|
||
try:
|
||
conn = sqlite3.connect(str(DB))
|
||
conn.execute("""
|
||
INSERT INTO macro_context_log
|
||
(data_timestamp, session, has_valid_data, indices, structure, created_at)
|
||
VALUES (?, ?, 1, ?, ?, ?)
|
||
""", (ts, session, indices_json, structure, ts))
|
||
conn.commit()
|
||
conn.close()
|
||
print(f"[SILENT] macro_context updated: {overall} {desc} {len(indices)} indices")
|
||
except Exception as e:
|
||
print(f"[SILENT] macro_context write failed: {e}")
|
||
|
||
if __name__ == "__main__":
|
||
main()
|