refactor: remove duplicate scripts + JSON constants + update docs
This commit is contained in:
@@ -19,10 +19,6 @@ from datetime import datetime, date
|
||||
from mo_data import read_portfolio, read_decisions
|
||||
from mofin_db import get_conn, write_holding_strategy
|
||||
|
||||
# 路径
|
||||
DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
|
||||
PORTFOLIO_PATH = "/home/hmo/web-dashboard/data/portfolio.json"
|
||||
|
||||
# 引入 strategy_tree 模块
|
||||
sys.path.insert(0, "/home/hmo/MoFin")
|
||||
try:
|
||||
@@ -132,10 +128,6 @@ def evaluate_all():
|
||||
conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
# [migrated to DB] — cold backup removed
|
||||
# with open(DECISIONS_PATH, "w") as f:
|
||||
# json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
|
||||
# 输出摘要(空 = 静默)
|
||||
lines = []
|
||||
init_note = f" | 自动初始化{auto_init_count}只" if auto_init_count else ""
|
||||
|
||||
@@ -20,7 +20,6 @@ from urllib.request import Request, urlopen
|
||||
from mo_data import read_decisions
|
||||
from mofin_db import get_conn, write_holding_strategy
|
||||
|
||||
DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
|
||||
SCANNER_STATE = "/home/hmo/web-dashboard/data/scanner_state.json"
|
||||
|
||||
|
||||
@@ -110,10 +109,6 @@ def main():
|
||||
conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
# [migrated to DB] — cold backup removed
|
||||
# with open(DECISIONS_PATH, "w") as f:
|
||||
# json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
|
||||
# 更新状态快照
|
||||
state = {"scenario": sid, "updated_at": now.isoformat(), "branches": {}}
|
||||
for e in decisions:
|
||||
|
||||
@@ -15,7 +15,6 @@ from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
from mofin_db import get_conn, write_capital_flow_cache
|
||||
|
||||
DATA_DIR = "/home/hmo/web-dashboard/data"
|
||||
DECISIONS_PATH = f"{DATA_DIR}/decisions.json"
|
||||
CACHE_PATH = f"{DATA_DIR}/capital_flow_cache.json"
|
||||
|
||||
UA = "Mozilla/5.0"
|
||||
|
||||
+93
-94
@@ -1,94 +1,93 @@
|
||||
#!/usr/bin/env python3
|
||||
"""data_governance.py — MoFin 数据治理 (no_agent)
|
||||
|
||||
1. holding_strategies 去重归档
|
||||
2. 检查缺失策略的持仓
|
||||
3. 报告数据健康状况
|
||||
"""
|
||||
|
||||
import json, sqlite3
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
|
||||
BASE = Path("/home/hmo/MoFin")
|
||||
DATA = BASE / "data"
|
||||
DB_PATH = DATA / "mofin.db"
|
||||
DECISIONS_PATH = DATA / "decisions.json"
|
||||
|
||||
|
||||
def clean_holding_strategies(conn):
|
||||
"""归档旧策略,只保留每只股票最新一条"""
|
||||
codes = conn.execute(
|
||||
"SELECT code, COUNT(*) as cnt, MAX(created_at) as latest "
|
||||
"FROM holding_strategies GROUP BY code HAVING cnt > 1"
|
||||
).fetchall()
|
||||
|
||||
total_archived = 0
|
||||
for code, cnt, latest in codes:
|
||||
# 标记除了最新一条以外的所有记录为已归档
|
||||
conn.execute(
|
||||
"UPDATE holding_strategies SET superseded_at=? "
|
||||
"WHERE code=? AND created_at<? AND superseded_at IS NULL",
|
||||
(datetime.now().isoformat(), code, latest))
|
||||
total_archived += cnt - 1
|
||||
|
||||
return total_archived
|
||||
|
||||
|
||||
def check_missing_strategies(conn, decisions_list):
|
||||
"""检查持仓股中哪些没有对应的策略记录"""
|
||||
holdings = [d for d in decisions_list if d.get("type") == "持仓策略" and d.get("status") != "closed"]
|
||||
missing = []
|
||||
for h in holdings:
|
||||
code = h["code"]
|
||||
row = conn.execute(
|
||||
"SELECT id FROM holding_strategies WHERE code=? AND superseded_at IS NULL LIMIT 1",
|
||||
(code,)).fetchone()
|
||||
if not row:
|
||||
missing.append(h)
|
||||
return missing
|
||||
|
||||
|
||||
def main():
|
||||
conn = sqlite3.connect(str(DB_PATH))
|
||||
|
||||
# 1. 清理 holding_strategies
|
||||
archived = clean_holding_strategies(conn)
|
||||
conn.commit()
|
||||
remaining = conn.execute(
|
||||
"SELECT COUNT(*) FROM holding_strategies WHERE superseded_at IS NULL").fetchone()[0]
|
||||
print(f"holding_strategies: 归档{archived}条过期记录 | 剩余活跃{remaining}条")
|
||||
|
||||
# 2. 检查缺失策略的持仓
|
||||
decisions = mo_data.read_decisions()
|
||||
decisions_list = decisions.get("decisions", [])
|
||||
missing = check_missing_strategies(conn, decisions_list)
|
||||
if missing:
|
||||
print(f"\n⚠️ {len(missing)}只持仓没有对应策略记录:")
|
||||
for m in missing:
|
||||
print(f" {m.get('name','?')}({m['code']}): {m.get('type','')}")
|
||||
else:
|
||||
print("✅ 所有持仓都有策略记录")
|
||||
|
||||
# 3. 深套统计
|
||||
deep = []
|
||||
for d in decisions_list:
|
||||
cost = d.get("cost", 0) or d.get("avg_price", 0) or 0
|
||||
price = d.get("price", 0)
|
||||
if cost > 0 and price > 0:
|
||||
loss = (price - cost) / cost * 100
|
||||
if loss < -25:
|
||||
deep.append((d.get("name",""), d["code"], loss, d.get("stop_loss",0)))
|
||||
if deep:
|
||||
print(f"\n🔴 {len(deep)}只深套(>-25%):")
|
||||
for name, code, loss, sl in deep:
|
||||
print(f" {name}({code}): {loss:.0f}% 止损={sl}")
|
||||
else:
|
||||
print("\n✅ 无深套持仓")
|
||||
|
||||
conn.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
#!/usr/bin/env python3
|
||||
"""data_governance.py — MoFin 数据治理 (no_agent)
|
||||
|
||||
1. holding_strategies 去重归档
|
||||
2. 检查缺失策略的持仓
|
||||
3. 报告数据健康状况
|
||||
"""
|
||||
|
||||
import json, sqlite3
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
|
||||
BASE = Path("/home/hmo/MoFin")
|
||||
DATA = BASE / "data"
|
||||
DB_PATH = DATA / "mofin.db"
|
||||
|
||||
|
||||
def clean_holding_strategies(conn):
|
||||
"""归档旧策略,只保留每只股票最新一条"""
|
||||
codes = conn.execute(
|
||||
"SELECT code, COUNT(*) as cnt, MAX(created_at) as latest "
|
||||
"FROM holding_strategies GROUP BY code HAVING cnt > 1"
|
||||
).fetchall()
|
||||
|
||||
total_archived = 0
|
||||
for code, cnt, latest in codes:
|
||||
# 标记除了最新一条以外的所有记录为已归档
|
||||
conn.execute(
|
||||
"UPDATE holding_strategies SET superseded_at=? "
|
||||
"WHERE code=? AND created_at<? AND superseded_at IS NULL",
|
||||
(datetime.now().isoformat(), code, latest))
|
||||
total_archived += cnt - 1
|
||||
|
||||
return total_archived
|
||||
|
||||
|
||||
def check_missing_strategies(conn, decisions_list):
|
||||
"""检查持仓股中哪些没有对应的策略记录"""
|
||||
holdings = [d for d in decisions_list if d.get("type") == "持仓策略" and d.get("status") != "closed"]
|
||||
missing = []
|
||||
for h in holdings:
|
||||
code = h["code"]
|
||||
row = conn.execute(
|
||||
"SELECT id FROM holding_strategies WHERE code=? AND superseded_at IS NULL LIMIT 1",
|
||||
(code,)).fetchone()
|
||||
if not row:
|
||||
missing.append(h)
|
||||
return missing
|
||||
|
||||
|
||||
def main():
|
||||
conn = sqlite3.connect(str(DB_PATH))
|
||||
|
||||
# 1. 清理 holding_strategies
|
||||
archived = clean_holding_strategies(conn)
|
||||
conn.commit()
|
||||
remaining = conn.execute(
|
||||
"SELECT COUNT(*) FROM holding_strategies WHERE superseded_at IS NULL").fetchone()[0]
|
||||
print(f"holding_strategies: 归档{archived}条过期记录 | 剩余活跃{remaining}条")
|
||||
|
||||
# 2. 检查缺失策略的持仓
|
||||
decisions = mo_data.read_decisions()
|
||||
decisions_list = decisions.get("decisions", [])
|
||||
missing = check_missing_strategies(conn, decisions_list)
|
||||
if missing:
|
||||
print(f"\n⚠️ {len(missing)}只持仓没有对应策略记录:")
|
||||
for m in missing:
|
||||
print(f" {m.get('name','?')}({m['code']}): {m.get('type','')}")
|
||||
else:
|
||||
print("✅ 所有持仓都有策略记录")
|
||||
|
||||
# 3. 深套统计
|
||||
deep = []
|
||||
for d in decisions_list:
|
||||
cost = d.get("cost", 0) or d.get("avg_price", 0) or 0
|
||||
price = d.get("price", 0)
|
||||
if cost > 0 and price > 0:
|
||||
loss = (price - cost) / cost * 100
|
||||
if loss < -25:
|
||||
deep.append((d.get("name",""), d["code"], loss, d.get("stop_loss",0)))
|
||||
if deep:
|
||||
print(f"\n🔴 {len(deep)}只深套(>-25%):")
|
||||
for name, code, loss, sl in deep:
|
||||
print(f" {name}({code}): {loss:.0f}% 止损={sl}")
|
||||
else:
|
||||
print("\n✅ 无深套持仓")
|
||||
|
||||
conn.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
+88
-90
@@ -1,90 +1,88 @@
|
||||
#!/usr/bin/env python3
|
||||
"""data_validate.py — 数据自检,在所有报告产出前执行
|
||||
|
||||
检查清单:
|
||||
1. 总资产 = 市值 + 现金 (误差 < 1%)
|
||||
2. 持仓 vs 决策交叉检查
|
||||
3. 币种一致性(港股必须currency=HKD)
|
||||
4. 数据时效:portfolio.json/decisions.json 今日已更新
|
||||
|
||||
返回值:通过→退出码0,输出"OK"。失败→退出码1,输出问题描述。
|
||||
"""
|
||||
|
||||
import json, sys
|
||||
from datetime import datetime, timezone
|
||||
from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
|
||||
DATA_DIR = "/home/hmo/web-dashboard/data"
|
||||
PORTFOLIO_PATH = f"{DATA_DIR}/portfolio.json"
|
||||
DECISIONS_PATH = f"{DATA_DIR}/decisions.json"
|
||||
STALE_REPORT = f"{DATA_DIR}/strategy_staleness_report.json"
|
||||
|
||||
issues = []
|
||||
|
||||
# ── 1. 总资产校验 ────────────────────────────────────────────
|
||||
try:
|
||||
pf = mo_data.read_portfolio()
|
||||
mv_calc = sum(h["shares"] * h["price"] for h in pf.get("holdings", []) if h.get("price"))
|
||||
stored_ta = pf.get("total_assets", 0)
|
||||
cash = pf.get("cash", 0)
|
||||
|
||||
expected_ta = round(mv_calc + cash, 2)
|
||||
if stored_ta > 0 and abs(stored_ta - expected_ta) / stored_ta > 0.01:
|
||||
issues.append(f"总资产不匹配: 存储{stored_ta} ≠ 计算{expected_ta} (市值{mv_calc}+现金{cash})")
|
||||
except Exception as e:
|
||||
issues.append(f"portfolio.json读取失败: {e}")
|
||||
|
||||
# ── 2. 持仓 vs 决策交叉检查 ──────────────────────────────────
|
||||
try:
|
||||
dec = mo_data.read_decisions()
|
||||
dec_codes = {}
|
||||
for d in dec.get("decisions", []):
|
||||
dec_codes[d["code"]] = d
|
||||
|
||||
for h in pf.get("holdings", []):
|
||||
code = h["code"]
|
||||
if code not in dec_codes:
|
||||
issues.append(f"持仓{code}({h.get('name','?')}) 在decisions.json中无对应决策")
|
||||
|
||||
for code, d in dec_codes.items():
|
||||
if d.get("status") == "active" and d.get("type") == "持仓策略":
|
||||
if not any(h["code"] == code for h in pf.get("holdings", [])):
|
||||
issues.append(f"决策{code}({d.get('name','?')})标记持仓但portfolio.json中无此股")
|
||||
except Exception as e:
|
||||
issues.append(f"决策检查失败: {e}")
|
||||
|
||||
# ── 3. 币种一致性 ────────────────────────────────────────────
|
||||
try:
|
||||
for d in dec.get("decisions", []):
|
||||
code = str(d.get("code", ""))
|
||||
# 港股必须标记currency
|
||||
if len(code) == 5 and code[0] in ("0", "1"):
|
||||
cur = d.get("currency", "")
|
||||
if cur not in ("HKD", "CNY"):
|
||||
issues.append(f"港股{code}({d.get('name','?')}) 缺currency标记,不可靠")
|
||||
# 如果标记了HKD,stop_loss也应该是合理的HKD价(>10)
|
||||
sl = d.get("stop_loss", 0)
|
||||
if cur == "HKD" and sl > 0 and sl < 1:
|
||||
issues.append(f"港股{code} currency=HKD但stop_loss={sl} 异常低")
|
||||
except Exception as e:
|
||||
issues.append(f"币种检查失败: {e}")
|
||||
|
||||
# ── 4. 数据时效 ──────────────────────────────────────────────
|
||||
today = datetime.now().strftime("%Y-%m-%d")
|
||||
try:
|
||||
if pf.get("updated_at", "").startswith(today):
|
||||
pass # OK
|
||||
else:
|
||||
issues.append(f"portfolio.json updated_at={pf.get('updated_at','?')} 不是今日")
|
||||
except:
|
||||
pass
|
||||
|
||||
# ── 输出 ──────────────────────────────────────────────────────
|
||||
if issues:
|
||||
print("DATA_VALIDATE_FAIL")
|
||||
for i in issues:
|
||||
print(f" ⚠️ {i}")
|
||||
sys.exit(1)
|
||||
else:
|
||||
print("DATA_VALIDATE_OK")
|
||||
sys.exit(0)
|
||||
#!/usr/bin/env python3
|
||||
"""data_validate.py — 数据自检,在所有报告产出前执行
|
||||
|
||||
检查清单:
|
||||
1. 总资产 = 市值 + 现金 (误差 < 1%)
|
||||
2. 持仓 vs 决策交叉检查
|
||||
3. 币种一致性(港股必须currency=HKD)
|
||||
4. 数据时效:portfolio.json/decisions.json 今日已更新
|
||||
|
||||
返回值:通过→退出码0,输出"OK"。失败→退出码1,输出问题描述。
|
||||
"""
|
||||
|
||||
import json, sys
|
||||
from datetime import datetime, timezone
|
||||
from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
|
||||
DATA_DIR = "/home/hmo/web-dashboard/data"
|
||||
STALE_REPORT = f"{DATA_DIR}/strategy_staleness_report.json"
|
||||
|
||||
issues = []
|
||||
|
||||
# ── 1. 总资产校验 ────────────────────────────────────────────
|
||||
try:
|
||||
pf = mo_data.read_portfolio()
|
||||
mv_calc = sum(h["shares"] * h["price"] for h in pf.get("holdings", []) if h.get("price"))
|
||||
stored_ta = pf.get("total_assets", 0)
|
||||
cash = pf.get("cash", 0)
|
||||
|
||||
expected_ta = round(mv_calc + cash, 2)
|
||||
if stored_ta > 0 and abs(stored_ta - expected_ta) / stored_ta > 0.01:
|
||||
issues.append(f"总资产不匹配: 存储{stored_ta} ≠ 计算{expected_ta} (市值{mv_calc}+现金{cash})")
|
||||
except Exception as e:
|
||||
issues.append(f"portfolio.json读取失败: {e}")
|
||||
|
||||
# ── 2. 持仓 vs 决策交叉检查 ──────────────────────────────────
|
||||
try:
|
||||
dec = mo_data.read_decisions()
|
||||
dec_codes = {}
|
||||
for d in dec.get("decisions", []):
|
||||
dec_codes[d["code"]] = d
|
||||
|
||||
for h in pf.get("holdings", []):
|
||||
code = h["code"]
|
||||
if code not in dec_codes:
|
||||
issues.append(f"持仓{code}({h.get('name','?')}) 在decisions.json中无对应决策")
|
||||
|
||||
for code, d in dec_codes.items():
|
||||
if d.get("status") == "active" and d.get("type") == "持仓策略":
|
||||
if not any(h["code"] == code for h in pf.get("holdings", [])):
|
||||
issues.append(f"决策{code}({d.get('name','?')})标记持仓但portfolio.json中无此股")
|
||||
except Exception as e:
|
||||
issues.append(f"决策检查失败: {e}")
|
||||
|
||||
# ── 3. 币种一致性 ────────────────────────────────────────────
|
||||
try:
|
||||
for d in dec.get("decisions", []):
|
||||
code = str(d.get("code", ""))
|
||||
# 港股必须标记currency
|
||||
if len(code) == 5 and code[0] in ("0", "1"):
|
||||
cur = d.get("currency", "")
|
||||
if cur not in ("HKD", "CNY"):
|
||||
issues.append(f"港股{code}({d.get('name','?')}) 缺currency标记,不可靠")
|
||||
# 如果标记了HKD,stop_loss也应该是合理的HKD价(>10)
|
||||
sl = d.get("stop_loss", 0)
|
||||
if cur == "HKD" and sl > 0 and sl < 1:
|
||||
issues.append(f"港股{code} currency=HKD但stop_loss={sl} 异常低")
|
||||
except Exception as e:
|
||||
issues.append(f"币种检查失败: {e}")
|
||||
|
||||
# ── 4. 数据时效 ──────────────────────────────────────────────
|
||||
today = datetime.now().strftime("%Y-%m-%d")
|
||||
try:
|
||||
if pf.get("updated_at", "").startswith(today):
|
||||
pass # OK
|
||||
else:
|
||||
issues.append(f"portfolio.json updated_at={pf.get('updated_at','?')} 不是今日")
|
||||
except:
|
||||
pass
|
||||
|
||||
# ── 输出 ──────────────────────────────────────────────────────
|
||||
if issues:
|
||||
print("DATA_VALIDATE_FAIL")
|
||||
for i in issues:
|
||||
print(f" ⚠️ {i}")
|
||||
sys.exit(1)
|
||||
else:
|
||||
print("DATA_VALIDATE_OK")
|
||||
sys.exit(0)
|
||||
|
||||
@@ -5,9 +5,6 @@ import json, sys, os
|
||||
from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
from mofin_db import get_conn, write_holding_strategy
|
||||
|
||||
DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
|
||||
DECISIONS_BAK = DECISIONS_PATH + ".bak"
|
||||
|
||||
try:
|
||||
dec = read_decisions()
|
||||
except Exception as e:
|
||||
@@ -40,18 +37,9 @@ for d in dec.get("decisions", []):
|
||||
if new_trig:
|
||||
print(f" {code} {name}: trigger={new_trig}")
|
||||
|
||||
# 备份
|
||||
os.makedirs(os.path.dirname(DECISIONS_BAK), exist_ok=True)
|
||||
with open(DECISIONS_BAK, 'w') as f:
|
||||
json.dump(mo_data.read_decisions(), f, indent=2, ensure_ascii=False)
|
||||
|
||||
# DB 写入(替代 json.dump)
|
||||
conn = get_conn()
|
||||
for d in dec.get("decisions", []):
|
||||
write_holding_strategy(conn, d.get("code", ""), d.get("name", ""), d)
|
||||
conn.close()
|
||||
# [migrated to DB] — cold backup removed
|
||||
# with open(DECISIONS_PATH, 'w') as f:
|
||||
# json.dump(dec, f, indent=2, ensure_ascii=False)
|
||||
|
||||
print(f"\n共{count}只,已更新trigger字段")
|
||||
|
||||
@@ -19,7 +19,6 @@ from mo_data import read_decisions
|
||||
from mofin_db import get_conn, write_holdings_batch, write_portfolio_summary, write_holding_strategy
|
||||
|
||||
STOCKS_FILE = "/home/hmo/stocks/holding.xls"
|
||||
PORTFOLIO_PATH = "/home/hmo/web-dashboard/data/portfolio.json"
|
||||
DB_PATH = "/home/hmo/web-dashboard/data/mofin.db"
|
||||
|
||||
|
||||
@@ -151,9 +150,6 @@ def main():
|
||||
'updated_at': datetime.now().strftime('%Y-%m-%d %H:%M'),
|
||||
'source': STOCKS_FILE,
|
||||
}
|
||||
# [migrated to DB] — cold backup removed; DB writes below
|
||||
# with open(PORTFOLIO_PATH, 'w') as f:
|
||||
# json.dump(portfolio, f, indent=2, ensure_ascii=False)
|
||||
# DB 写入
|
||||
try:
|
||||
conn = get_conn()
|
||||
|
||||
+258
-261
@@ -1,261 +1,258 @@
|
||||
#!/usr/bin/env python3
|
||||
"""intraday_health_check.py — 盘中高频轻量自检 (no_agent)
|
||||
|
||||
每15分钟检查最关键的活动组件,只查会直接影响盘中运行的。
|
||||
发现问题→写TODO(消费管道与每日体检共享)。
|
||||
"""
|
||||
|
||||
import json, os, sqlite3, subprocess, urllib.request, sys, socket
|
||||
from pathlib import Path
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
# ── MoFin path ─────────────────────────────────────────────────────
|
||||
sys.path.insert(0, "/home/hmo/MoFin")
|
||||
from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
|
||||
BASE = Path("/home/hmo/MoFin")
|
||||
DATA = BASE / "data"
|
||||
DB_PATH = DATA / "mofin.db"
|
||||
CRON_JOBS = Path("/home/hmo/.hermes/profiles/position-analyst/cron/jobs.json")
|
||||
GATEWAY_URL = "http://localhost:8643/v1/chat/completions"
|
||||
GATEWAY_KEY = "hermes123"
|
||||
|
||||
ISSUES = []
|
||||
OK_COUNT = 0
|
||||
|
||||
|
||||
def log(ok, msg):
|
||||
global OK_COUNT
|
||||
if ok:
|
||||
OK_COUNT += 1
|
||||
else:
|
||||
ISSUES.append(msg)
|
||||
|
||||
|
||||
def check_port(port):
|
||||
try:
|
||||
r = subprocess.run(["ss", "-tlnp"], capture_output=True, text=True, timeout=5)
|
||||
return f":{port}" in r.stdout
|
||||
except:
|
||||
return False
|
||||
|
||||
|
||||
def check_http(url, timeout=5):
|
||||
"""检查HTTP可达性,5秒超时防止hang住"""
|
||||
try:
|
||||
for k in list(os.environ.keys()):
|
||||
if 'proxy' in k.lower():
|
||||
os.environ.pop(k)
|
||||
req = urllib.request.Request(url, method="GET")
|
||||
urllib.request.urlopen(req, timeout=timeout)
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
|
||||
def db_today_count(table, date_col):
|
||||
today = datetime.now().strftime("%Y-%m-%d")
|
||||
try:
|
||||
conn = sqlite3.connect(str(DB_PATH))
|
||||
r = conn.execute(f"SELECT COUNT(*) FROM {table} WHERE date({date_col}) = ?", (today,)).fetchone()
|
||||
conn.close()
|
||||
return r[0]
|
||||
except:
|
||||
return -1
|
||||
|
||||
|
||||
def check_xiaoguo():
|
||||
"""小果管道:进程/scanner有数据/API可达(降级不报错)"""
|
||||
# 进程 — 不一定有常驻进程(no_agent cron模式)
|
||||
# 数据 — 今日有扫描记录
|
||||
scans_today = db_today_count("xiaoguo_scan_tracker", "last_scanned_at")
|
||||
if scans_today <= 0:
|
||||
# 可能是小果离线了,不报严重,记录即可
|
||||
return
|
||||
# API — 用socket快速检测可达性(3s超时)
|
||||
try:
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.settimeout(3)
|
||||
s.connect(("node122", 18003))
|
||||
s.close()
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
PORTFOLIO_PATH = str(DATA / "portfolio.json")
|
||||
|
||||
|
||||
def check_price_monitor():
|
||||
"""价格监控:检查price_monitor cron最近是否运行 + 数据是否更新
|
||||
|
||||
注意:price_events 存储的是区间偏离事件(价格穿过买入区/止损/止盈边界),
|
||||
不是心跳信号。横盘期/无操作信号时自然不会有新事件。因此不检查event数,
|
||||
改为检查 cron 最后运行时间和 portfolio.json 数据新鲜度。
|
||||
"""
|
||||
# 检查cron最近运行记录
|
||||
cron_ok = False
|
||||
try:
|
||||
with open(str(CRON_JOBS)) as f:
|
||||
data = json.load(f)
|
||||
jobs_list = data.get("jobs", []) if isinstance(data.get("jobs"), list) else []
|
||||
if not jobs_list:
|
||||
jobs_list = list(data.get("jobs", {}).values())
|
||||
for job in jobs_list:
|
||||
if not job:
|
||||
continue
|
||||
script = job.get("script") or ""
|
||||
name = job.get("name") or ""
|
||||
if "price_monitor" in script or "价格监控" in name:
|
||||
last_run = job.get("last_run_at")
|
||||
if last_run:
|
||||
last_dt = datetime.fromisoformat(last_run)
|
||||
# 兼容带时区和无时区两种格式
|
||||
ref_now = datetime.now(last_dt.tzinfo) if last_dt.tzinfo else datetime.now()
|
||||
elapsed = (ref_now - last_dt).total_seconds()
|
||||
if elapsed < 600: # 10分钟内运行过
|
||||
cron_ok = True
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not cron_ok:
|
||||
log(False, "价格监控cron无最近运行记录(>10分钟未运行)")
|
||||
return
|
||||
|
||||
# 检查portfolio.json数据新鲜度
|
||||
# 兼容 '2026-07-02 10:43'(price_monitor写入,无秒)和 '2026-07-02 10:43:53'(DB写入,有秒)
|
||||
def _parse_updated_at(ts: str) -> datetime | None:
|
||||
for fmt in ("%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M"):
|
||||
try:
|
||||
return datetime.strptime(ts, fmt)
|
||||
except ValueError:
|
||||
continue
|
||||
return None
|
||||
|
||||
try:
|
||||
pf = read_portfolio()
|
||||
pf_updated = pf.get("updated_at", "")
|
||||
if pf_updated:
|
||||
pf_dt = _parse_updated_at(pf_updated)
|
||||
if pf_dt is None:
|
||||
log(False, f"价格数据updated_at格式无法解析: {pf_updated}")
|
||||
else:
|
||||
seconds_ago = (datetime.now() - pf_dt).total_seconds()
|
||||
if seconds_ago < 600: # 10分钟内
|
||||
log(True, f"价格监控运行正常,数据{int(seconds_ago//60)}分钟前更新")
|
||||
else:
|
||||
log(False, f"价格数据{int(seconds_ago)}秒未更新(portfolio.json)")
|
||||
else:
|
||||
log(False, "portfolio.json缺少updated_at字段")
|
||||
except Exception as e:
|
||||
log(False, f"价格数据新鲜度检查失败: {e}")
|
||||
|
||||
|
||||
def check_bots():
|
||||
zhiwei = subprocess.run(["systemctl", "is-active", "xmpp-zhiwei.service"],
|
||||
capture_output=True, text=True, timeout=5).stdout.strip() == "active"
|
||||
xiaoguo = subprocess.run(["systemctl", "is-active", "xmpp-xiaoguo.service"],
|
||||
capture_output=True, text=True, timeout=5).stdout.strip() == "active"
|
||||
log(zhiwei, "知微XMPP Bot离线")
|
||||
log(xiaoguo, "小果XMPP Bot离线")
|
||||
|
||||
|
||||
def check_gateways():
|
||||
log(check_port(8643), "知微Gateway :8643 未监听")
|
||||
log(check_port(8645), "小果Gateway :8645 未监听")
|
||||
|
||||
|
||||
def check_signal_pipeline():
|
||||
"""信号从xiaoguo_scanner→signal_news→consumer是否通畅"""
|
||||
unproc = 0
|
||||
try:
|
||||
conn = sqlite3.connect(str(DB_PATH))
|
||||
r = conn.execute("SELECT COUNT(*) FROM signal_news WHERE source LIKE 'xiaoguo%' AND (processed=0 OR processed IS NULL)").fetchone()
|
||||
unproc = r[0]
|
||||
conn.close()
|
||||
except:
|
||||
pass
|
||||
log(unproc < 30, f"信号堆积: {unproc}条未处理(需<30)")
|
||||
|
||||
# 宏观风险状态检查
|
||||
try:
|
||||
risk_path = DATA / "macro_risk_state.json"
|
||||
if risk_path.exists():
|
||||
risk = json.loads(risk_path.read_text())
|
||||
level = risk.get("level", "none")
|
||||
expired = risk.get("expired", False)
|
||||
# 提取摘要做原因描述(state.json用signals数组,不是reason字段)
|
||||
signals = risk.get("signals", [])
|
||||
reason = ""
|
||||
if signals and isinstance(signals, list) and len(signals) > 0:
|
||||
first_sig = signals[0]
|
||||
summary = first_sig.get("summary", "")
|
||||
if summary:
|
||||
reason = summary[:80].replace("\n", " ")
|
||||
if level == "high" and not expired:
|
||||
reason_clean = reason.replace("【高风险】", "").strip()[:60]
|
||||
log(False, f"🔴 宏观风险HIGH: {reason_clean}")
|
||||
elif level == "high" and expired:
|
||||
log(True, f"⏳ 宏观风险HIGH已过期(无新信号超过15分钟)")
|
||||
elif level == "medium":
|
||||
log(True, f"⚠️ 宏观风险MEDIUM: {reason}")
|
||||
else:
|
||||
log(True, "无宏观风险状态文件(可能未生成)")
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def write_todos():
|
||||
if not ISSUES:
|
||||
return
|
||||
for msg in ISSUES:
|
||||
title = f"[盘中自检] {msg}"
|
||||
try:
|
||||
conn = sqlite3.connect(str(DB_PATH), timeout=10)
|
||||
conn.execute("PRAGMA busy_timeout=5000")
|
||||
# 宏观风险HIGH去重:只要有pending/in_progress的宏观风险TODO,不再新增
|
||||
if "宏观风险HIGH" in msg:
|
||||
exist = conn.execute(
|
||||
"SELECT id FROM todos WHERE title LIKE '%宏观风险HIGH%' AND status IN ('pending','in_progress') LIMIT 1"
|
||||
).fetchone()
|
||||
else:
|
||||
exist = conn.execute(
|
||||
"SELECT id FROM todos WHERE title=? AND status IN ('pending','in_progress')", (title,)
|
||||
).fetchone()
|
||||
if not exist:
|
||||
conn.execute(
|
||||
"INSERT INTO todos (title, description, priority, source, status, fix_action) "
|
||||
"VALUES (?, ?, 'high', 'intraday_check', 'pending', NULL)",
|
||||
(title, f"盘中自动发现: {msg}"))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def main():
|
||||
now = datetime.now()
|
||||
# 只在交易时段运行
|
||||
if now.weekday() >= 5 or now.hour < 9 or now.hour >= 15:
|
||||
print("[SILENT] 非交易时段")
|
||||
return
|
||||
|
||||
check_bots()
|
||||
check_gateways()
|
||||
check_xiaoguo()
|
||||
if 9 <= now.hour < 16:
|
||||
check_price_monitor()
|
||||
check_signal_pipeline()
|
||||
|
||||
write_todos()
|
||||
|
||||
if ISSUES:
|
||||
print(f"盘中自检 | {now.strftime('%H:%M')} | {len(ISSUES)}项异常:")
|
||||
for i in ISSUES:
|
||||
print(f" ⚠️ {i}")
|
||||
else:
|
||||
print(f"[SILENT] 盘中自检通过 | {OK_COUNT}项正常")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
#!/usr/bin/env python3
|
||||
"""intraday_health_check.py — 盘中高频轻量自检 (no_agent)
|
||||
|
||||
每15分钟检查最关键的活动组件,只查会直接影响盘中运行的。
|
||||
发现问题→写TODO(消费管道与每日体检共享)。
|
||||
"""
|
||||
|
||||
import json, os, sqlite3, subprocess, urllib.request, sys, socket
|
||||
from pathlib import Path
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
# ── MoFin path ─────────────────────────────────────────────────────
|
||||
sys.path.insert(0, "/home/hmo/MoFin")
|
||||
from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
|
||||
BASE = Path("/home/hmo/MoFin")
|
||||
DATA = BASE / "data"
|
||||
DB_PATH = DATA / "mofin.db"
|
||||
CRON_JOBS = Path("/home/hmo/.hermes/profiles/position-analyst/cron/jobs.json")
|
||||
GATEWAY_URL = "http://localhost:8643/v1/chat/completions"
|
||||
GATEWAY_KEY = "hermes123"
|
||||
|
||||
ISSUES = []
|
||||
OK_COUNT = 0
|
||||
|
||||
|
||||
def log(ok, msg):
|
||||
global OK_COUNT
|
||||
if ok:
|
||||
OK_COUNT += 1
|
||||
else:
|
||||
ISSUES.append(msg)
|
||||
|
||||
|
||||
def check_port(port):
|
||||
try:
|
||||
r = subprocess.run(["ss", "-tlnp"], capture_output=True, text=True, timeout=5)
|
||||
return f":{port}" in r.stdout
|
||||
except:
|
||||
return False
|
||||
|
||||
|
||||
def check_http(url, timeout=5):
|
||||
"""检查HTTP可达性,5秒超时防止hang住"""
|
||||
try:
|
||||
for k in list(os.environ.keys()):
|
||||
if 'proxy' in k.lower():
|
||||
os.environ.pop(k)
|
||||
req = urllib.request.Request(url, method="GET")
|
||||
urllib.request.urlopen(req, timeout=timeout)
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
|
||||
def db_today_count(table, date_col):
|
||||
today = datetime.now().strftime("%Y-%m-%d")
|
||||
try:
|
||||
conn = sqlite3.connect(str(DB_PATH))
|
||||
r = conn.execute(f"SELECT COUNT(*) FROM {table} WHERE date({date_col}) = ?", (today,)).fetchone()
|
||||
conn.close()
|
||||
return r[0]
|
||||
except:
|
||||
return -1
|
||||
|
||||
|
||||
def check_xiaoguo():
|
||||
"""小果管道:进程/scanner有数据/API可达(降级不报错)"""
|
||||
# 进程 — 不一定有常驻进程(no_agent cron模式)
|
||||
# 数据 — 今日有扫描记录
|
||||
scans_today = db_today_count("xiaoguo_scan_tracker", "last_scanned_at")
|
||||
if scans_today <= 0:
|
||||
# 可能是小果离线了,不报严重,记录即可
|
||||
return
|
||||
# API — 用socket快速检测可达性(3s超时)
|
||||
try:
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.settimeout(3)
|
||||
s.connect(("node122", 18003))
|
||||
s.close()
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def check_price_monitor():
|
||||
"""价格监控:检查price_monitor cron最近是否运行 + 数据是否更新
|
||||
|
||||
注意:price_events 存储的是区间偏离事件(价格穿过买入区/止损/止盈边界),
|
||||
不是心跳信号。横盘期/无操作信号时自然不会有新事件。因此不检查event数,
|
||||
改为检查 cron 最后运行时间和 portfolio.json 数据新鲜度。
|
||||
"""
|
||||
# 检查cron最近运行记录
|
||||
cron_ok = False
|
||||
try:
|
||||
with open(str(CRON_JOBS)) as f:
|
||||
data = json.load(f)
|
||||
jobs_list = data.get("jobs", []) if isinstance(data.get("jobs"), list) else []
|
||||
if not jobs_list:
|
||||
jobs_list = list(data.get("jobs", {}).values())
|
||||
for job in jobs_list:
|
||||
if not job:
|
||||
continue
|
||||
script = job.get("script") or ""
|
||||
name = job.get("name") or ""
|
||||
if "price_monitor" in script or "价格监控" in name:
|
||||
last_run = job.get("last_run_at")
|
||||
if last_run:
|
||||
last_dt = datetime.fromisoformat(last_run)
|
||||
# 兼容带时区和无时区两种格式
|
||||
ref_now = datetime.now(last_dt.tzinfo) if last_dt.tzinfo else datetime.now()
|
||||
elapsed = (ref_now - last_dt).total_seconds()
|
||||
if elapsed < 600: # 10分钟内运行过
|
||||
cron_ok = True
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not cron_ok:
|
||||
log(False, "价格监控cron无最近运行记录(>10分钟未运行)")
|
||||
return
|
||||
|
||||
# 检查portfolio.json数据新鲜度
|
||||
# 兼容 '2026-07-02 10:43'(price_monitor写入,无秒)和 '2026-07-02 10:43:53'(DB写入,有秒)
|
||||
def _parse_updated_at(ts: str) -> datetime | None:
|
||||
for fmt in ("%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M"):
|
||||
try:
|
||||
return datetime.strptime(ts, fmt)
|
||||
except ValueError:
|
||||
continue
|
||||
return None
|
||||
|
||||
try:
|
||||
pf = read_portfolio()
|
||||
pf_updated = pf.get("updated_at", "")
|
||||
if pf_updated:
|
||||
pf_dt = _parse_updated_at(pf_updated)
|
||||
if pf_dt is None:
|
||||
log(False, f"价格数据updated_at格式无法解析: {pf_updated}")
|
||||
else:
|
||||
seconds_ago = (datetime.now() - pf_dt).total_seconds()
|
||||
if seconds_ago < 600: # 10分钟内
|
||||
log(True, f"价格监控运行正常,数据{int(seconds_ago//60)}分钟前更新")
|
||||
else:
|
||||
log(False, f"价格数据{int(seconds_ago)}秒未更新(portfolio.json)")
|
||||
else:
|
||||
log(False, "portfolio.json缺少updated_at字段")
|
||||
except Exception as e:
|
||||
log(False, f"价格数据新鲜度检查失败: {e}")
|
||||
|
||||
|
||||
def check_bots():
|
||||
zhiwei = subprocess.run(["systemctl", "is-active", "xmpp-zhiwei.service"],
|
||||
capture_output=True, text=True, timeout=5).stdout.strip() == "active"
|
||||
xiaoguo = subprocess.run(["systemctl", "is-active", "xmpp-xiaoguo.service"],
|
||||
capture_output=True, text=True, timeout=5).stdout.strip() == "active"
|
||||
log(zhiwei, "知微XMPP Bot离线")
|
||||
log(xiaoguo, "小果XMPP Bot离线")
|
||||
|
||||
|
||||
def check_gateways():
|
||||
log(check_port(8643), "知微Gateway :8643 未监听")
|
||||
log(check_port(8645), "小果Gateway :8645 未监听")
|
||||
|
||||
|
||||
def check_signal_pipeline():
|
||||
"""信号从xiaoguo_scanner→signal_news→consumer是否通畅"""
|
||||
unproc = 0
|
||||
try:
|
||||
conn = sqlite3.connect(str(DB_PATH))
|
||||
r = conn.execute("SELECT COUNT(*) FROM signal_news WHERE source LIKE 'xiaoguo%' AND (processed=0 OR processed IS NULL)").fetchone()
|
||||
unproc = r[0]
|
||||
conn.close()
|
||||
except:
|
||||
pass
|
||||
log(unproc < 30, f"信号堆积: {unproc}条未处理(需<30)")
|
||||
|
||||
# 宏观风险状态检查
|
||||
try:
|
||||
risk_path = DATA / "macro_risk_state.json"
|
||||
if risk_path.exists():
|
||||
risk = json.loads(risk_path.read_text())
|
||||
level = risk.get("level", "none")
|
||||
expired = risk.get("expired", False)
|
||||
# 提取摘要做原因描述(state.json用signals数组,不是reason字段)
|
||||
signals = risk.get("signals", [])
|
||||
reason = ""
|
||||
if signals and isinstance(signals, list) and len(signals) > 0:
|
||||
first_sig = signals[0]
|
||||
summary = first_sig.get("summary", "")
|
||||
if summary:
|
||||
reason = summary[:80].replace("\n", " ")
|
||||
if level == "high" and not expired:
|
||||
reason_clean = reason.replace("【高风险】", "").strip()[:60]
|
||||
log(False, f"🔴 宏观风险HIGH: {reason_clean}")
|
||||
elif level == "high" and expired:
|
||||
log(True, f"⏳ 宏观风险HIGH已过期(无新信号超过15分钟)")
|
||||
elif level == "medium":
|
||||
log(True, f"⚠️ 宏观风险MEDIUM: {reason}")
|
||||
else:
|
||||
log(True, "无宏观风险状态文件(可能未生成)")
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def write_todos():
|
||||
if not ISSUES:
|
||||
return
|
||||
for msg in ISSUES:
|
||||
title = f"[盘中自检] {msg}"
|
||||
try:
|
||||
conn = sqlite3.connect(str(DB_PATH), timeout=10)
|
||||
conn.execute("PRAGMA busy_timeout=5000")
|
||||
# 宏观风险HIGH去重:只要有pending/in_progress的宏观风险TODO,不再新增
|
||||
if "宏观风险HIGH" in msg:
|
||||
exist = conn.execute(
|
||||
"SELECT id FROM todos WHERE title LIKE '%宏观风险HIGH%' AND status IN ('pending','in_progress') LIMIT 1"
|
||||
).fetchone()
|
||||
else:
|
||||
exist = conn.execute(
|
||||
"SELECT id FROM todos WHERE title=? AND status IN ('pending','in_progress')", (title,)
|
||||
).fetchone()
|
||||
if not exist:
|
||||
conn.execute(
|
||||
"INSERT INTO todos (title, description, priority, source, status, fix_action) "
|
||||
"VALUES (?, ?, 'high', 'intraday_check', 'pending', NULL)",
|
||||
(title, f"盘中自动发现: {msg}"))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def main():
|
||||
now = datetime.now()
|
||||
# 只在交易时段运行
|
||||
if now.weekday() >= 5 or now.hour < 9 or now.hour >= 15:
|
||||
print("[SILENT] 非交易时段")
|
||||
return
|
||||
|
||||
check_bots()
|
||||
check_gateways()
|
||||
check_xiaoguo()
|
||||
if 9 <= now.hour < 16:
|
||||
check_price_monitor()
|
||||
check_signal_pipeline()
|
||||
|
||||
write_todos()
|
||||
|
||||
if ISSUES:
|
||||
print(f"盘中自检 | {now.strftime('%H:%M')} | {len(ISSUES)}项异常:")
|
||||
for i in ISSUES:
|
||||
print(f" ⚠️ {i}")
|
||||
else:
|
||||
print(f"[SILENT] 盘中自检通过 | {OK_COUNT}项正常")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -1,806 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""price_monitor.py — 高频价格监控脚本(批量版)
|
||||
规则:进入区间报一次,离开区间报一次,中间不重复。
|
||||
每次运行时一次性刷新所有持仓+自选股的实时价。
|
||||
"""
|
||||
import json
|
||||
import urllib.request
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
# ── MoFin unified model ──────────────────────────────────────────────
|
||||
sys.path.insert(0, "/home/hmo/MoFin")
|
||||
from mo_models import is_hk_stock, get_hk_rate, calc_total_assets, calc_total_mv, calc_position_pct
|
||||
from mofin_db import get_conn, write_holdings_batch, write_portfolio_summary, write_price_event, write_watchlist_stock, write_holding_strategy, write_holding_strategy
|
||||
from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
|
||||
DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
|
||||
PORTFOLIO_PATH = "/home/hmo/web-dashboard/data/portfolio.json"
|
||||
WATCHLIST_PATH = "/home/hmo/web-dashboard/data/watchlist.json"
|
||||
BREACH_PATH = "/home/hmo/.hermes/zone_breach.json"
|
||||
STATE_PATH = os.path.expanduser("~/.hermes/price_trigger_state.json")
|
||||
EVENTS_PATH = "/home/hmo/web-dashboard/data/price_events.json"
|
||||
|
||||
# 策略重评依赖(技术面驱动,非机械百分比)
|
||||
sys.path.insert(0, "/home/hmo/web-dashboard")
|
||||
try:
|
||||
from strategy_lifecycle import reassess_strategy
|
||||
HAS_REASSESS = True
|
||||
except ImportError:
|
||||
HAS_REASSESS = False
|
||||
|
||||
try:
|
||||
HK_RATE = get_hk_rate()
|
||||
except Exception:
|
||||
HK_RATE = 0.87 # ultimate fallback
|
||||
|
||||
# 分支系统与情景检测
|
||||
try:
|
||||
sys.path.insert(0, '/home/hmo/MoFin')
|
||||
from strategy_tree import detect_scenario, evaluate_branches
|
||||
HAS_TREE = True
|
||||
except Exception:
|
||||
HAS_TREE = False
|
||||
def detect_scenario(): return {}
|
||||
def evaluate_branches(*a, **kw): return []
|
||||
|
||||
# 情景缓存(每次run_once刷新)
|
||||
_SCENARIO_CACHE = {}
|
||||
_BRANCH_CACHE = {} # code -> branches list
|
||||
|
||||
UA = "Mozilla/5.0"
|
||||
|
||||
# ── 批量拉取价格 ──────────────────────────────────────────────────────────
|
||||
|
||||
def fetch_all_prices(codes):
|
||||
"""腾讯批量行情API:仅用于A股(沪市/深市)
|
||||
A股:sh600110 / sz000001
|
||||
港股已迁移至 fetch_hk_eastmoney()(东方财富实时行情)
|
||||
返回 {code: (price, change, change_pct)}
|
||||
"""
|
||||
if not codes:
|
||||
return {}
|
||||
|
||||
# 只处理A股(6位代码),港股走东方财富
|
||||
a_codes = [c for c in codes if len(str(c).strip()) == 6]
|
||||
if not a_codes:
|
||||
return {}
|
||||
|
||||
symbols = []
|
||||
code_map = {}
|
||||
for code in a_codes:
|
||||
code_s = str(code).strip()
|
||||
if code_s.startswith(('5', '6', '9')):
|
||||
sym = f"sh{code_s}"
|
||||
else:
|
||||
sym = f"sz{code_s}"
|
||||
symbols.append(sym)
|
||||
code_map[sym] = code_s
|
||||
|
||||
url = f"http://qt.gtimg.cn/q={','.join(symbols)}"
|
||||
try:
|
||||
req = urllib.request.Request(url, headers={"User-Agent": UA})
|
||||
with urllib.request.urlopen(req, timeout=10) as r:
|
||||
text = r.read().decode("gbk")
|
||||
except Exception as e:
|
||||
print(f"⚠️ 腾讯A股拉取失败: {e}", file=sys.stderr)
|
||||
return {}
|
||||
|
||||
results = {}
|
||||
for line in text.strip().split("\n"):
|
||||
line = line.strip()
|
||||
if not line or "=" not in line:
|
||||
continue
|
||||
try:
|
||||
raw_value = line.split("=", 1)[1].strip().strip('"').strip(";")
|
||||
fields = raw_value.split("~")
|
||||
if len(fields) < 6:
|
||||
continue
|
||||
sym = line.split("=", 1)[0].strip().lstrip("v_")
|
||||
orig_code = code_map.get(sym)
|
||||
if not orig_code:
|
||||
continue
|
||||
price = float(fields[3]) if fields[3] else 0
|
||||
prev_close = float(fields[4]) if fields[4] else 0
|
||||
change = price - prev_close if prev_close > 0 else 0
|
||||
change_pct = fields[32] if len(fields) > 32 and fields[32] else "0"
|
||||
results[orig_code] = (price, change, change_pct)
|
||||
except (ValueError, IndexError):
|
||||
continue
|
||||
|
||||
return results
|
||||
|
||||
|
||||
# ── 港股实时行情(新浪财经批量版,实时,无延迟)─────────────────────────────
|
||||
|
||||
def fetch_hk_sina_batch(codes):
|
||||
"""新浪财经港股批量实时行情 — 一次HTTP请求获取全部港股。
|
||||
|
||||
新浪港股API(hq.sinajs.cn)支持批量查询,返回实时数据。
|
||||
对比东财逐股查询(0.2s间隔×17只=3.4s),新浪1次请求搞定。
|
||||
|
||||
API: https://hq.sinajs.cn/list=hk00700,hk09988
|
||||
格式: hq_str_hk00700="TENCENT,腾讯控股,当前价,昨收,开盘,最高,最低,涨跌额,涨跌幅,..."
|
||||
|
||||
返回 {code: (price, change, change_pct)}
|
||||
"""
|
||||
if not codes:
|
||||
return {}
|
||||
|
||||
hk_codes = [str(c).strip() for c in codes if len(str(c).strip()) <= 5]
|
||||
if not hk_codes:
|
||||
return {}
|
||||
|
||||
symbols = [f"hk{c}" for c in hk_codes]
|
||||
url = f"https://hq.sinajs.cn/list={','.join(symbols)}"
|
||||
|
||||
try:
|
||||
# 新浪要求有 Referer,且需绕过系统代理(某些环境下东财/新浪走代理会断连)
|
||||
proxy_handler = urllib.request.ProxyHandler({})
|
||||
opener = urllib.request.build_opener(proxy_handler)
|
||||
req = urllib.request.Request(url, headers={
|
||||
"User-Agent": "Mozilla/5.0",
|
||||
"Referer": "https://finance.sina.com.cn",
|
||||
})
|
||||
with opener.open(req, timeout=10) as r:
|
||||
text = r.read().decode("gbk")
|
||||
except Exception as e:
|
||||
print(f"⚠️ 新浪港股批量拉取失败: {e}", file=sys.stderr)
|
||||
return {}
|
||||
|
||||
results = {}
|
||||
for line in text.strip().split("\n"):
|
||||
line = line.strip()
|
||||
if "=" not in line:
|
||||
continue
|
||||
try:
|
||||
code = line.split("=", 1)[0].replace("hq_str_hk", "").replace("var ", "").strip()
|
||||
raw = line.split("=", 1)[1].strip().strip('"').strip(";")
|
||||
fields = raw.split(",")
|
||||
if len(fields) < 9:
|
||||
continue
|
||||
price = float(fields[2]) if fields[2] else 0
|
||||
prev_close = float(fields[3]) if fields[3] else 0
|
||||
change_amt = float(fields[7]) if fields[7] else 0
|
||||
change_pct = fields[8] if fields[8] else "0"
|
||||
# 新浪 field[2] 可能非实时最新价,用 prev_close + change 计算更准确
|
||||
if prev_close > 0 and abs(change_amt) > 0:
|
||||
price = round(prev_close + change_amt, 2)
|
||||
change = round(change_amt, 2)
|
||||
if price > 0:
|
||||
results[code] = (price, change, change_pct)
|
||||
except (ValueError, IndexError):
|
||||
continue
|
||||
|
||||
return results
|
||||
|
||||
|
||||
# ── 港股备用通道(东方财富逐股 + 腾讯15min延迟)───────────────────────────
|
||||
|
||||
def fetch_hk_eastmoney_fallback(codes):
|
||||
"""东方财富港股实时行情(备用通道),逐股查询、间隔1秒避免限流。
|
||||
|
||||
FTP 说明:港股限流严重,不适合主通道,降级为备用。
|
||||
建议用上面的 fetch_hk_sina_batch() 做主通道。
|
||||
|
||||
返回 {code: (price, change, change_pct)}
|
||||
Fallback: 仍失败时回退到腾讯 qt.gtimg.cn(15分钟延迟)
|
||||
"""
|
||||
if not codes:
|
||||
return {}
|
||||
|
||||
hk_codes = [str(c).strip() for c in codes if len(str(c).strip()) <= 5]
|
||||
if not hk_codes:
|
||||
return {}
|
||||
|
||||
results = {}
|
||||
|
||||
# 东方财富逐股查询,1秒间隔避免限流
|
||||
for code in hk_codes:
|
||||
try:
|
||||
url = (f"https://push2.eastmoney.com/api/qt/stock/get"
|
||||
f"?secid=116.{code}"
|
||||
f"&fields=f43,f170,f60,f57,f58"
|
||||
f"&fltt=2")
|
||||
proxy_handler = urllib.request.ProxyHandler({})
|
||||
opener = urllib.request.build_opener(proxy_handler)
|
||||
req = urllib.request.Request(url, headers={
|
||||
"User-Agent": UA,
|
||||
"Referer": "https://quote.eastmoney.com/",
|
||||
})
|
||||
with opener.open(req, timeout=5) as r:
|
||||
resp = json.loads(r.read().decode("utf-8"))
|
||||
|
||||
if resp.get("rc") != 0:
|
||||
continue
|
||||
item = resp.get("data", {})
|
||||
if not item:
|
||||
continue
|
||||
price = float(item.get("f43", 0)) if item.get("f43") else 0
|
||||
prev_close = float(item.get("f60", 0)) if item.get("f60") else 0
|
||||
change = round(price - prev_close, 2) if prev_close > 0 else 0
|
||||
change_pct = str(item.get("f170", "0"))
|
||||
if price > 0:
|
||||
results[code] = (price, change, change_pct)
|
||||
time.sleep(1.0) # 1秒间隔,大幅降低限流概率
|
||||
except Exception as e:
|
||||
print(f" [东财备用 {code}] {e}", file=sys.stderr)
|
||||
continue
|
||||
|
||||
# Fallback: 腾讯 qt.gtimg.cn(15分钟延迟)
|
||||
missing = [c for c in hk_codes if c not in results]
|
||||
if missing:
|
||||
try:
|
||||
fallback = _fetch_hk_tencent_fallback(missing)
|
||||
results.update(fallback)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def _fetch_hk_tencent_fallback(codes):
|
||||
"""腾讯港股行情(15分钟延迟,仅作 fallback)"""
|
||||
symbols = [f"hk{c}" for c in codes]
|
||||
url = f"http://qt.gtimg.cn/q={','.join(symbols)}"
|
||||
req = urllib.request.Request(url, headers={"User-Agent": UA})
|
||||
with urllib.request.urlopen(req, timeout=10) as r:
|
||||
text = r.read().decode("gbk")
|
||||
|
||||
code_map = {f"hk{c}": c for c in codes}
|
||||
results = {}
|
||||
for line in text.strip().split("\n"):
|
||||
if "=" not in line:
|
||||
continue
|
||||
try:
|
||||
raw = line.split("=", 1)[1].strip().strip('"').strip(";")
|
||||
fields = raw.split("~")
|
||||
if len(fields) < 6:
|
||||
continue
|
||||
sym = line.split("=", 1)[0].strip().lstrip("v_")
|
||||
orig = code_map.get(sym)
|
||||
if not orig:
|
||||
continue
|
||||
price = float(fields[3]) if fields[3] else 0
|
||||
prev_close = float(fields[4]) if fields[4] else 0
|
||||
change = price - prev_close if prev_close > 0 else 0
|
||||
change_pct = fields[32] if len(fields) > 32 and fields[32] else "0"
|
||||
results[orig] = (price, change, change_pct)
|
||||
except (ValueError, IndexError):
|
||||
continue
|
||||
return results
|
||||
|
||||
|
||||
def refresh_data_prices():
|
||||
"""一次性刷新portfolio.json和watchlist.json的所有实时价"""
|
||||
all_codes = set()
|
||||
|
||||
# 收集所有需要拉取的代码
|
||||
try:
|
||||
pf = read_portfolio()
|
||||
for s in pf.get('holdings', []):
|
||||
all_codes.add(s['code'])
|
||||
except:
|
||||
pf = {"holdings": []}
|
||||
|
||||
try:
|
||||
wl = read_watchlist()
|
||||
for s in wl.get('stocks', []):
|
||||
all_codes.add(s['code'])
|
||||
except:
|
||||
wl = {"stocks": []}
|
||||
|
||||
if not all_codes:
|
||||
return 0
|
||||
|
||||
# 分批拉取:A股走腾讯(实时) + 港股走新浪批量(实时,无限流)
|
||||
all_list = list(all_codes)
|
||||
prices = fetch_all_prices(all_list) # A股(腾讯,实时)
|
||||
hk_prices = fetch_hk_sina_batch(all_list) # 港股(新浪批量,实时)
|
||||
# 新浪未覆盖的走备用通道(东财逐股→腾讯15min延迟)
|
||||
hk_codes_missing = [c for c in all_list if len(str(c).strip()) <= 5 and c not in hk_prices]
|
||||
if hk_codes_missing:
|
||||
fallback = fetch_hk_eastmoney_fallback(hk_codes_missing)
|
||||
hk_prices.update(fallback)
|
||||
prices.update(hk_prices)
|
||||
updated = 0
|
||||
|
||||
# 保存全量实时价快照(供报告管道消费,确保分析用最新数据)
|
||||
try:
|
||||
live = {"updated_at": datetime.now().isoformat(), "prices": {}}
|
||||
for code in all_codes:
|
||||
if code in prices:
|
||||
p, c, chg = prices[code]
|
||||
live["prices"][code] = {"price": p, "change_pct": chg}
|
||||
# json.dump(live, ...) — 已迁移到 DB,见根 price_monitor.py
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 更新portfolio(只在价格变化时写入,避免触发文件变更通知)
|
||||
changed = False
|
||||
for s in pf.get('holdings', []):
|
||||
if s['code'] in prices:
|
||||
price, _, change_pct = prices[s['code']]
|
||||
if price > 0:
|
||||
# 港股:API返回HKD,需转RMB
|
||||
if is_hk_stock(s['code']):
|
||||
price = round(price * HK_RATE, 2)
|
||||
old = s.get('price', 0)
|
||||
if abs(old - price) > 0.001:
|
||||
s['price'] = round(price, 2)
|
||||
s['change_pct'] = float(change_pct) if change_pct else 0
|
||||
updated += 1
|
||||
changed = True
|
||||
if changed:
|
||||
pf['updated_at'] = datetime.now().strftime('%Y-%m-%d %H:%M')
|
||||
pf['total_mv'] = calc_total_mv(pf.get('holdings', []))
|
||||
pf['total_assets'] = calc_total_assets(pf)
|
||||
pf['position_pct'] = calc_position_pct(pf)
|
||||
# DB 写入(替代 json.dump,强制币种约束)
|
||||
try:
|
||||
conn = get_conn()
|
||||
write_holdings_batch(conn, pf['holdings'])
|
||||
write_portfolio_summary(conn, pf)
|
||||
conn.close()
|
||||
except Exception as e:
|
||||
print(f" [DB写入失败] {e}", flush=True)
|
||||
# [migrated to DB] — JSON cold backup removed
|
||||
# json.dump(pf, open(PORTFOLIO_PATH, 'w'), ensure_ascii=False, indent=2)
|
||||
elif pf.get('updated_at'):
|
||||
try:
|
||||
last_ts = datetime.strptime(pf['updated_at'], '%Y-%m-%d %H:%M')
|
||||
if (datetime.now() - last_ts).total_seconds() > 600:
|
||||
pf['updated_at'] = datetime.now().strftime('%Y-%m-%d %H:%M')
|
||||
# DB 写入(时间戳更新)
|
||||
try:
|
||||
conn2 = get_conn()
|
||||
write_portfolio_summary(conn2, pf)
|
||||
conn2.close()
|
||||
except Exception:
|
||||
pass
|
||||
# [migrated to DB] — cold backup removed
|
||||
# json.dump(pf, open(PORTFOLIO_PATH, 'w'), ensure_ascii=False, indent=2)
|
||||
except:
|
||||
pass
|
||||
|
||||
# 更新watchlist(只在价格变化时写入)
|
||||
changed = False
|
||||
for s in wl.get('stocks', []):
|
||||
if s['code'] in prices:
|
||||
price, _, change_pct = prices[s['code']]
|
||||
if price > 0:
|
||||
# 港股:API返回HKD,需转RMB
|
||||
if is_hk_stock(s['code']):
|
||||
price = round(price * HK_RATE, 2)
|
||||
old = s.get('price', 0)
|
||||
if abs(old - price) > 0.001:
|
||||
s['price'] = round(price, 2)
|
||||
s['change_pct'] = float(change_pct) if change_pct else 0
|
||||
updated += 1
|
||||
changed = True
|
||||
if changed:
|
||||
wl['updated_at'] = datetime.now().isoformat()
|
||||
# DB 写入(替代 json.dump)
|
||||
try:
|
||||
conn = get_conn()
|
||||
for s in wl.get('stocks', []):
|
||||
s['currency'] = 'CNY' # 自选股价格统一CNY
|
||||
write_watchlist_stock(conn, s)
|
||||
conn.close()
|
||||
except Exception as e:
|
||||
print(f" [DB watchlist写入失败] {e}", flush=True)
|
||||
# [migrated to DB] — cold backup removed; DB writes above
|
||||
# json.dump(wl, open(WATCHLIST_PATH, 'w'), ensure_ascii=False, indent=2)
|
||||
|
||||
# --- 汇总值重算(使用 mo_models 唯一公式)---
|
||||
try:
|
||||
live_market_value = calc_total_mv(pf.get('holdings', []))
|
||||
old_mv = pf.get('total_mv', 0)
|
||||
|
||||
if abs(old_mv - live_market_value) > 0.01:
|
||||
pf['total_mv'] = round(live_market_value, 2)
|
||||
|
||||
pf['total_assets'] = calc_total_assets(pf)
|
||||
if pf['total_assets'] > 0:
|
||||
pf['position_pct'] = calc_position_pct(pf)
|
||||
pf['updated_at'] = datetime.now().strftime('%Y-%m-%d %H:%M')
|
||||
# DB 写入
|
||||
try:
|
||||
conn = get_conn()
|
||||
write_portfolio_summary(conn, pf)
|
||||
conn.close()
|
||||
except Exception as e:
|
||||
print(f" [DB汇总写入失败] {e}", flush=True)
|
||||
# [migrated to DB] — cold backup removed; DB writes above
|
||||
# json.dump(pf, open(PORTFOLIO_PATH, 'w'), ensure_ascii=False, indent=2)
|
||||
except Exception as e:
|
||||
print(f" [汇总重算失败] {e}", flush=True)
|
||||
# --- 结束汇总重算 ---
|
||||
|
||||
return updated
|
||||
|
||||
|
||||
# ── 分支系统辅助函数 ──────────────────────────────────────────────────────
|
||||
|
||||
def _branch_alert_suffix(code, price, shares=0, cost=0):
|
||||
"""返回分支信息后缀:「 | 情景→动作」"""
|
||||
if not HAS_TREE or not _SCENARIO_CACHE.get('id'):
|
||||
return ""
|
||||
try:
|
||||
sc_id = _SCENARIO_CACHE['id']
|
||||
results = evaluate_branches(code, sc_id, price, shares, cost)
|
||||
for r in results:
|
||||
if r.get('applicable'):
|
||||
_record_branch_trigger(code, r.get('branch_id',''), price)
|
||||
branch_action = r.get('action_type', r.get('action', 'hold'))
|
||||
return f" | {sc_id}→{branch_action}"
|
||||
except Exception:
|
||||
pass
|
||||
return ""
|
||||
|
||||
|
||||
def _record_branch_trigger(code, branch_id, price):
|
||||
"""记录分支触发事件(自成长:trigger_count+1)"""
|
||||
try:
|
||||
raw = read_decisions()
|
||||
for d in raw.get('decisions', []):
|
||||
if d.get('code') == code and d.get('strategy_tree',{}).get('branches'):
|
||||
for b in d['strategy_tree']['branches']:
|
||||
if b['id'] == branch_id:
|
||||
b.setdefault('trigger_count', 0)
|
||||
b['trigger_count'] += 1
|
||||
b['last_trigger_price'] = round(price, 2)
|
||||
b['last_triggered'] = datetime.now().isoformat()
|
||||
break
|
||||
# DB 写入(替代 json.dump)
|
||||
try:
|
||||
conn3 = get_conn()
|
||||
for d in raw.get('decisions', []):
|
||||
write_holding_strategy(conn3, d.get('code', ''), d.get('name', ''), d)
|
||||
conn3.close()
|
||||
except Exception:
|
||||
pass
|
||||
# [migrated to DB] — cold backup removed
|
||||
# json.dump(raw, open(DECISIONS_PATH, 'w'), ensure_ascii=False, indent=2)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
# ── 区间偏离检测 ──────────────────────────────────────────────────────────
|
||||
|
||||
def load_state():
|
||||
try:
|
||||
with open(STATE_PATH) as f:
|
||||
return json.load(f)
|
||||
except:
|
||||
return {}
|
||||
|
||||
def save_state(state):
|
||||
os.makedirs(os.path.dirname(STATE_PATH), exist_ok=True)
|
||||
with open(STATE_PATH, 'w') as f:
|
||||
json.dump(state, f, ensure_ascii=False, indent=2)
|
||||
|
||||
def load_breaches():
|
||||
try:
|
||||
with open(BREACH_PATH) as f:
|
||||
return json.load(f)
|
||||
except:
|
||||
return {}
|
||||
|
||||
def save_breaches(data):
|
||||
os.makedirs(os.path.dirname(BREACH_PATH), exist_ok=True)
|
||||
with open(BREACH_PATH, 'w') as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
|
||||
|
||||
def load_events():
|
||||
try:
|
||||
with open(EVENTS_PATH) as f:
|
||||
return json.load(f)
|
||||
except:
|
||||
return {"events": []}
|
||||
|
||||
|
||||
def save_events(events):
|
||||
os.makedirs(os.path.dirname(EVENTS_PATH), exist_ok=True)
|
||||
with open(EVENTS_PATH, 'w') as f:
|
||||
json.dump(events, f, ensure_ascii=False, indent=2)
|
||||
|
||||
|
||||
def record_event(code, name, event_type, price, trigger_value, event_label=""):
|
||||
"""记录一次价格触发事件到 price_events.json + SQLite"""
|
||||
events = load_events()
|
||||
now = datetime.now().isoformat()
|
||||
events["events"].append({
|
||||
"code": code,
|
||||
"name": name,
|
||||
"event_type": event_type, # entry_zone, stop_loss, take_profit, exit_zone
|
||||
"price": round(price, 2),
|
||||
"trigger_value": trigger_value,
|
||||
"event_label": event_label,
|
||||
"timestamp": now,
|
||||
"date": datetime.now().strftime("%Y-%m-%d"),
|
||||
})
|
||||
# 保留最近10000条
|
||||
events["events"] = events["events"][-10000:]
|
||||
save_events(events)
|
||||
|
||||
# ── SQLite 双写 ──
|
||||
try:
|
||||
from mofin_db import get_conn, init_all_tables, write_price_event
|
||||
conn = get_conn()
|
||||
init_all_tables(conn)
|
||||
write_price_event(conn, code, name, event_type, price, trigger_value, event_label)
|
||||
conn.close()
|
||||
except Exception:
|
||||
pass # SQLite 写入失败不影响主流程
|
||||
|
||||
|
||||
def get_trigger_zones(d):
|
||||
"""返回该decision所有可监控的区间列表,从顶层字段读取"""
|
||||
zones = []
|
||||
is_holding = d.get('shares', 0) > 0
|
||||
# 买入区间(自选和持仓都监控)
|
||||
el = d.get("entry_low", 0)
|
||||
eh = d.get("entry_high", 0)
|
||||
if el and eh and float(el) > 0 and float(eh) > 0:
|
||||
try:
|
||||
zones.append(("entry_zone", "买入区间", float(el), float(eh)))
|
||||
except:
|
||||
pass
|
||||
# 止损+止盈(只有持仓才监控,自选无意义)
|
||||
if is_holding:
|
||||
sl = d.get("stop_loss", 0)
|
||||
if sl and float(sl) > 0:
|
||||
try:
|
||||
zones.append(("stop_loss", "止损", 0, float(sl)))
|
||||
except:
|
||||
pass
|
||||
tp = d.get("take_profit", 0)
|
||||
if tp and float(tp) > 0:
|
||||
try:
|
||||
zones.append(("take_profit_zone", "止盈区间", 0, float(tp)))
|
||||
except:
|
||||
pass
|
||||
return zones
|
||||
|
||||
|
||||
def run_once(round_label=""):
|
||||
"""执行一轮完整的监控流程"""
|
||||
global _SCENARIO_CACHE, _BRANCH_CACHE
|
||||
label = f" [{round_label}]" if round_label else ""
|
||||
start = time.time()
|
||||
|
||||
# 刷新情景与分支缓存(每轮更新)
|
||||
_SCENARIO_CACHE = detect_scenario() if HAS_TREE else {}
|
||||
_BRANCH_CACHE = {}
|
||||
try:
|
||||
raw = read_decisions()
|
||||
for d in raw.get('decisions', []):
|
||||
tree = d.get('strategy_tree', {})
|
||||
if tree and tree.get('branches'):
|
||||
_BRANCH_CACHE[d['code']] = tree['branches']
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# === 第一步:一次性刷新所有价格 ===
|
||||
refreshed = refresh_data_prices()
|
||||
|
||||
# === 第二步:检查触发条件 ===
|
||||
try:
|
||||
dec = read_decisions()
|
||||
except:
|
||||
print(f"❌{label} 无法读取decisions.json", file=sys.stderr)
|
||||
return
|
||||
|
||||
active = [d for d in dec.get("decisions", []) if d.get("status") in ("active", "updated")]
|
||||
state = load_state()
|
||||
outputs = []
|
||||
state_updated = False
|
||||
|
||||
# 收集所有需要检查的代码
|
||||
check_codes = set()
|
||||
for d in active:
|
||||
if get_trigger_zones(d):
|
||||
check_codes.add(d["code"])
|
||||
|
||||
# 批量拉取这些股票的价格
|
||||
prices = fetch_all_prices(list(check_codes))
|
||||
|
||||
for d in active:
|
||||
code = d["code"]
|
||||
|
||||
zones = get_trigger_zones(d)
|
||||
if not zones:
|
||||
continue
|
||||
|
||||
price_info = prices.get(code)
|
||||
if not price_info:
|
||||
continue
|
||||
price, _, _ = price_info
|
||||
if price == 0:
|
||||
continue
|
||||
|
||||
name = d.get("name", code)
|
||||
if code not in state:
|
||||
state[code] = {}
|
||||
|
||||
for key, label, lo, hi in zones:
|
||||
in_zone = lo <= price <= hi
|
||||
prev_in_zone = state[code].get(key, None)
|
||||
|
||||
if in_zone and prev_in_zone != True:
|
||||
if key == "stop_loss":
|
||||
branch_sfx = _branch_alert_suffix(code, price, d.get('shares',0), d.get('cost',0))
|
||||
outputs.append(f"⚠️ {name}({code}) {price} → 跌破止损{hi}!{branch_sfx}")
|
||||
record_event(code, name, "stop_loss", price, str(hi))
|
||||
else:
|
||||
extra = ""
|
||||
if "_price" in key:
|
||||
batch_shares = d.get(key.replace("_price", "_shares"), "")
|
||||
action = d.get(key.replace("_price", "_action"), "")
|
||||
if batch_shares:
|
||||
extra = f" {action}{batch_shares}股" if action else f" {batch_shares}股"
|
||||
elif key in ("take_profit_zone",):
|
||||
act = d.get("take_profit_action", "")
|
||||
if act:
|
||||
extra = f"({act})"
|
||||
branch_sfx = _branch_alert_suffix(code, price, d.get('shares',0), d.get('cost',0))
|
||||
outputs.append(f"⚡ {name}({code}) {price} → 进入{label}{lo}~{hi}{extra}{branch_sfx}")
|
||||
record_event(code, name, "entry_zone", price, f"{lo}~{hi}", label)
|
||||
state[code][key] = True
|
||||
state_updated = True
|
||||
|
||||
elif not in_zone and prev_in_zone == True:
|
||||
if key != "stop_loss":
|
||||
outputs.append(f"📌 {name}({code}) {price} → 离开{label}{lo}~{hi}")
|
||||
state[code][key] = False
|
||||
state_updated = True
|
||||
|
||||
# === 第三步:买入区偏离检测 + 自动重评 ===
|
||||
reassesed_codes = []
|
||||
for d in active:
|
||||
code = d["code"]
|
||||
name = d.get("name", code)
|
||||
price_info = prices.get(code)
|
||||
if not price_info:
|
||||
continue
|
||||
price, _, _ = price_info
|
||||
if price == 0:
|
||||
continue
|
||||
|
||||
# 从 decisions.json 中读取 analysis 的买入区
|
||||
entry_low = d.get("entry_low", 0)
|
||||
entry_high = d.get("entry_high", 0)
|
||||
if not entry_low or not entry_high:
|
||||
continue
|
||||
|
||||
in_buy_zone = entry_low <= price <= entry_high
|
||||
prev_in_buy_zone = state.get(code, {}).get("__buy_zone", None)
|
||||
|
||||
# 状态变化时才触发:True→False离区 或 False→True进区
|
||||
# [2026-07-01 fix] prev_in_buy_zone is None(新加自选首次检测)
|
||||
# 也要触发——否则新自选全程不走重评,timing_signal卡在初始值
|
||||
if in_buy_zone and (prev_in_buy_zone == False or prev_in_buy_zone is None):
|
||||
# 进入买入区 → 触发技术面重评,更新止损/止盈/信号
|
||||
outputs.append(f"🔄 {name}({code}) {price} → 重新进入买入区{entry_low}~{entry_high},触发技术面重评")
|
||||
do_reassess = True
|
||||
elif not in_buy_zone and prev_in_buy_zone == True:
|
||||
# 离开买入区 → 立即重评,更新止损/止盈/区间
|
||||
outputs.append(f"🔄 {name}({code}) {price} → 离开买入区{entry_low}~{entry_high},立即技术面重评")
|
||||
do_reassess = True
|
||||
else:
|
||||
do_reassess = False
|
||||
|
||||
if do_reassess and HAS_REASSESS:
|
||||
try:
|
||||
cost = d.get("cost", 0) or 0
|
||||
shares = d.get("shares", 0) or 0
|
||||
profit_pct = (price - cost) / cost * 100 if cost else 0
|
||||
is_deep_loss = profit_pct < -20
|
||||
sentiment = "neutral"
|
||||
if d.get("tech_snapshot"):
|
||||
if "bearish" in d["tech_snapshot"]:
|
||||
sentiment = "bearish"
|
||||
elif "bullish" in d["tech_snapshot"]:
|
||||
sentiment = "bullish"
|
||||
|
||||
# 调用技术面驱动重评(非机械百分比)
|
||||
result = reassess_strategy(
|
||||
code, name, price, cost, shares,
|
||||
current_action=d.get("action", ""),
|
||||
volume_signal="中性", sentiment=sentiment,
|
||||
)
|
||||
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:
|
||||
outputs.append(f" ⚠️ 重评失败: {e}")
|
||||
|
||||
# 更新买入区状态
|
||||
if "__buy_zone" not in state.get(code, {}):
|
||||
if code not in state:
|
||||
state[code] = {}
|
||||
state[code]["__buy_zone"] = in_buy_zone
|
||||
state_updated = True
|
||||
|
||||
# 如果有重评过的股票,更新 decisions.json
|
||||
if reassesed_codes and HAS_REASSESS:
|
||||
try:
|
||||
# 重新 regenerate_all 只针对受影响的股票效率太低
|
||||
# 直接全量重评(regenerate_all 内部会批量拉价格、做技术分析)
|
||||
from strategy_lifecycle import regenerate_all
|
||||
r = regenerate_all(stdout=False)
|
||||
outputs.append(f" ✅ 策略已全量重评: {r.get('ok',0)}/{r.get('total',0)}成功")
|
||||
outputs.append(f" 📌 触发股票: {', '.join(reassesed_codes)}")
|
||||
except Exception as e:
|
||||
outputs.append(f" ⚠️ 全量重评失败: {e}")
|
||||
|
||||
# === 3.5 资金流异常检测(2026-06-27 新增)===
|
||||
try:
|
||||
from mofin_db import get_conn, read_capital_flow_cache
|
||||
cf = read_capital_flow_cache(get_conn())
|
||||
# 检查所有 active decision 中的资金流异常
|
||||
for d in active:
|
||||
code = d["code"]
|
||||
stock_cf = cf.get("stocks", {}).get(code, {})
|
||||
analysis = stock_cf.get("analysis", {})
|
||||
alerts = analysis.get("alerts", [])
|
||||
if alerts:
|
||||
name = d.get("name", code)
|
||||
for a in alerts:
|
||||
outputs.append(f" 💰 {name}({code}) {a}")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# === 第四步:情景变化检测 + 输出 → 直接推XMPP ===
|
||||
now_str = datetime.now().strftime("%H:%M:%S")
|
||||
elapsed = time.time() - start
|
||||
|
||||
# 情景变化检测(跨轮对比)
|
||||
if HAS_TREE and _SCENARIO_CACHE.get('id'):
|
||||
prev_scenario = state.get('_system', {}).get('last_scenario', '')
|
||||
curr_scenario = _SCENARIO_CACHE['id']
|
||||
if prev_scenario and curr_scenario != prev_scenario:
|
||||
combo = _SCENARIO_CACHE.get('combo_action', '')
|
||||
outputs.insert(0, f"🌀 情景切换: {prev_scenario}→{curr_scenario} | {combo}")
|
||||
if outputs:
|
||||
state.setdefault('_system', {})['last_scenario'] = curr_scenario
|
||||
state_updated = True
|
||||
elif not prev_scenario:
|
||||
state.setdefault('_system', {})['last_scenario'] = curr_scenario
|
||||
state_updated = True
|
||||
|
||||
if outputs:
|
||||
# 简短一行一个触发
|
||||
for o in outputs:
|
||||
print(o)
|
||||
# 推送XMPP(只推关键事件:止损跌破+情景切换+资金流异动,不推买入区进出/重评等操作细节)
|
||||
critical = [o for o in outputs if o.startswith(("⚠️", "🌀", "💰"))]
|
||||
if critical:
|
||||
try:
|
||||
body = "\n".join([f"{now_str}"] + critical)
|
||||
payload = json.dumps({
|
||||
"to": "hmo@yoin.fun", "body": body, "type": "chat",
|
||||
}).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
"http://127.0.0.1:5805/", data=payload,
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
urllib.request.urlopen(req, timeout=5)
|
||||
except Exception:
|
||||
pass
|
||||
# else: SILENT — 无触发,无输出,不推
|
||||
|
||||
if state_updated:
|
||||
save_state(state)
|
||||
|
||||
|
||||
def main():
|
||||
"""每cron触发跑一轮"""
|
||||
run_once()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -20,8 +20,6 @@ from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
from mofin_db import get_conn, write_holdings_batch, write_portfolio_summary, write_holding_strategy
|
||||
|
||||
PORTFOLIO_PATH = "/home/hmo/web-dashboard/data/portfolio.json"
|
||||
DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
|
||||
|
||||
def parse_args():
|
||||
args = {}
|
||||
|
||||
@@ -16,7 +16,6 @@ from datetime import datetime, timedelta
|
||||
from mo_data import read_decisions
|
||||
from mofin_db import get_conn, write_holding_strategy
|
||||
|
||||
DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
|
||||
PRUNE_LOG = "/home/hmo/MoFin/data/prune_log.json"
|
||||
|
||||
|
||||
@@ -33,10 +32,6 @@ def save_decisions(data):
|
||||
conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
# [migrated to DB] — cold backup removed
|
||||
# with open(DECISIONS_PATH, "w") as f:
|
||||
# json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
|
||||
|
||||
def main():
|
||||
data = load_decisions()
|
||||
|
||||
+284
-287
@@ -1,287 +1,284 @@
|
||||
#!/usr/bin/env python3
|
||||
"""stale_detector.py — 检查所有策略,标记价格偏离/过期的策略
|
||||
|
||||
读取 decisions.json 的扁平列表。自选策略和持仓策略分开判断。
|
||||
可被 cron no_agent 模式调用:stdout 注入到后续 LLM 分析。
|
||||
|
||||
输出格式:
|
||||
[FLAG] [自选/持仓] 股票名(代码) 价XX | 买入A~B | 问题
|
||||
|
||||
用法:
|
||||
python3 stale_detector.py
|
||||
"""
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
from datetime import datetime, timezone
|
||||
sys.path.insert(0, '/home/hmo/MoFin')
|
||||
from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
|
||||
DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
|
||||
PORTFOLIO_PATH = "/home/hmo/web-dashboard/data/portfolio.json"
|
||||
|
||||
|
||||
def fetch_prices(codes):
|
||||
"""统一价格源:优先 stock_quote.py,腾讯API降级为兜底"""
|
||||
if not codes:
|
||||
return {}
|
||||
# 尝试用 stock_quote.py 获取(脚本强制规范)
|
||||
try:
|
||||
import subprocess
|
||||
script = None
|
||||
for p in ["/home/hmo/MoFin/scripts/stock_quote.py", "/home/hmo/MoFin/stock_quote.py"]:
|
||||
if os.path.exists(p):
|
||||
script = p
|
||||
break
|
||||
if script:
|
||||
result = subprocess.run(
|
||||
[sys.executable, script] + [str(c) for c in codes],
|
||||
capture_output=True, text=True, timeout=30
|
||||
)
|
||||
if result.returncode == 0 and result.stdout.strip():
|
||||
results = {}
|
||||
for line in result.stdout.strip().split("\n"):
|
||||
if not line.strip():
|
||||
continue
|
||||
try:
|
||||
item = json.loads(line)
|
||||
code = str(item.get("code", ""))
|
||||
price = item.get("price")
|
||||
change = item.get("change_pct", 0)
|
||||
if code and price is not None:
|
||||
results[code] = (float(price), float(change))
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
continue
|
||||
if results:
|
||||
return results
|
||||
except Exception as e:
|
||||
print(f"[STALE] stock_quote.py 回退: {e}", file=sys.stderr)
|
||||
|
||||
# 兜底:腾讯API(不应依赖,仅作为最后手段)
|
||||
import urllib.request
|
||||
symbols, code_map = [], {}
|
||||
for c in codes:
|
||||
c = str(c).strip()
|
||||
p = "sh" if (len(c) == 6 and c[0] in "569") else "sz" if len(c) == 6 else "hk"
|
||||
sym = f"{p}{c}"
|
||||
symbols.append(sym)
|
||||
code_map[sym] = c
|
||||
try:
|
||||
req = urllib.request.Request(
|
||||
f"http://qt.gtimg.cn/q={','.join(symbols)}",
|
||||
headers={"User-Agent": "curl/7.81"},
|
||||
)
|
||||
with urllib.request.urlopen(req, timeout=10) as r:
|
||||
text = r.read().decode("gbk")
|
||||
except Exception as e:
|
||||
print(f"FETCH_FAIL (fallback): {e}", file=sys.stderr)
|
||||
return {}
|
||||
|
||||
results = {}
|
||||
for line in text.strip().split("\n"):
|
||||
if "=" not in line:
|
||||
continue
|
||||
try:
|
||||
raw = line.split("=", 1)[1].strip().strip('"').strip(";")
|
||||
fld = raw.split("~")
|
||||
if len(fld) < 6:
|
||||
continue
|
||||
sym = line.split("=", 1)[0].strip().lstrip("v_")
|
||||
oc = code_map.get(sym)
|
||||
if not oc:
|
||||
continue
|
||||
p = float(fld[3]) if fld[3] else 0
|
||||
c = fld[32] if len(fld) > 32 else "0"
|
||||
results[oc] = (p, c)
|
||||
except (ValueError, IndexError):
|
||||
continue
|
||||
return results
|
||||
|
||||
|
||||
def main():
|
||||
decisions_list = read_decisions()
|
||||
if not isinstance(decisions_list, list):
|
||||
decisions_list = decisions_list.get("decisions", []) if isinstance(decisions_list, dict) else []
|
||||
|
||||
# 只保留有买入区的条目,排除已关闭的(inactive/closed)
|
||||
EXCLUDED_STATUSES = ("closed", "inactive")
|
||||
to_check = [d for d in decisions_list if (d.get("entry_low") is not None or d.get("entry_high") is not None) and d.get("status") not in EXCLUDED_STATUSES]
|
||||
if not to_check:
|
||||
print("[SILENT] 无需要检查的策略")
|
||||
return 0
|
||||
|
||||
# ----- 组合级监测:读取总仓位 + 弱势比例 -----
|
||||
position_pct = 0
|
||||
cash = 0
|
||||
total_assets = 0
|
||||
try:
|
||||
pf = read_portfolio()
|
||||
position_pct = pf.get("position_pct", 0)
|
||||
cash = pf.get("cash", 0)
|
||||
total_assets = pf.get("total_assets", 0)
|
||||
except Exception:
|
||||
pass
|
||||
# 统计持仓策略中弱势/深套的比例
|
||||
weak_count = 0
|
||||
holding_count = 0
|
||||
for d in decisions_list:
|
||||
if d.get("type") == "持仓策略" and d.get("status") not in ("closed", "inactive"):
|
||||
holding_count += 1
|
||||
cat = d.get("stock_category", "")
|
||||
if cat in ("弱势", "深套"):
|
||||
weak_count += 1
|
||||
weak_ratio = (weak_count / holding_count * 100) if holding_count > 0 else 0
|
||||
|
||||
prices = fetch_prices([d["code"] for d in to_check])
|
||||
now = datetime.now(timezone.utc).astimezone()
|
||||
found = 0
|
||||
|
||||
for d in to_check:
|
||||
code = d["code"]
|
||||
name = d.get("name", code)
|
||||
el = d.get("entry_low")
|
||||
eh = d.get("entry_high")
|
||||
sl = d.get("stop_loss")
|
||||
tp = d.get("take_profit")
|
||||
ts = d.get("created_at") or d.get("timestamp") or d.get("updated_at", "")
|
||||
is_wl = "自选" in (d.get("type", ""))
|
||||
|
||||
pi = prices.get(code)
|
||||
if not pi:
|
||||
continue
|
||||
price, chg = pi
|
||||
if price <= 0:
|
||||
continue
|
||||
|
||||
issues, flags = [], []
|
||||
tag = "[自选]" if is_wl else "[持仓]"
|
||||
|
||||
# -- 偏离 --
|
||||
if is_wl and el and eh:
|
||||
# 读取 timing_signal 判断策略有效性(timing_signal 字段优先,fallback to action)
|
||||
current_str = d.get("current", "") or ""
|
||||
timing_signal = d.get("timing_signal", "") or current_str
|
||||
has_nonbuy_signal = any(kw in timing_signal for kw in [
|
||||
"等企稳再入", "等企稳", "弱势持有", "观望",
|
||||
"不建议买入", "谨慎买入",
|
||||
])
|
||||
|
||||
# 直接计算 R/R(不依赖文本匹配)
|
||||
rr_invalid = False
|
||||
if sl and sl > 0 and tp and tp > 0 and price > sl:
|
||||
rr = (tp - price) / (price - sl)
|
||||
if rr < 1.5:
|
||||
rr_invalid = True
|
||||
# 也检查 tp 是否接近或低于成本(微盈/浮亏止盈)
|
||||
cost = d.get("cost", 0)
|
||||
if cost and cost > 0 and tp <= cost * 1.05:
|
||||
rr_invalid = True
|
||||
|
||||
strategy_deficient = has_nonbuy_signal or rr_invalid
|
||||
# 对自选无止盈位的也标记(策略不完整)
|
||||
if not tp or tp == 0:
|
||||
strategy_deficient = True
|
||||
|
||||
if el <= price <= eh:
|
||||
flags.append("[WL_IN]")
|
||||
if strategy_deficient:
|
||||
flags.append("[STRATEGY_STALE]")
|
||||
prefix = "⚠️仓位挤占 " if position_pct > 80 else ""
|
||||
issues.append(f"[STRATEGY_STALE] {prefix}价{price:.2f}在买入区{el}~{eh}但策略不完整({'RR='+f'{rr:.2f}<1.5' if rr_invalid else '无止盈位' if not tp else '非买入信号'}),买入区需重评")
|
||||
else:
|
||||
prefix = "⚠️仓位挤占 " if position_pct > 80 else ""
|
||||
issues.append(f"[PUSH] {prefix}价{price:.2f}入买入区{el}~{eh}")
|
||||
elif price > eh * 1.35:
|
||||
flags.append("[WL_HIGH]")
|
||||
issues.append(f"价{price:.2f}高出买入区+{((price/eh)-1)*100:.0f}%,买入区需重评")
|
||||
elif price > eh * 1.20:
|
||||
flags.append("[WL_DRIFT]")
|
||||
issues.append(f"价{price:.2f}高于买入区+{((price/eh)-1)*100:.0f}%")
|
||||
elif not is_wl and eh:
|
||||
dp = (price / eh - 1) * 100
|
||||
if dp > 35:
|
||||
flags.append("[SEVERE]")
|
||||
issues.append(f"偏离买入区上沿+{dp:.0f}%")
|
||||
elif dp > 20:
|
||||
flags.append("[DRIFT]")
|
||||
issues.append(f"偏离买入区上沿+{dp:.0f}%")
|
||||
elif dp > 10:
|
||||
flags.append("[WARN]")
|
||||
issues.append(f"偏离买入区上沿+{dp:.0f}%")
|
||||
# 持仓在买入区内但 R/R 不达标
|
||||
if el and sl and sl > 0 and tp and tp > 0 and price > sl:
|
||||
if el <= price <= eh:
|
||||
rr = (tp - price) / (price - sl)
|
||||
if rr < 1.5:
|
||||
flags.append("[RR_WARN]")
|
||||
issues.append(f"买入区内RR仅{rr:.2f}<1.5,策略需重评")
|
||||
|
||||
# -- 距止损/止盈(仅持仓) --
|
||||
if not is_wl:
|
||||
if sl and sl > 0:
|
||||
dsl = (price / sl - 1) * 100
|
||||
if dsl < 5:
|
||||
# 成本基准校验:浮盈>5%时止损是利润保护,不是危险信号
|
||||
# (mirrors NEAR_TP cost_check logic at line 195-198)
|
||||
cost = d.get("cost")
|
||||
if cost and cost > 0 and price > cost * 1.05:
|
||||
flags.append("[PROFIT_PROTECT]")
|
||||
pnl = (price / cost - 1) * 100
|
||||
issues.append(f"距止损仅{dsl:.1f}%(利润保护,浮盈{pnl:.0f}%)")
|
||||
else:
|
||||
flags.append("[NEAR_SL]")
|
||||
issues.append(f"距止损仅{dsl:.1f}%")
|
||||
if tp and tp > 0:
|
||||
dtp = (tp / price - 1) * 100
|
||||
if dtp < 5:
|
||||
# 成本基准校验:止盈标记只有在盈利≥5%时才有效
|
||||
cost_check = True
|
||||
cost = d.get("cost")
|
||||
if cost and cost > 0 and price < cost * 1.05:
|
||||
cost_check = False
|
||||
if cost_check:
|
||||
flags.append("[NEAR_TP]")
|
||||
issues.append(f"距止盈仅{dtp:.1f}%")
|
||||
|
||||
# -- 过期 --
|
||||
stale_limit = 30 if is_wl else 14
|
||||
if ts:
|
||||
try:
|
||||
ud = datetime.fromisoformat(ts)
|
||||
if ud.tzinfo is None:
|
||||
ud = ud.replace(tzinfo=timezone.utc)
|
||||
days = (now - ud).days
|
||||
if days > stale_limit:
|
||||
flags.append("[STALE]")
|
||||
issues.append(f"{days}天未更新(>{stale_limit})")
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
if issues:
|
||||
print(f"{' '.join(flags)} {tag} {name}({code}) 价{price:.2f}{chg} | 买入{el}~{eh} | {'; '.join(issues)}")
|
||||
found += 1
|
||||
|
||||
if found == 0:
|
||||
print("[SILENT] 所有策略正常")
|
||||
|
||||
# ----- 组合级警报 -----
|
||||
portfolio_alerts = 0
|
||||
if holding_count > 0:
|
||||
if weak_ratio > 40:
|
||||
print(f"\n[PORTFOLIO_WEAK] 组合弱势比例{weak_ratio:.0f}% ({weak_count}/{holding_count})!仓位{position_pct:.1f}% → 建议系统性减仓")
|
||||
portfolio_alerts += 1
|
||||
elif weak_ratio > 30:
|
||||
print(f"\n[PORTFOLIO_WEAK_MILD] 组合弱势比例{weak_ratio:.0f}% ({weak_count}/{holding_count}),仓位{position_pct:.1f}%,关注")
|
||||
portfolio_alerts += 1
|
||||
if position_pct > 80 and holding_count > 0:
|
||||
# 仓位过满提醒
|
||||
print(f"[PORTFOLIO_FULL] 总仓位{position_pct:.1f}% > 80%,现金{cash:.0f}({cash/total_assets*100:.1f}%)")
|
||||
portfolio_alerts += 1
|
||||
if portfolio_alerts > 0:
|
||||
found += portfolio_alerts
|
||||
|
||||
return found
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
#!/usr/bin/env python3
|
||||
"""stale_detector.py — 检查所有策略,标记价格偏离/过期的策略
|
||||
|
||||
读取 decisions.json 的扁平列表。自选策略和持仓策略分开判断。
|
||||
可被 cron no_agent 模式调用:stdout 注入到后续 LLM 分析。
|
||||
|
||||
输出格式:
|
||||
[FLAG] [自选/持仓] 股票名(代码) 价XX | 买入A~B | 问题
|
||||
|
||||
用法:
|
||||
python3 stale_detector.py
|
||||
"""
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
from datetime import datetime, timezone
|
||||
sys.path.insert(0, '/home/hmo/MoFin')
|
||||
from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
|
||||
|
||||
def fetch_prices(codes):
|
||||
"""统一价格源:优先 stock_quote.py,腾讯API降级为兜底"""
|
||||
if not codes:
|
||||
return {}
|
||||
# 尝试用 stock_quote.py 获取(脚本强制规范)
|
||||
try:
|
||||
import subprocess
|
||||
script = None
|
||||
for p in ["/home/hmo/MoFin/scripts/stock_quote.py", "/home/hmo/MoFin/stock_quote.py"]:
|
||||
if os.path.exists(p):
|
||||
script = p
|
||||
break
|
||||
if script:
|
||||
result = subprocess.run(
|
||||
[sys.executable, script] + [str(c) for c in codes],
|
||||
capture_output=True, text=True, timeout=30
|
||||
)
|
||||
if result.returncode == 0 and result.stdout.strip():
|
||||
results = {}
|
||||
for line in result.stdout.strip().split("\n"):
|
||||
if not line.strip():
|
||||
continue
|
||||
try:
|
||||
item = json.loads(line)
|
||||
code = str(item.get("code", ""))
|
||||
price = item.get("price")
|
||||
change = item.get("change_pct", 0)
|
||||
if code and price is not None:
|
||||
results[code] = (float(price), float(change))
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
continue
|
||||
if results:
|
||||
return results
|
||||
except Exception as e:
|
||||
print(f"[STALE] stock_quote.py 回退: {e}", file=sys.stderr)
|
||||
|
||||
# 兜底:腾讯API(不应依赖,仅作为最后手段)
|
||||
import urllib.request
|
||||
symbols, code_map = [], {}
|
||||
for c in codes:
|
||||
c = str(c).strip()
|
||||
p = "sh" if (len(c) == 6 and c[0] in "569") else "sz" if len(c) == 6 else "hk"
|
||||
sym = f"{p}{c}"
|
||||
symbols.append(sym)
|
||||
code_map[sym] = c
|
||||
try:
|
||||
req = urllib.request.Request(
|
||||
f"http://qt.gtimg.cn/q={','.join(symbols)}",
|
||||
headers={"User-Agent": "curl/7.81"},
|
||||
)
|
||||
with urllib.request.urlopen(req, timeout=10) as r:
|
||||
text = r.read().decode("gbk")
|
||||
except Exception as e:
|
||||
print(f"FETCH_FAIL (fallback): {e}", file=sys.stderr)
|
||||
return {}
|
||||
|
||||
results = {}
|
||||
for line in text.strip().split("\n"):
|
||||
if "=" not in line:
|
||||
continue
|
||||
try:
|
||||
raw = line.split("=", 1)[1].strip().strip('"').strip(";")
|
||||
fld = raw.split("~")
|
||||
if len(fld) < 6:
|
||||
continue
|
||||
sym = line.split("=", 1)[0].strip().lstrip("v_")
|
||||
oc = code_map.get(sym)
|
||||
if not oc:
|
||||
continue
|
||||
p = float(fld[3]) if fld[3] else 0
|
||||
c = fld[32] if len(fld) > 32 else "0"
|
||||
results[oc] = (p, c)
|
||||
except (ValueError, IndexError):
|
||||
continue
|
||||
return results
|
||||
|
||||
|
||||
def main():
|
||||
decisions_list = read_decisions()
|
||||
if not isinstance(decisions_list, list):
|
||||
decisions_list = decisions_list.get("decisions", []) if isinstance(decisions_list, dict) else []
|
||||
|
||||
# 只保留有买入区的条目,排除已关闭的(inactive/closed)
|
||||
EXCLUDED_STATUSES = ("closed", "inactive")
|
||||
to_check = [d for d in decisions_list if (d.get("entry_low") is not None or d.get("entry_high") is not None) and d.get("status") not in EXCLUDED_STATUSES]
|
||||
if not to_check:
|
||||
print("[SILENT] 无需要检查的策略")
|
||||
return 0
|
||||
|
||||
# ----- 组合级监测:读取总仓位 + 弱势比例 -----
|
||||
position_pct = 0
|
||||
cash = 0
|
||||
total_assets = 0
|
||||
try:
|
||||
pf = read_portfolio()
|
||||
position_pct = pf.get("position_pct", 0)
|
||||
cash = pf.get("cash", 0)
|
||||
total_assets = pf.get("total_assets", 0)
|
||||
except Exception:
|
||||
pass
|
||||
# 统计持仓策略中弱势/深套的比例
|
||||
weak_count = 0
|
||||
holding_count = 0
|
||||
for d in decisions_list:
|
||||
if d.get("type") == "持仓策略" and d.get("status") not in ("closed", "inactive"):
|
||||
holding_count += 1
|
||||
cat = d.get("stock_category", "")
|
||||
if cat in ("弱势", "深套"):
|
||||
weak_count += 1
|
||||
weak_ratio = (weak_count / holding_count * 100) if holding_count > 0 else 0
|
||||
|
||||
prices = fetch_prices([d["code"] for d in to_check])
|
||||
now = datetime.now(timezone.utc).astimezone()
|
||||
found = 0
|
||||
|
||||
for d in to_check:
|
||||
code = d["code"]
|
||||
name = d.get("name", code)
|
||||
el = d.get("entry_low")
|
||||
eh = d.get("entry_high")
|
||||
sl = d.get("stop_loss")
|
||||
tp = d.get("take_profit")
|
||||
ts = d.get("created_at") or d.get("timestamp") or d.get("updated_at", "")
|
||||
is_wl = "自选" in (d.get("type", ""))
|
||||
|
||||
pi = prices.get(code)
|
||||
if not pi:
|
||||
continue
|
||||
price, chg = pi
|
||||
if price <= 0:
|
||||
continue
|
||||
|
||||
issues, flags = [], []
|
||||
tag = "[自选]" if is_wl else "[持仓]"
|
||||
|
||||
# -- 偏离 --
|
||||
if is_wl and el and eh:
|
||||
# 读取 timing_signal 判断策略有效性(timing_signal 字段优先,fallback to action)
|
||||
current_str = d.get("current", "") or ""
|
||||
timing_signal = d.get("timing_signal", "") or current_str
|
||||
has_nonbuy_signal = any(kw in timing_signal for kw in [
|
||||
"等企稳再入", "等企稳", "弱势持有", "观望",
|
||||
"不建议买入", "谨慎买入",
|
||||
])
|
||||
|
||||
# 直接计算 R/R(不依赖文本匹配)
|
||||
rr_invalid = False
|
||||
if sl and sl > 0 and tp and tp > 0 and price > sl:
|
||||
rr = (tp - price) / (price - sl)
|
||||
if rr < 1.5:
|
||||
rr_invalid = True
|
||||
# 也检查 tp 是否接近或低于成本(微盈/浮亏止盈)
|
||||
cost = d.get("cost", 0)
|
||||
if cost and cost > 0 and tp <= cost * 1.05:
|
||||
rr_invalid = True
|
||||
|
||||
strategy_deficient = has_nonbuy_signal or rr_invalid
|
||||
# 对自选无止盈位的也标记(策略不完整)
|
||||
if not tp or tp == 0:
|
||||
strategy_deficient = True
|
||||
|
||||
if el <= price <= eh:
|
||||
flags.append("[WL_IN]")
|
||||
if strategy_deficient:
|
||||
flags.append("[STRATEGY_STALE]")
|
||||
prefix = "⚠️仓位挤占 " if position_pct > 80 else ""
|
||||
issues.append(f"[STRATEGY_STALE] {prefix}价{price:.2f}在买入区{el}~{eh}但策略不完整({'RR='+f'{rr:.2f}<1.5' if rr_invalid else '无止盈位' if not tp else '非买入信号'}),买入区需重评")
|
||||
else:
|
||||
prefix = "⚠️仓位挤占 " if position_pct > 80 else ""
|
||||
issues.append(f"[PUSH] {prefix}价{price:.2f}入买入区{el}~{eh}")
|
||||
elif price > eh * 1.35:
|
||||
flags.append("[WL_HIGH]")
|
||||
issues.append(f"价{price:.2f}高出买入区+{((price/eh)-1)*100:.0f}%,买入区需重评")
|
||||
elif price > eh * 1.20:
|
||||
flags.append("[WL_DRIFT]")
|
||||
issues.append(f"价{price:.2f}高于买入区+{((price/eh)-1)*100:.0f}%")
|
||||
elif not is_wl and eh:
|
||||
dp = (price / eh - 1) * 100
|
||||
if dp > 35:
|
||||
flags.append("[SEVERE]")
|
||||
issues.append(f"偏离买入区上沿+{dp:.0f}%")
|
||||
elif dp > 20:
|
||||
flags.append("[DRIFT]")
|
||||
issues.append(f"偏离买入区上沿+{dp:.0f}%")
|
||||
elif dp > 10:
|
||||
flags.append("[WARN]")
|
||||
issues.append(f"偏离买入区上沿+{dp:.0f}%")
|
||||
# 持仓在买入区内但 R/R 不达标
|
||||
if el and sl and sl > 0 and tp and tp > 0 and price > sl:
|
||||
if el <= price <= eh:
|
||||
rr = (tp - price) / (price - sl)
|
||||
if rr < 1.5:
|
||||
flags.append("[RR_WARN]")
|
||||
issues.append(f"买入区内RR仅{rr:.2f}<1.5,策略需重评")
|
||||
|
||||
# -- 距止损/止盈(仅持仓) --
|
||||
if not is_wl:
|
||||
if sl and sl > 0:
|
||||
dsl = (price / sl - 1) * 100
|
||||
if dsl < 5:
|
||||
# 成本基准校验:浮盈>5%时止损是利润保护,不是危险信号
|
||||
# (mirrors NEAR_TP cost_check logic at line 195-198)
|
||||
cost = d.get("cost")
|
||||
if cost and cost > 0 and price > cost * 1.05:
|
||||
flags.append("[PROFIT_PROTECT]")
|
||||
pnl = (price / cost - 1) * 100
|
||||
issues.append(f"距止损仅{dsl:.1f}%(利润保护,浮盈{pnl:.0f}%)")
|
||||
else:
|
||||
flags.append("[NEAR_SL]")
|
||||
issues.append(f"距止损仅{dsl:.1f}%")
|
||||
if tp and tp > 0:
|
||||
dtp = (tp / price - 1) * 100
|
||||
if dtp < 5:
|
||||
# 成本基准校验:止盈标记只有在盈利≥5%时才有效
|
||||
cost_check = True
|
||||
cost = d.get("cost")
|
||||
if cost and cost > 0 and price < cost * 1.05:
|
||||
cost_check = False
|
||||
if cost_check:
|
||||
flags.append("[NEAR_TP]")
|
||||
issues.append(f"距止盈仅{dtp:.1f}%")
|
||||
|
||||
# -- 过期 --
|
||||
stale_limit = 30 if is_wl else 14
|
||||
if ts:
|
||||
try:
|
||||
ud = datetime.fromisoformat(ts)
|
||||
if ud.tzinfo is None:
|
||||
ud = ud.replace(tzinfo=timezone.utc)
|
||||
days = (now - ud).days
|
||||
if days > stale_limit:
|
||||
flags.append("[STALE]")
|
||||
issues.append(f"{days}天未更新(>{stale_limit})")
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
if issues:
|
||||
print(f"{' '.join(flags)} {tag} {name}({code}) 价{price:.2f}{chg} | 买入{el}~{eh} | {'; '.join(issues)}")
|
||||
found += 1
|
||||
|
||||
if found == 0:
|
||||
print("[SILENT] 所有策略正常")
|
||||
|
||||
# ----- 组合级警报 -----
|
||||
portfolio_alerts = 0
|
||||
if holding_count > 0:
|
||||
if weak_ratio > 40:
|
||||
print(f"\n[PORTFOLIO_WEAK] 组合弱势比例{weak_ratio:.0f}% ({weak_count}/{holding_count})!仓位{position_pct:.1f}% → 建议系统性减仓")
|
||||
portfolio_alerts += 1
|
||||
elif weak_ratio > 30:
|
||||
print(f"\n[PORTFOLIO_WEAK_MILD] 组合弱势比例{weak_ratio:.0f}% ({weak_count}/{holding_count}),仓位{position_pct:.1f}%,关注")
|
||||
portfolio_alerts += 1
|
||||
if position_pct > 80 and holding_count > 0:
|
||||
# 仓位过满提醒
|
||||
print(f"[PORTFOLIO_FULL] 总仓位{position_pct:.1f}% > 80%,现金{cash:.0f}({cash/total_assets*100:.1f}%)")
|
||||
portfolio_alerts += 1
|
||||
if portfolio_alerts > 0:
|
||||
found += portfolio_alerts
|
||||
|
||||
return found
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -15,12 +15,7 @@ from datetime import datetime
|
||||
from mo_data import read_decisions, read_portfolio
|
||||
from mo_data import read_decisions, read_portfolio
|
||||
|
||||
DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
|
||||
OUTPUT_PATH = "/home/hmo/web-dashboard/data/strategy_staleness_report.json"
|
||||
PORTFOLIO_PATH = "/home/hmo/web-dashboard/data/portfolio.json"
|
||||
|
||||
# Fallback: if new path not found, use old path
|
||||
FALLBACK_PATH = "/home/hmo/data/decisions.json"
|
||||
|
||||
WARN_DAYS = 14 # 超过14天未更新→警告
|
||||
CRITICAL_DAYS = 21 # 超过21天→严重警告
|
||||
@@ -57,10 +52,6 @@ def parse_buy_zone(current):
|
||||
return None, None
|
||||
|
||||
def main():
|
||||
# Try new path first, fall back to old format
|
||||
path = DECISIONS_PATH if os.path.exists(DECISIONS_PATH) else FALLBACK_PATH
|
||||
is_new_format = (path == DECISIONS_PATH)
|
||||
|
||||
data = read_decisions()
|
||||
|
||||
# Filter: exclude closed strategies
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+297
-298
@@ -1,298 +1,297 @@
|
||||
#!/usr/bin/env python3
|
||||
"""strategy_review.py — 三层策略复盘 (no_agent)
|
||||
|
||||
每层独立评估:
|
||||
1. 信号层 — 买入/卖出/持有的timing对不对?
|
||||
2. 执行层 — 止损/止盈设得合理吗?
|
||||
3. 综合层 — 这波操作整体赚钱了吗?
|
||||
|
||||
用法:
|
||||
python3 scripts/strategy_review.py
|
||||
"""
|
||||
|
||||
import json, sqlite3, sys, time, urllib.request
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
from collections import Counter
|
||||
from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
|
||||
BASE = Path("/home/hmo/MoFin")
|
||||
DATA = BASE / "data"
|
||||
DB_PATH = DATA / "mofin.db"
|
||||
DECISIONS_PATH = DATA / "decisions.json"
|
||||
|
||||
# 失败模式定义(执行层)
|
||||
EXEC_FAILURES = {
|
||||
"stop_too_tight": {"label": "止损过紧", "fix": "放宽止损到强支撑×0.95,给价格波动留空间"},
|
||||
"tp_too_close": {"label": "止盈过近", "fix": "止盈放到更高阻力位,让利润奔跑"},
|
||||
"stop_too_loose": {"label": "止损过宽", "fix": "收紧止损,少亏当赢"},
|
||||
"tp_too_far": {"label": "止盈过远", "fix": "止盈靠近合理阻力位,提高兑现概率"},
|
||||
}
|
||||
|
||||
# 失败模式定义(信号层)
|
||||
SIGNAL_FAILURES = {
|
||||
"wrong_direction": {"label": "方向看反", "fix": "检查多周期趋势判断逻辑"},
|
||||
"entry_too_early": {"label": "入场过早", "fix": "等缩量确认支撑再入,不追回调"},
|
||||
"bad_signal": {"label": "信号误判", "fix": "修正timing_signal合成权重"},
|
||||
"regime_mismatch": {"label": "情景错配", "fix": "加入市场情景过滤条件"},
|
||||
}
|
||||
|
||||
|
||||
def fetch_price(code):
|
||||
# DB 优先
|
||||
try: from mofin_db import get_price_from_db; p, _ = get_price_from_db(code); return p if p else 0
|
||||
except: pass
|
||||
# Fallback: 腾讯 API
|
||||
try:
|
||||
prefix = "sh" if code.startswith(('60','68','51','56','50')) else "sz" if code.startswith(('00','30','15')) else "hk"
|
||||
url = f"http://qt.gtimg.cn/q={prefix}{code}"
|
||||
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
|
||||
resp = urllib.request.urlopen(req, timeout=5).read().decode('gbk')
|
||||
fld = resp.split('=')[1].strip().strip('"').strip(';').split('~')
|
||||
return float(fld[3]) if len(fld) > 3 else 0
|
||||
except:
|
||||
return 0
|
||||
|
||||
|
||||
def evaluate_strategy(s, price):
|
||||
"""三层评估单条策略,返回 (signal_verdict, exec_verdict, overall_verdict, detail)"""
|
||||
code = s.get("code", "")
|
||||
name = s.get("name", "")
|
||||
sl = s.get("stop_loss", 0) or 0
|
||||
tp = s.get("take_profit", 0) or 0
|
||||
entry_low = s.get("entry_low", 0) or 0
|
||||
entry_high = s.get("entry_high", 0) or 0
|
||||
cost = s.get("cost", 0) or s.get("avg_price", 0) or 0
|
||||
signal = (s.get("timing_signal", "") or s.get("current", "") or "").lower()
|
||||
created = s.get("created_at", "") or s.get("timestamp", "")
|
||||
s_type = s.get("type", "") # 持仓策略/自选策略
|
||||
|
||||
if not created or not price:
|
||||
return "skip", "skip", "skip", "数据不足"
|
||||
|
||||
# 计算运行天数
|
||||
try:
|
||||
days = (datetime.now() - datetime.fromisoformat(created)).days
|
||||
except:
|
||||
days = 0
|
||||
|
||||
# ─── 综合层:赚钱了吗? ───
|
||||
if cost > 0 and s_type == "持仓策略":
|
||||
profit_pct = (price - cost) / cost * 100
|
||||
if profit_pct > 5:
|
||||
overall = "盈利"
|
||||
elif profit_pct > -5:
|
||||
overall = "持平"
|
||||
else:
|
||||
overall = f"亏损{profit_pct:.0f}%"
|
||||
elif tp > 0 and price >= tp:
|
||||
overall = "触止盈"
|
||||
elif sl > 0 and price <= sl:
|
||||
overall = "触止损"
|
||||
else:
|
||||
overall = "持有中"
|
||||
|
||||
# ─── 信号层:timing对不对? ───
|
||||
is_buy_signal = any(kw in signal for kw in ["买入", "加仓", "追涨", "可买"])
|
||||
is_sell_signal = any(kw in signal for kw in ["卖出", "减仓", "止损", "离场"])
|
||||
is_hold_signal = any(kw in signal for kw in ["持有", "观望", "等待", "持股"])
|
||||
|
||||
signal_verdict = "待定"
|
||||
signal_fail = None
|
||||
|
||||
if is_buy_signal or is_hold_signal:
|
||||
if sl > 0 and price <= sl:
|
||||
# 买入/持有信号下触发止损 → 信号方向可能错了
|
||||
signal_verdict = "存疑"
|
||||
signal_fail = "wrong_direction"
|
||||
elif tp > 0 and price >= tp * 0.95:
|
||||
signal_verdict = "正确"
|
||||
elif entry_low > 0 and price < entry_low * 0.85:
|
||||
signal_verdict = "存疑"
|
||||
signal_fail = "entry_too_early"
|
||||
elif days > 30 and tp > 0 and price < entry_low:
|
||||
signal_verdict = "存疑"
|
||||
signal_fail = "wrong_direction"
|
||||
else:
|
||||
signal_verdict = "待定"
|
||||
elif is_sell_signal:
|
||||
if sl > 0 and price <= sl:
|
||||
signal_verdict = "正确"
|
||||
elif price > (cost or entry_low or 0) * 1.05:
|
||||
signal_verdict = "存疑"
|
||||
signal_fail = "bad_signal"
|
||||
else:
|
||||
signal_verdict = "待定"
|
||||
else:
|
||||
# 无明确信号
|
||||
if price > (entry_high or 0):
|
||||
signal_verdict = "待定(价涨)"
|
||||
elif sl > 0 and price <= sl * 1.05:
|
||||
signal_verdict = "待定(近止损)"
|
||||
else:
|
||||
signal_verdict = "待定"
|
||||
|
||||
# ─── 执行层:止损/止盈设得好不好? ───
|
||||
exec_verdict = "待定"
|
||||
exec_fail = None
|
||||
|
||||
# 取近期最高/最低价(判断卖飞/洗盘)
|
||||
recent_high = 0
|
||||
recent_low = 0
|
||||
sl_recovery = False
|
||||
if tp > 0 or sl > 0:
|
||||
try:
|
||||
prefix = "sh" if code.startswith(('60','68','51','56','50')) else "sz" if code.startswith(('00','30','15')) else "hk"
|
||||
url = f"http://ifzq.gtimg.cn/appstock/app/fqkline/get?param={prefix}{code},day,,,60,qfq"
|
||||
import subprocess as sp
|
||||
r = sp.run(["curl", "-s", "--max-time", "3", url], capture_output=True, text=True, timeout=5)
|
||||
if r.returncode == 0 and r.stdout:
|
||||
data = json.loads(r.stdout)
|
||||
day_key = 'qfqday' if prefix != 'hk' else 'day'
|
||||
bars = data.get('data', {}).get(f'{prefix}{code}', {}).get(day_key, [])
|
||||
if bars:
|
||||
prices = [(float(b[2]), float(b[3]), b[0]) for b in bars if len(b) > 3] # (high, low, date)
|
||||
recent_high = max(p[0] for p in prices)
|
||||
recent_low = min(p[1] for p in prices)
|
||||
# 检查止损触发后的走势:是否后来反弹了?
|
||||
if sl > 0:
|
||||
# 找出价格低于SL的K线
|
||||
below_sl = [p for p in prices if p[1] <= sl]
|
||||
above_sl_later = [p for p in prices if p[1] > sl * 1.03]
|
||||
if below_sl and above_sl_later:
|
||||
# 曾跌破SL,但后来涨回去了 → 洗盘
|
||||
first_below = min(below_sl, key=lambda x: x[2])
|
||||
last_above = max(above_sl_later, key=lambda x: x[2])
|
||||
if last_above[2] > first_below[2]:
|
||||
sl_recovery = True
|
||||
except:
|
||||
pass
|
||||
|
||||
if sl > 0 and price <= sl:
|
||||
if sl_recovery:
|
||||
exec_verdict = "洗盘(触发后反弹)"
|
||||
exec_fail = "stop_too_tight"
|
||||
elif price >= sl * 0.95:
|
||||
exec_verdict = "临界(差一点触发)"
|
||||
exec_fail = "stop_too_tight"
|
||||
else:
|
||||
exec_verdict = "已触发"
|
||||
elif tp > 0 and (price >= tp or recent_high >= tp):
|
||||
# 止盈触发或曾触发过
|
||||
max_price = max(price, recent_high)
|
||||
if max_price <= tp * 1.05:
|
||||
exec_verdict = "已触发"
|
||||
else:
|
||||
overshoot = (max_price - tp) / tp * 100
|
||||
exec_verdict = f"卖飞({overshoot:.0f}%)"
|
||||
exec_fail = "tp_too_close"
|
||||
elif days > 45 and tp > 0 and price < entry_low:
|
||||
exec_verdict = "存疑(久未达标)"
|
||||
exec_fail = "tp_too_far"
|
||||
elif sl > 0 and price >= entry_low and price <= entry_high:
|
||||
exec_verdict = "持有中"
|
||||
else:
|
||||
exec_verdict = "待定"
|
||||
|
||||
return signal_verdict, exec_verdict, overall, signal_fail, exec_fail
|
||||
|
||||
|
||||
def review():
|
||||
start = time.time()
|
||||
decisions = mo_data.read_decisions()
|
||||
strategies = decisions.get("decisions", [])
|
||||
|
||||
conn = sqlite3.connect(str(DB_PATH))
|
||||
|
||||
stats = {"correct": 0, "wrong": 0, "mixed": 0, "pending": 0, "total": 0}
|
||||
signal_fails = Counter()
|
||||
exec_fails = Counter()
|
||||
detail_lines = []
|
||||
|
||||
for s in strategies:
|
||||
if s.get("status") == "closed":
|
||||
continue
|
||||
stats["total"] += 1
|
||||
code = s.get("code", "")
|
||||
name = s.get("name", "")
|
||||
price = fetch_price(code)
|
||||
if not price:
|
||||
detail_lines.append(f" ⏭️ {name}({code}): 无行情")
|
||||
stats["pending"] += 1
|
||||
continue
|
||||
|
||||
sv, ev, overall, sf, ef = evaluate_strategy(s, price)
|
||||
|
||||
# 综合评级
|
||||
if overall in ("盈利", "触止盈"):
|
||||
if sv == "正确" or "存疑" not in sv:
|
||||
stats["correct"] += 1
|
||||
else:
|
||||
stats["mixed"] += 1
|
||||
elif overall in ("触止损",) and "存疑" in sv:
|
||||
stats["wrong"] += 1
|
||||
elif "存疑" in sv or "存疑" in ev:
|
||||
stats["wrong"] += 1
|
||||
elif overall in ("持有中", "持平"):
|
||||
stats["mixed"] += 1
|
||||
else:
|
||||
stats["pending"] += 1
|
||||
|
||||
# 记录失败模式
|
||||
if sf:
|
||||
signal_fails[sf] += 1
|
||||
if ef:
|
||||
exec_fails[ef] += 1
|
||||
|
||||
# 逐条摘要
|
||||
tags = []
|
||||
if overall in ("盈利", "触止盈"):
|
||||
tags.append("✅")
|
||||
elif overall == "触止损":
|
||||
tags.append("❌")
|
||||
else:
|
||||
tags.append("⏳")
|
||||
tags.append(f"信号:{sv}")
|
||||
tags.append(f"执行:{ev}")
|
||||
tags.append(f"整体:{overall}")
|
||||
detail_lines.append(f" {' | '.join(tags)} {name}({code})")
|
||||
|
||||
# 写入accuracy_stats
|
||||
conn.execute(
|
||||
"INSERT OR REPLACE INTO accuracy_stats (id, total_advice, correct, wrong, partial, pending, "
|
||||
"accuracy_pct, updated_at) VALUES (1, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(stats["total"], stats["correct"], stats["wrong"],
|
||||
stats["mixed"], stats["pending"],
|
||||
round(stats["correct"] / max(stats["total"] - stats["pending"], 1) * 100, 1),
|
||||
datetime.now().isoformat()))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
# 输出
|
||||
total_eval = stats["total"] - stats["pending"]
|
||||
accuracy = stats["correct"] / max(total_eval, 1) * 100
|
||||
|
||||
print(f"策略复盘 | {datetime.now().strftime('%Y-%m-%d')} | {stats['total']}条 | ({time.time()-start:.0f}s)")
|
||||
print(f" ✅正确 {stats['correct']} | ❌错误 {stats['wrong']} | ⚠️部分 {stats['mixed']} | ⏳待定 {stats['pending']}")
|
||||
print(f" 综合准确率: {accuracy:.1f}%")
|
||||
|
||||
if signal_fails:
|
||||
print(f"\n📡 信号层失败模式:")
|
||||
for mode, cnt in signal_fails.most_common():
|
||||
info = SIGNAL_FAILURES.get(mode, {})
|
||||
print(f" {info.get('label', mode)}({cnt}次): {info.get('fix', '')}")
|
||||
|
||||
if exec_fails:
|
||||
print(f"\n🎯 执行层失败模式:")
|
||||
for mode, cnt in exec_fails.most_common():
|
||||
info = EXEC_FAILURES.get(mode, {})
|
||||
print(f" {info.get('label', mode)}({cnt}次): {info.get('fix', '')}")
|
||||
|
||||
if detail_lines:
|
||||
print(f"\n逐条复盘:")
|
||||
for line in detail_lines:
|
||||
print(line)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
review()
|
||||
#!/usr/bin/env python3
|
||||
"""strategy_review.py — 三层策略复盘 (no_agent)
|
||||
|
||||
每层独立评估:
|
||||
1. 信号层 — 买入/卖出/持有的timing对不对?
|
||||
2. 执行层 — 止损/止盈设得合理吗?
|
||||
3. 综合层 — 这波操作整体赚钱了吗?
|
||||
|
||||
用法:
|
||||
python3 scripts/strategy_review.py
|
||||
"""
|
||||
|
||||
import json, sqlite3, sys, time, urllib.request
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
from collections import Counter
|
||||
from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
|
||||
BASE = Path("/home/hmo/MoFin")
|
||||
DATA = BASE / "data"
|
||||
DB_PATH = DATA / "mofin.db"
|
||||
|
||||
# 失败模式定义(执行层)
|
||||
EXEC_FAILURES = {
|
||||
"stop_too_tight": {"label": "止损过紧", "fix": "放宽止损到强支撑×0.95,给价格波动留空间"},
|
||||
"tp_too_close": {"label": "止盈过近", "fix": "止盈放到更高阻力位,让利润奔跑"},
|
||||
"stop_too_loose": {"label": "止损过宽", "fix": "收紧止损,少亏当赢"},
|
||||
"tp_too_far": {"label": "止盈过远", "fix": "止盈靠近合理阻力位,提高兑现概率"},
|
||||
}
|
||||
|
||||
# 失败模式定义(信号层)
|
||||
SIGNAL_FAILURES = {
|
||||
"wrong_direction": {"label": "方向看反", "fix": "检查多周期趋势判断逻辑"},
|
||||
"entry_too_early": {"label": "入场过早", "fix": "等缩量确认支撑再入,不追回调"},
|
||||
"bad_signal": {"label": "信号误判", "fix": "修正timing_signal合成权重"},
|
||||
"regime_mismatch": {"label": "情景错配", "fix": "加入市场情景过滤条件"},
|
||||
}
|
||||
|
||||
|
||||
def fetch_price(code):
|
||||
# DB 优先
|
||||
try: from mofin_db import get_price_from_db; p, _ = get_price_from_db(code); return p if p else 0
|
||||
except: pass
|
||||
# Fallback: 腾讯 API
|
||||
try:
|
||||
prefix = "sh" if code.startswith(('60','68','51','56','50')) else "sz" if code.startswith(('00','30','15')) else "hk"
|
||||
url = f"http://qt.gtimg.cn/q={prefix}{code}"
|
||||
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
|
||||
resp = urllib.request.urlopen(req, timeout=5).read().decode('gbk')
|
||||
fld = resp.split('=')[1].strip().strip('"').strip(';').split('~')
|
||||
return float(fld[3]) if len(fld) > 3 else 0
|
||||
except:
|
||||
return 0
|
||||
|
||||
|
||||
def evaluate_strategy(s, price):
|
||||
"""三层评估单条策略,返回 (signal_verdict, exec_verdict, overall_verdict, detail)"""
|
||||
code = s.get("code", "")
|
||||
name = s.get("name", "")
|
||||
sl = s.get("stop_loss", 0) or 0
|
||||
tp = s.get("take_profit", 0) or 0
|
||||
entry_low = s.get("entry_low", 0) or 0
|
||||
entry_high = s.get("entry_high", 0) or 0
|
||||
cost = s.get("cost", 0) or s.get("avg_price", 0) or 0
|
||||
signal = (s.get("timing_signal", "") or s.get("current", "") or "").lower()
|
||||
created = s.get("created_at", "") or s.get("timestamp", "")
|
||||
s_type = s.get("type", "") # 持仓策略/自选策略
|
||||
|
||||
if not created or not price:
|
||||
return "skip", "skip", "skip", "数据不足"
|
||||
|
||||
# 计算运行天数
|
||||
try:
|
||||
days = (datetime.now() - datetime.fromisoformat(created)).days
|
||||
except:
|
||||
days = 0
|
||||
|
||||
# ─── 综合层:赚钱了吗? ───
|
||||
if cost > 0 and s_type == "持仓策略":
|
||||
profit_pct = (price - cost) / cost * 100
|
||||
if profit_pct > 5:
|
||||
overall = "盈利"
|
||||
elif profit_pct > -5:
|
||||
overall = "持平"
|
||||
else:
|
||||
overall = f"亏损{profit_pct:.0f}%"
|
||||
elif tp > 0 and price >= tp:
|
||||
overall = "触止盈"
|
||||
elif sl > 0 and price <= sl:
|
||||
overall = "触止损"
|
||||
else:
|
||||
overall = "持有中"
|
||||
|
||||
# ─── 信号层:timing对不对? ───
|
||||
is_buy_signal = any(kw in signal for kw in ["买入", "加仓", "追涨", "可买"])
|
||||
is_sell_signal = any(kw in signal for kw in ["卖出", "减仓", "止损", "离场"])
|
||||
is_hold_signal = any(kw in signal for kw in ["持有", "观望", "等待", "持股"])
|
||||
|
||||
signal_verdict = "待定"
|
||||
signal_fail = None
|
||||
|
||||
if is_buy_signal or is_hold_signal:
|
||||
if sl > 0 and price <= sl:
|
||||
# 买入/持有信号下触发止损 → 信号方向可能错了
|
||||
signal_verdict = "存疑"
|
||||
signal_fail = "wrong_direction"
|
||||
elif tp > 0 and price >= tp * 0.95:
|
||||
signal_verdict = "正确"
|
||||
elif entry_low > 0 and price < entry_low * 0.85:
|
||||
signal_verdict = "存疑"
|
||||
signal_fail = "entry_too_early"
|
||||
elif days > 30 and tp > 0 and price < entry_low:
|
||||
signal_verdict = "存疑"
|
||||
signal_fail = "wrong_direction"
|
||||
else:
|
||||
signal_verdict = "待定"
|
||||
elif is_sell_signal:
|
||||
if sl > 0 and price <= sl:
|
||||
signal_verdict = "正确"
|
||||
elif price > (cost or entry_low or 0) * 1.05:
|
||||
signal_verdict = "存疑"
|
||||
signal_fail = "bad_signal"
|
||||
else:
|
||||
signal_verdict = "待定"
|
||||
else:
|
||||
# 无明确信号
|
||||
if price > (entry_high or 0):
|
||||
signal_verdict = "待定(价涨)"
|
||||
elif sl > 0 and price <= sl * 1.05:
|
||||
signal_verdict = "待定(近止损)"
|
||||
else:
|
||||
signal_verdict = "待定"
|
||||
|
||||
# ─── 执行层:止损/止盈设得好不好? ───
|
||||
exec_verdict = "待定"
|
||||
exec_fail = None
|
||||
|
||||
# 取近期最高/最低价(判断卖飞/洗盘)
|
||||
recent_high = 0
|
||||
recent_low = 0
|
||||
sl_recovery = False
|
||||
if tp > 0 or sl > 0:
|
||||
try:
|
||||
prefix = "sh" if code.startswith(('60','68','51','56','50')) else "sz" if code.startswith(('00','30','15')) else "hk"
|
||||
url = f"http://ifzq.gtimg.cn/appstock/app/fqkline/get?param={prefix}{code},day,,,60,qfq"
|
||||
import subprocess as sp
|
||||
r = sp.run(["curl", "-s", "--max-time", "3", url], capture_output=True, text=True, timeout=5)
|
||||
if r.returncode == 0 and r.stdout:
|
||||
data = json.loads(r.stdout)
|
||||
day_key = 'qfqday' if prefix != 'hk' else 'day'
|
||||
bars = data.get('data', {}).get(f'{prefix}{code}', {}).get(day_key, [])
|
||||
if bars:
|
||||
prices = [(float(b[2]), float(b[3]), b[0]) for b in bars if len(b) > 3] # (high, low, date)
|
||||
recent_high = max(p[0] for p in prices)
|
||||
recent_low = min(p[1] for p in prices)
|
||||
# 检查止损触发后的走势:是否后来反弹了?
|
||||
if sl > 0:
|
||||
# 找出价格低于SL的K线
|
||||
below_sl = [p for p in prices if p[1] <= sl]
|
||||
above_sl_later = [p for p in prices if p[1] > sl * 1.03]
|
||||
if below_sl and above_sl_later:
|
||||
# 曾跌破SL,但后来涨回去了 → 洗盘
|
||||
first_below = min(below_sl, key=lambda x: x[2])
|
||||
last_above = max(above_sl_later, key=lambda x: x[2])
|
||||
if last_above[2] > first_below[2]:
|
||||
sl_recovery = True
|
||||
except:
|
||||
pass
|
||||
|
||||
if sl > 0 and price <= sl:
|
||||
if sl_recovery:
|
||||
exec_verdict = "洗盘(触发后反弹)"
|
||||
exec_fail = "stop_too_tight"
|
||||
elif price >= sl * 0.95:
|
||||
exec_verdict = "临界(差一点触发)"
|
||||
exec_fail = "stop_too_tight"
|
||||
else:
|
||||
exec_verdict = "已触发"
|
||||
elif tp > 0 and (price >= tp or recent_high >= tp):
|
||||
# 止盈触发或曾触发过
|
||||
max_price = max(price, recent_high)
|
||||
if max_price <= tp * 1.05:
|
||||
exec_verdict = "已触发"
|
||||
else:
|
||||
overshoot = (max_price - tp) / tp * 100
|
||||
exec_verdict = f"卖飞({overshoot:.0f}%)"
|
||||
exec_fail = "tp_too_close"
|
||||
elif days > 45 and tp > 0 and price < entry_low:
|
||||
exec_verdict = "存疑(久未达标)"
|
||||
exec_fail = "tp_too_far"
|
||||
elif sl > 0 and price >= entry_low and price <= entry_high:
|
||||
exec_verdict = "持有中"
|
||||
else:
|
||||
exec_verdict = "待定"
|
||||
|
||||
return signal_verdict, exec_verdict, overall, signal_fail, exec_fail
|
||||
|
||||
|
||||
def review():
|
||||
start = time.time()
|
||||
decisions = mo_data.read_decisions()
|
||||
strategies = decisions.get("decisions", [])
|
||||
|
||||
conn = sqlite3.connect(str(DB_PATH))
|
||||
|
||||
stats = {"correct": 0, "wrong": 0, "mixed": 0, "pending": 0, "total": 0}
|
||||
signal_fails = Counter()
|
||||
exec_fails = Counter()
|
||||
detail_lines = []
|
||||
|
||||
for s in strategies:
|
||||
if s.get("status") == "closed":
|
||||
continue
|
||||
stats["total"] += 1
|
||||
code = s.get("code", "")
|
||||
name = s.get("name", "")
|
||||
price = fetch_price(code)
|
||||
if not price:
|
||||
detail_lines.append(f" ⏭️ {name}({code}): 无行情")
|
||||
stats["pending"] += 1
|
||||
continue
|
||||
|
||||
sv, ev, overall, sf, ef = evaluate_strategy(s, price)
|
||||
|
||||
# 综合评级
|
||||
if overall in ("盈利", "触止盈"):
|
||||
if sv == "正确" or "存疑" not in sv:
|
||||
stats["correct"] += 1
|
||||
else:
|
||||
stats["mixed"] += 1
|
||||
elif overall in ("触止损",) and "存疑" in sv:
|
||||
stats["wrong"] += 1
|
||||
elif "存疑" in sv or "存疑" in ev:
|
||||
stats["wrong"] += 1
|
||||
elif overall in ("持有中", "持平"):
|
||||
stats["mixed"] += 1
|
||||
else:
|
||||
stats["pending"] += 1
|
||||
|
||||
# 记录失败模式
|
||||
if sf:
|
||||
signal_fails[sf] += 1
|
||||
if ef:
|
||||
exec_fails[ef] += 1
|
||||
|
||||
# 逐条摘要
|
||||
tags = []
|
||||
if overall in ("盈利", "触止盈"):
|
||||
tags.append("✅")
|
||||
elif overall == "触止损":
|
||||
tags.append("❌")
|
||||
else:
|
||||
tags.append("⏳")
|
||||
tags.append(f"信号:{sv}")
|
||||
tags.append(f"执行:{ev}")
|
||||
tags.append(f"整体:{overall}")
|
||||
detail_lines.append(f" {' | '.join(tags)} {name}({code})")
|
||||
|
||||
# 写入accuracy_stats
|
||||
conn.execute(
|
||||
"INSERT OR REPLACE INTO accuracy_stats (id, total_advice, correct, wrong, partial, pending, "
|
||||
"accuracy_pct, updated_at) VALUES (1, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(stats["total"], stats["correct"], stats["wrong"],
|
||||
stats["mixed"], stats["pending"],
|
||||
round(stats["correct"] / max(stats["total"] - stats["pending"], 1) * 100, 1),
|
||||
datetime.now().isoformat()))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
# 输出
|
||||
total_eval = stats["total"] - stats["pending"]
|
||||
accuracy = stats["correct"] / max(total_eval, 1) * 100
|
||||
|
||||
print(f"策略复盘 | {datetime.now().strftime('%Y-%m-%d')} | {stats['total']}条 | ({time.time()-start:.0f}s)")
|
||||
print(f" ✅正确 {stats['correct']} | ❌错误 {stats['wrong']} | ⚠️部分 {stats['mixed']} | ⏳待定 {stats['pending']}")
|
||||
print(f" 综合准确率: {accuracy:.1f}%")
|
||||
|
||||
if signal_fails:
|
||||
print(f"\n📡 信号层失败模式:")
|
||||
for mode, cnt in signal_fails.most_common():
|
||||
info = SIGNAL_FAILURES.get(mode, {})
|
||||
print(f" {info.get('label', mode)}({cnt}次): {info.get('fix', '')}")
|
||||
|
||||
if exec_fails:
|
||||
print(f"\n🎯 执行层失败模式:")
|
||||
for mode, cnt in exec_fails.most_common():
|
||||
info = EXEC_FAILURES.get(mode, {})
|
||||
print(f" {info.get('label', mode)}({cnt}次): {info.get('fix', '')}")
|
||||
|
||||
if detail_lines:
|
||||
print(f"\n逐条复盘:")
|
||||
for line in detail_lines:
|
||||
print(line)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
review()
|
||||
|
||||
@@ -24,9 +24,6 @@ BASE = Path("/home/hmo/MoFin")
|
||||
DATA = BASE / "data"
|
||||
DB_PATH = DATA / "mofin.db"
|
||||
|
||||
# 自选池和决策文件
|
||||
WATCHLIST_PATH = DATA / "watchlist.json"
|
||||
DECISIONS_PATH = DATA / "decisions.json"
|
||||
SIGNAL_MAX_AGE_HOURS = 4 # 只处理4小时内产生的信号
|
||||
|
||||
|
||||
@@ -273,8 +270,7 @@ def main():
|
||||
conn2.close()
|
||||
except Exception:
|
||||
pass
|
||||
# [migrated to DB] — JSON cold backup removed
|
||||
# WATCHLIST_PATH.write_text(json.dumps(wl, ensure_ascii=False, indent=2))
|
||||
|
||||
except:
|
||||
pass
|
||||
elif action == "monitor":
|
||||
|
||||
Reference in New Issue
Block a user