branch_scanner: 修price_lower遗漏+只输最优分支

1. check_condition 新增 price_lower 检查(买入区下界)
   之前 buy_dip 分支的 price_lower: >=entry_low 从未被验证
   导致跌出买入区的股票也错误触发买入信号

2. 输出改为每只股票只列最优优先级的适用分支
   之前列出所有适用分支(多分支冲突:同一只股同时止损+买入)
   优先级:止损(P0) > 回调买入(P1) > 追涨(P2) > 减仓(P3) > 止盈(P4) > 持有(P99)
   每只股票只列一项,无矛盾
This commit is contained in:
知微
2026-06-24 10:34:46 +08:00
parent ee1849a6a3
commit e1c426fb96
5 changed files with 247 additions and 211 deletions
+23 -5
View File
@@ -61,7 +61,7 @@ def save_decisions(data):
def check_condition(branch, scenario_id, price):
"""检查分支条件是否满足"""
"""检查分支条件是否满足 — 同步 strategy_tree._check_branch_condition 逻辑"""
cond = branch.get("condition", {})
required_scenario = cond.get("scenario", "")
if required_scenario and required_scenario != scenario_id:
@@ -76,6 +76,18 @@ def check_condition(branch, scenario_id, price):
if op == ">" and not (price > val): return False
if op == "<=" and not (price <= val): return False
if op == ">=" and not (price >= val): return False
# 买入区下界(price_lower)检查 — 之前遗漏
price_lower = cond.get("price_lower", "")
if price_lower and price:
ops = re.findall(r"([<>=!]+)\s*([\d.]+)", price_lower)
for op, val_str in ops:
val = float(val_str)
if op == "<" and not (price < val): return False
if op == ">" and not (price > val): return False
if op == "<=" and not (price <= val): return False
if op == ">=" and not (price >= val): return False
return True
@@ -114,11 +126,17 @@ def main():
if not price:
continue
for br in branches:
# 找出所有适用的分支,选最优(优先级数字最低)
best = None
for br in sorted(branches, key=lambda b: b.get("priority", 999)):
if check_condition(br, sid, price):
br["trigger_count"] = br.get("trigger_count", 0) + 1
br["last_triggered"] = today
triggered.append((code, entry.get("name", ""), br))
best = br
break
if best:
best["trigger_count"] = best.get("trigger_count", 0) + 1
best["last_triggered"] = today
triggered.append((code, entry.get("name", ""), best))
if triggered:
save_decisions(data)