diff --git a/DEVLOG.md b/DEVLOG.md index 3bc1926..b521620 100644 --- a/DEVLOG.md +++ b/DEVLOG.md @@ -4,7 +4,7 @@ 09:25在155确认资金提示的ValueError实际为雷赛智能缺少盘前分钟行情,数量对账正常;09:30行情自然恢复后现金差0。修改只读估值为逐策略保留未知值,现金、数量继续独立核对,严格划拨和日结健康判断不放宽。成本均价固定显示三位,导出保留原始加权精度并按金额计算盈亏,补零股保护。用户确认48.296是含费成本,而原QMT成交及最新持仓均48.290、费用未知,故不改写成交或猜测手续费。自然执行依据刷新还暴露Decimal无法编码,复用既有JSON类型转换修复。 -独立复核还发现公示导出把负向手续费流水再次相减,误加为平仓收益;已在导出边界修正费用和退款方向,保留原账本符号。新增估值18、精度及费用11、执行依据编码5项隔离检查,页面与全量验证及实际发布结果见[本次接续](docs/策略估值与成本精度修复_2026-11-04.md)。本条为实施记录,部署结果另补,不能当作已上线回执。 +独立复核还发现公示导出把负向手续费流水再次相减,误加为平仓收益;已在导出边界修正费用和退款方向,保留原账本符号。导出金额采用十进制累计、半分四舍五入,与Excel公式一致;市值与成本分别到分再相减,缺行情按成本顶价时不制造虚假盈亏。新增估值18、精度及费用14、执行依据编码5项隔离检查,页面与全量验证及实际发布结果见[本次接续](docs/策略估值与成本精度修复_2026-11-04.md)。本条为实施记录,部署结果另补,不能当作已上线回执。 > 这份文件的用途只有一个:**让下一次交接不必重新查一遍**。 > 每个重要节点写一条,写完就补,不攒到最后。一条节点包含五样东西: diff --git a/app/services/publish_export.py b/app/services/publish_export.py index 51a69ed..b9c08c1 100644 --- a/app/services/publish_export.py +++ b/app/services/publish_export.py @@ -45,6 +45,7 @@ import logging import os from copy import copy from datetime import date, datetime +from decimal import Decimal, ROUND_HALF_UP from app.repo import downstream_repo, pms_repo from app.services import market, param_store @@ -59,6 +60,16 @@ TEMPLATE_PATH = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__f # 1 标题 · 2 起始时间 · 3 更新时间 · 4 持仓表头 · 5 持仓样式行 · 6 持仓小计 # 7 卖出表头 · 8 卖出样式行 · 9 卖出小计 · 10 存量合计行 · 11 底部指标行 _R_HPROTO, _R_HSUB, _R_CHEAD, _R_CPROTO, _R_CSUB, _R_SUM1, _R_SUM2 = 5, 6, 7, 8, 9, 10, 11 +_ZERO = Decimal('0') + + +def _decimal(value): + return value if isinstance(value, Decimal) else Decimal(str(value or 0)) + + +def _money(value): + """与表格 ROUND(..., 2) 一致,半分远离零舍入;不用于原始均价。""" + return _decimal(value).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP) # ---------------------------------------------------------------- 取数与口径 @@ -105,7 +116,7 @@ def _fee_map() -> dict: 读不到按空 —— 费用缺席只影响平仓行含费口径, 表尾会把未摊费用标出来, 不拦导出。""" try: - return {(int(r["ymd"]), r["ts_code"]): -float(r["fee"] or 0) + return {(int(r["ymd"]), r["ts_code"]): -_decimal(r["fee"]) for r in pms_repo.fee_sum_by_code()} except Exception as e: # noqa: BLE001 logger.warning("公示导出读费用失败 (平仓行按不含费出): %s", e) @@ -148,9 +159,9 @@ def _compute_snapshot() -> dict: for r in open_lots: code = r["ts_code"] qty = int(r.get("qty") or 0) - cost = float(r.get("open_price") or 0) + cost = _decimal(r.get("open_price")) od = _as_date(r.get("open_date")) or today - g = grp.setdefault(code, {"qty": 0, "amount": 0.0, "open_date": od}) + g = grp.setdefault(code, {"qty": 0, "amount": _ZERO, "open_date": od}) g["qty"] += qty g["amount"] += cost * qty g["open_date"] = min(g["open_date"], od) @@ -168,8 +179,8 @@ def _compute_snapshot() -> dict: holdings.append({ "account": account, "code": code.split(".")[0], "name": _display_name(names.get(code), code), - "amount": round(g["amount"], 2), "open_date": g["open_date"], - "upd_date": today, "structure": structure, "cost": cost, + "amount": float(_money(g["amount"])), "open_date": g["open_date"], + "upd_date": today, "structure": structure, "cost": float(cost), "qty": g["qty"], "price": float(px), "days": _days(g["open_date"], today), }) @@ -181,57 +192,58 @@ def _compute_snapshot() -> dict: cq = int(r.get("closed_qty") or 0) if cq <= 0: continue - cost = float(r.get("open_price") or 0) - settle = float(r.get("close_avg_price") or 0) + cost = _decimal(r.get("open_price")) + settle = _decimal(r.get("close_avg_price")) od = _as_date(r.get("open_date")) or today cd = _as_date(r.get("updated_at")) or today - g = cgrp.setdefault((code, cd), {"qty": 0, "amount": 0.0, "settle_amt": 0.0, - "pnl": 0.0, "open_date": od}) + g = cgrp.setdefault((code, cd), {"qty": 0, "amount": _ZERO, "settle_amt": _ZERO, + "pnl": _ZERO, "open_date": od}) g["qty"] += cq g["amount"] += cost * cq g["settle_amt"] += settle * cq - g["pnl"] += float(r.get("realized_pnl") or 0) + g["pnl"] += _decimal(r.get("realized_pnl")) g["open_date"] = min(g["open_date"], od) - closed, fee_alloc = [], 0.0 + closed, fee_alloc = [], _ZERO for (code, cd) in sorted(cgrp, key=lambda k: (k[1], k[0])): g = cgrp[(code, cd)] - fee = fees.get((int(cd.strftime("%Y%m%d")), code), 0.0) + fee = fees.get((int(cd.strftime("%Y%m%d")), code), _ZERO) fee_alloc += fee closed.append({ "account": account, "code": code.split(".")[0], "name": _display_name(names.get(code), code), - "amount": round(g["amount"], 2), "open_date": g["open_date"], + "amount": float(_money(g["amount"])), "open_date": g["open_date"], "close_date": cd, "structure": structure, - "cost": g["amount"] / g["qty"], "qty": g["qty"], - "settle": g["settle_amt"] / g["qty"], + "cost": float(g["amount"] / g["qty"]), "qty": g["qty"], + "settle": float(g["settle_amt"] / g["qty"]), "days": _days(g["open_date"], cd), - "pnl": round(g["pnl"] - fee, 2), + "pnl": float(_money(g["pnl"] - fee)), }) # 未摊入明细的费用 (买入侧 + 无代码归属的校准行): 表尾如实标注, 不悄悄吞掉 try: # 仓储返回有符号现金流,导出费用按成本口径反号;退款不可取绝对值。 - fee_total = -float(pms_repo.sum_fee_all()) + fee_total = -_decimal(pms_repo.sum_fee_all()) except Exception: # noqa: BLE001 fee_total = fee_alloc - fee_resid = round(fee_total - fee_alloc, 2) + fee_resid = float(_money(fee_total - fee_alloc)) - hold_cost = sum(h["amount"] for h in holdings) + hold_cost = sum((_decimal(h["amount"]) for h in holdings), _ZERO) # 金额先核算到分,均价只在页面/单元格格式上显示三位;不能先截均价再乘股数。 - hold_pnl = sum(round(h["price"] * h["qty"] - h["amount"], 2) for h in holdings) - realized = sum(c["pnl"] for c in closed) + hold_pnl = sum((_money(_decimal(h["price"]) * h["qty"]) - _decimal(h["amount"]) + for h in holdings), _ZERO) + realized = sum((_decimal(c["pnl"]) for c in closed), _ZERO) total_pnl = hold_pnl + realized - nav = 1.0 + total_pnl / scale - openable = scale + realized # 可开仓总金额 = 净值规模 + 已实现盈亏 (模板口径) + nav = 1.0 + float(total_pnl) / scale + openable = _decimal(scale) + realized # 可开仓总金额 = 净值规模 + 已实现盈亏 (模板口径) return { "today": today, "scale": scale, "holdings": holdings, "closed": closed, - "hold_cost": round(hold_cost, 2), "hold_pnl": round(hold_pnl, 2), - "closed_cost": round(sum(c["amount"] for c in closed), 2), - "realized": round(realized, 2), "total_pnl": round(total_pnl, 2), - "nav": round(nav, 4), "openable": round(openable, 2), - "open_room": round(openable - hold_cost, 2), - "pos_ratio": round(hold_cost / openable, 4) if openable > 0 else None, + "hold_cost": float(_money(hold_cost)), "hold_pnl": float(_money(hold_pnl)), + "closed_cost": float(_money(sum((_decimal(c["amount"]) for c in closed), _ZERO))), + "realized": float(_money(realized)), "total_pnl": float(_money(total_pnl)), + "nav": round(nav, 4), "openable": float(_money(openable)), + "open_room": float(_money(openable - hold_cost)), + "pos_ratio": round(float(hold_cost / openable), 4) if openable > 0 else None, "price_missing": sorted(price_missing), "fee_resid": fee_resid, "title": str(param_store.get("PMS_PUBLISH_TITLE") or "量化产品基本信息"), } @@ -296,7 +308,7 @@ def _entry_row(ws, row, seq, e, *, date2, px): _put(ws, row, 7, f"=ROUND(K{row}*L{row},2)") # S 总持股浮动盈亏: 持仓 = 市值减成本金额;平仓 = 含费实得,沿用账本数值 if e.get("pnl") is None: - _put(ws, row, 19, f"=ROUND(M{row}*L{row}-G{row},2)") + _put(ws, row, 19, f"=ROUND(ROUND(M{row}*L{row},2)-G{row},2)") else: _put(ws, row, 19, e["pnl"]) _put(ws, row, 20, f"=IF(G{row}=0,0,S{row}/G{row})") # T 持有收益率 diff --git a/app/web/static/rotation.html b/app/web/static/rotation.html index 581d0f8..9d2a88d 100644 --- a/app/web/static/rotation.html +++ b/app/web/static/rotation.html @@ -12,7 +12,7 @@
规则读取中。
目标数量读取中。
| 排名 | 代码 | 名称 | 总市值(亿元) | 收盘价 | 全日成交额(万元) | 资格 | 实际股数 | 名单变动 | 初始金额/日成交额 |
|---|
| 策略 | 账面现金 | 资产 | 净投入 | 盈亏 | 单位净值 | 最大回撤 | 累计费用 |
|---|
| 策略 | 股票 | 股数 | 账面成本 | 持仓权重 | 估值 |
|---|
历史研究收益不等于账户轮换收益。纸面结果单独记录,资金划拨不计为盈利。开盘窗口容量与实际成交偏差在执行后记录。
| 策略 | 账面现金 | 资产 | 净投入 | 盈亏 | 单位净值 | 最大回撤 | 已入账费用 |
|---|
| 策略 | 股票 | 股数 | 账面成本 | 持仓权重 | 估值 |
|---|
历史研究收益不等于账户轮换收益。纸面结果单独记录,资金划拨不计为盈利。费用仅包含已入账金额,未回报费用不视为零。开盘窗口容量与实际成交偏差在执行后记录。