feat: macro_context/market数据全部DB优先,JSON回退

- 建 macro_context_log 表,macro_context_collector.py 双写
- strategy_lifecycle.py load_macro_context() 优先DB
- strategy_tree.py detect_scenario() 优先DB
- stale_push_wlin.py load_macro_line() 优先DB
- xiaoguo_signal_consumer.py 大盘判断优先DB
- stock_profile.py load_macro() 优先DB
- system_audit.py 管道审计改查DB market_snapshots
- JSON保留作fallback,确保过渡期不中断
This commit is contained in:
知微
2026-06-24 22:34:08 +08:00
parent 843012e045
commit 39ff4d95f7
8 changed files with 132 additions and 39 deletions
+30 -13
View File
@@ -286,22 +286,39 @@ def compute_sector_adjustment(code, market_ctx, stock_sector_map):
def load_macro_context():
"""读取宏观上下文,返回 (bias, desc)bias 取 0.8/1.0/1.1 分别对应 bearish/neutral/bullish"""
"""读取宏观上下文,返回 (bias, desc)优先 DB,回退 JSON"""
try:
with open(MACRO_CONTEXT_PATH) as f:
ctx = json.load(f)
overall = ctx.get("structure", {}).get("overall", "neutral")
desc = ctx.get("structure", {}).get("description", "")
if "bearish" in overall:
return 0.8, f"宏观{desc}"
elif overall == "bullish":
return 1.05, f"宏观{desc}"
elif overall == "strong_bullish":
return 1.1, f"宏观{desc}"
import sqlite3
from pathlib import Path
conn = sqlite3.connect(str(Path(__file__).parent.parent / "data" / "mofin.db"))
row = conn.execute(
"SELECT indices, structure FROM macro_context_log "
"WHERE has_valid_data=1 ORDER BY created_at DESC LIMIT 1"
).fetchone()
conn.close()
if row:
indices = json.loads(row[0]) if row[0] else {}
structure = json.loads(row[1]) if row[1] else {}
overall = structure.get("overall", "neutral")
desc = structure.get("description", "")
else:
return 1.0, f"宏观{desc}"
raise ValueError("no db data")
except Exception:
return 1.0, "宏观未加载"
try:
with open(MACRO_CONTEXT_PATH) as f:
ctx = json.load(f)
overall = ctx.get("structure", {}).get("overall", "neutral")
desc = ctx.get("structure", {}).get("description", "")
except Exception:
return 1.0, "宏观未加载"
if "bearish" in overall:
return 0.8, f"宏观{desc}"
elif overall == "bullish":
return 1.05, f"宏观{desc}"
elif overall == "strong_bullish":
return 1.1, f"宏观{desc}"
else:
return 1.0, f"宏观{desc}"
def batch_fetch_prices(codes):