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
+25 -3
View File
@@ -112,8 +112,18 @@ def load_macro_line():
"""加载大盘和市场的简要描述"""
parts = []
try:
with open(MACRO_CTX) as f:
m = json.load(f).get("structure", {})
# 优先 DB
import sqlite3
db = sqlite3.connect("/home/hmo/MoFin/data/mofin.db")
row = db.execute(
"SELECT structure FROM macro_context_log "
"WHERE has_valid_data=1 ORDER BY created_at DESC LIMIT 1"
).fetchone()
db.close()
if row and row[0]:
m = json.loads(row[0])
else:
raise ValueError("no db data")
overall = m.get("overall", "neutral")
desc = m.get("description", "")
if "bearish" in overall:
@@ -123,7 +133,19 @@ def load_macro_line():
elif desc:
parts.append(f"大盘{desc}")
except Exception:
pass
try:
with open(MACRO_CTX) as f:
m = json.load(f).get("structure", {})
overall = m.get("overall", "neutral")
desc = m.get("description", "")
if "bearish" in overall:
parts.append("大盘偏弱")
elif overall == "bullish":
parts.append("大盘偏强")
elif desc:
parts.append(f"大盘{desc}")
except Exception:
pass
try:
with open(MARKET_JSON) as f:
mk = json.load(f)
+28 -6
View File
@@ -74,19 +74,41 @@ def quick_assess(quote):
score = 0
reasons = []
# 大盘环境(简化:交易日9:30-15:00且在涨
# 引用 macro_context.json 中的大盘方向
# 大盘环境,从DB读(回退JSON
try:
mc = json.loads((DATA / "macro_context.json").read_text())
sh = mc.get("shanghai", {}).get("change_pct", 0)
import sqlite3
conn = sqlite3.connect(str(DB_PATH))
row = conn.execute(
"SELECT indices FROM macro_context_log WHERE has_valid_data=1 ORDER BY created_at DESC LIMIT 1"
).fetchone()
conn.close()
if row and row[0]:
mc = json.loads(row[0])
else:
raise ValueError
sh = 0
for k, v in mc.items():
if "上证" in k:
sh = v.get("change_pct", 0)
break
if sh > 0.5:
score += 1
reasons.append(f"大盘+{sh:.1f}%偏强")
elif sh < -0.5:
score -= 1
reasons.append(f"大盘{sh:.1f}%偏弱")
except:
pass
except Exception:
try:
mc = json.loads((DATA / "macro_context.json").read_text())
sh = mc.get("shanghai", {}).get("change_pct", 0)
if sh > 0.5:
score += 1
reasons.append(f"大盘+{sh:.1f}%偏强")
elif sh < -0.5:
score -= 1
reasons.append(f"大盘{sh:.1f}%偏弱")
except:
pass
# 技术面:涨跌幅
chg = quote.get("change_pct", 0)