refactor: remove duplicate scripts + JSON constants + update docs

This commit is contained in:
知微
2026-07-04 09:30:50 +08:00
parent 7d6b08953c
commit 8b114d6e9e
26 changed files with 2200 additions and 4913 deletions
+16
View File
@@ -5,6 +5,22 @@
--- ---
## 2026-07-03 — 代码重构
### 删除重复文件
- `scripts/strategy_lifecycle.py` — 旧版本,缺少 quality gateroot 版本是规范
- `scripts/price_monitor.py` — 旧版本,root 版本是规范
### 清理 JSON 路径常量
- 移除 scripts/ 中 12 个文件的 DECISIONS_PATH / PORTFOLIO_PATH / WATCHLIST_PATH 声明
- 移除 root level 中无用的 JSON 路径常量
### 文档更新
- `docs/SYSTEM_ARCHITECTURE.md` — 完整重写(v5.0
- `CHANGELOG.md` — 补充重构记录
---
## 2026-07-03 — JSON 彻底移除 + 币种架构修正 ## 2026-07-03 — JSON 彻底移除 + 币种架构修正
### JSON→DB 全面迁移 (32+ 文件) ### JSON→DB 全面迁移 (32+ 文件)
+109
View File
@@ -0,0 +1,109 @@
# MoFin 系统架构
> 最后更新:2026-07-03
> 维护人:Sisyphus (小小莫) + Zhiwei (知微)
---
## 一、数据层
### 数据库
所有数据存储在 SQLite`/home/hmo/web-dashboard/data/mofin.db`
| 表 | 用途 | 写入方 |
|----|------|--------|
| `holdings` | 持仓列表 | price_monitor, import_holding_xls |
| `portfolio_summary` | 总资产/市值/仓位 | price_monitor, regenerate_all |
| `holding_strategies` | 策略/决策 | regenerate_all, server.py API |
| `watchlist_stocks` | 自选股 | price_monitor, regenerate_all |
| `cash_log` | 现金变更审计 | write_cash_log |
| `market_snapshots` | 大盘数据 | market_watch |
| `sector_snapshots` | 板块数据 | market_watch |
| `live_prices` | 实时价格快照 | price_monitor |
| `mtf_cache` | 多周期K线缓存 | multi_timeframe |
| `capital_flow_cache` | 资金流缓存 | capital_flow_collector |
| `price_events` | 价格触发事件 | price_monitor |
### 数据访问
- **读取**`mo_data.py``read_portfolio()` / `read_decisions()` / `read_watchlist()`
- **写入**`mofin_db.py``write_holdings_batch()` / `write_holding_strategy()` / `write_portfolio_summary()`
JSON 文件已全部移除(portfolio.json, decisions.json, watchlist.json 等)。
### 币种
| 品种 | 个股存储 | 汇总 |
|------|---------|------|
| 港股 | HKD (currency='HKD') | CNY (×汇率) |
| A股 | CNY (currency='CNY') | CNY |
`calc_total_assets()` 汇总时自动将港股 HKD 转 CNY。汇率由 `hk_rate.py` 实时获取。
---
## 二、核心模块
```
MoFin/
├── mo_models.py # 统一数据模型:calc_total_assets, is_hk_stock, to_cny
├── mo_data.py # 统一读取层:read_portfolio, read_decisions, read_watchlist
├── mofin_db.py # DB 层:建表 + 写函数 + 查询函数
├── mo_config.py # 配置单例
├── price_monitor.py # 价格更新(cron: */2 9-16 1-5
├── strategy_lifecycle.py # 策略生命周期(regenerate_all + quality gates
├── server.py # Flask Web API(端口 8899
├── market_watch.py # 大盘采集(cron: */30 9-15
├── market_screener.py # 全市场筛选(cron: */30 9-15
├── strategy_tree.py # 策略树/分支管理
├── strategy_evaluator.py # 策略评估
├── hk_rate.py # 港币汇率(API + 缓存)
├── multi_timeframe.py # 多周期K线分析
├── stock_profile.py # 个股画像
├── data_freshness.py # 数据新鲜度校验
├── system_audit.py # 系统审计
├── system_health_check.py# 系统健康检查
├── prompt_manager/ # LLM Prompt 管理
├── scripts/ # 工具/一次性脚本
└── docs/ # 文档
```
---
## 三、数据流
```
开盘 (9:00-16:00)
├── price_monitor (每2分钟)
│ ├── 东财/腾讯 API → 拉价格
│ ├── write_holdings_batch → holdings 表
│ └── write_portfolio_summary → portfolio_summary 表
├── market_watch (每30分钟)
│ └── write_market_snapshot → market_snapshots + sector_snapshots
├── market_screener (每30分钟)
│ └── 读 market_snapshots → 小果 LLM 筛选 → candidate_pool
└── stale_push_wlin (每30分钟)
└── 读持仓+决策 → 区间检测 → XMPP 推送
盘后
├── system_audit (17:30) → 全局审计报告
├── strategy_review (20:00) → 策略复盘
└── regenerate_all (手动/定时) → 全量策略重评
```
---
## 四、版本
| 版本 | 日期 | 关键变更 |
|------|------|---------|
| 5.0 | 2026-07-03 | JSON 彻底移除,纯 DB。币种修正(港股 HKD,汇总 CNY)。消除重复文件。 |
| 4.0 | 2026-07-01 | mo_data 统一读取层,cash_log 表 |
| 3.0 | 2026-06-30 | mo_models 统一数据模型,DSA 集成 |
| 2.0 | 2026-06-29 | 初始架构重构 |
+30 -8
View File
@@ -13,10 +13,10 @@ import sys
from datetime import datetime, timedelta from datetime import datetime, timedelta
from pathlib import Path from pathlib import Path
DATA_DIR = Path(__file__).parent / "data" from mo_data import read_decisions, read_portfolio
DECISIONS_PATH = DATA_DIR / "decisions.json" from mofin_db import get_conn, write_holding_strategy
PORTFOLIO_PATH = DATA_DIR / "portfolio.json"
ACCURACY_PATH = DATA_DIR / "accuracy_stats.json" ACCURACY_PATH = Path(__file__).parent / "data" / "accuracy_stats.json"
def load_json(path, default=None): def load_json(path, default=None):
try: try:
@@ -125,8 +125,8 @@ def evaluate_advice(advice, current_holding):
def run(): def run():
force = "--force" in sys.argv force = "--force" in sys.argv
decisions = load_json(DECISIONS_PATH, {"decisions": []}) decisions = read_decisions()
portfolio = load_json(PORTFOLIO_PATH, {"holdings": []}) portfolio = read_portfolio()
old_stats = load_json(ACCURACY_PATH, {}) old_stats = load_json(ACCURACY_PATH, {})
results = [] results = []
@@ -165,8 +165,30 @@ def run():
"result": result, "result": result,
}) })
# 保存更新后的 decisions.json(含评估标记) # 保存更新后的 decisions 到 DB(含评估标记)
save_json(DECISIONS_PATH, decisions) conn = get_conn()
for entry in decisions.get("decisions", []):
code = entry.get("code", "")
name = entry.get("name", code)
write_holding_strategy(conn, code, name, entry)
# 写入 advice_timeline 评估标记
for adv in entry.get("advice_timeline", []):
conn.execute(
"""INSERT OR REPLACE INTO advice_timeline
(id, code, date, direction, price, summary, status,
evaluated, result, evaluated_at, report_id)
VALUES (
(SELECT id FROM advice_timeline WHERE code=? AND date=? AND direction=? AND summary=?),
?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)""",
(code, adv.get("date", ""), adv.get("direction", ""), adv.get("summary", ""),
code, adv.get("date", ""), adv.get("direction", ""),
adv.get("price"), adv.get("summary", ""), adv.get("status", ""),
1 if adv.get("evaluated") else 0,
adv.get("result", ""), adv.get("evaluated_at", ""),
adv.get("report_id", "")))
conn.commit()
conn.close()
# 计算准确率 # 计算准确率
evaluated = total["correct"] + total["wrong"] + total["partial"] evaluated = total["correct"] + total["wrong"] + total["partial"]
+2 -3
View File
@@ -17,8 +17,8 @@ branch_scanner.py — 分支自成长数据采集器(全静默)
import json, sys, re import json, sys, re
from datetime import datetime from datetime import datetime
from urllib.request import Request, urlopen from urllib.request import Request, urlopen
from mo_data import read_decisions
DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
SCANNER_STATE = "/home/hmo/web-dashboard/data/scanner_state.json" SCANNER_STATE = "/home/hmo/web-dashboard/data/scanner_state.json"
@@ -82,8 +82,7 @@ def main():
scenario = get_scenario() scenario = get_scenario()
sid = scenario.get("id", "unknown") sid = scenario.get("id", "unknown")
with open(DECISIONS_PATH) as f: data = read_decisions()
data = json.load(f)
decisions = data.get("decisions", []) decisions = data.get("decisions", [])
for entry in decisions: for entry in decisions:
+3 -4
View File
@@ -24,8 +24,6 @@ from datetime import datetime
from pathlib import Path from pathlib import Path
DATA_DIR = Path(__file__).parent / "data" DATA_DIR = Path(__file__).parent / "data"
DECISIONS_PATH = DATA_DIR / "decisions.json"
PORTFOLIO_PATH = DATA_DIR / "portfolio.json"
PROFILES_PATH = DATA_DIR / "stock_profiles.json" PROFILES_PATH = DATA_DIR / "stock_profiles.json"
OUTPUT_PATH = DATA_DIR / "evaluation_input.json" OUTPUT_PATH = DATA_DIR / "evaluation_input.json"
@@ -227,8 +225,9 @@ def get_decisions_info(decisions):
def run(): def run():
# 加载数据 # 加载数据
decisions = load_json(DECISIONS_PATH, {"decisions": []}) from mo_data import read_decisions, read_portfolio
portfolio = load_json(PORTFOLIO_PATH, {"holdings": []}) decisions = read_decisions()
portfolio = read_portfolio()
profiles = load_json(PROFILES_PATH, {"profiles": []}) profiles = load_json(PROFILES_PATH, {"profiles": []})
# 获取行业映射 # 获取行业映射
+5
View File
@@ -451,6 +451,11 @@
"content": "**港股概览:** 恒指+1.8%反弹,持仓分化。腾讯(00700)浮亏-4.6%目标已超但盈亏比偏低;丘钛(01478)深套-49.7%及万科(02202)深套-52.4%均暂远离止损(距12-22", "content": "**港股概览:** 恒指+1.8%反弹,持仓分化。腾讯(00700)浮亏-4.6%目标已超但盈亏比偏低;丘钛(01478)深套-49.7%及万科(02202)深套-52.4%均暂远离止损(距12-22",
"report_id": "cron_d42f2ce3b479_2026-06-29_20-08-09" "report_id": "cron_d42f2ce3b479_2026-06-29_20-08-09"
}, },
{
"time": "2026-07-04T08:56:00.135656",
"content": "③ 丘钛科技(01478) 距止损6.48仅+7.3%!仓位7.97%",
"report_id": "cron_d42f2ce3b479_2026-07-03_20-03-44"
},
{ {
"time": "2026-06-01T10:25:54.503460", "time": "2026-06-01T10:25:54.503460",
"content": "丘钛科技(01478) 仓位8.58% +4.10%→ 持有,走势健康", "content": "丘钛科技(01478) 仓位8.58% +4.10%→ 持有,走势健康",
+5
View File
@@ -146,6 +146,11 @@
"content": "- 中芯国际688981 151.0 | 买入区143.67~150.85 **(现价刚穿上沿)**", "content": "- 中芯国际688981 151.0 | 买入区143.67~150.85 **(现价刚穿上沿)**",
"report_id": "cron_d42f2ce3b479_2026-06-29_20-08-09" "report_id": "cron_d42f2ce3b479_2026-06-29_20-08-09"
}, },
{
"time": "2026-07-04T08:56:00.135656",
"content": "② 中芯国际A(688981) 已跌破止损位!仓位5.44%",
"report_id": "cron_d42f2ce3b479_2026-07-03_20-03-44"
},
{ {
"time": "2026-06-11T08:55:23.441938", "time": "2026-06-11T08:55:23.441938",
"content": "• 中芯国际(688981) 竞价125.00(-1.81%),策略买入区116~136内", "content": "• 中芯国际(688981) 竞价125.00(-1.81%),策略买入区116~136内",
+2
View File
@@ -62,6 +62,7 @@ class MoConfig:
@property @property
def live_prices_path(self) -> Path: def live_prices_path(self) -> Path:
"""⚠️ DEPRECATED: 实时价格已迁移到 mofin_db.live_prices 表。"""
return self.data_dir / "live_prices.json" return self.data_dir / "live_prices.json"
@property @property
@@ -70,6 +71,7 @@ class MoConfig:
@property @property
def multi_tf_cache_path(self) -> Path: def multi_tf_cache_path(self) -> Path:
"""⚠️ DEPRECATED: 多周期缓存已迁移到 mofin_db.mtf_cache 表。"""
return self.data_dir / "multi_tf_cache.json" return self.data_dir / "multi_tf_cache.json"
@property @property
-8
View File
@@ -19,10 +19,6 @@ from datetime import datetime, date
from mo_data import read_portfolio, read_decisions from mo_data import read_portfolio, read_decisions
from mofin_db import get_conn, write_holding_strategy 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 模块 # 引入 strategy_tree 模块
sys.path.insert(0, "/home/hmo/MoFin") sys.path.insert(0, "/home/hmo/MoFin")
try: try:
@@ -132,10 +128,6 @@ def evaluate_all():
conn.close() conn.close()
except Exception: except Exception:
pass pass
# [migrated to DB] — cold backup removed
# with open(DECISIONS_PATH, "w") as f:
# json.dump(data, f, indent=2, ensure_ascii=False)
# 输出摘要(空 = 静默) # 输出摘要(空 = 静默)
lines = [] lines = []
init_note = f" | 自动初始化{auto_init_count}" if auto_init_count else "" init_note = f" | 自动初始化{auto_init_count}" if auto_init_count else ""
-5
View File
@@ -20,7 +20,6 @@ from urllib.request import Request, urlopen
from mo_data import read_decisions from mo_data import read_decisions
from mofin_db import get_conn, write_holding_strategy 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" SCANNER_STATE = "/home/hmo/web-dashboard/data/scanner_state.json"
@@ -110,10 +109,6 @@ def main():
conn.close() conn.close()
except Exception: except Exception:
pass 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": {}} state = {"scenario": sid, "updated_at": now.isoformat(), "branches": {}}
for e in decisions: for e in decisions:
-1
View File
@@ -15,7 +15,6 @@ from mo_data import read_portfolio, read_decisions, read_watchlist
from mofin_db import get_conn, write_capital_flow_cache from mofin_db import get_conn, write_capital_flow_cache
DATA_DIR = "/home/hmo/web-dashboard/data" DATA_DIR = "/home/hmo/web-dashboard/data"
DECISIONS_PATH = f"{DATA_DIR}/decisions.json"
CACHE_PATH = f"{DATA_DIR}/capital_flow_cache.json" CACHE_PATH = f"{DATA_DIR}/capital_flow_cache.json"
UA = "Mozilla/5.0" UA = "Mozilla/5.0"
-1
View File
@@ -14,7 +14,6 @@ from mo_data import read_portfolio, read_decisions, read_watchlist
BASE = Path("/home/hmo/MoFin") BASE = Path("/home/hmo/MoFin")
DATA = BASE / "data" DATA = BASE / "data"
DB_PATH = DATA / "mofin.db" DB_PATH = DATA / "mofin.db"
DECISIONS_PATH = DATA / "decisions.json"
def clean_holding_strategies(conn): def clean_holding_strategies(conn):
-2
View File
@@ -15,8 +15,6 @@ from datetime import datetime, timezone
from mo_data import read_portfolio, read_decisions, read_watchlist from mo_data import read_portfolio, read_decisions, read_watchlist
DATA_DIR = "/home/hmo/web-dashboard/data" 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" STALE_REPORT = f"{DATA_DIR}/strategy_staleness_report.json"
issues = [] issues = []
-12
View File
@@ -5,9 +5,6 @@ import json, sys, os
from mo_data import read_portfolio, read_decisions, read_watchlist from mo_data import read_portfolio, read_decisions, read_watchlist
from mofin_db import get_conn, write_holding_strategy from mofin_db import get_conn, write_holding_strategy
DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
DECISIONS_BAK = DECISIONS_PATH + ".bak"
try: try:
dec = read_decisions() dec = read_decisions()
except Exception as e: except Exception as e:
@@ -40,18 +37,9 @@ for d in dec.get("decisions", []):
if new_trig: if new_trig:
print(f" {code} {name}: trigger={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 # DB 写入(替代 json.dump
conn = get_conn() conn = get_conn()
for d in dec.get("decisions", []): for d in dec.get("decisions", []):
write_holding_strategy(conn, d.get("code", ""), d.get("name", ""), d) write_holding_strategy(conn, d.get("code", ""), d.get("name", ""), d)
conn.close() 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字段") print(f"\n{count}只,已更新trigger字段")
-4
View File
@@ -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 from mofin_db import get_conn, write_holdings_batch, write_portfolio_summary, write_holding_strategy
STOCKS_FILE = "/home/hmo/stocks/holding.xls" 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" 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'), 'updated_at': datetime.now().strftime('%Y-%m-%d %H:%M'),
'source': STOCKS_FILE, '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 写入 # DB 写入
try: try:
conn = get_conn() conn = get_conn()
-3
View File
@@ -82,9 +82,6 @@ def check_xiaoguo():
pass pass
PORTFOLIO_PATH = str(DATA / "portfolio.json")
def check_price_monitor(): def check_price_monitor():
"""价格监控:检查price_monitor cron最近是否运行 + 数据是否更新 """价格监控:检查price_monitor cron最近是否运行 + 数据是否更新
-806
View File
@@ -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请求获取全部港股。
新浪港股APIhq.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.cn15分钟延迟)
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()
-2
View File
@@ -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__)))) 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 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(): def parse_args():
args = {} args = {}
-5
View File
@@ -16,7 +16,6 @@ from datetime import datetime, timedelta
from mo_data import read_decisions from mo_data import read_decisions
from mofin_db import get_conn, write_holding_strategy 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" PRUNE_LOG = "/home/hmo/MoFin/data/prune_log.json"
@@ -33,10 +32,6 @@ def save_decisions(data):
conn.close() conn.close()
except Exception: except Exception:
pass 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(): def main():
data = load_decisions() data = load_decisions()
-3
View File
@@ -17,9 +17,6 @@ from datetime import datetime, timezone
sys.path.insert(0, '/home/hmo/MoFin') sys.path.insert(0, '/home/hmo/MoFin')
from mo_data import read_portfolio, read_decisions, read_watchlist 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): def fetch_prices(codes):
"""统一价格源:优先 stock_quote.py,腾讯API降级为兜底""" """统一价格源:优先 stock_quote.py,腾讯API降级为兜底"""
-9
View File
@@ -15,12 +15,7 @@ from datetime import datetime
from mo_data import read_decisions, read_portfolio from mo_data import read_decisions, read_portfolio
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" 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天未更新→警告 WARN_DAYS = 14 # 超过14天未更新→警告
CRITICAL_DAYS = 21 # 超过21天→严重警告 CRITICAL_DAYS = 21 # 超过21天→严重警告
@@ -57,10 +52,6 @@ def parse_buy_zone(current):
return None, None return None, None
def main(): 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() data = read_decisions()
# Filter: exclude closed strategies # Filter: exclude closed strategies
File diff suppressed because it is too large Load Diff
-1
View File
@@ -19,7 +19,6 @@ from mo_data import read_portfolio, read_decisions, read_watchlist
BASE = Path("/home/hmo/MoFin") BASE = Path("/home/hmo/MoFin")
DATA = BASE / "data" DATA = BASE / "data"
DB_PATH = DATA / "mofin.db" DB_PATH = DATA / "mofin.db"
DECISIONS_PATH = DATA / "decisions.json"
# 失败模式定义(执行层) # 失败模式定义(执行层)
EXEC_FAILURES = { EXEC_FAILURES = {
+1 -5
View File
@@ -24,9 +24,6 @@ BASE = Path("/home/hmo/MoFin")
DATA = BASE / "data" DATA = BASE / "data"
DB_PATH = DATA / "mofin.db" DB_PATH = DATA / "mofin.db"
# 自选池和决策文件
WATCHLIST_PATH = DATA / "watchlist.json"
DECISIONS_PATH = DATA / "decisions.json"
SIGNAL_MAX_AGE_HOURS = 4 # 只处理4小时内产生的信号 SIGNAL_MAX_AGE_HOURS = 4 # 只处理4小时内产生的信号
@@ -273,8 +270,7 @@ def main():
conn2.close() conn2.close()
except Exception: except Exception:
pass pass
# [migrated to DB] — JSON cold backup removed
# WATCHLIST_PATH.write_text(json.dumps(wl, ensure_ascii=False, indent=2))
except: except:
pass pass
elif action == "monitor": elif action == "monitor":
+8 -7
View File
@@ -438,9 +438,6 @@ try:
except ImportError: except ImportError:
HAS_PROMPT_TRACKING = False HAS_PROMPT_TRACKING = False
PORTFOLIO_PATH = "/home/hmo/web-dashboard/data/portfolio.json"
WATCHLIST_PATH = "/home/hmo/web-dashboard/data/watchlist.json"
def safe_json_load(path, default=None): def safe_json_load(path, default=None):
"""安全加载 JSON,遇到坏数据自动修复""" """安全加载 JSON,遇到坏数据自动修复"""
if not os.path.exists(path): if not os.path.exists(path):
@@ -2046,8 +2043,12 @@ def regenerate_all(stdout=True):
pf = {"holdings": holdings} pf = {"holdings": holdings}
wl = {"stocks": wl_stocks} wl = {"stocks": wl_stocks}
except Exception: except Exception:
pf = safe_json_load(PORTFOLIO_PATH, {}) try:
wl = safe_json_load(WATCHLIST_PATH, {}) pf = read_portfolio()
wl = read_watchlist()
except Exception:
pf = {}
wl = {}
all_stocks = {} all_stocks = {}
for item in pf.get("holdings", []): for item in pf.get("holdings", []):
@@ -2068,7 +2069,7 @@ def regenerate_all(stdout=True):
# 加载现有 decisions.json 以便追踪变更 # 加载现有 decisions.json 以便追踪变更
decisions_path = "/home/hmo/web-dashboard/data/decisions.json" decisions_path = "/home/hmo/web-dashboard/data/decisions.json"
try: try:
existing_decisions = {d["code"]: d for d in mo_data.read_decisions().get("decisions", []) if d.get("code")} existing_decisions = {d["code"]: d for d in read_decisions().get("decisions", []) if d.get("code")}
except: except:
existing_decisions = {} existing_decisions = {}
@@ -2445,7 +2446,7 @@ def regenerate_all(stdout=True):
# 写回数据文件 — 保留现有字段(现金、总资产等)不丢 # 写回数据文件 — 保留现有字段(现金、总资产等)不丢
try: try:
existing_pf = mo_data.read_portfolio() existing_pf = read_portfolio()
except Exception: except Exception:
existing_pf = {} existing_pf = {}
# 保留 price/change_pct — price_monitor 维护的实时价,regenerate_all 不应清除 # 保留 price/change_pct — price_monitor 维护的实时价,regenerate_all 不应清除
+70 -16
View File
@@ -9,8 +9,6 @@ from datetime import datetime, timedelta
from pathlib import Path from pathlib import Path
DATA_DIR = Path("/home/hmo/web-dashboard/data") DATA_DIR = Path("/home/hmo/web-dashboard/data")
DECISIONS_PATH = DATA_DIR / "decisions.json"
PORTFOLIO_PATH = DATA_DIR / "portfolio.json"
EVENTS_PATH = DATA_DIR / "price_events.json" EVENTS_PATH = DATA_DIR / "price_events.json"
EVALUATION_PATH = DATA_DIR / "evaluation.json" EVALUATION_PATH = DATA_DIR / "evaluation.json"
ACCURACY_PATH = DATA_DIR / "accuracy_stats.json" ACCURACY_PATH = DATA_DIR / "accuracy_stats.json"
@@ -88,10 +86,24 @@ def run():
# 3. 数据文件检查 # 3. 数据文件检查
lines.append("") lines.append("")
lines.append("【数据文件】") lines.append("【数据文件】")
# DB 优先:从 SQLite 查询代替 JSON 文件检查
try:
from mo_data import read_portfolio, read_decisions, read_watchlist
pf = read_portfolio()
lines.append(check(len(pf.get("holdings", [])) > 0, f"portfolio.json DB记录: {len(pf.get('holdings', []))}"))
ok_count += 1
wl = read_watchlist()
lines.append(check(len(wl.get("stocks", [])) > 0, f"watchlist.json DB记录: {len(wl.get('stocks', []))}"))
ok_count += 1
dec = read_decisions()
lines.append(check(len(dec.get("decisions", [])) > 0, f"decisions.json DB记录: {len(dec.get('decisions', []))}"))
ok_count += 1
except Exception:
lines.append(check(False, "MoFin DB 数据读取失败"))
warn_count += 3
# 仍为 JSON 文件的检查
files = { files = {
"portfolio.json": PORTFOLIO_PATH,
"watchlist.json": DATA_DIR / "watchlist.json",
"decisions.json": DECISIONS_PATH,
"market.json": DATA_DIR / "market.json", "market.json": DATA_DIR / "market.json",
"price_events.json": EVENTS_PATH, "price_events.json": EVENTS_PATH,
"evaluation.json": EVALUATION_PATH, "evaluation.json": EVALUATION_PATH,
@@ -145,8 +157,13 @@ def run():
# 6. 建议记录统计 # 6. 建议记录统计
lines.append("") lines.append("")
lines.append("【建议记录】") lines.append("【建议记录】")
decisions = load_json(DECISIONS_PATH, {"decisions": []}) try:
total_advice = sum(len(d.get("advice_timeline", [])) for d in decisions.get("decisions", [])) from mo_data import read_decisions
dec = read_decisions()
total_advice = sum(len(d.get("advice_timeline", [])) for d in dec.get("decisions", []))
except Exception:
dec = {"decisions": []}
total_advice = 0
lines.append(check(total_advice > 0, f"建议记录: {total_advice}")) lines.append(check(total_advice > 0, f"建议记录: {total_advice}"))
if total_advice == 0: if total_advice == 0:
issues.append("所有策略建议记录为空") issues.append("所有策略建议记录为空")
@@ -173,18 +190,44 @@ def run():
# 8. 数据新鲜度 # 8. 数据新鲜度
lines.append("") lines.append("")
lines.append("【数据新鲜度】") lines.append("【数据新鲜度】")
# 各数据文件的合理最大陈旧时间(小时) # DB 优先:从 SQLite 查最新更新时间
try:
from mofin_db import get_conn
conn = get_conn()
db_checks = {
"portfolio (DB)": ("SELECT MAX(updated_at) FROM holdings", 24),
"decisions (DB)": ("SELECT MAX(updated_at) FROM holding_strategies WHERE status IN ('active','updated')", 48),
}
for name, (sql, threshold) in db_checks.items():
row = conn.execute(sql).fetchone()
ts = row[0] if row and row[0] else None
if not ts:
lines.append(check(False, f"{name} 无更新时间戳"))
issues.append(f"{name} 无更新时间戳")
warn_count += 1
continue
mtime = datetime.strptime(ts[:19], "%Y-%m-%d %H:%M:%S") if len(ts) >= 19 else datetime.strptime(ts[:10], "%Y-%m-%d")
hours_ago = (now - mtime).total_seconds() / 3600
fresh = hours_ago < threshold
time_str = f"{hours_ago:.0f}h前" if hours_ago >= 1 else f"{hours_ago*60:.0f}分钟前"
lines.append(check(fresh, f"{name} 更新于 {time_str} (阈值{threshold}h)"))
if not fresh:
issues.append(f"{name} 超过{threshold}h未更新(最近更新:{time_str}")
warn_count += 1
else:
ok_count += 1
conn.close()
except Exception:
lines.append(check(False, "DB 数据新鲜度检查失败"))
# JSON 文件新鲜度(仅限尚未迁移到 DB 的)
freshness_thresholds = { freshness_thresholds = {
"portfolio.json": 24, # 每日有数据即可
"decisions.json": 48, # 策略参数更新频率较低
"multi_tf_cache.json": 24, # K线缓存每日更新 "multi_tf_cache.json": 24, # K线缓存每日更新
"macro_context.json": 24, # 宏观数据每日2次 "macro_context.json": 24, # 宏观数据每日2次
"market.json": 48, # 行业数据每日更新 "market.json": 48, # 行业数据每日更新
"strategy_staleness_report.json": 24, # 时效性报告每日生成 "strategy_staleness_report.json": 24, # 时效性报告每日生成
} }
data_files = { data_files = {
"portfolio.json": PORTFOLIO_PATH,
"decisions.json": DECISIONS_PATH,
"multi_tf_cache.json": DATA_DIR / "multi_tf_cache.json", "multi_tf_cache.json": DATA_DIR / "multi_tf_cache.json",
"macro_context.json": DATA_DIR / "macro_context.json", "macro_context.json": DATA_DIR / "macro_context.json",
"market.json": DATA_DIR / "market.json", "market.json": DATA_DIR / "market.json",
@@ -228,16 +271,27 @@ def run():
# 价格数据更新时间检查(盘中应有当日数据) # 价格数据更新时间检查(盘中应有当日数据)
is_trading_day = now.weekday() < 5 # 周一到周五 is_trading_day = now.weekday() < 5 # 周一到周五
if is_trading_day and now.hour >= 9 and now.hour < 16: if is_trading_day and now.hour >= 9 and now.hour < 16:
if PORTFOLIO_PATH.exists(): try:
mtime = datetime.fromtimestamp(PORTFOLIO_PATH.stat().st_mtime) from mofin_db import get_conn
hours_ago = (now - mtime).total_seconds() / 3600 conn = get_conn()
row = conn.execute("SELECT MAX(updated_at) FROM holdings WHERE is_active=1").fetchone()
conn.close()
ts = row[0] if row and row[0] else None
if ts:
mtime = datetime.strptime(ts[:19], "%Y-%m-%d %H:%M:%S") if len(ts) >= 19 else datetime.strptime(ts[:10], "%Y-%m-%d")
has_intraday_data = mtime.date() == now.date() has_intraday_data = mtime.date() == now.date()
lines.append(check(has_intraday_data, f"盘中有当日价格数据 {'' if has_intraday_data else ''}(最近{mtime.strftime('%H:%M')})")) lines.append(check(has_intraday_data, f"盘中有当日价格数据 {'' if has_intraday_data else ''}(最近{mtime.strftime('%H:%M')})"))
if not has_intraday_data: if not has_intraday_data:
issues.append(f"盘中交易时段但portfolio.json无今日数据(最近更新{mtime.strftime('%m-%d %H:%M')}") issues.append(f"盘中交易时段但DB holdings无今日数据(最近更新{mtime.strftime('%m-%d %H:%M')}")
warn_count += 1 warn_count += 1
else: else:
ok_count += 1 ok_count += 1
else:
lines.append(check(False, "盘中DB holdings无价格更新记录"))
warn_count += 1
except Exception:
lines.append(check(False, "盘中DB价格数据检查失败"))
warn_count += 1
# 汇总 # 汇总
total = ok_count + warn_count total = ok_count + warn_count