chore: baseline — ORM migration + systemd deploy + MCP server + daily check
- ORM migration: models/ops 重构 - deploy: 标准化 systemd timer/service 部署体系 - MCP server: 5 个只读工具(任务/状态/数据集) - daily check: 数据一致性巡检 - watch: task_watch 后台监控 - kline_daily: 麦蕊优先,时间门禁15:00 - 删除: task_industry_sector, task_sector_features
This commit is contained in:
@@ -3,15 +3,14 @@
|
||||
所有 `SyncTask` 子类在此集中注册,外部通过 `get_task(dataset_id)` 或
|
||||
遍历 `TASKS` 字典使用。
|
||||
"""
|
||||
from app.tasks.task_industry_sector import SyncIndustrySector
|
||||
from app.tasks.task_kline_5min import SyncKline5Min
|
||||
from app.tasks.task_kline_daily import SyncKlineDaily
|
||||
from app.tasks.task_kline_index import SyncKlineIndex
|
||||
from app.tasks.task_longhubang import SyncLonghubang
|
||||
from app.tasks.task_mairui_ma_daily import SyncMairuiMADaily
|
||||
from app.tasks.task_mairui_indicators import SyncMairuiIndicators
|
||||
from app.tasks.task_market_regime import SyncMarketRegime
|
||||
from app.tasks.task_moneyflow import SyncMoneyflow
|
||||
from app.tasks.task_sector_features import SyncSectorFeatures
|
||||
from app.tasks.task_share_snapshot import SyncShareSnapshot
|
||||
from app.tasks.task_stock_node import SyncStockNode
|
||||
from app.tasks.task_stocks_basic import SyncStocksBasic
|
||||
@@ -26,13 +25,12 @@ TASKS: dict[str, type] = {
|
||||
SyncKline5Min,
|
||||
SyncTickTrade,
|
||||
SyncMoneyflow,
|
||||
SyncIndustrySector,
|
||||
SyncSectorFeatures,
|
||||
SyncShareSnapshot,
|
||||
SyncMarketRegime,
|
||||
SyncLonghubang,
|
||||
SyncStockNode,
|
||||
SyncMairuiMADaily,
|
||||
SyncMairuiIndicators,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
"""同步任务:股票-行业映射(Baostock)。
|
||||
|
||||
数据流:Baostock query_stock_industry() → 写 industry / sectors / stock_sector_map 三张表。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import update
|
||||
|
||||
from app.core.db import ops as db_ops
|
||||
from app.core.db.models import Stock
|
||||
from app.core.datasource.base import registry as ds_registry
|
||||
from app.core.datasource.registry import is_source_ready
|
||||
from app.core.datasource.utils import to_hermes
|
||||
from app.core.sync.base import SyncTask
|
||||
from app.core.sync.registry import mark_sync_blocked
|
||||
from app.core.utils.logging import get_logger
|
||||
|
||||
logger = get_logger("sync.industry_sector")
|
||||
|
||||
|
||||
def _build_sector_key(taxonomy: str, sector_name: str) -> str:
|
||||
tax = (taxonomy or "").strip() or "default"
|
||||
name = (sector_name or "").strip()
|
||||
return f"{tax}::{name}"
|
||||
|
||||
|
||||
class SyncIndustrySector(SyncTask):
|
||||
dataset_id = "industry_sector"
|
||||
|
||||
def _run(self, *, trigger_source: str = "manual", **kwargs) -> dict[str, Any]:
|
||||
bs = ds_registry.get("datasource_baostock")
|
||||
if bs is None:
|
||||
return {"status": "error", "message": "Baostock 数据源未注册"}
|
||||
ok, reason = is_source_ready(bs.key)
|
||||
if not ok:
|
||||
mark_sync_blocked(self.dataset_id, message=f"Baostock 未就绪: {reason}")
|
||||
return {"status": "blocked", "message": f"Baostock 未就绪: {reason}"}
|
||||
|
||||
self._progress(message="拉取股票-行业映射...")
|
||||
t0 = time.time()
|
||||
try:
|
||||
raw_rows = bs.fetch_industry_map()
|
||||
except Exception as e:
|
||||
return {"status": "error", "message": f"拉取失败: {e}"}
|
||||
|
||||
if not raw_rows:
|
||||
return {"status": "warning", "message": "Baostock 未返回行业数据"}
|
||||
|
||||
# 合并:sectors + stock_sector_map + industry
|
||||
sectors_rows: dict[str, dict] = {}
|
||||
stock_sector_rows: list[dict] = []
|
||||
industry_rows: list[dict] = []
|
||||
|
||||
for r in raw_rows:
|
||||
code6 = str(r["code"]).zfill(6)
|
||||
name = (r.get("industry_name") or "").strip()
|
||||
if not name:
|
||||
continue
|
||||
classification = (r.get("industry_classification") or "").strip()
|
||||
sector_key = _build_sector_key(classification, name)
|
||||
|
||||
sectors_rows[sector_key] = {
|
||||
"sector_key": sector_key,
|
||||
"sector_name": name,
|
||||
"taxonomy": classification,
|
||||
"level": "",
|
||||
"source": "baostock_query_stock_industry",
|
||||
"enabled": 1,
|
||||
}
|
||||
stock_sector_rows.append({"stock_code": to_hermes(code6), "sector_key": sector_key})
|
||||
industry_rows.append({
|
||||
"code": to_hermes(code6),
|
||||
"industry_name": name,
|
||||
"industry_classification": classification,
|
||||
"update_date": r.get("update_date", ""),
|
||||
})
|
||||
|
||||
# 写库(全量替换)
|
||||
try:
|
||||
db_ops.replace_all_sectors(list(sectors_rows.values()))
|
||||
db_ops.replace_all_stock_sector_map(stock_sector_rows)
|
||||
db_ops.replace_all_industries(industry_rows)
|
||||
except Exception as e:
|
||||
return {"status": "error", "message": f"写库失败: {e}"}
|
||||
|
||||
# 回填 stocks.industry 字段(让 stocks 表也能直接看到)
|
||||
# 走 ORM update(Stock) + IN (SHxxx, SZxxx, BJxxx, ...)
|
||||
try:
|
||||
# 按 industry_name 分组,对每组用一个 IN 批量更新
|
||||
by_industry: dict[str, list[str]] = {}
|
||||
for r in industry_rows:
|
||||
name = r["industry_name"]
|
||||
code = r["code"]
|
||||
if name not in by_industry:
|
||||
by_industry[name] = []
|
||||
by_industry[name].append(code)
|
||||
for ind_name, codes in by_industry.items():
|
||||
# 6 位 code × 3 个交易所前缀(SH/SZ/BJ)
|
||||
code_variants = [
|
||||
f"{prefix}{c}" for c in codes for prefix in ("SH", "SZ", "BJ")
|
||||
]
|
||||
with db_ops.get_session() as s:
|
||||
s.execute(
|
||||
update(Stock)
|
||||
.where(Stock.code.in_(code_variants))
|
||||
.values(industry=ind_name)
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"回填 stocks.industry 失败(不影响主任务): {e}")
|
||||
|
||||
elapsed = round(time.time() - t0, 1)
|
||||
msg = f"行业 {len(sectors_rows)}个 映射{len(stock_sector_rows)}条, {elapsed}s"
|
||||
return {
|
||||
"status": "ok",
|
||||
"message": msg,
|
||||
"sectors": len(sectors_rows),
|
||||
"stock_sector_map": len(stock_sector_rows),
|
||||
"elapsed_sec": elapsed,
|
||||
}
|
||||
@@ -17,7 +17,7 @@ from __future__ import annotations
|
||||
|
||||
import os
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed, TimeoutError as FuturesTimeoutError
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Any, Optional
|
||||
|
||||
@@ -37,6 +37,10 @@ MAIRUI_5MIN_FLOOR = datetime(2023, 6, 14, tzinfo=timezone.utc)
|
||||
WINDOW_DAYS = 365
|
||||
# 增量时往前多取的天数(防交易日历边界漏当天)
|
||||
INCREMENT_OVERLAP_DAYS = 2
|
||||
# 2026-07-08 教训: 数据源层有 20s timeout,task 层再硬兜底,
|
||||
# 防 SDK 升级 / 网络层 bug 让单只股票卡住(7月7日 4.4只/秒 后突然 0 持续 24h)
|
||||
# 7月9日 mairui/雪球 间歇性 "服务器连接失败" + Broken pipe,拉慢。给 120s 容忍。
|
||||
FETCH_HARD_TIMEOUT = 120.0 # 5min K 一只拉多年,单只允许更久
|
||||
|
||||
|
||||
class SyncKline5Min(SyncTask):
|
||||
@@ -99,7 +103,12 @@ class SyncKline5Min(SyncTask):
|
||||
|
||||
# ── 2) 内存算计划 ──
|
||||
# PG TIMESTAMPTZ 是 timezone-aware,所以 end 也必须 tz-aware 否则比较会炸
|
||||
end = datetime.now(timezone.utc)
|
||||
# 2026-07-12 修复: end 改为上海时区最近交易日 15:00,避免 UTC 与上海时间混用
|
||||
from app.core.datasource.utils import effective_market_date
|
||||
end_date_str = effective_market_date()
|
||||
end = datetime.strptime(f"{end_date_str} 15:00:00", "%Y-%m-%d %H:%M:%S").replace(
|
||||
tzinfo=timezone(timedelta(hours=8))
|
||||
)
|
||||
plans = self._plan(stock_codes, end, snapshots)
|
||||
if not plans:
|
||||
msg = f"5min K线 — 全部 {len(stock_codes)} 只都已最新(无需同步)"
|
||||
@@ -129,12 +138,17 @@ class SyncKline5Min(SyncTask):
|
||||
t0 = time.time()
|
||||
ok_cnt = fail_cnt = 0
|
||||
rows_total = 0
|
||||
with ThreadPoolExecutor(max_workers=max_workers) as pool:
|
||||
futures = {pool.submit(self._sync_one, primary, p[0], p[1], p[2]): p[0] for p in plans}
|
||||
for i, future in enumerate(as_completed(futures), 1):
|
||||
# 不用 `with ThreadPoolExecutor(...) as pool:` — 它的 __exit__ 默认 wait=True,
|
||||
# 一旦某个 worker 卡在 xueqiu IO,主线程会在 as_completed 触发 60s timeout 后
|
||||
# 仍被 __exit__ 阻塞等 worker 退出 → 进程挂死(2026-07-09 13:21 教训)。
|
||||
# 改成手动管理 + shutdown(wait=False),主线程能立刻退出。
|
||||
pool = ThreadPoolExecutor(max_workers=max_workers)
|
||||
futures = {pool.submit(self._sync_one, primary, p[0], p[1], p[2]): p[0] for p in plans}
|
||||
try:
|
||||
for i, future in enumerate(as_completed(futures, timeout=FETCH_HARD_TIMEOUT), 1):
|
||||
c6 = futures[future]
|
||||
try:
|
||||
res = future.result()
|
||||
res = future.result(timeout=0.1) # 已被 as_completed 释放
|
||||
if res["status"] == "ok":
|
||||
ok_cnt += 1
|
||||
rows_total += res["rows"]
|
||||
@@ -142,6 +156,9 @@ class SyncKline5Min(SyncTask):
|
||||
fail_cnt += 1
|
||||
if res.get("error"):
|
||||
logger.warning(f"[5min {c6}] {res['error']}")
|
||||
except FuturesTimeoutError:
|
||||
fail_cnt += 1
|
||||
logger.warning(f"[5min {c6}] 内部 race timeout, 记 fail")
|
||||
except Exception as e:
|
||||
fail_cnt += 1
|
||||
logger.warning(f"[5min {c6}] {e}")
|
||||
@@ -150,6 +167,64 @@ class SyncKline5Min(SyncTask):
|
||||
message=f"5min 进度 {i}/{len(plans)} OK:{ok_cnt} FAIL:{fail_cnt}",
|
||||
current=i, total=len(plans), current_step=c6,
|
||||
)
|
||||
except FuturesTimeoutError:
|
||||
stuck = [c6 for _, c6 in futures.items() if not _.done()]
|
||||
logger.error(
|
||||
"[5min] 所有 fetcher 卡死 (>%ss), %d 只股票未完成",
|
||||
FETCH_HARD_TIMEOUT, len(stuck),
|
||||
)
|
||||
for f, c6 in futures.items():
|
||||
try:
|
||||
if not f.done() and hasattr(f, "cancel"):
|
||||
f.cancel()
|
||||
except Exception as e:
|
||||
logger.warning(f"[5min] cancel future for {c6} 失败: {e}")
|
||||
try:
|
||||
pool.shutdown(wait=False)
|
||||
except Exception as e:
|
||||
logger.warning(f"[5min] pool.shutdown(wait=False) 失败: {e}")
|
||||
|
||||
# ── 补偿:冷却后重入队列 ──
|
||||
# mairui 偶发全 hang(中间件重启/网络抖动),等 30s 再试一次。
|
||||
# 如果还 hang 就放弃,下次调度增量重拉。
|
||||
if stuck:
|
||||
cooldown = 30
|
||||
logger.warning(f"[5min] 冷却 {cooldown}s 后重试 {len(stuck)} 只…")
|
||||
time.sleep(cooldown)
|
||||
retry_ok = retry_fail = retry_rows = 0
|
||||
retry_pool = ThreadPoolExecutor(max_workers=max_workers)
|
||||
plan_dict = {p[0]: (p[1], p[2]) for p in plans}
|
||||
retry_futs = {}
|
||||
for c6 in stuck:
|
||||
if c6 in plan_dict:
|
||||
s, e = plan_dict[c6]
|
||||
# 重试直接用雪球 fallback(Mairui 已全 hang)
|
||||
retry_futs[retry_pool.submit(self._fallback_xueqiu_5m_and_write, c6, s, e)] = c6
|
||||
if retry_futs:
|
||||
for fut in as_completed(retry_futs, timeout=FETCH_HARD_TIMEOUT):
|
||||
c6 = retry_futs[fut]
|
||||
try:
|
||||
res = fut.result(timeout=0.1)
|
||||
if res["status"] == "ok":
|
||||
retry_ok += 1
|
||||
retry_rows += res["rows"]
|
||||
else:
|
||||
retry_fail += 1
|
||||
except Exception:
|
||||
retry_fail += 1
|
||||
try:
|
||||
retry_pool.shutdown(wait=False)
|
||||
except Exception:
|
||||
pass
|
||||
ok_cnt += retry_ok
|
||||
fail_cnt -= retry_ok # 从 fail 挪到 ok
|
||||
rows_total += retry_rows
|
||||
logger.warning(
|
||||
f"[5min] 重试结果: {retry_ok}成 {retry_fail}败 "
|
||||
f"共{retry_rows}行, 救回 {retry_ok} 只"
|
||||
)
|
||||
else:
|
||||
pool.shutdown(wait=True)
|
||||
elapsed = round(time.time() - t0, 1)
|
||||
msg = (
|
||||
f"5min K线 {ok_cnt}成 {fail_cnt}败 共{rows_total}行, "
|
||||
@@ -192,10 +267,16 @@ class SyncKline5Min(SyncTask):
|
||||
})
|
||||
cur = w_end + timedelta(days=1)
|
||||
except Exception as e:
|
||||
return {"status": "fail", "error": str(e)}
|
||||
logger.warning(f"[5min {code6}] mairui 失败, 尝试雪球 5m fallback: {e}")
|
||||
all_rows = self._fallback_xueqiu_5m(code6, start, end)
|
||||
|
||||
if not all_rows:
|
||||
return {"status": "fail", "error": "no data"}
|
||||
# Mairui 无数据, 尝试雪球 fallback
|
||||
logger.info(f"[5min {code6}] mairui 无数据, 尝试雪球 fallback")
|
||||
all_rows = self._fallback_xueqiu_5m(code6, start, end)
|
||||
|
||||
if not all_rows:
|
||||
return {"status": "fail", "error": "no data (mairui + xueqiu fallback)"}
|
||||
try:
|
||||
CHUNK = 5000
|
||||
for i in range(0, len(all_rows), CHUNK):
|
||||
@@ -203,3 +284,77 @@ class SyncKline5Min(SyncTask):
|
||||
except Exception as e:
|
||||
return {"status": "fail", "error": f"db write: {e}"}
|
||||
return {"status": "ok", "rows": len(all_rows)}
|
||||
|
||||
@staticmethod
|
||||
def _fallback_xueqiu_5m_and_write(code6: str, start: datetime, end: datetime) -> dict:
|
||||
"""雪球 5m fallback + 直接写库,对齐 _sync_one 返回格式供 retry 逻辑使用。"""
|
||||
rows = SyncKline5Min._fallback_xueqiu_5m(code6, start, end)
|
||||
if not rows:
|
||||
return {"status": "fail", "error": "no data via xueqiu"}
|
||||
try:
|
||||
CHUNK = 5000
|
||||
for i in range(0, len(rows), CHUNK):
|
||||
db_ops.upsert_kline_5min(rows[i: i + CHUNK])
|
||||
except Exception as e:
|
||||
return {"status": "fail", "error": f"db write: {e}"}
|
||||
return {"status": "ok", "rows": len(rows)}
|
||||
|
||||
@staticmethod
|
||||
def _fallback_xueqiu_5m(code6: str, start: datetime, end: datetime) -> list[dict]:
|
||||
"""通过雪球 5m K线 fallback 拉取数据,对齐 _sync_one 返回格式。"""
|
||||
import os
|
||||
from datetime import datetime as dt_mod
|
||||
from app.core.datasource.utils import code6_to_xueqiu, to_hermes
|
||||
|
||||
token_raw = os.environ.get("XUEQIU_TOKEN", "").strip()
|
||||
if not token_raw:
|
||||
# 从 .env 读
|
||||
try:
|
||||
with open("/home/gao/Development/quant_home/market_sync/.env") as f:
|
||||
for line in f:
|
||||
if line.startswith("XUEQIU_TOKEN="):
|
||||
token_raw = line.strip().split("=", 1)[1]
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
if not token_raw:
|
||||
logger.warning("[xueqiu_5m] 无 XUEQIU_TOKEN, 跳过 fallback")
|
||||
return []
|
||||
|
||||
import pysnowball as ball
|
||||
ball.set_token(token_raw)
|
||||
|
||||
symbol = code6_to_xueqiu(code6)
|
||||
# 计算需要多少根 5m bar (每天 48 根, 加 buffer)
|
||||
days_needed = max((end - start).days + 2, 1)
|
||||
count = days_needed * 48
|
||||
|
||||
try:
|
||||
data = ball.kline(symbol, "5m", min(count, 5000))
|
||||
except Exception as e:
|
||||
logger.warning(f"[xueqiu_5m {code6}] 请求失败: {e}")
|
||||
return []
|
||||
|
||||
items = data.get("data", {}).get("item", [])
|
||||
if not items:
|
||||
return []
|
||||
|
||||
columns = data["data"]["column"] # [timestamp, volume, open, high, low, close, ...]
|
||||
idx = {c: i for i, c in enumerate(columns)}
|
||||
hermes = to_hermes(code6)
|
||||
rows = []
|
||||
for item in items:
|
||||
ts = item[idx["timestamp"]] / 1000
|
||||
bar_time = dt_mod.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S")
|
||||
rows.append({
|
||||
"stock_code": hermes,
|
||||
"bar_time": bar_time,
|
||||
"open": float(item[idx["open"]]),
|
||||
"high": float(item[idx["high"]]),
|
||||
"low": float(item[idx["low"]]),
|
||||
"close": float(item[idx["close"]]),
|
||||
"volume": float(item[idx["volume"]]),
|
||||
"amount": 0.0,
|
||||
"turnover_rate": 0.0,
|
||||
})
|
||||
return rows
|
||||
|
||||
+152
-29
@@ -19,6 +19,7 @@ import os
|
||||
import queue
|
||||
import threading
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed, TimeoutError as FuturesTimeoutError
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
@@ -32,15 +33,19 @@ from app.core.utils.logging import get_logger
|
||||
|
||||
logger = get_logger("sync.kline_daily")
|
||||
|
||||
# 主源优先级:mairui(5 RPS,免费 + 稳定 + 单接口含5min)→ 雪球 → Baostock → 新浪
|
||||
# 经验:sina 在单日窗口 (start=end=某天) 会返回空,雪球更可靠
|
||||
PRIMARY_PRIORITY = ["datasource_xueqiu", "datasource_mairui", "datasource_xinlang"]
|
||||
# 源优先级:麦蕊 → 雪球 → 新浪
|
||||
# 2026-07-21 改: 麦蕊15:10已有数据,雪球要15:40后才齐,麦蕊为主源提速
|
||||
PRIMARY_PRIORITY = ["datasource_mairui", "datasource_xueqiu", "datasource_xinlang"]
|
||||
DEFAULT_START = "2018-01-01"
|
||||
# 写库批量:攒够 BATCH_SIZE 行就 executemany 一次 + commit
|
||||
BATCH_SIZE = 2000
|
||||
# fetcher 并发:实测单线程最稳(5000 只 × 0.2s = 17 分钟,足够)
|
||||
# 雪球 token 限速 10 RPS,单线程 0.1s/只 已经打满。多 worker 会触发风控 hang
|
||||
DEFAULT_FETCH_WORKERS = 1
|
||||
# 2026-07-08 教训: 数据源层虽然加了 fetch timeout (20s), task 层也加硬上限兜底,
|
||||
# 防止数据源 SDK 升级后又被绕过。60s 对应 xueqiu/mairui 的 15-20s 上限 + DB upsert
|
||||
# / DataFrame 处理余量 + 7月9日 mairui 间歇慢请求的容忍窗口。
|
||||
FETCH_HARD_TIMEOUT = 180.0
|
||||
|
||||
|
||||
class SyncKlineDaily(SyncTask):
|
||||
@@ -57,14 +62,15 @@ class SyncKlineDaily(SyncTask):
|
||||
max_workers: int = DEFAULT_FETCH_WORKERS,
|
||||
**kwargs,
|
||||
) -> dict[str, Any]:
|
||||
# 1. 选主源
|
||||
primary = self._pick_primary()
|
||||
if primary is None:
|
||||
# 1. 获取所有可用源(按优先级),支持 per-stock fallback
|
||||
sources = self._get_available_sources()
|
||||
if not sources:
|
||||
return {"status": "error", "message": "所有 K 线数据源均不可用"}
|
||||
source_names = ", ".join(s.name for s in sources)
|
||||
|
||||
self._progress(
|
||||
message=f"使用 {primary.name} 同步日K线 (fetcher={max_workers}, batch={BATCH_SIZE})...",
|
||||
current_step=primary.key,
|
||||
message=f"使用 [{source_names}] 同步日K线 (fetcher={max_workers}, batch={BATCH_SIZE})...",
|
||||
current_step=sources[0].key,
|
||||
)
|
||||
|
||||
# 2. 拉股票列表
|
||||
@@ -108,7 +114,6 @@ class SyncKlineDaily(SyncTask):
|
||||
)
|
||||
|
||||
# 4. 并发 fetch + 攒 batch 写库(线程安全)
|
||||
# mairui 等外部 API 无并发问题;DB 写串行加锁
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
t0 = time.time()
|
||||
batch_lock = threading.Lock()
|
||||
@@ -116,6 +121,7 @@ class SyncKlineDaily(SyncTask):
|
||||
pending_codes: dict[str, str] = {}
|
||||
ok_lock = threading.Lock()
|
||||
ok_cnt = [0]
|
||||
fallback_ok_cnt = [0]
|
||||
fail_cnt = [0]
|
||||
rows_cnt = [0]
|
||||
|
||||
@@ -138,15 +144,7 @@ class SyncKlineDaily(SyncTask):
|
||||
except Exception as e:
|
||||
logger.warning(f"update synced_at 失败 {c6}: {e}")
|
||||
|
||||
def _fetch_one(code6: str, fetch_start: str) -> tuple[str, list[dict] | None, str | None]:
|
||||
try:
|
||||
df = primary.fetch_kline_daily(code6, fetch_start, _end)
|
||||
except Exception as e:
|
||||
return (code6, None, str(e)[:120])
|
||||
if df is None or df.empty:
|
||||
return (code6, None, "no data")
|
||||
# 写库用 hermes(带 SH/SZ 前缀)—— 2026-07-01 重构:历史上 kline_stock 用
|
||||
# 6位 code 与 stocks.code(SH600519) 格式不一致,跨表 JOIN 失败。现统一。
|
||||
def _rows_from_df(code6: str, df: pd.DataFrame) -> tuple[list[dict], str]:
|
||||
hermes = to_hermes(code6)
|
||||
rows = []
|
||||
for _, r in df.iterrows():
|
||||
@@ -162,20 +160,56 @@ class SyncKlineDaily(SyncTask):
|
||||
})
|
||||
latest = df["trade_date"].max()
|
||||
latest = latest.strftime("%Y-%m-%d") if hasattr(latest, "strftime") else str(latest)[:10]
|
||||
return (code6, rows, latest)
|
||||
return (rows, latest)
|
||||
|
||||
def _fetch_one(code6: str, fetch_start: str) -> tuple[str, list[dict] | None, str | None, bool]:
|
||||
errors = []
|
||||
for src in sources:
|
||||
try:
|
||||
df = src.fetch_kline_daily(code6, fetch_start, _end)
|
||||
except Exception as e:
|
||||
errors.append(f"{src.key}: {e}")
|
||||
continue
|
||||
if df is None or df.empty:
|
||||
errors.append(f"{src.key}: no data")
|
||||
continue
|
||||
rows, latest = _rows_from_df(code6, df)
|
||||
is_fallback = src is not sources[0]
|
||||
return (code6, rows, latest, is_fallback)
|
||||
return (code6, None, "; ".join(errors)[:200], False)
|
||||
|
||||
total = len(jobs)
|
||||
with ThreadPoolExecutor(max_workers=max_workers) as pool:
|
||||
futures = {pool.submit(_fetch_one, c6, fs): c6 for c6, fs in jobs}
|
||||
done_cnt = 0
|
||||
for future in as_completed(futures):
|
||||
# 不用 `with ThreadPoolExecutor(...) as pool:` — 它的 __exit__ 默认 wait=True,
|
||||
# 一旦某个 worker 卡在 xueqiu IO,主线程会在 as_completed 触发 timeout 后
|
||||
# 仍被 __exit__ 阻塞等 worker 退出 → 进程挂死(2026-07-09 13:21 教训)。
|
||||
# 改成手动管理 + shutdown(wait=False),主线程能立刻退出。
|
||||
pool = ThreadPoolExecutor(max_workers=max_workers)
|
||||
futures = {pool.submit(_fetch_one, c6, fs): c6 for c6, fs in jobs}
|
||||
done_cnt = 0
|
||||
try:
|
||||
for future in as_completed(futures, timeout=FETCH_HARD_TIMEOUT):
|
||||
done_cnt += 1
|
||||
code6, rows, latest = future.result()
|
||||
code6 = futures[future]
|
||||
try:
|
||||
code6_r, rows, latest, is_fallback = future.result(timeout=0.1)
|
||||
except FuturesTimeoutError:
|
||||
with ok_lock:
|
||||
fail_cnt[0] += 1
|
||||
logger.warning(f"[kline {code6}] 内部 race timeout, 记 fail")
|
||||
continue
|
||||
except Exception as e:
|
||||
with ok_lock:
|
||||
fail_cnt[0] += 1
|
||||
logger.warning(f"[kline {code6}] 异常: {e}")
|
||||
continue
|
||||
if rows is None:
|
||||
with ok_lock:
|
||||
fail_cnt[0] += 1
|
||||
logger.warning(f"[kline {code6}] {latest}")
|
||||
continue
|
||||
if is_fallback:
|
||||
with ok_lock:
|
||||
fallback_ok_cnt[0] += 1
|
||||
with batch_lock:
|
||||
batch_rows.extend(rows)
|
||||
pending_codes[code6] = latest
|
||||
@@ -194,32 +228,121 @@ class SyncKlineDaily(SyncTask):
|
||||
message=f"进度 {done_cnt}/{total} OK:{ok_cnt[0]} FAIL:{fail_cnt[0]} {rate:.1f}只/秒",
|
||||
current=done_cnt, total=total, current_step=code6,
|
||||
)
|
||||
except FuturesTimeoutError:
|
||||
# 30s 内没 future 完成 → 所有 worker 都被卡死(数据源层 timeout 兜底失败)
|
||||
# 取消 pending futures,标 fail。shutdown(wait=False) 让主线程立刻返回,
|
||||
# 不被卡住的 worker 拖死。
|
||||
# 2026-07-11 修复: 防御性 cancel,避免异常处理链里变量被遮蔽导致
|
||||
# AttributeError: 'str' object has no attribute 'cancel'。
|
||||
stuck = [(f, c6) for f, c6 in futures.items() if not f.done()]
|
||||
logger.error(
|
||||
"所有 fetcher 卡死 (>%ss), %d 只股票未完成, 标 fail 后退出主循环",
|
||||
FETCH_HARD_TIMEOUT, len(stuck),
|
||||
)
|
||||
with ok_lock:
|
||||
fail_cnt[0] += len(stuck)
|
||||
for f, c6 in stuck:
|
||||
try:
|
||||
if hasattr(f, "cancel"):
|
||||
f.cancel()
|
||||
except Exception as cancel_err:
|
||||
logger.warning(f"[kline] cancel future for {c6} 失败: {cancel_err}")
|
||||
try:
|
||||
pool.shutdown(wait=False)
|
||||
except Exception as shutdown_err:
|
||||
logger.warning(f"[kline] pool.shutdown(wait=False) 失败: {shutdown_err}")
|
||||
|
||||
# ── 补偿:冷却后重入队列 ──
|
||||
# 雪球/mairui 偶发全 hang(服务端限流/网络抖动),等 30s 再试一次。
|
||||
# 单线程拉取场景下,卡住通常意味着当前这只股票请求 hang 死;
|
||||
# 冷却后把未完成的股票重新排队重试,救回临时故障。
|
||||
if stuck:
|
||||
cooldown = 30
|
||||
logger.warning(f"[kline] 冷却 {cooldown}s 后重试 {len(stuck)} 只…")
|
||||
time.sleep(cooldown)
|
||||
retry_ok = 0
|
||||
retry_fb = 0
|
||||
retry_rows = 0
|
||||
retry_pool = ThreadPoolExecutor(max_workers=max_workers)
|
||||
jobs_dict = {c6: fs for c6, fs in jobs}
|
||||
retry_futs = {}
|
||||
for _, c6 in stuck:
|
||||
if c6 in jobs_dict:
|
||||
retry_futs[retry_pool.submit(_fetch_one, c6, jobs_dict[c6])] = c6
|
||||
if retry_futs:
|
||||
try:
|
||||
for fut in as_completed(retry_futs, timeout=FETCH_HARD_TIMEOUT):
|
||||
c6 = retry_futs[fut]
|
||||
try:
|
||||
code6_r, rows, latest, is_fallback = fut.result(timeout=0.1)
|
||||
except Exception:
|
||||
logger.warning(f"[kline {c6}] 重试异常")
|
||||
continue
|
||||
if rows is None:
|
||||
logger.warning(f"[kline {c6}] 重试仍失败: {latest}")
|
||||
continue
|
||||
with batch_lock:
|
||||
batch_rows.extend(rows)
|
||||
pending_codes[c6] = latest
|
||||
cur_size = len(batch_rows)
|
||||
with ok_lock:
|
||||
retry_ok += 1
|
||||
retry_rows += len(rows)
|
||||
if is_fallback:
|
||||
retry_fb += 1
|
||||
if cur_size >= BATCH_SIZE:
|
||||
_flush()
|
||||
except FuturesTimeoutError:
|
||||
logger.error(
|
||||
"[kline] 重试仍卡死 (>%ss), %d 只股票放弃",
|
||||
FETCH_HARD_TIMEOUT, sum(1 for f in retry_futs if not f.done()),
|
||||
)
|
||||
try:
|
||||
retry_pool.shutdown(wait=False)
|
||||
except Exception:
|
||||
pass
|
||||
with ok_lock:
|
||||
ok_cnt[0] += retry_ok
|
||||
fail_cnt[0] -= retry_ok
|
||||
fallback_ok_cnt[0] += retry_fb
|
||||
rows_cnt[0] += retry_rows
|
||||
logger.warning(
|
||||
f"[kline] 重试结果: {retry_ok}成 "
|
||||
f"({retry_fb}只备胎) 共{retry_rows}行, 救回 {retry_ok} 只"
|
||||
)
|
||||
else:
|
||||
pool.shutdown(wait=True)
|
||||
|
||||
# 收尾 flush
|
||||
_flush()
|
||||
|
||||
elapsed = round(time.time() - t0, 1)
|
||||
msg = f"日K线 {ok_cnt[0]}成 {fail_cnt[0]}败 {skipped}跳 共{rows_cnt[0]}行, {elapsed}s"
|
||||
fb = fallback_ok_cnt[0]
|
||||
fb_info = f" ({fb}只备胎)" if fb else ""
|
||||
msg = f"日K线 {ok_cnt[0]}成 {fail_cnt[0]}败 {skipped}跳 共{rows_cnt[0]}行{fb_info}, {elapsed}s"
|
||||
return {
|
||||
"status": "ok" if fail_cnt[0] == 0 else "warning",
|
||||
"message": msg,
|
||||
"ok": ok_cnt[0],
|
||||
"fail": fail_cnt[0],
|
||||
"fallback_ok": fb,
|
||||
"skip": skipped,
|
||||
"rows": rows_cnt[0],
|
||||
"elapsed_sec": elapsed,
|
||||
"primary_source": primary.key,
|
||||
"primary_source": sources[0].key if sources else "",
|
||||
"sources": [s.key for s in sources],
|
||||
}
|
||||
|
||||
def _pick_primary(self):
|
||||
def _get_available_sources(self):
|
||||
from app.core.datasource.registry import is_source_ready, run_health_check, get_health_status
|
||||
if not get_health_status():
|
||||
run_health_check()
|
||||
available = []
|
||||
for key in PRIMARY_PRIORITY:
|
||||
ok, _ = is_source_ready(key)
|
||||
if ok:
|
||||
return ds_registry.get(key)
|
||||
return None
|
||||
available.append(ds_registry.get(key))
|
||||
return available
|
||||
|
||||
def _sync_one(self, primary, code6: str, start: str, end: str) -> dict:
|
||||
"""保留这个方法以兼容外部调用(已不用,但单只测试可能用到)"""
|
||||
|
||||
@@ -0,0 +1,261 @@
|
||||
"""同步任务:mairui 日 K 级别技术指标 MACD / KDJ / BOLL。
|
||||
|
||||
数据源:mairui /hsstock/history/{indicator}/{symbol}/d/n/{licence}
|
||||
- macd → market_data.kline_stock_macd_daily (diff, dea, macd, ema12, ema26)
|
||||
- kdj → market_data.kline_stock_kdj_daily (k, d, j)
|
||||
- boll → market_data.kline_stock_boll_daily (upper, mid, lower ← mairui u/m/d)
|
||||
|
||||
设计(对齐 task_kline_daily):
|
||||
- 全市场 A 股循环,每只票分别拉 3 个指标
|
||||
- 增量:读各指标表 MAX(trade_date),只补新增区间(回看 5 天防漏)
|
||||
- 并发 fetch + 攒 BATCH 批量 upsert,写库串行加锁
|
||||
- stock_code 落库用 hermes 格式(SH600519),与项目其他表一致
|
||||
|
||||
参考实现:pre_limit_seeker/production/sync_mairui_indicators.py(parquet 版)。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from app.core.db import ops as db_ops
|
||||
from app.core.datasource.base import registry as ds_registry
|
||||
from app.core.datasource.utils import is_a_share_code, to_code6, to_hermes
|
||||
from app.core.sync.base import SyncTask, _effective_sync_end
|
||||
from app.core.utils.logging import get_logger
|
||||
|
||||
logger = get_logger("sync.mairui_indicators")
|
||||
|
||||
DEFAULT_START = "2018-01-01"
|
||||
BATCH_SIZE = 2000
|
||||
# mairui 钻石档 100 RPS;5 workers × 10 RPS/worker = 50 RPS,留 buffer
|
||||
DEFAULT_FETCH_WORKERS = 5
|
||||
|
||||
# 指标 → (mairui 源字段, 落库列名, upsert 函数)
|
||||
INDICATORS: dict[str, dict[str, Any]] = {
|
||||
"macd": {
|
||||
"src_fields": ["diff", "dea", "macd", "ema12", "ema26"],
|
||||
"db_cols": ["diff", "dea", "macd", "ema12", "ema26"],
|
||||
"upsert": db_ops.upsert_kline_stock_macd_daily_rows,
|
||||
},
|
||||
"kdj": {
|
||||
"src_fields": ["k", "d", "j"],
|
||||
"db_cols": ["k", "d", "j"],
|
||||
"upsert": db_ops.upsert_kline_stock_kdj_daily_rows,
|
||||
},
|
||||
"boll": {
|
||||
# mairui: u=上轨, m=中轨, d=下轨 → 落库 upper/mid/lower
|
||||
"src_fields": ["u", "m", "d"],
|
||||
"db_cols": ["upper", "mid", "lower"],
|
||||
"upsert": db_ops.upsert_kline_stock_boll_daily_rows,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class SyncMairuiIndicators(SyncTask):
|
||||
dataset_id = "mairui_indicators"
|
||||
|
||||
def _run(
|
||||
self,
|
||||
*,
|
||||
trigger_source: str = "manual",
|
||||
indicators: list[str] | None = None,
|
||||
codes: list[str] | None = None,
|
||||
start: str | None = None,
|
||||
end: str | None = None,
|
||||
incremental: bool = True,
|
||||
max_workers: int = DEFAULT_FETCH_WORKERS,
|
||||
**kwargs,
|
||||
) -> dict[str, Any]:
|
||||
targets = [i.lower() for i in (indicators or ["macd", "kdj", "boll"])]
|
||||
for i in targets:
|
||||
if i not in INDICATORS:
|
||||
return {"status": "error", "message": f"未知指标 {i!r},可选 {list(INDICATORS)}"}
|
||||
|
||||
source = self._pick_source()
|
||||
if source is None:
|
||||
return {"status": "error", "message": "mairui 数据源不可用(licence 未配置?)"}
|
||||
|
||||
# 股票列表(hermes 格式)
|
||||
if codes:
|
||||
hermes_codes = [to_hermes(c) for c in codes]
|
||||
else:
|
||||
hermes_codes = [
|
||||
to_hermes(c) for c in db_ops.iter_stock_codes(active_only=True)
|
||||
if is_a_share_code(c)
|
||||
]
|
||||
limit_raw = os.environ.get("MARKET_DATA_STOCK_LIMIT", "").strip()
|
||||
if limit_raw.isdigit() and int(limit_raw) > 0:
|
||||
hermes_codes = hermes_codes[: int(limit_raw)]
|
||||
if not hermes_codes:
|
||||
return {"status": "error", "message": "无股票代码(stocks 表为空?)"}
|
||||
|
||||
_end = end or _effective_sync_end()
|
||||
end_date_obj = datetime.strptime(_end, "%Y-%m-%d").date()
|
||||
|
||||
t0 = time.time()
|
||||
agg_ok = 0
|
||||
agg_fail = 0
|
||||
agg_rows = 0
|
||||
per_indicator: dict[str, dict[str, int]] = {}
|
||||
|
||||
for ind in targets:
|
||||
self._progress(message=f"[{ind}] 规划增量区间...", current_step=ind)
|
||||
jobs = self._plan_jobs(ind, hermes_codes, start, incremental, end_date_obj)
|
||||
total = len(jobs)
|
||||
skipped = len(hermes_codes) - total
|
||||
logger.info(f"[mairui_indicators] {ind}: to_pull={total}, skip={skipped}")
|
||||
self._progress(
|
||||
message=f"[{ind}] 开始同步 {total} 只 (跳过 {skipped} 只已最新)",
|
||||
current=0, total=total, current_step=ind,
|
||||
)
|
||||
|
||||
ok, fail, rows = self._sync_indicator(
|
||||
source, ind, jobs, _end, max_workers,
|
||||
)
|
||||
per_indicator[ind] = {"ok": ok, "fail": fail, "skip": skipped, "rows": rows}
|
||||
agg_ok += ok
|
||||
agg_fail += fail
|
||||
agg_rows += rows
|
||||
|
||||
elapsed = round(time.time() - t0, 1)
|
||||
parts = ", ".join(
|
||||
f"{k}({v['ok']}成/{v['fail']}败/{v['rows']}行)"
|
||||
for k, v in per_indicator.items()
|
||||
)
|
||||
msg = f"指标 {parts};共 {agg_rows} 行, {elapsed}s"
|
||||
logger.info(f"[mairui_indicators] {msg}")
|
||||
return {
|
||||
"status": "ok" if agg_fail == 0 else "warning",
|
||||
"message": msg,
|
||||
"ok": agg_ok,
|
||||
"fail": agg_fail,
|
||||
"rows": agg_rows,
|
||||
"elapsed_sec": elapsed,
|
||||
"per_indicator": per_indicator,
|
||||
}
|
||||
|
||||
def _plan_jobs(
|
||||
self, indicator: str, hermes_codes: list[str],
|
||||
start: str | None, incremental: bool, end_date_obj,
|
||||
) -> list[tuple[str, str]]:
|
||||
"""返回 [(hermes_code, fetch_start), ...]。"""
|
||||
jobs: list[tuple[str, str]] = []
|
||||
if incremental and not start:
|
||||
for hc in hermes_codes:
|
||||
last = db_ops.get_indicator_max_date(indicator, hc)
|
||||
if last is None:
|
||||
fetch_start = DEFAULT_START
|
||||
elif datetime.strptime(last, "%Y-%m-%d").date() >= end_date_obj:
|
||||
continue
|
||||
else:
|
||||
fetch_start = (
|
||||
datetime.strptime(last, "%Y-%m-%d").date() - timedelta(days=5)
|
||||
).strftime("%Y-%m-%d")
|
||||
jobs.append((hc, fetch_start))
|
||||
else:
|
||||
fetch_start = start or DEFAULT_START
|
||||
jobs = [(hc, fetch_start) for hc in hermes_codes]
|
||||
return jobs
|
||||
|
||||
def _sync_indicator(
|
||||
self, source, indicator: str, jobs: list[tuple[str, str]],
|
||||
end: str, max_workers: int,
|
||||
) -> tuple[int, int, int]:
|
||||
cfg = INDICATORS[indicator]
|
||||
upsert_fn = cfg["upsert"]
|
||||
src_fields = cfg["src_fields"]
|
||||
db_cols = cfg["db_cols"]
|
||||
|
||||
batch_lock = threading.Lock()
|
||||
batch_rows: list[dict] = []
|
||||
ok_lock = threading.Lock()
|
||||
ok_cnt = [0]
|
||||
fail_cnt = [0]
|
||||
rows_cnt = [0]
|
||||
|
||||
def _flush():
|
||||
with batch_lock:
|
||||
if not batch_rows:
|
||||
return
|
||||
to_write = list(batch_rows)
|
||||
batch_rows.clear()
|
||||
try:
|
||||
upsert_fn(to_write)
|
||||
except Exception as e:
|
||||
logger.error(f"[{indicator}] batch upsert 失败 ({len(to_write)} 行): {e}")
|
||||
|
||||
def _fetch_one(hermes_code: str, fetch_start: str):
|
||||
code6 = to_code6(hermes_code)
|
||||
try:
|
||||
df = source.fetch_indicator_daily(indicator, code6, fetch_start, end)
|
||||
except Exception as e:
|
||||
return (hermes_code, None, str(e)[:120])
|
||||
if df is None or df.empty:
|
||||
return (hermes_code, None, "no data")
|
||||
rows = []
|
||||
for _, r in df.iterrows():
|
||||
td = r["trade_date"]
|
||||
# 2026-07-11 修复: mairui 可能返回整行 NaN/NaT(如 688 早期无指标),
|
||||
# float(NaT) 会抛 TypeError。用 pandas.isna 统一跳过无效值。
|
||||
row = {
|
||||
"stock_code": hermes_code,
|
||||
"trade_date": td.strftime("%Y-%m-%d") if hasattr(td, "strftime") else str(td)[:10],
|
||||
"source": "mairui",
|
||||
}
|
||||
for src_f, db_c in zip(src_fields, db_cols):
|
||||
v = r.get(src_f)
|
||||
if v is None or (hasattr(v, "isna") and v.isna()) or (isinstance(v, float) and v != v):
|
||||
row[db_c] = None
|
||||
else:
|
||||
try:
|
||||
row[db_c] = float(v)
|
||||
except Exception:
|
||||
row[db_c] = None
|
||||
rows.append(row)
|
||||
return (hermes_code, rows, None)
|
||||
|
||||
total = len(jobs)
|
||||
t0 = time.time()
|
||||
with ThreadPoolExecutor(max_workers=max_workers) as pool:
|
||||
futures = {pool.submit(_fetch_one, hc, fs): hc for hc, fs in jobs}
|
||||
done = 0
|
||||
for fut in as_completed(futures):
|
||||
done += 1
|
||||
hermes_code, rows, err = fut.result()
|
||||
if rows is None:
|
||||
with ok_lock:
|
||||
fail_cnt[0] += 1
|
||||
continue
|
||||
with batch_lock:
|
||||
batch_rows.extend(rows)
|
||||
cur = len(batch_rows)
|
||||
with ok_lock:
|
||||
ok_cnt[0] += 1
|
||||
rows_cnt[0] += len(rows)
|
||||
if cur >= BATCH_SIZE:
|
||||
_flush()
|
||||
if done % 100 == 0 or done == total:
|
||||
el = time.time() - t0
|
||||
rate = done / el if el > 0 else 0
|
||||
self._progress(
|
||||
message=f"[{indicator}] {done}/{total} OK:{ok_cnt[0]} FAIL:{fail_cnt[0]} {rate:.1f}只/秒",
|
||||
current=done, total=total, current_step=indicator,
|
||||
)
|
||||
_flush()
|
||||
return ok_cnt[0], fail_cnt[0], rows_cnt[0]
|
||||
|
||||
def _pick_source(self):
|
||||
from app.core.datasource.registry import (
|
||||
get_health_status, is_source_ready, run_health_check,
|
||||
)
|
||||
if not get_health_status():
|
||||
run_health_check()
|
||||
ok, _ = is_source_ready("datasource_mairui")
|
||||
if ok:
|
||||
return ds_registry.get("datasource_mairui")
|
||||
return None
|
||||
@@ -3,7 +3,6 @@
|
||||
基于本地 kline_stock 数据聚合:advancers / decliners / turnover / advance_ratio。"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
import numpy as np
|
||||
@@ -12,7 +11,7 @@ from sqlalchemy import text
|
||||
|
||||
from app.core.db import ops as db_ops
|
||||
from app.core.db.orm import engine as pg_engine
|
||||
from app.core.sync.base import SyncTask
|
||||
from app.core.sync.base import SyncTask, _effective_sync_end
|
||||
from app.core.utils.logging import get_logger
|
||||
|
||||
logger = get_logger("sync.market_regime")
|
||||
@@ -34,7 +33,16 @@ class SyncMarketRegime(SyncTask):
|
||||
dataset_id = "market_regime"
|
||||
|
||||
def _run(self, *, trigger_source: str = "manual", start: str = DEFAULT_START, **kwargs) -> dict[str, Any]:
|
||||
end = datetime.now().strftime("%Y-%m-%d")
|
||||
# 2026-07-12 修复: end 用 kline_stock 最大 trade_date,而不是 datetime.now()
|
||||
# 避免节假日/周末跑时把非交易日当 end,导致 SQL 条件不精确
|
||||
try:
|
||||
with pg_engine.connect() as conn:
|
||||
max_date = conn.execute(
|
||||
text("SELECT MAX(trade_date) FROM market_data.kline_stock")
|
||||
).scalar()
|
||||
end = str(max_date) if max_date else _effective_sync_end()
|
||||
except Exception:
|
||||
end = _effective_sync_end()
|
||||
self._progress(message=f"构建 market_regime {start} ~ {end}...")
|
||||
|
||||
# 1. 取所有非退市股票代码
|
||||
|
||||
@@ -1,183 +0,0 @@
|
||||
"""同步任务:行业聚合特征(衍生计算)。
|
||||
|
||||
输入:kline_stock(日 K 线)+ industry(股票→行业映射)
|
||||
输出:
|
||||
- sector_indices :行业日线(trade_date × sector_name × close × sector_amplitude)
|
||||
- sector_features_daily :行业特征(+ sector_ret + ema10/20/200 + score)
|
||||
|
||||
算法(参考 dashboard/api/services/sync/sector.py 重写,持久层改为 PG):
|
||||
1) 每只股票日涨跌幅 pct_chg = (close/prev_close - 1) * 100
|
||||
2) 按行业聚合 sector_ret = 该行业所有股票当日 pct_chg 的均值
|
||||
3) 行业指数 close = 100 × ∏(1 + sector_ret/100) (基点 100,复合收益)
|
||||
4) 行业振幅 sector_amplitude = 该行业所有股票当日 (high-low)/close 的均值
|
||||
5) EMA10/20/200 = close 的指数移动平均(adjust=False)
|
||||
6) score: 0/1/2 — close>ema200 + 1,ema10>ema20 + 1
|
||||
|
||||
无外部 API 调用,纯本地计算;增量逻辑以"全量重算"实现(数据量小 ~75K 行)。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
import pandas as pd
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
from app.core.config import settings
|
||||
from app.core.db import ops as db_ops
|
||||
from app.core.sync.base import SyncTask
|
||||
from app.core.utils.logging import get_logger
|
||||
|
||||
logger = get_logger("sync.sector_features")
|
||||
|
||||
|
||||
# 起算点:DB 里 kline_stock 最早一天 往前 5 年,或固定一个默认起点
|
||||
# (不用写死 2021-04-24,让数据自己决定;下面自动算)
|
||||
DEFAULT_START_YEARS_BACK = 5
|
||||
|
||||
# 输出基点(行业指数 close 从多少开始)
|
||||
INDEX_BASE = 100.0
|
||||
|
||||
|
||||
class SyncSectorFeatures(SyncTask):
|
||||
dataset_id = "sector_features"
|
||||
|
||||
def _run(
|
||||
self,
|
||||
*,
|
||||
trigger_source: str = "manual",
|
||||
codes: list[str] | None = None,
|
||||
max_workers: int = 1, # 本任务纯本地计算,单线程足够
|
||||
**kwargs,
|
||||
) -> dict[str, Any]:
|
||||
t0 = time.time()
|
||||
logger.info("[sector] 启动行业聚合特征计算")
|
||||
|
||||
# ── 1) 拉 kline_stock(日线)只取需要的列 ──
|
||||
# 走 SQLAlchemy engine + PG URL(pd.read_sql 一次性读 1.1kw 行 → pandas DataFrame)
|
||||
engine = create_engine(settings.pg_sqlalchemy_url())
|
||||
df_kline = pd.read_sql(
|
||||
'SELECT stock_code, trade_date, open, high, low, "close", volume '
|
||||
'FROM market_data.kline_stock ORDER BY stock_code, trade_date',
|
||||
engine,
|
||||
)
|
||||
if df_kline.empty:
|
||||
return {"status": "error", "message": "kline_stock 为空"}
|
||||
df_kline["stock_code"] = df_kline["stock_code"].astype(str).str.zfill(6)
|
||||
df_kline["trade_date"] = pd.to_datetime(df_kline["trade_date"], errors="coerce")
|
||||
logger.info(f"[sector] kline_stock: {len(df_kline):,} 行, {df_kline['stock_code'].nunique()} 只, "
|
||||
f"{df_kline['trade_date'].min().date()} ~ {df_kline['trade_date'].max().date()}")
|
||||
|
||||
# ── 2) 拉 industry(股票→行业映射)──
|
||||
df_ind = pd.read_sql(
|
||||
"SELECT code, industry_name FROM market_data.industry WHERE industry_name IS NOT NULL",
|
||||
engine,
|
||||
)
|
||||
if df_ind.empty:
|
||||
return {"status": "error", "message": "industry 表为空"}
|
||||
df_ind["code"] = df_ind["code"].astype(str).str.zfill(6)
|
||||
df_ind = df_ind.drop_duplicates(subset=["code"], keep="first")
|
||||
logger.info(f"[sector] industry 映射: {len(df_ind):,} 行, {df_ind['industry_name'].nunique()} 个行业")
|
||||
|
||||
# ── 3) 算每只股票日涨跌幅 + 振幅 ──
|
||||
df = df_kline.merge(df_ind, left_on="stock_code", right_on="code", how="inner")
|
||||
df = df.sort_values(["stock_code", "trade_date"])
|
||||
df["prev_close"] = df.groupby("stock_code")["close"].shift(1)
|
||||
df["stock_amplitude"] = (df["high"] - df["low"]) / df["close"]
|
||||
df = df.dropna(subset=["prev_close", "industry_name"])
|
||||
df["pct_chg"] = (df["close"] / df["prev_close"] - 1.0) * 100.0
|
||||
logger.info(f"[sector] 合并后: {len(df):,} 行, 行业 {df['industry_name'].nunique()} 个")
|
||||
|
||||
# ── 4) 按行业 + 日期聚合 ──
|
||||
sector_daily = (
|
||||
df.groupby(["industry_name", "trade_date"], as_index=False).agg(
|
||||
sector_ret=("pct_chg", "mean"),
|
||||
sector_amplitude=("stock_amplitude", "mean"),
|
||||
)
|
||||
.rename(columns={"industry_name": "sector_name"})
|
||||
.sort_values(["sector_name", "trade_date"])
|
||||
.reset_index(drop=True)
|
||||
)
|
||||
logger.info(f"[sector] 行业日聚合: {len(sector_daily):,} 行")
|
||||
|
||||
# ── 5) 算行业指数 close(基点 100,复合)──
|
||||
sector_index = self._build_index(sector_daily)
|
||||
|
||||
# ── 6) EMA + score ──
|
||||
sector_index = self._calc_ema(sector_index)
|
||||
sector_index["score"] = sector_index.apply(
|
||||
lambda r: self._calc_score(r["close"], r["ema10"], r["ema20"], r["ema200"]),
|
||||
axis=1,
|
||||
)
|
||||
|
||||
# 整理列名
|
||||
out_cols = ["trade_date", "sector_name", "sector_ret", "sector_amplitude",
|
||||
"close", "ema10", "ema20", "ema200", "score"]
|
||||
sector_index = sector_index[out_cols]
|
||||
sector_index["trade_date"] = sector_index["trade_date"].dt.strftime("%Y-%m-%d")
|
||||
# NaN → None(让 MySQL 接受 NULL)
|
||||
sector_index = sector_index.where(pd.notnull(sector_index), None)
|
||||
|
||||
# ── 7) 写入两张表 ──
|
||||
# 7a. sector_indices (4 列)
|
||||
si_rows = sector_index[["trade_date", "sector_name", "close", "sector_amplitude"]].to_dict("records")
|
||||
db_ops.replace_all_sector_indices(si_rows)
|
||||
# 7b. sector_features_daily (9 列)
|
||||
sf_rows = sector_index.to_dict("records")
|
||||
db_ops.replace_all_sector_features(sf_rows)
|
||||
|
||||
elapsed = round(time.time() - t0, 1)
|
||||
msg = (
|
||||
f"行业聚合 {sector_index['sector_name'].nunique()} 个行业 "
|
||||
f"× {sector_index['trade_date'].nunique()} 天, "
|
||||
f"共 {len(sector_index):,} 行, {elapsed}s"
|
||||
)
|
||||
logger.info(f"[sector] {msg}")
|
||||
return {
|
||||
"status": "ok",
|
||||
"message": msg,
|
||||
"sectors": int(sector_index["sector_name"].nunique()),
|
||||
"days": int(sector_index["trade_date"].nunique()),
|
||||
"rows": int(len(sector_index)),
|
||||
"elapsed_sec": elapsed,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _build_index(sector_daily: pd.DataFrame) -> pd.DataFrame:
|
||||
"""行业指数 close = INDEX_BASE × ∏(1 + sector_ret/100)"""
|
||||
out = []
|
||||
for sector_name, group in sector_daily.groupby("sector_name", sort=False):
|
||||
g = group.sort_values("trade_date").copy()
|
||||
close_vals = []
|
||||
cur_base = INDEX_BASE
|
||||
for ret in g["sector_ret"].to_numpy(dtype=float):
|
||||
if pd.isna(ret):
|
||||
close_vals.append(cur_base)
|
||||
else:
|
||||
cur_base = cur_base * (1 + float(ret) / 100.0)
|
||||
close_vals.append(cur_base)
|
||||
g["close"] = close_vals
|
||||
out.append(g)
|
||||
return pd.concat(out, ignore_index=True)
|
||||
|
||||
@staticmethod
|
||||
def _calc_ema(sector_index: pd.DataFrame) -> pd.DataFrame:
|
||||
"""每个行业分别算 EMA10/20/200"""
|
||||
out = []
|
||||
for sector_name, group in sector_index.groupby("sector_name", sort=False):
|
||||
g = group.sort_values("trade_date").copy()
|
||||
g["ema10"] = g["close"].ewm(span=10, adjust=False).mean()
|
||||
g["ema20"] = g["close"].ewm(span=20, adjust=False).mean()
|
||||
g["ema200"] = g["close"].ewm(span=200, adjust=False).mean()
|
||||
out.append(g)
|
||||
return pd.concat(out, ignore_index=True)
|
||||
|
||||
@staticmethod
|
||||
def _calc_score(close, ema10, ema20, ema200) -> int:
|
||||
score = 0
|
||||
if pd.notna(close) and pd.notna(ema200) and close > ema200:
|
||||
score += 1
|
||||
if pd.notna(ema10) and pd.notna(ema20) and ema10 > ema20:
|
||||
score += 1
|
||||
return int(score)
|
||||
@@ -91,16 +91,25 @@ class SyncShareSnapshot(SyncTask):
|
||||
# 与 stocks.code 保持一致,方便 JOIN。
|
||||
# 历史 bug:之前 share 表写的是裸 6 位 c6,导致 share JOIN stocks 失败。
|
||||
# 一次性 UPDATE(2026-07-01)已把存量 6.3 万行加上前缀。
|
||||
# trade_date 必须用源数据返回的交易日(数据源层负责"最近已完成交易日"
|
||||
# 兜底),不允许用 today 兜底 — 7月9日 早盘跑任务时 today=7月9日,
|
||||
# 但股本快照实际对应 7月8日 收盘。早期代码用 today 会让 share 表里
|
||||
# 同一股本被重复写多行,统计/回溯出错。
|
||||
trade_date = r.get("trade_date")
|
||||
if not trade_date:
|
||||
fail_cnt += 1
|
||||
logger.warning(f"[share {c6}] 源数据未返回 trade_date,跳过(避免用运行日兜底)")
|
||||
continue
|
||||
hermes = self._to_hermes(c6)
|
||||
db_ops.update_stock_share_snapshot(
|
||||
code=hermes,
|
||||
total_share=r["total_share"],
|
||||
float_share=r["float_share"],
|
||||
trade_date=r.get("trade_date", today),
|
||||
trade_date=trade_date,
|
||||
)
|
||||
rows_to_share_table.append({
|
||||
"stock_code": hermes,
|
||||
"trade_date": r.get("trade_date", today),
|
||||
"trade_date": trade_date,
|
||||
"total_share": r["total_share"],
|
||||
"float_share": r["float_share"],
|
||||
})
|
||||
|
||||
@@ -104,7 +104,6 @@ class SyncStocksBasic(SyncTask):
|
||||
exchange=old.get("exchange", ""),
|
||||
list_date=old.get("list_date", ""),
|
||||
listing_status="delisted",
|
||||
industry=old.get("industry", ""),
|
||||
)
|
||||
delisted += 1
|
||||
|
||||
@@ -197,11 +196,19 @@ class SyncStocksBasic(SyncTask):
|
||||
if r is None:
|
||||
fail += 1
|
||||
else:
|
||||
# trade_date 必须是源数据返回的交易日(数据源层用 effective_market_date
|
||||
# 兜底为"最近已完成交易日"),绝不允许用 today 兜底 — 7月9日 早盘跑
|
||||
# 时 today=7月9日,但股本快照实际对应 7月8日 收盘。
|
||||
trade_date = r.get("trade_date")
|
||||
if not trade_date:
|
||||
fail += 1
|
||||
logger.warning(f"[share {c6}] 源数据未返回 trade_date,跳过(避免用运行日兜底)")
|
||||
continue
|
||||
db_ops.update_stock_share_snapshot(
|
||||
code=self._to_hermes(c6),
|
||||
total_share=r["total_share"],
|
||||
float_share=r["float_share"],
|
||||
trade_date=r.get("trade_date", today),
|
||||
trade_date=trade_date,
|
||||
)
|
||||
ok += 1
|
||||
if done % 100 == 0:
|
||||
|
||||
@@ -12,12 +12,15 @@
|
||||
trade_date 可能比 wall-clock 早一天;重跑靠 PK + ON CONFLICT 幂等
|
||||
4) 单只股票日均 5w-10w tick,CHUNK=2000 upsert 防 driver 撑爆
|
||||
5) 数据是"当天 only",无 start/end 窗口;不存增量概念
|
||||
6) 2026-07-13 加固: 加上外层 FETCH_HARD_TIMEOUT,避免 mairui 挂起时
|
||||
as_completed 无 timeout 导致整个 task 永久卡住(systemd 2h 超时杀)。
|
||||
改用手动 pool 管理 + shutdown(wait=False),与 kline_daily 对齐。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed, TimeoutError as FuturesTimeoutError
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
@@ -36,6 +39,10 @@ MAIRUI_TICK_TRADE_PUBLISH_HOUR = 21
|
||||
MAIRUI_TICK_TRADE_PUBLISH_MIN = 0
|
||||
# 21:00 之后再跑(留 5 分钟缓冲,等 mairui 完整入库)
|
||||
TICK_TRADE_RUN_GATE = (MAIRUI_TICK_TRADE_PUBLISH_HOUR, MAIRUI_TICK_TRADE_PUBLISH_MIN + 5)
|
||||
# 外层超时: mairui _fetch 有 20s timeout,但 as_completed 无 timeout 的话
|
||||
# 若所有 worker 同时被 mairui 挂起(罕见),task 会永久卡住等 systemd 2h 杀。
|
||||
# 120s 内无任何 future 完成 → 判全部失败退出。
|
||||
FETCH_HARD_TIMEOUT = 120.0
|
||||
|
||||
|
||||
def _passes_publish_gate(now: Optional[datetime] = None, *, force: bool = False) -> tuple[bool, str]:
|
||||
@@ -108,15 +115,18 @@ class SyncTickTrade(SyncTask):
|
||||
)
|
||||
|
||||
# ── 执行 ──
|
||||
# 不用 `with ThreadPoolExecutor` — 与外层 as_completed timeout 配合,
|
||||
# 超时后 `with` 的 shutdown(wait=True) 会阻塞等卡死线程 → 进程挂死(同 kline_daily 教训)。
|
||||
t0 = time.time()
|
||||
ok_cnt = fail_cnt = 0
|
||||
rows_total = 0
|
||||
with ThreadPoolExecutor(max_workers=max_workers) as pool:
|
||||
futures = {pool.submit(self._sync_one, mr, c6): c6 for c6 in stock_codes}
|
||||
for i, future in enumerate(as_completed(futures), 1):
|
||||
pool = ThreadPoolExecutor(max_workers=max_workers)
|
||||
futures = {pool.submit(self._sync_one, mr, c6): c6 for c6 in stock_codes}
|
||||
try:
|
||||
for i, future in enumerate(as_completed(futures, timeout=FETCH_HARD_TIMEOUT), 1):
|
||||
c6 = futures[future]
|
||||
try:
|
||||
res = future.result()
|
||||
res = future.result(timeout=0.1)
|
||||
if res["status"] == "ok":
|
||||
ok_cnt += 1
|
||||
rows_total += res["rows"]
|
||||
@@ -124,6 +134,9 @@ class SyncTickTrade(SyncTask):
|
||||
fail_cnt += 1
|
||||
if res.get("error"):
|
||||
logger.warning(f"[tick_trade {c6}] {res['error']}")
|
||||
except FuturesTimeoutError:
|
||||
fail_cnt += 1
|
||||
logger.warning(f"[tick_trade {c6}] 内部 race timeout, 记 fail")
|
||||
except Exception as e:
|
||||
fail_cnt += 1
|
||||
logger.warning(f"[tick_trade {c6}] {e}")
|
||||
@@ -132,6 +145,25 @@ class SyncTickTrade(SyncTask):
|
||||
message=f"tick_trade 进度 {i}/{total} OK:{ok_cnt} FAIL:{fail_cnt} 行:{rows_total}",
|
||||
current=i, total=total, current_step=c6,
|
||||
)
|
||||
except FuturesTimeoutError:
|
||||
stuck = [(f, c6) for f, c6 in futures.items() if not f.done()]
|
||||
logger.error(
|
||||
"[tick_trade] 所有 fetcher 卡死 (>%ss), %d 只股票未完成, 标 fail 后退出",
|
||||
FETCH_HARD_TIMEOUT, len(stuck),
|
||||
)
|
||||
fail_cnt += len(stuck)
|
||||
for f, c6 in stuck:
|
||||
try:
|
||||
if hasattr(f, "cancel"):
|
||||
f.cancel()
|
||||
except Exception as e:
|
||||
logger.warning(f"[tick_trade] cancel future for {c6} 失败: {e}")
|
||||
try:
|
||||
pool.shutdown(wait=False)
|
||||
except Exception as e:
|
||||
logger.warning(f"[tick_trade] pool.shutdown(wait=False) 失败: {e}")
|
||||
else:
|
||||
pool.shutdown(wait=True)
|
||||
elapsed = round(time.time() - t0, 1)
|
||||
msg = f"逐笔 {ok_cnt}成 {fail_cnt}败 共{rows_total}行, {elapsed}s"
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user