feat: mo_data.py unified read layer (DB-first, JSON fallback) + cash_log table + batch JSON→DB migration (16 files)
This commit is contained in:
@@ -11,6 +11,7 @@ from datetime import datetime
|
||||
from urllib.request import urlopen, Request
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from threading import Semaphore
|
||||
from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
|
||||
DATA_DIR = "/home/hmo/web-dashboard/data"
|
||||
DECISIONS_PATH = f"{DATA_DIR}/decisions.json"
|
||||
@@ -135,7 +136,7 @@ def main():
|
||||
codes = set()
|
||||
# 读取持仓+自选
|
||||
try:
|
||||
dec = json.load(open(DECISIONS_PATH))
|
||||
dec = mo_data.read_decisions()
|
||||
for d in dec.get("decisions", []):
|
||||
c = d.get("code", "")
|
||||
if c:
|
||||
|
||||
@@ -2,12 +2,13 @@
|
||||
"""Remove held stocks from watchlist"""
|
||||
|
||||
import json, os
|
||||
from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
|
||||
WL = "/home/hmo/web-dashboard/data/watchlist.json"
|
||||
DEC = "/home/hmo/web-dashboard/data/decisions.json"
|
||||
|
||||
holding_codes = set()
|
||||
pf = json.load(open("/home/hmo/web-dashboard/data/portfolio.json"))
|
||||
pf = mo_data.read_portfolio()
|
||||
for h in pf.get("holdings", []):
|
||||
c = h.get("code", "")
|
||||
if c:
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
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"
|
||||
@@ -60,7 +61,7 @@ def main():
|
||||
print(f"holding_strategies: 归档{archived}条过期记录 | 剩余活跃{remaining}条")
|
||||
|
||||
# 2. 检查缺失策略的持仓
|
||||
decisions = json.loads(DECISIONS_PATH.read_text())
|
||||
decisions = mo_data.read_decisions()
|
||||
decisions_list = decisions.get("decisions", [])
|
||||
missing = check_missing_strategies(conn, decisions_list)
|
||||
if missing:
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
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"
|
||||
@@ -22,7 +23,7 @@ issues = []
|
||||
|
||||
# ── 1. 总资产校验 ────────────────────────────────────────────
|
||||
try:
|
||||
pf = json.load(open(PORTFOLIO_PATH))
|
||||
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)
|
||||
@@ -35,7 +36,7 @@ except Exception as e:
|
||||
|
||||
# ── 2. 持仓 vs 决策交叉检查 ──────────────────────────────────
|
||||
try:
|
||||
dec = json.load(open(DECISIONS_PATH))
|
||||
dec = mo_data.read_decisions()
|
||||
dec_codes = {}
|
||||
for d in dec.get("decisions", []):
|
||||
dec_codes[d["code"]] = d
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
"""修复decisions.json中所有决策的trigger字段(由regenerate_all负责填充)"""
|
||||
|
||||
import json, sys, os
|
||||
from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
|
||||
DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
|
||||
DECISIONS_BAK = DECISIONS_PATH + ".bak"
|
||||
@@ -42,7 +43,7 @@ for d in dec.get("decisions", []):
|
||||
# 备份
|
||||
os.makedirs(os.path.dirname(DECISIONS_BAK), exist_ok=True)
|
||||
with open(DECISIONS_BAK, 'w') as f:
|
||||
json.dump(json.load(open(DECISIONS_PATH)), f, indent=2, ensure_ascii=False)
|
||||
json.dump(mo_data.read_decisions(), f, indent=2, ensure_ascii=False)
|
||||
|
||||
with open(DECISIONS_PATH, 'w') as f:
|
||||
json.dump(dec, f, indent=2, ensure_ascii=False)
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
import json, os, sqlite3, subprocess, urllib.request
|
||||
from pathlib import Path
|
||||
from datetime import datetime, timedelta
|
||||
from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
|
||||
BASE = Path("/home/hmo/MoFin")
|
||||
DATA = BASE / "data"
|
||||
@@ -113,7 +114,7 @@ def check_price_monitor():
|
||||
|
||||
# 检查portfolio.json数据新鲜度
|
||||
try:
|
||||
pf = json.load(open(PORTFOLIO_PATH))
|
||||
pf = mo_data.read_portfolio()
|
||||
pf_updated = pf.get("updated_at", "")
|
||||
if pf_updated:
|
||||
pf_dt = datetime.strptime(pf_updated, "%Y-%m-%d %H:%M")
|
||||
|
||||
+789
-788
File diff suppressed because it is too large
Load Diff
+149
-148
@@ -1,148 +1,149 @@
|
||||
#!/usr/bin/env python3
|
||||
"""process_trade.py — 处理交易截图,更新 portfolio.json
|
||||
|
||||
Dad 发交易截图后,填入以下信息运行此脚本:
|
||||
python3 process_trade.py --action buy --code 600563 --shares 100 --price 189.20
|
||||
|
||||
它会:
|
||||
1. 更新 holdings(加/减股数,归零则移除)
|
||||
2. 更新 cash(买入减现金,卖出加现金)
|
||||
3. 同步更新 decisions.json 的 shares 字段
|
||||
4. 记录 changelog
|
||||
|
||||
用法:
|
||||
python3 process_trade.py --action sell --code 600563 --shares 100 --price 189.20
|
||||
python3 process_trade.py --action buy --code 300308 --shares 50 --price 1230.00
|
||||
"""
|
||||
import json, sys, os
|
||||
from datetime import datetime
|
||||
|
||||
PORTFOLIO_PATH = "/home/hmo/web-dashboard/data/portfolio.json"
|
||||
DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
|
||||
|
||||
def parse_args():
|
||||
args = {}
|
||||
for i, a in enumerate(sys.argv[1:]):
|
||||
if a.startswith("--"):
|
||||
key = a.lstrip("-")
|
||||
val = sys.argv[i+2] if i+2 < len(sys.argv) and not sys.argv[i+2].startswith("--") else None
|
||||
args[key] = val
|
||||
return args
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
action = args.get("action", "")
|
||||
code = args.get("code", "")
|
||||
shares = int(float(args.get("shares", 0)))
|
||||
price = float(args.get("price", 0))
|
||||
name = args.get("name", "")
|
||||
|
||||
if not action or not code or not shares or not price:
|
||||
print("用法: python3 process_trade.py --action sell --code 600563 --shares 100 --price 189.20")
|
||||
sys.exit(1)
|
||||
|
||||
now = datetime.now().strftime("%Y-%m-%d %H:%M")
|
||||
cost = shares * price
|
||||
|
||||
# 读数据
|
||||
pf = json.load(open(PORTFOLIO_PATH))
|
||||
dec = json.load(open(DECISIONS_PATH))
|
||||
|
||||
if action == "sell":
|
||||
# 找持仓
|
||||
found = None
|
||||
for h in pf["holdings"]:
|
||||
if h["code"] == code:
|
||||
found = h
|
||||
break
|
||||
if not found:
|
||||
print(f"❌ 错误: 代码 {code} 未在持仓中找到")
|
||||
sys.exit(1)
|
||||
old_shares = found.get("shares", 0)
|
||||
if old_shares < shares:
|
||||
print(f"❌ 错误: 持仓只有 {old_shares} 股,不够卖 {shares} 股")
|
||||
sys.exit(1)
|
||||
# 减股数
|
||||
found["shares"] = old_shares - shares
|
||||
found["updated_at"] = now
|
||||
# 归零则移除
|
||||
if found["shares"] <= 0:
|
||||
pf["holdings"] = [h for h in pf["holdings"] if h["code"] != code]
|
||||
print(f" 已全部清仓,从持仓移除")
|
||||
# 加现金
|
||||
old_cash = pf.get("cash", 0) or 0
|
||||
pf["cash"] = round(old_cash + cost, 2)
|
||||
print(f" ✅ 卖出 {name}({code}) {shares}股 @{price} = {cost:.2f}")
|
||||
print(f" 现金: {old_cash} → {pf['cash']}")
|
||||
|
||||
elif action == "buy":
|
||||
# 找是否已有该股
|
||||
found = None
|
||||
for h in pf["holdings"]:
|
||||
if h["code"] == code:
|
||||
found = h
|
||||
break
|
||||
if found:
|
||||
# 加权平均成本
|
||||
old_shares = found.get("shares", 0) or 0
|
||||
old_cost = found.get("cost", 0) or 0
|
||||
total_cost = old_cost * old_shares + cost
|
||||
new_shares = old_shares + shares
|
||||
new_avg_cost = round(total_cost / new_shares, 2) if new_shares > 0 else price
|
||||
found["shares"] = new_shares
|
||||
found["cost"] = new_avg_cost
|
||||
found["updated_at"] = now
|
||||
else:
|
||||
pf["holdings"].append({
|
||||
"code": code, "name": name, "shares": shares,
|
||||
"cost": price, "price": price, "updated_at": now
|
||||
})
|
||||
# 减现金
|
||||
old_cash = pf.get("cash", 0) or 0
|
||||
pf["cash"] = round(old_cash - cost, 2)
|
||||
print(f" ✅ 买入 {name}({code}) {shares}股 @{price} = {cost:.2f}")
|
||||
print(f" 现金: {old_cash} → {pf['cash']}")
|
||||
|
||||
# 同步 decisions.json 的 shares
|
||||
for d in dec.get("decisions", []):
|
||||
if d["code"] == code:
|
||||
old_dec_shares = d.get("shares", 0) or 0
|
||||
d["shares"] = (d.get("shares", 0) or 0) + (shares if action == "buy" else -shares)
|
||||
if d["shares"] <= 0 and action == "sell":
|
||||
d["shares"] = 0
|
||||
d["type"] = "自选策略"
|
||||
d.setdefault("changelog", []).append({
|
||||
"time": now,
|
||||
"event": action,
|
||||
"shares": shares,
|
||||
"price": price,
|
||||
"total": cost
|
||||
})
|
||||
break
|
||||
|
||||
# 写入 — DB 优先
|
||||
pf["updated_at"] = now
|
||||
try:
|
||||
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
|
||||
conn = get_conn()
|
||||
write_holdings_batch(conn, pf.get('holdings', []))
|
||||
write_portfolio_summary(conn, pf)
|
||||
for d in dec.get('decisions', []):
|
||||
write_holding_strategy(conn, d.get('code', ''), d.get('name', ''), d)
|
||||
conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
json.dump(pf, open(PORTFOLIO_PATH, "w"), indent=2, ensure_ascii=False)
|
||||
json.dump(dec, open(DECISIONS_PATH, "w"), indent=2, ensure_ascii=False)
|
||||
|
||||
# 重算总资产
|
||||
total_mv = sum((h.get("shares",0) or 0) * (h.get("price",0) or 0) for h in pf["holdings"])
|
||||
total = round(total_mv + (pf.get("cash",0) or 0), 2)
|
||||
print(f"\n📊 持仓市值: {total_mv:.2f}")
|
||||
print(f"📊 现金: {pf.get('cash',0):.2f}")
|
||||
print(f"📊 总资产: {total:.2f}")
|
||||
print(f"📊 持仓 {len(pf['holdings'])} 只")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
#!/usr/bin/env python3
|
||||
"""process_trade.py — 处理交易截图,更新 portfolio.json
|
||||
|
||||
Dad 发交易截图后,填入以下信息运行此脚本:
|
||||
python3 process_trade.py --action buy --code 600563 --shares 100 --price 189.20
|
||||
|
||||
它会:
|
||||
1. 更新 holdings(加/减股数,归零则移除)
|
||||
2. 更新 cash(买入减现金,卖出加现金)
|
||||
3. 同步更新 decisions.json 的 shares 字段
|
||||
4. 记录 changelog
|
||||
|
||||
用法:
|
||||
python3 process_trade.py --action sell --code 600563 --shares 100 --price 189.20
|
||||
python3 process_trade.py --action buy --code 300308 --shares 50 --price 1230.00
|
||||
"""
|
||||
import json, sys, os
|
||||
from datetime import datetime
|
||||
from mo_data import read_portfolio, read_decisions, read_watchlist
|
||||
|
||||
PORTFOLIO_PATH = "/home/hmo/web-dashboard/data/portfolio.json"
|
||||
DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
|
||||
|
||||
def parse_args():
|
||||
args = {}
|
||||
for i, a in enumerate(sys.argv[1:]):
|
||||
if a.startswith("--"):
|
||||
key = a.lstrip("-")
|
||||
val = sys.argv[i+2] if i+2 < len(sys.argv) and not sys.argv[i+2].startswith("--") else None
|
||||
args[key] = val
|
||||
return args
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
action = args.get("action", "")
|
||||
code = args.get("code", "")
|
||||
shares = int(float(args.get("shares", 0)))
|
||||
price = float(args.get("price", 0))
|
||||
name = args.get("name", "")
|
||||
|
||||
if not action or not code or not shares or not price:
|
||||
print("用法: python3 process_trade.py --action sell --code 600563 --shares 100 --price 189.20")
|
||||
sys.exit(1)
|
||||
|
||||
now = datetime.now().strftime("%Y-%m-%d %H:%M")
|
||||
cost = shares * price
|
||||
|
||||
# 读数据
|
||||
pf = mo_data.read_portfolio()
|
||||
dec = mo_data.read_decisions()
|
||||
|
||||
if action == "sell":
|
||||
# 找持仓
|
||||
found = None
|
||||
for h in pf["holdings"]:
|
||||
if h["code"] == code:
|
||||
found = h
|
||||
break
|
||||
if not found:
|
||||
print(f"❌ 错误: 代码 {code} 未在持仓中找到")
|
||||
sys.exit(1)
|
||||
old_shares = found.get("shares", 0)
|
||||
if old_shares < shares:
|
||||
print(f"❌ 错误: 持仓只有 {old_shares} 股,不够卖 {shares} 股")
|
||||
sys.exit(1)
|
||||
# 减股数
|
||||
found["shares"] = old_shares - shares
|
||||
found["updated_at"] = now
|
||||
# 归零则移除
|
||||
if found["shares"] <= 0:
|
||||
pf["holdings"] = [h for h in pf["holdings"] if h["code"] != code]
|
||||
print(f" 已全部清仓,从持仓移除")
|
||||
# 加现金
|
||||
old_cash = pf.get("cash", 0) or 0
|
||||
pf["cash"] = round(old_cash + cost, 2)
|
||||
print(f" ✅ 卖出 {name}({code}) {shares}股 @{price} = {cost:.2f}")
|
||||
print(f" 现金: {old_cash} → {pf['cash']}")
|
||||
|
||||
elif action == "buy":
|
||||
# 找是否已有该股
|
||||
found = None
|
||||
for h in pf["holdings"]:
|
||||
if h["code"] == code:
|
||||
found = h
|
||||
break
|
||||
if found:
|
||||
# 加权平均成本
|
||||
old_shares = found.get("shares", 0) or 0
|
||||
old_cost = found.get("cost", 0) or 0
|
||||
total_cost = old_cost * old_shares + cost
|
||||
new_shares = old_shares + shares
|
||||
new_avg_cost = round(total_cost / new_shares, 2) if new_shares > 0 else price
|
||||
found["shares"] = new_shares
|
||||
found["cost"] = new_avg_cost
|
||||
found["updated_at"] = now
|
||||
else:
|
||||
pf["holdings"].append({
|
||||
"code": code, "name": name, "shares": shares,
|
||||
"cost": price, "price": price, "updated_at": now
|
||||
})
|
||||
# 减现金
|
||||
old_cash = pf.get("cash", 0) or 0
|
||||
pf["cash"] = round(old_cash - cost, 2)
|
||||
print(f" ✅ 买入 {name}({code}) {shares}股 @{price} = {cost:.2f}")
|
||||
print(f" 现金: {old_cash} → {pf['cash']}")
|
||||
|
||||
# 同步 decisions.json 的 shares
|
||||
for d in dec.get("decisions", []):
|
||||
if d["code"] == code:
|
||||
old_dec_shares = d.get("shares", 0) or 0
|
||||
d["shares"] = (d.get("shares", 0) or 0) + (shares if action == "buy" else -shares)
|
||||
if d["shares"] <= 0 and action == "sell":
|
||||
d["shares"] = 0
|
||||
d["type"] = "自选策略"
|
||||
d.setdefault("changelog", []).append({
|
||||
"time": now,
|
||||
"event": action,
|
||||
"shares": shares,
|
||||
"price": price,
|
||||
"total": cost
|
||||
})
|
||||
break
|
||||
|
||||
# 写入 — DB 优先
|
||||
pf["updated_at"] = now
|
||||
try:
|
||||
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
|
||||
conn = get_conn()
|
||||
write_holdings_batch(conn, pf.get('holdings', []))
|
||||
write_portfolio_summary(conn, pf)
|
||||
for d in dec.get('decisions', []):
|
||||
write_holding_strategy(conn, d.get('code', ''), d.get('name', ''), d)
|
||||
conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
json.dump(pf, open(PORTFOLIO_PATH, "w"), indent=2, ensure_ascii=False)
|
||||
json.dump(dec, open(DECISIONS_PATH, "w"), indent=2, ensure_ascii=False)
|
||||
|
||||
# 重算总资产
|
||||
total_mv = sum((h.get("shares",0) or 0) * (h.get("price",0) or 0) for h in pf["holdings"])
|
||||
total = round(total_mv + (pf.get("cash",0) or 0), 2)
|
||||
print(f"\n📊 持仓市值: {total_mv:.2f}")
|
||||
print(f"📊 现金: {pf.get('cash',0):.2f}")
|
||||
print(f"📊 总资产: {total:.2f}")
|
||||
print(f"📊 持仓 {len(pf['holdings'])} 只")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
+287
-286
@@ -1,286 +1,287 @@
|
||||
#!/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
|
||||
|
||||
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 = json.load(open(DECISIONS_PATH))
|
||||
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:
|
||||
with open(PORTFOLIO_PATH) as f:
|
||||
pf = json.load(f)
|
||||
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
|
||||
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 = mo_data.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:
|
||||
with open(PORTFOLIO_PATH) as f:
|
||||
pf = json.load(f)
|
||||
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()
|
||||
|
||||
+2074
-2073
File diff suppressed because it is too large
Load Diff
@@ -14,6 +14,7 @@ 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"
|
||||
@@ -198,7 +199,7 @@ def evaluate_strategy(s, price):
|
||||
|
||||
def review():
|
||||
start = time.time()
|
||||
decisions = json.loads(DECISIONS_PATH.read_text())
|
||||
decisions = mo_data.read_decisions()
|
||||
strategies = decisions.get("decisions", [])
|
||||
|
||||
conn = sqlite3.connect(str(DB_PATH))
|
||||
|
||||
Reference in New Issue
Block a user