fix: price_monitor三处进区触发改为真12维LLM重评(_do_llm_reassess调per_stock_reassess)+30min冷却(补上次被回滚的commit,根治技术快算)

This commit is contained in:
xxm
2026-08-20 05:33:56 +08:00
parent 44e56089f3
commit cf9b87ef4d
+70 -7
View File
@@ -81,6 +81,73 @@ from market_config import kline_symbol, market_for_code
sys.path.insert(0, "/home/hmo/web-dashboard")
try:
from strategy_lifecycle import reassess_strategy, reassess_with_context
import subprocess as _sp2
_REASSESS_SCRIPT = "/home/hmo/MoFin/deploy/profile-scripts/per_stock_reassess.py"
_REASSESS_OLD = "/home/hmo/MoFin/data/.price_reassess_last.json"
def _done_reassess_recently(code):
"""冷却检查:同票 30 分钟内已触发过 12维重评 → 跳过(防每2分钟重复烧LLM)"""
try:
import os as _os
if _os.path.exists(_REASSESS_OLD):
_last = json.load(open(_REASSESS_OLD, encoding="utf-8"))
if code in _last and time.time() - _last[code] < 1800:
return True
except Exception:
pass
return False
def _mark_reassess(code):
"""记录重评时间(冷却标记)"""
try:
import os as _os
_last = {}
if _os.path.exists(_REASSESS_OLD):
try:
_last = json.load(open(_REASSESS_OLD, encoding="utf-8"))
except Exception:
_last = {}
_last[code] = time.time()
json.dump(_last, open(_REASSESS_OLD, "w"), ensure_ascii=False)
except Exception:
pass
def _do_llm_reassess(code, name, price, cost, shares, current_action):
"""真正12维LLM重评(per_stock_reassess.py)。返回最新参数的dict或None。"""
if _done_reassess_recently(code):
print(f"{code} 30分钟内已重评,跳过(冷却)", flush=True)
return None
try:
_r = _sp2.run(
[sys.executable, _REASSESS_SCRIPT, code],
capture_output=True, text=True, timeout=180
)
_mark_reassess(code)
except Exception as e:
print(f" ⚠️ {code} 12维重评异常: {e}", file=sys.stderr)
return None
# 重评后从DB读最新参数(子进程已写库)
try:
import sqlite3 as _sq
_c = _sq.connect("/home/hmo/MoFin/data/mofin.db", timeout=30)
_c.row_factory = _sq.Row
_row = _c.execute(
"SELECT timing_signal, action, stop_loss, take_profit, entry_low, entry_high, rr_ratio "
"FROM holding_strategies WHERE code=? AND status='active'", (code,)).fetchone()
_c.close()
if _row:
return {
"timing_signal": _row["timing_signal"] or "",
"action": _row["action"] or "",
"stop_loss": _row["stop_loss"] or 0,
"take_profit": _row["take_profit"] or 0,
"entry_low": _row["entry_low"] or 0,
"entry_high": _row["entry_high"] or 0,
"rr_ratio": _row["rr_ratio"] or 0,
}
except Exception as e:
print(f" ⚠️ {code} 读重评结果失败: {e}", file=sys.stderr)
return None
HAS_REASSESS = True
except ImportError:
HAS_REASSESS = False
@@ -607,7 +674,7 @@ def run_once(round_label=""):
cost = d.get("cost", 0) or 0
shares = d.get("shares", 0) or 0
current_action = d.get("action", "")
result = reassess_with_context(code, name, price, cost, shares, current_action)
result = _do_llm_reassess(code, name, price, cost, shares, current_action)
if result:
timing_signal = result.get("timing_signal", "")
action = result.get("action", "")
@@ -679,7 +746,7 @@ def run_once(round_label=""):
cost = d.get("cost", 0) or 0
shares = d.get("shares", 0) or 0
current_action = d.get("action", "")
result = reassess_with_context(code, name, price, cost, shares, current_action)
result = _do_llm_reassess(code, name, price, cost, shares, current_action)
if result:
timing_signal = result.get("timing_signal", "")
action = result.get("action", "")
@@ -834,11 +901,7 @@ def run_once(round_label=""):
sentiment = "bullish"
# 调用技术面驱动重评(非机械百分比)
result = reassess_strategy(
code, name, price, cost, shares,
current_action=d.get("action", ""),
volume_signal="中性", sentiment=sentiment,
)
result = _do_llm_reassess(code, name, price, cost, shares, d.get("action", ""))
outputs.append(f" 📊 新策略: 损{result['stop_loss']}{result['take_profit']}{result['entry_low']}~{result['entry_high']} RR={result['rr_ratio']}")
reassesed_codes.append(code)
except Exception as e: