376 lines
20 KiB
Python
376 lines
20 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
组合快照 (账本 + 行情 + 行业 → 方案生成器/规则闸/页面的统一输入)
|
||
================================================================
|
||
一处组装, 三处复用: 命令方案生成、组合约束校验、管理页面「持仓与账本」。
|
||
|
||
安全垫连续为负天数 (清弱票判定所需) 的存放:
|
||
DDL 未设该列, 故以单行运行参数 PMS_CUSHION_NEG_STREAK (JSON 映射) 承载,
|
||
由日终结算 (ledger_service.daily_settle) 维护 —— 不改表结构, 数据可查可重算。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import logging
|
||
import math
|
||
|
||
from app.core import cushion as cu
|
||
from app.core import strategy_book as books
|
||
from app.core import ws_codec as wsc
|
||
from app.repo import pms_repo, qmt_repo
|
||
from app.services import industry, market, param_store
|
||
|
||
logger = logging.getLogger("pms.portfolio")
|
||
|
||
NEG_STREAK_KEY = "PMS_CUSHION_NEG_STREAK"
|
||
CASH_WS, CASH_EST = "ws", "estimate"
|
||
|
||
|
||
def position_groups(original_view: dict) -> list:
|
||
"""页面按策略展示;保留主观量化快照和交易计算的默认范围。"""
|
||
from config.settings import settings
|
||
|
||
def group(book, name, view, *, ready=True):
|
||
rows = [{**r, "book_id": book} for r in view.get("positions", [])]
|
||
held = [r for r in rows if r["total_qty"] > 0]
|
||
totals = view.get("totals", {})
|
||
missing = view.get("price_missing", [])
|
||
from app.core.holding_performance import calculate
|
||
failures = []
|
||
# 收益展示直接读本策略批次,避免负成本在交易安全垫中截断后丢失利润。
|
||
# 账本、avg_cost 与策略使用的 cushion 字段均不在这里改写。
|
||
for row in held:
|
||
try:
|
||
with books.scope(book):
|
||
lots = pms_repo.list_lots(row['ts_code'], status=None, limit=2001)
|
||
if len(lots) >= 2001:
|
||
raise ValueError('批次超过完整核验上限')
|
||
row['performance'] = calculate(row, lots)
|
||
except Exception:
|
||
logger.exception('持仓收益批次读取失败 %s %s', book, row['ts_code'])
|
||
row['performance'] = calculate(row, [])
|
||
if not row['performance']['ready']:
|
||
failures.append(row['ts_code'])
|
||
performance_ready = ready and not missing and not failures
|
||
cost = sum(row['performance']['net_cost'] for row in held) if performance_ready else None
|
||
profit = sum(row['performance']['profit'] for row in held) if performance_ready else None
|
||
return {"book_id": book, "name": name, "positions": rows, "held": held,
|
||
"ready": ready, "price_missing": missing,
|
||
"performance_ready": performance_ready, "performance_missing": failures,
|
||
"totals": {"names_count": len(held),
|
||
"market_value": totals.get("portfolio_mv", 0) if ready else None,
|
||
"cost_basis": round(cost, 2) if cost is not None else None,
|
||
"pnl_pct": profit / cost if performance_ready and cost > 0 else None,
|
||
"pnl": round(profit, 2) if performance_ready else None}}
|
||
|
||
result = [group(books.ORIGINAL, "主观量化", original_view)]
|
||
if not settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||
result.append(group(books.MICRO, "微盘轮动", {}))
|
||
return result
|
||
try:
|
||
with books.scope(books.MICRO):
|
||
micro = positions_view()
|
||
result.append(group(books.MICRO, "微盘轮动", micro))
|
||
except Exception:
|
||
logger.exception("微盘持仓读取失败")
|
||
result.append(group(books.MICRO, "微盘轮动", {}, ready=False))
|
||
return result
|
||
|
||
|
||
def holding_performance(groups: list) -> dict:
|
||
"""合并各策略当前持仓,收益率按合计摊薄成本加权,不相加或平均百分比。"""
|
||
rows = []
|
||
for group in groups:
|
||
totals = group["totals"]
|
||
ready = group["ready"] and not group["price_missing"] and group.get('performance_ready', True)
|
||
rows.append({"book_id": group["book_id"], "name": group["name"], "ready": ready,
|
||
"names_count": totals["names_count"],
|
||
"profit": totals["pnl"] if ready else None,
|
||
"profit_pct": totals["pnl_pct"] if ready else None,
|
||
"cost_basis": totals["cost_basis"] if ready else None})
|
||
ready = bool(rows) and all(row["ready"] for row in rows)
|
||
profit = sum(row["profit"] for row in rows) if ready else None
|
||
cost = sum(row["cost_basis"] for row in rows) if ready else None
|
||
return {"ready": ready, "profit": round(profit, 2) if ready else None,
|
||
"profit_pct": profit / cost if ready and cost > 0 else None,
|
||
"strategies": rows, "reason": None if ready else "部分策略持仓估值待补齐"}
|
||
|
||
|
||
def position_lots(ts_code: str, book_id: str, status: str | None) -> list:
|
||
"""同名股票按策略查看批次;旧表未迁移时微盘必须为空。"""
|
||
from config.settings import settings
|
||
if book_id not in (books.ORIGINAL, books.MICRO):
|
||
raise ValueError("未知策略")
|
||
if book_id == books.MICRO and not settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||
return []
|
||
with books.scope(book_id):
|
||
return pms_repo.list_lots(ts_code, status=status)
|
||
|
||
|
||
def cash_view() -> dict:
|
||
"""真实可用资金 (ws 资金快照 §5.7 kind=funds)。取不到则回落估算并**标明是估的**。
|
||
|
||
为什么必须有这一层
|
||
------------------
|
||
原来全系统只有一个 `cash_est = PMS_TOTAL_SCALE − 持仓市值` —— 那是**从参数算出来的虚数**,
|
||
与账户里真有多少钱毫无关系。`PMS_TOTAL_SCALE` 是用户命令参数 (「我打算投多少」),
|
||
`available_cash` 是账户事实 (「现在有多少钱」), 两者是不同的约束, 不该混:
|
||
* scale 管**仓位纪律** —— 单股不超几成、总仓不超几成、最多几只
|
||
* 可用资金管**买不买得起** —— 这一单的钱够不够
|
||
07-30 实测: scale=200 万, 而模拟账户 total_asset 只有 98.1 万、available_cash 97.1 万。
|
||
少了第二个约束, 规划器会按 200 万排出账户根本执行不了的方案, 规则闸一路放行, 直到 QMT
|
||
回 `INSUFFICIENT_CASH` 才被拒 —— 而 PMS 收到 reject 不自动重发, 择时下一跳又算又发又拒,
|
||
页面看着一切正常, 实际一单也下不去。
|
||
|
||
available_cash 已包含可用的卖出回笼。sell_return_today 是回笼统计,不能重复相加。
|
||
2026-10-26 真机核对:可用 1,806,071.62 加账户市值 130,190,正好等于总资产
|
||
1,936,261.62;再加回笼统计会虚增可用资金 1,644,823.13 元。
|
||
账户市值和冻结资金随同一快照返回,页面不能把它们与独立行情估值混成一组账户余额。
|
||
"""
|
||
max_age = param_store.get_int("PMS_RECON_WS_SNAPSHOT_MAX_AGE_SEC", 900)
|
||
out = {"source": CASH_EST, "cash_avail": None, "total_asset": None,
|
||
"account_market_value": None, "frozen_cash": None,
|
||
"age_sec": None, "why": ""}
|
||
try:
|
||
snap = qmt_repo.latest_snapshot("funds")
|
||
except Exception as e:
|
||
# 异常细节只进日志。这个字段有三个下游都直接显示给人看
|
||
# (建仓跳过原因、日报、待办清单),不能把异常原文送到人眼前。
|
||
logger.warning("读交易通道的资金快照失败: %s: %s", type(e).__name__, e)
|
||
out["why"] = "查询账户资金时程序出错,详细原因已记进运行日志"
|
||
return out
|
||
if not snap:
|
||
out["why"] = "交易通道从没送回过资金数据,通道可能没启动"
|
||
return out
|
||
age = snap.get("age_sec")
|
||
if age is not None and age > max_age:
|
||
out.update({"why": f"资金数据停在 {age / 60:.0f} 分钟前,"
|
||
f"超过了 {max_age / 60:.0f} 分钟的有效期", "age_sec": age})
|
||
return out
|
||
f = wsc.parse_funds_snapshot(snap["payload"])
|
||
avail = f.get("available_cash")
|
||
if avail is None:
|
||
out["why"] = "拿到了资金数据,但里面找不到可用余额这一项"
|
||
return out
|
||
out.update({"source": CASH_WS, "age_sec": age, "total_asset": f.get("total_asset"),
|
||
"cash_avail": round(float(avail), 2),
|
||
"account_market_value": f.get("market_value"), "frozen_cash": f.get("frozen_cash")})
|
||
return out
|
||
|
||
|
||
def neg_streak_map() -> dict:
|
||
try:
|
||
raw = pms_repo.get_param(NEG_STREAK_KEY)
|
||
return json.loads(raw) if raw else {}
|
||
except Exception:
|
||
return {}
|
||
|
||
|
||
def save_neg_streak(m: dict) -> dict:
|
||
"""写「安全垫连续为负天数」。**必须返回成败, 调用方必须接。**
|
||
|
||
这个计数是 `plan_reduce_exposure` 一档「清弱票」的唯一判据 (连负 N 日 → 优先清)。
|
||
原来这里把异常吞成 warning 就返回 None, 上层 `_settle_cushion` 照样报
|
||
{"updated": n, ...}、`daily_settle` 照样 ok=True —— 计数永远停在旧值, 降仓命令的
|
||
第一档静默失灵, 改成卖别的票, 而没有任何一处报错。(2026-07-31 静默失败专项)
|
||
"""
|
||
try:
|
||
pms_repo.set_param(NEG_STREAK_KEY, json.dumps(m, ensure_ascii=False), "system")
|
||
return {"ok": True}
|
||
except Exception as e:
|
||
logger.error("安全垫连负天数写入失败: %s —— 「清弱票」判据将停在旧值", e)
|
||
return {"ok": False, "error": f"{type(e).__name__}: {e}"}
|
||
|
||
|
||
def positions_view(*, with_price: bool = True) -> dict:
|
||
"""账本持仓 + 现价/安全垫/行业/占比。取不到现价的票用摊薄成本兜底并列入 price_missing。"""
|
||
p = param_store.sizing_params()
|
||
rows = pms_repo.list_positions()
|
||
codes = [r["ts_code"] for r in rows]
|
||
prices = market.get_prices(codes) if (with_price and codes) else {}
|
||
sectors = industry.get_many(codes) if codes else {}
|
||
streak = neg_streak_map()
|
||
scale = float(p["scale"] or 0)
|
||
|
||
out, missing = [], []
|
||
for r in rows:
|
||
code = r["ts_code"]
|
||
avg_cost = float(r.get("avg_cost") or 0)
|
||
px = prices.get(code)
|
||
# 取不到现价时拿摊薄成本顶上, 是为了市值/占比还能算 (否则整张页面塌掉)。
|
||
# **但安全垫绝不能跟着算出来**: px := avg_cost 会让 cushion 恰好等于 0.0, 而 0.0
|
||
# 与"真的不赚不亏"完全同形。后果不是显示难看, 是**凭空产生卖单** —— 一只安全垫
|
||
# 峰值 8% 的停牌票会被判成"回吐至 0%(过半)"而触发 TRIM 保垫减仓, 而保垫减仓
|
||
# **不设确认门槛、直接落指令**; 反过来浮亏 −50% 的票补仓评估档一次都不会触发。
|
||
# 停牌票在行情库里永远没有当日分钟线, 所以这是常规路径不是罕见路径。
|
||
# 价格可以估, 安全垫必须留 None ——「拿不到 ≠ 是 0」。
|
||
price_ok = bool(px and px > 0)
|
||
if not price_ok:
|
||
px = avg_cost or 0.0
|
||
if int(r.get("total_qty") or 0) > 0:
|
||
missing.append(code)
|
||
qty = int(r.get("total_qty") or 0)
|
||
mv = qty * px
|
||
cp = (px / avg_cost - 1.0) if (price_ok and avg_cost > 0) else None
|
||
out.append({
|
||
"ts_code": code, "status": r.get("status"), "frozen_reason": r.get("frozen_reason"),
|
||
# price_ok=False → 这一行的 price 是拿摊薄成本顶的, 不是行情。动作引擎据此整只
|
||
# 跳过并留痕 (见 action_engine.scan) —— 不是靠 cushion_pct 恰好为 None 兜住。
|
||
"price": round(px, 3), "price_ok": price_ok,
|
||
"total_qty": qty, "avail_qty": int(r.get("avail_qty") or 0),
|
||
"base_qty": int(r.get("base_qty") or 0), "fill_qty": int(r.get("fill_qty") or 0),
|
||
"add_qty": int(r.get("add_qty") or 0), "dca_qty": int(r.get("dca_qty") or 0),
|
||
"t0_qty": int(r.get("t0_qty") or 0), "avg_cost": round(avg_cost, 3) or None,
|
||
"market_value": round(mv, 2),
|
||
"cushion_pct": round(cp, 4) if cp is not None else None,
|
||
"cushion_state": cu.cushion_state(cp, p["cushion_solid"]),
|
||
"cushion_peak": float(r.get("cushion_peak") or 0),
|
||
"neg_cushion_days": int(streak.get(code, 0)),
|
||
"pct_of_scale": round(mv / scale, 4) if scale > 0 else None,
|
||
"target_pct": float(r.get("target_pct") or 0) or None,
|
||
"stop_ref": float(r.get("stop_ref") or 0) or None,
|
||
"support_ref": float(r.get("support_ref") or 0) or None,
|
||
"pressure_ref": float(r.get("pressure_ref") or 0) or None,
|
||
"ref_source": r.get("ref_source"),
|
||
"t0_enabled": int(r.get("t0_enabled") or 0), "t0_ratio": r.get("t0_ratio"),
|
||
"realized_t_profit": float(r.get("realized_t_profit") or 0),
|
||
"sector": sectors.get(code),
|
||
})
|
||
|
||
held = [x for x in out if x["total_qty"] > 0]
|
||
port_mv = sum(x["market_value"] for x in held)
|
||
sector_names, sector_mv = {}, {}
|
||
for x in held:
|
||
s = x.get("sector")
|
||
if not s:
|
||
continue
|
||
sector_names[s] = sector_names.get(s, 0) + 1
|
||
sector_mv[s] = sector_mv.get(s, 0.0) + x["market_value"]
|
||
cost_sum = sum((x["avg_cost"] or 0) * x["total_qty"] for x in held)
|
||
cash = cash_view()
|
||
totals = {
|
||
"scale": scale, "portfolio_mv": round(port_mv, 2),
|
||
"portfolio_pct": round(port_mv / scale, 4) if scale > 0 else None,
|
||
# cash_est 是 scale−市值 的**估算**, 与账户里有多少钱无关;
|
||
# cash_avail 才是账户事实 (ws 资金快照)。两个都留着并标明来源, 免得后来者
|
||
# 又把估算值当真钱用 —— 见 cash_view 的说明。
|
||
"names_count": len(held), "cash_est": round(scale - port_mv, 2) if scale > 0 else None,
|
||
"cash_avail": cash["cash_avail"], "cash_source": cash["source"],
|
||
"cash_age_sec": cash["age_sec"], "cash_why": cash["why"],
|
||
"total_asset": cash["total_asset"],
|
||
"account_market_value": cash["account_market_value"], "frozen_cash": cash["frozen_cash"],
|
||
"float_pnl": round(port_mv - cost_sum, 2),
|
||
"float_pnl_pct": round(port_mv / cost_sum - 1, 4) if cost_sum > 0 else None,
|
||
"cost_basis": round(cost_sum, 2),
|
||
"sector_names": sector_names, "sector_mv": sector_mv,
|
||
"solid_names": len([x for x in held if x["cushion_state"] == "SOLID"]),
|
||
"neg_names": len([x for x in held if (x["cushion_pct"] or 0) < 0]),
|
||
}
|
||
return {"positions": out, "held": held, "totals": totals, "params": p,
|
||
"price_missing": missing, "sector_ready": industry.ready()}
|
||
|
||
|
||
def caps_ctx(view: dict, *, ts_code=None, is_new_name=False, sector=None) -> dict:
|
||
"""组装 check_all_caps / planner 所需的组合上下文 (加仓前快照)。"""
|
||
p, t = view["params"], view["totals"]
|
||
stock_mv = 0.0
|
||
if ts_code:
|
||
for x in view["positions"]:
|
||
if x["ts_code"] == ts_code:
|
||
stock_mv = x["market_value"]
|
||
sector = sector or x.get("sector")
|
||
is_new_name = x["total_qty"] <= 0
|
||
break
|
||
else:
|
||
is_new_name = True
|
||
ready = view.get("sector_ready", False)
|
||
return {
|
||
"scale": p["scale"], "portfolio_cap": p["portfolio_cap"], "stock_cap": p["stock_cap"],
|
||
"max_names": p["max_names"], "portfolio_mv": t["portfolio_mv"],
|
||
"names_count": t["names_count"], "stock_mv": stock_mv, "is_new_name": is_new_name,
|
||
"sector": sector if ready else None,
|
||
"sector_names": int(t["sector_names"].get(sector, 0)) if ready and sector else 0,
|
||
"sector_mv": float(t["sector_mv"].get(sector, 0.0)) if ready and sector else 0.0,
|
||
"sector_names_map": t["sector_names"] if ready else {},
|
||
"sector_mv_map": t["sector_mv"] if ready else {},
|
||
"sector_max_names": p["sector_max_names"], "sector_max_ratio": p["sector_max_ratio"],
|
||
"cash_reserve": p["cash_reserve"], "sector_source_ready": ready,
|
||
# 真实可用资金随 caps 一起进规则闸 (executor 已经在传 caps, 不必再改它的取数)。
|
||
# cash_source 一定要跟着传: 规则闸靠它区分「校验过了」和「拿不到数据只能降级」。
|
||
"cash_avail": t.get("cash_avail"), "cash_source": t.get("cash_source", "estimate"),
|
||
}
|
||
|
||
|
||
def account_performance(cash=None) -> dict:
|
||
"""使用账户净投入作为统一基线,券商总资产不依赖策略账本对齐。"""
|
||
from config.settings import settings
|
||
empty = {"ready": False, "profit": None, "profit_pct": None, "started_at": None,
|
||
"reason": "账户收益基线尚未建立"}
|
||
if not settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||
return empty
|
||
try:
|
||
from app.services import rotation_accounting
|
||
view = rotation_accounting.view()
|
||
empty["started_at"] = view.get("started_at")
|
||
metrics = view.get("metrics") or {}
|
||
capitals = [metrics.get(book, {}).get("net_capital") for book in (books.ORIGINAL, books.MICRO)]
|
||
capital = sum(capitals) if all(v is not None and math.isfinite(v) for v in capitals) else None
|
||
cash = cash or {}
|
||
asset, age = cash.get("total_asset"), cash.get("cash_age_sec")
|
||
difference = view.get("cash_difference")
|
||
reconciled = difference is not None and abs(difference) <= .02
|
||
if (empty["started_at"] and capital is not None and capital > 0 and cash.get("cash_source") == CASH_WS
|
||
and age is not None and age >= 0 and asset is not None and math.isfinite(asset) and asset >= 0):
|
||
profit = asset - capital
|
||
return {**empty, "ready": True, "profit": round(profit, 2), "profit_pct": profit / capital,
|
||
"source": "qmt", "reason": ("按券商总资产计算,不等于策略收益。" if reconciled else
|
||
"按券商总资产计算,策略账本仍待核对。")}
|
||
if not view.get("ready"):
|
||
empty["reason"] = view.get("reason") or "账户估值待补齐"
|
||
return empty
|
||
if difference is not None and not reconciled:
|
||
empty["reason"] = "账户现金与策略账本待核对"
|
||
return empty
|
||
metrics = view.get("metrics") or {}
|
||
profit = metrics.get("account", {}).get("profit")
|
||
if profit is None or not math.isfinite(profit):
|
||
return empty
|
||
return {"ready": True, "profit": round(profit, 2),
|
||
"profit_pct": profit / capital if capital is not None and capital > 0 else None,
|
||
"started_at": view.get("started_at"), "reason": None}
|
||
except Exception:
|
||
logger.exception("账户累计盈亏读取失败")
|
||
empty["reason"] = "账户累计盈亏暂不可用"
|
||
return empty
|
||
|
||
|
||
def overview() -> dict:
|
||
"""页面顶部仪表 (总览)。任何一环取不到都不该让页面开不了 —— 逐项 try。"""
|
||
try:
|
||
v = positions_view()
|
||
except Exception as e:
|
||
return {"ok": False, "error": f"{type(e).__name__}: {e}"}
|
||
t, p = v["totals"], v["params"]
|
||
brake_until = param_store.get_int("PMS_BRAKE_UNTIL", 0)
|
||
return {
|
||
"ok": True,
|
||
"scale": t["scale"], "portfolio_mv": t["portfolio_mv"], "portfolio_pct": t["portfolio_pct"],
|
||
"portfolio_cap": p["portfolio_cap"], "names_count": t["names_count"],
|
||
"max_names": p["max_names"], "cash_est": t["cash_est"],
|
||
# 真钱与来源一起给页面 —— 只显示 cash_est 的话, 那个数在 scale 与账户不一致时
|
||
# 会让人以为钱多得是 (07-30: cash_est 199 万 vs 真实可用 97 万)
|
||
"cash_avail": t["cash_avail"], "cash_source": t["cash_source"],
|
||
"total_asset": t["total_asset"], "cash_why": t["cash_why"],
|
||
"account_performance": account_performance(t),
|
||
"holding_performance": holding_performance(position_groups(v)),
|
||
"account_market_value": t["account_market_value"], "frozen_cash": t["frozen_cash"],
|
||
"float_pnl": t["float_pnl"],
|
||
"float_pnl_pct": t["float_pnl_pct"], "solid_names": t["solid_names"],
|
||
"neg_names": t["neg_names"], "autonomy": p["autonomy"],
|
||
"buy_halt": p["buy_halt"], "exec_halt": p["exec_halt"],
|
||
"brake_until": brake_until, "sector_ready": v["sector_ready"],
|
||
"price_missing": v["price_missing"],
|
||
"cap_room": round(p["portfolio_cap"] * t["scale"] - t["portfolio_mv"], 2)
|
||
if t["scale"] else None,
|
||
}
|