refactor: 数据层重构 — 统一 SQLite 访问层 + 多脚本双写

新建 mofin_db.py 共享数据库模块:
- get_conn() 统一连接管理 (WAL + Row factory + 外键)
- init_all_tables() 幂等建表 (12张表: market/sector/stock/kline/fundamentals/sectors/holdings/strategies/watchlist/candidates/score_history/events/evaluations)
- write_market_snapshot() 市场快照双写
- write_klines() K线数据双写 (stocks + daily/weekly/monthly + fundamentals)
- write_price_event() 价格事件双写
- migrate_stock_sectors() 一次性迁移 stock_sector_map.json
- query_*() 通用查询函数 (sector_trend/top_inflow/consecutive_inflow/market_mood/db_stats)

重构现有脚本:
- market_watch.py: 删除内联 DB 代码,改用 mofin_db
- multi_timeframe.py: _save_local_history() 加 SQLite 双写
- price_monitor.py: record_event() 加 SQLite 双写
- mofin_query.py: 改用 mofin_db 查询函数

新增:
- migrate_sectors.py: 一次性迁移脚本

清理:
- get_realtime_prices.py: 死代码 (只读 portfolio.json,不调API)
This commit is contained in:
hmo
2026-06-20 16:25:36 +08:00
parent 8926b11090
commit 0924cf3124
7 changed files with 568 additions and 308 deletions
+29
View File
@@ -28,6 +28,32 @@ KLINE_URL = "http://web.ifzq.gtimg.cn/appstock/app/fqkline/get?param={market}{co
QUOTE_URL = "http://qt.gtimg.cn/q={market}{code}"
def _write_klines_to_db(code: str, daily: list, weekly: list, monthly: list, fundamentals: dict = None):
"""K线数据双写 SQLite(失败不影响缓存写入)"""
try:
from mofin_db import get_conn, init_all_tables, write_klines
conn = get_conn()
init_all_tables(conn)
# 从 stock_profiles.json 获取名称
name = code
try:
import json
profiles_path = os.path.join(DATA_DIR, "stock_profiles.json")
if os.path.exists(profiles_path):
with open(profiles_path, encoding="utf-8") as f:
profiles = json.load(f)
for p in profiles.get("profiles", []):
if p.get("code") == code:
name = p.get("name", code)
break
except Exception:
pass
write_klines(conn, code, name, daily, weekly, monthly, fundamentals)
conn.close()
except Exception:
pass # SQLite 写入失败不影响主流程
def _market_prefix(code: str) -> str:
"""根据代码确定市场前缀"""
raw = str(code).split("_")[0]
@@ -572,6 +598,9 @@ def _save_local_history(code: str, daily: list, weekly: list, monthly: list):
cache_data[code] = stock
_MTF_CACHE_DATA = cache_data # 更新模块级缓存
# ── SQLite 双写 ──
_write_klines_to_db(code, daily, weekly, monthly, stock.get("fundamentals"))
def batch_update_all(codes: list):
"""批量更新多只股票的多周期数据"""