From 0ea114fae786ed322c71024f32101b6633385ebf Mon Sep 17 00:00:00 2001 From: baobao Date: Mon, 16 Nov 2026 09:37:01 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=B2=E6=AD=A2=E7=BD=91=E6=A0=BC=E6=94=B9?= =?UTF-8?q?=E5=8F=82=E5=90=8E=E9=87=8D=E5=A4=8D=E5=A2=9E=E5=8A=A0=E5=90=8C?= =?UTF-8?q?=E4=BB=B7=E5=BA=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DEVLOG.md | 5 + app/core/grid_inventory.py | 37 ++ app/services/strategy_runner.py | 9 + .../trading_remediation_20261116.json | 460 ++++++++++++++++++ docs/交易计划与风险约束整改接续_2026-11-16.md | 12 +- docs/杰普特与四方交易链路核验_2026-11-16.md | 49 ++ scripts/run_tests.py | 2 +- scripts/test_grid_execution.py | 99 ++++ 8 files changed, 667 insertions(+), 6 deletions(-) create mode 100644 docs/evidence/trading_remediation_20261116.json create mode 100644 docs/杰普特与四方交易链路核验_2026-11-16.md diff --git a/DEVLOG.md b/DEVLOG.md index 78ee6e1..5c2f3e5 100644 --- a/DEVLOG.md +++ b/DEVLOG.md @@ -3132,3 +3132,8 @@ a44c994于10:53:29仅发布155的web,镜像pms:proposal-copy-a44c994。开发 ## 2026年11月16日 交易计划与风险约束整改规格 用户要求先形成可指导开发AI且有验收标准的整改方案,再逐步推进。基于产品372e350建立独立整改分支,保留主工作树的研究内容与未提交文件。新增规格,明确五个工作包、预算与库存约束、26项验收场景、155发布与回退边界,并登记开发入口与接续。当前尚未修改业务代码或部署;运行核验进行中,不用历史回执代替当前健康。 + + +## 2026年11月16日 网格库存跨版本约束与交易核验 + +09:26:38在155只读核实杰普特原提议确实将15695元试探金额抬到62780元,四方两笔31.37元网格库存与旧批次未占新版买入档相互吻合。保存脱敏事实和七个运行源码摘要;未更改账本或参数。完成首个可独立发布的网格修复:真实未平库存要求新的完整低价台阶,评估与落单前共同复核,部分卖出不释放整批约束,全部入账卖出后可恢复同价循环。开发机Python3.12专项62例、全量85组通过。试探预算与退出方案仍按规格继续,不能把网格修复当作全部整改完成。尚未部署。 diff --git a/app/core/grid_inventory.py b/app/core/grid_inventory.py index 58105dc..257063c 100644 --- a/app/core/grid_inventory.py +++ b/app/core/grid_inventory.py @@ -68,11 +68,48 @@ def project(strategy, lots, parents): raise ValueError('网格批次重复') filled[key] = {'qty': qty, 'price': float(cost), 'sell_target': price_tick(target, sell=True), 'entry_level': entry if progress.get('grid_params') == params else None, + 'original_entry_level': entry, 'buy_instruction_id': parent['instruction_id'], 'lot_id': lot['id']} invested += qty * cost return {'filled_levels': filled, 'invested': float(invested), 'inventory_source': 'booked_lots'} +def buy_blocker(filled, params, entry): + """未平库存必须等到更低一整档机会;参数变化和部分卖出不释放此约束。""" + ceiling = Decimal(str(price_tick(entry))) + costs = [] + for row in filled.values(): + qty = int(row.get('qty') or 0) + if qty < 0: + raise ValueError('网格库存数量异常,暂停新增买入') + if qty == 0: + continue + cost = Decimal(str(row.get('price'))) + if not cost.is_finite() or cost <= 0: + raise ValueError('网格实际买入成本不可核实,暂停新增买入') + costs.append(cost) + if not costs: + return '' + step = Decimal(str(params.get('step') or 0)) + pct = Decimal(str(params.get('step_pct') or 0)) + if not step.is_finite() or not pct.is_finite() or step < 0: + raise ValueError('网格档距无法核实,暂停新增买入') + lowest = min(costs) + if step > 0: + boundary = lowest - step + elif 0 < pct < 1: + boundary = lowest * (1 - pct) + else: + raise ValueError('网格档距无法核实,暂停新增买入') + boundary = boundary.quantize(Decimal('.01'), rounding=ROUND_FLOOR) + if boundary <= 0: + return '已有网格库存,当前档距下没有更低的有效买入价格' + if ceiling > boundary: + return (f'已有未卖出的网格仓,最低买入成本{lowest:.2f}元;' + f'新增买入须降至{boundary:.2f}元及以下,或等待原网格仓卖出') + return '' + + def sell_slice(filled, qty, *, price=None): """按账本顺序取连续的可卖批次;任一批未到价便停止,不跨过它核销后面的批次。""" remaining = int(qty) diff --git a/app/services/strategy_runner.py b/app/services/strategy_runner.py index 851f479..13d9d7b 100644 --- a/app/services/strategy_runner.py +++ b/app/services/strategy_runner.py @@ -470,6 +470,11 @@ def _eval_grid(st, pos, day, now, ctx): entry, exit_price = levels[k], levels[k+1] if any(v.get('entry_level') == entry and int(v.get('qty') or 0) > 0 for v in filled.values()): return None + from app.core.grid_inventory import buy_blocker + blocked = buy_blocker(filled, params, entry) + if blocked: + ctx.setdefault('notes', []).append(f"{st['ts_code']} {blocked}") + return None ceiling = price_tick(entry) need = per_lot * ceiling if _f(state.get('invested')) + need > _f(params.get('max_capital')) + 1e-6: @@ -522,6 +527,10 @@ def _validate_grid_decision(st, dec): raise ValueError('网格买入档位、相邻卖出线或价格上限无法核实') if any(v.get('entry_level') == entry for v in inventory['filled_levels'].values()): raise ValueError('该档已有实际库存,不能重复买入') + from app.core.grid_inventory import buy_blocker + blocked = buy_blocker(inventory['filled_levels'], st['params'], entry) + if blocked: + raise ValueError(blocked) if inventory['invested'] + qty * _f(dec['limit_price']) > _f(st['params'].get('max_capital')) + 1e-6: raise ValueError('网格买入超过剩余资金上限') diff --git a/docs/evidence/trading_remediation_20261116.json b/docs/evidence/trading_remediation_20261116.json new file mode 100644 index 0000000..b92df55 --- /dev/null +++ b/docs/evidence/trading_remediation_20261116.json @@ -0,0 +1,460 @@ +{ + "observed_at_beijing": "2026-11-16T09:26:38.763986", + "host": "factorevaluation", + "mode": "155模拟PMS", + "table_prefix": "", + "params": { + "PMS_DISPATCH_MODE": "ws", + "PMS_GLOBAL_BUY_HALT": false, + "PMS_GLOBAL_EXEC_HALT": false, + "PMS_AUTO_GRID_STEP_PCT": 0.04, + "PMS_RISK_EXIT_ENABLED": true, + "PMS_RISK_EXIT_START_AT": "2026-11-05T12:30:46.261348+08:00", + "PMS_TIMING_FUSION_ENABLED": true, + "PMS_TIMING_FUSION_START_AT": "2026-11-11T16:17:11.269641+08:00", + "PMS_SELF_DECIDE_TRIAL": true, + "PMS_ADVICE_TRIAL_CONF_MIN": 60 + }, + "source_checks": { + "app/services/advice_service.py": { + "worker_sha256": "41fdd47f3a4261018c344ae15ced621f7d116985f56cda7f16e5dbaad793ac5c", + "baseline_sha256": "41fdd47f3a4261018c344ae15ced621f7d116985f56cda7f16e5dbaad793ac5c", + "matched": true + }, + "app/services/proposal_service.py": { + "worker_sha256": "af7c51dfb061f3b587169bfb12cb0fd420f57e06e3a3d5ecb31c78b12a1d3adb", + "baseline_sha256": "af7c51dfb061f3b587169bfb12cb0fd420f57e06e3a3d5ecb31c78b12a1d3adb", + "matched": true + }, + "app/services/strategy_runner.py": { + "worker_sha256": "df01701a3cb0bfbb5134322a9d230a55c82f834c8a2c154438cc4d7228c8ad06", + "baseline_sha256": "df01701a3cb0bfbb5134322a9d230a55c82f834c8a2c154438cc4d7228c8ad06", + "matched": true + }, + "app/core/grid_inventory.py": { + "worker_sha256": "543aa146054e7db518b613351f8282c998b18960e59dd6b3bdcb5d4a33777111", + "baseline_sha256": "543aa146054e7db518b613351f8282c998b18960e59dd6b3bdcb5d4a33777111", + "matched": true + }, + "app/core/timing_fusion_contract.py": { + "worker_sha256": "09c306d489053d09ef838fc272dfff29ff95cd96c3d508ce4a2b35f3f9af2c62", + "baseline_sha256": "09c306d489053d09ef838fc272dfff29ff95cd96c3d508ce4a2b35f3f9af2c62", + "matched": true + }, + "app/services/risk_exit.py": { + "worker_sha256": "ea9d71b27f79baebe766e9b71aa19f49ebd78ffa5647f1f4a19b9ea47372e95c", + "baseline_sha256": "ea9d71b27f79baebe766e9b71aa19f49ebd78ffa5647f1f4a19b9ea47372e95c", + "matched": true + }, + "app/services/strategy_advisor.py": { + "worker_sha256": "434bbad07847cc3a86501048a808b0dc150c76bcf6cdf038ad11493b90833dcb", + "baseline_sha256": "434bbad07847cc3a86501048a808b0dc150c76bcf6cdf038ad11493b90833dcb", + "matched": true + } + }, + "raw_snapshot_sha256": "a0a8de941260af72ca827bfebc54cbcba322d13a254eb57802ebdd77b619d5d1", + "stocks": { + "688025.SH": { + "position": { + "total_qty": 0, + "avg_cost": null, + "opened_date": "2026-11-05" + }, + "lots": [ + { + "id": 1455, + "lot_type": "BASE", + "qty": 0, + "open_price": "313.760", + "open_date": "2026-11-05", + "instruction_id": "INS_20261105_688025SH_OPEN_001_67404b29b330", + "status": "CLOSED" + } + ], + "instructions": [ + { + "instruction_id": "RX_4730fa189bee7aa734e7_1", + "origin_type": "system", + "origin_id": "risk_exit", + "action": "EXIT", + "side": "sell", + "qty": 200, + "exec_qty": 200, + "exec_avg_price": null, + "limit_price": null, + "status": "CONFIRMED", + "created_at": "2026-11-06 09:37:03", + "progress": { + "leg": null, + "reason": "第二次独立清仓信号已确认,开始清仓", + "grid_entry": null, + "grid_exit": null, + "grid_params": null, + "risk_exit": { + "round_id": "4730fa189bee7aa734e7", + "events": [ + { + "at": "2026-11-06T09:36:29.308000+08:00", + "confidence": 0.95, + "content": "1e5feda187b18c3985aec46e30b3ce13c54f493373dc9af222396519521fb0f8", + "key": "952b734ad668655e713f80df388838f758e9c5d625a4a7084b1784dc660efd56", + "msg_id": "1793928723952-0", + "reason": "量比8.49倍远超2倍门槛放量破285支撑,主力净流出-9%确认机构出货,空头加速逻辑完全兑现", + "source": "intraday", + "source_id": "20261106_093629_688025.SH" + }, + { + "at": "2026-11-06T09:36:43.993000+08:00", + "confidence": 0.95, + "content": "26e47de7a52452ac8eb054a39b1dc73eb207197eb4999fc7f31eb3731245083d", + "key": "136be4cf3196982ad22d19246d0027e390cc8d00f5e90cf45a1fda77ddbab1c5", + "msg_id": "1793928738638-0", + "reason": "早盘破位285放量确认(量比8.49),午后反弹至298但实时主力净-9%、主散背离-18确认无主力支撑,板块short穿越日β恶劣,破位结构未修复,止损离场。", + "source": "intraday", + "source_id": "20261106_093643_688025.SH" + } + ], + "review_exit": null + } + } + }, + { + "instruction_id": "INS_20261105_688025SH_OPEN_001_67404b29b330", + "origin_type": "proposal", + "origin_id": "PRP_20261105_688025SH_OPEN", + "action": "OPEN", + "side": "buy", + "qty": 200, + "exec_qty": 200, + "exec_avg_price": null, + "limit_price": "313.90", + "status": "CONFIRMED", + "created_at": "2026-11-05 10:13:53", + "progress": { + "leg": null, + "reason": null, + "grid_entry": null, + "grid_exit": null, + "grid_params": null, + "risk_exit": null + } + } + ], + "proposals": [ + { + "proposal_id": "PRP_20261105_688025SH_OPEN", + "qty": 200, + "status": "ACCEPTED", + "created_at": "2026-11-05 10:13:03", + "judge_reason": "研判总等待时间已到,转人工确认", + "hard_numbers": { + "advice": { + "tier": "试探仓", + "factor": 0.16666666666666666, + "splits": [ + 1.0 + ], + "conditions": [ + "第二批要求技术面不看空", + "只在人工确认后下(研判把握度低或不可用)" + ], + "why": [ + "基本面看多、技术面无读数", + "研判 UNAVAILABLE、把握度 无" + ], + "hold_type": "长期持有类", + "delay_first": false, + "route": "放行", + "tight_trail": false, + "fund_stance": "看多", + "tech_stance": "无读数", + "priced_in": false, + "mechanical": { + "target_pct": 0.06, + "splits": [ + 0.5, + 0.25, + 0.25 + ] + }, + "version": "2", + "target_pct": 0.01, + "text": "建议方案:试探仓(目标 1.0%,分批 100%);机械方案 6.0% 分批 50%/25%/25%;条件:第二批要求技术面不看空;只在人工确认后下(研判把握度低或不可用);长期持有类;依据:基本面看多、技术面无读数;研判 UNAVAILABLE、把握度 无", + "needs_user_confirm": true + }, + "advice_resized": { + "from_qty": 300, + "to_qty": 200, + "tier": "试探仓", + "note": "目标 15,695 元买不足一手,抬到一手 200 股(4.00%)" + }, + "price": 313.9, + "target_pct": 0.04, + "target_amount": 62780.0, + "base_amount": 62780.0, + "judge_verdict": "UNAVAILABLE", + "judge_conf": null + } + } + ], + "risk": { + "book_id": "original", + "closed_at": "2026-11-06T09:45:02.283085+08:00", + "code": "688025.SH", + "count": 2, + "events": [ + { + "at": "2026-11-06T09:36:29.308000+08:00", + "confidence": 0.95, + "content": "1e5feda187b18c3985aec46e30b3ce13c54f493373dc9af222396519521fb0f8", + "key": "952b734ad668655e713f80df388838f758e9c5d625a4a7084b1784dc660efd56", + "msg_id": "1793928723952-0", + "reason": "量比8.49倍远超2倍门槛放量破285支撑,主力净流出-9%确认机构出货,空头加速逻辑完全兑现", + "source": "intraday", + "source_id": "20261106_093629_688025.SH" + }, + { + "at": "2026-11-06T09:36:43.993000+08:00", + "confidence": 0.95, + "content": "26e47de7a52452ac8eb054a39b1dc73eb207197eb4999fc7f31eb3731245083d", + "key": "136be4cf3196982ad22d19246d0027e390cc8d00f5e90cf45a1fda77ddbab1c5", + "msg_id": "1793928738638-0", + "reason": "早盘破位285放量确认(量比8.49),午后反弹至298但实时主力净-9%、主散背离-18确认无主力支撑,板块short穿越日β恶劣,破位结构未修复,止损离场。", + "source": "intraday", + "source_id": "20261106_093643_688025.SH" + } + ], + "exit_iid": "RX_4730fa189bee7aa734e7_1", + "exit_seq": 1, + "initialized_at": "2026-11-05T12:30:46.297993+08:00", + "lot_ids": [ + "1455" + ], + "opened_date": "2026-11-05", + "phase": "CLOSED", + "reason": "第二次独立清仓信号已确认,开始清仓", + "review": { + "at": "2026-11-02T13:17:05.037299+08:00", + "grade": "好", + "ref": "afc871564113750bdadf6e2a8cdb9ca8488fafdfcbca8a1ce3f9577b7501e22c" + }, + "round_id": "4730fa189bee7aa734e7", + "stopped_parents": [ + "INS_20261105_688025SH_OPEN_001_67404b29b330" + ] + } + }, + "601126.SH": { + "position": { + "total_qty": 2700, + "avg_cost": "31.313", + "opened_date": "2026-11-06" + }, + "lots": [ + { + "id": 1456, + "lot_type": "BASE", + "qty": 700, + "open_price": "31.150", + "open_date": "2026-11-06", + "instruction_id": "INS_20261106_601126SH_OPEN_93200", + "status": "OPEN" + }, + { + "id": 1458, + "lot_type": "ADD", + "qty": 1000, + "open_price": "31.370", + "open_date": "2026-11-09", + "instruction_id": "STR20261109094705_601126SH_g", + "status": "OPEN" + }, + { + "id": 1460, + "lot_type": "ADD", + "qty": 1000, + "open_price": "31.370", + "open_date": "2026-11-10", + "instruction_id": "STR20261110094004_601126SH_g", + "status": "OPEN" + } + ], + "instructions": [ + { + "instruction_id": "STR20261110094004_601126SH_g", + "origin_type": "strategy", + "origin_id": "STR_20261109_601126SH_188403", + "action": "ADD", + "side": "buy", + "qty": 1000, + "exec_qty": 1000, + "exec_avg_price": null, + "limit_price": "31.37", + "status": "CONFIRMED", + "created_at": "2026-11-10 09:40:05", + "progress": { + "leg": "grid_buy:1", + "reason": "网格买: 现价 31.37 跌破买入线 31.372,相邻卖出线 32.627,买 1000 股", + "grid_entry": 31.372, + "grid_exit": 32.627, + "grid_params": { + "center": 31.39, + "lower": 30.116, + "upper": 32.825, + "step_pct": 0.04, + "per_lot": 1000, + "max_capital": 67730.61 + }, + "risk_exit": null + } + }, + { + "instruction_id": "STR20261109100402_601126SH_g", + "origin_type": "strategy", + "origin_id": "STR_20261109_601126SH_188403", + "action": "TRIM", + "side": "sell", + "qty": 700, + "exec_qty": 0, + "exec_avg_price": null, + "limit_price": null, + "status": "CANCELLED", + "created_at": "2026-11-09 10:04:02", + "progress": { + "leg": "grid_sell:1", + "reason": "网格卖: 现价 31.38 涨破档1(买价 31.37), 卖 700 股", + "grid_entry": null, + "grid_exit": null, + "grid_params": null, + "risk_exit": null + } + }, + { + "instruction_id": "STR20261109094705_601126SH_g", + "origin_type": "strategy", + "origin_id": "STR_20261109_601126SH_188403", + "action": "ADD", + "side": "buy", + "qty": 1000, + "exec_qty": 1000, + "exec_avg_price": null, + "limit_price": null, + "status": "CONFIRMED", + "created_at": "2026-11-09 09:47:06", + "progress": { + "leg": "grid_buy:1", + "reason": "网格买: 现价 31.37 跌破档1(档价 30.744), 买 1000 股", + "grid_entry": null, + "grid_exit": null, + "grid_params": null, + "risk_exit": null + } + } + ], + "proposals": [], + "risk": { + "book_id": "original", + "code": "601126.SH", + "count": 1, + "events": [ + { + "action": "SELL", + "at": "2026-11-12T13:56:48+08:00", + "coverage": 2, + "expires_at": "2026-11-12T13:59:05.546250+08:00", + "groups": { + "main_fund": { + "direction": 0, + "evidence": [], + "state": "UNKNOWN", + "weight": 0.3333333333333333 + }, + "qrs": { + "direction": -1, + "evidence": [ + { + "at": "2026-11-12T09:30:00+08:00", + "direction": -1, + "expires_at": "2026-11-12T15:00:00+08:00", + "group": "qrs", + "identity": "372dc66d4823b83b47aecef68c7e62c1ba3a19025a7cbaff0a9bff35358dcedb", + "source": "daily_qrs_symmetric_down", + "ts_code": "601126.SH" + } + ], + "state": "VALID", + "weight": 0.3333333333333333 + }, + "v3f": { + "direction": -1, + "evidence": [ + { + "at": "2026-11-12T13:56:48+08:00", + "direction": -1, + "expires_at": "2026-11-12T14:41:48+08:00", + "group": "v3f", + "identity": "9b4548fc81f478ec3d7fcc717a806254691eb03756577a0af7d5b0b3fe6c9211", + "source": "mtf_v3f", + "ts_code": "601126.SH" + } + ], + "state": "VALID", + "weight": 0.3333333333333333 + } + }, + "key": "e7f7eb097cb26ead20fdb6cf6674c4abd1a1ad6768fd8392f110c51aa6af2b8d", + "reason": "三组独立证据:同向看空", + "score": -0.666666667, + "source": "timing_fusion", + "ts_code": "601126.SH", + "verified_at": "2026-11-12T13:57:05.546250+08:00", + "version": "timing_fusion_v1" + } + ], + "exit_seq": 0, + "initialized_at": "2026-11-06T09:45:02.283085+08:00", + "lot_ids": [ + "1456", + "1458", + "1460" + ], + "opened_date": "2026-11-06", + "phase": "STOP_BUY", + "reason": "第一次清仓信号已确认,停止新增买入,等待第二次独立信号", + "review": { + "at": "2026-11-02T12:15:00.791329+08:00", + "grade": "好", + "ref": "5caefe48f8776bc10d22c1e4802cbdbec3f49af24c14b17920e988c6490b3358" + }, + "round_id": "c4d5ad09d8b4c8720940", + "stopped_parents": [ + "INS_20261106_601126SH_OPEN_93200", + "STR20261109094705_601126SH_g", + "STR20261110094004_601126SH_g" + ] + }, + "grid_projected": { + "filled_levels": { + "1460": { + "qty": 1000, + "price": 31.37, + "sell_target": 32.63, + "entry_level": 31.372, + "buy_instruction_id": "STR20261110094004_601126SH_g", + "lot_id": 1460 + }, + "1458": { + "qty": 1000, + "price": 31.37, + "sell_target": 32.63, + "entry_level": null, + "buy_instruction_id": "STR20261109094705_601126SH_g", + "lot_id": 1458 + } + }, + "invested": 62740.0, + "inventory_source": "booked_lots" + } + } + } +} diff --git a/docs/交易计划与风险约束整改接续_2026-11-16.md b/docs/交易计划与风险约束整改接续_2026-11-16.md index 2922a65..edbe633 100644 --- a/docs/交易计划与风险约束整改接续_2026-11-16.md +++ b/docs/交易计划与风险约束整改接续_2026-11-16.md @@ -1,21 +1,23 @@ # 交易计划与风险约束整改接续 -更新时间:2026年11月16日,北京时间。当前阶段为规格落地与关键交易核验。 +最新运行核验时间:2026年11月16日09:26:38,北京时间。当前阶段为首批代码验证。 ## 当前工作 实施规格见[交易计划与风险约束整改规格](交易计划与风险约束整改规格_2026-11-16.md)。独立分支`codex/trading-remediation-20261116`基于产品`372e350`,工作树`/private/tmp/pms-trading-remediation-20261116`。原研究工作树及已有未提交内容保持。 -已核对代码中的试探仓向上取整、网格参数变更后丢失买入档位占用、网格下破只停买,以及二次确认的更新频率约束。具体交易归因仍待155原始指令与批次匹配。 +规格提交`4570fb3`。已完成[杰普特与四方交易链路核验](杰普特与四方交易链路核验_2026-11-16.md),真实提议确认杰普特15695元试探预算被抬到62780元;四方两批31.37元库存、旧批次无新版档位占用与第二笔31.372买入线相互吻合。七个相关worker源码摘要与基线一致。 + +先实现可独立发布的网格库存约束,减少一次发布涉及的交易路径。评估与落单前复核共用真实库存限制,参数变化不清除约束;已卖出并入账后允许同价循环。新增12例,开发机Python3.12隔离网格专项62例及全量85组通过,日志为`/private/tmp/pms-remediation-grid-full.log`。原Python3.11临时环境已被清理,未将环境启动失败算作代码失败或通过。 ## 验收状态 | 工作包 | 状态 | 下一项证据 | | --- | --- | --- | -| 关键交易核验 | 进行中 | 155当前身份、镜像、参数与两只股票逐笔链路 | +| 关键交易核验 | 主要路径已确认 | 杰普特两次模型完整输入仍缺;不阻塞确定性数量与库存修复 | | 试探预算约束 | 待开发 | 全路径预算传递及最低数量拒绝用例 | -| 网格库存约束 | 待开发 | 跨参数库存与真实下穿重放 | +| 网格库存约束 | 已开发,专项通过 | 155候选断网回归、发布与自然判收 | | 退出方案 | 待核验与设计 | 二次确认可达性和价格失效比较 | | 分层评价与产品验收 | 待实施 | 历史冻结名单覆盖与当前页面任务检查 | -本轮尚未修改业务代码、运行参数或部署。未触发模型、交易tick或委托。 +本轮已修改网格业务代码和隔离测试,尚未修改运行参数或部署。未触发模型、交易tick或委托。预算约束不能因网格专项通过而标为完成。 diff --git a/docs/杰普特与四方交易链路核验_2026-11-16.md b/docs/杰普特与四方交易链路核验_2026-11-16.md new file mode 100644 index 0000000..6c9835b --- /dev/null +++ b/docs/杰普特与四方交易链路核验_2026-11-16.md @@ -0,0 +1,49 @@ +# 杰普特与四方交易链路核验 + +2026年11月16日09:26:38,北京时间,在155模拟PMS重新读取提议、指令、批次、策略状态和风险事件。目标主机为factorevaluation,表前缀为空,监控范围校验为pms-sim-155,委托通道为ws。读取脚本只调用查询和库存投影,没有触发模型、交易调度、补账或委托。 + +本机2280连接握手超时后,使用已有SSH配置指向的2255映射,核对主机身份后完成读取。运行web为`pms:etf-names-975fa5a`,worker为`pms:watch-alert-71f8278`;两者只挂载日志目录。唯一WS仍为`pms:risk-exit-64449c6`,未操作。与整改相关的七个worker源码文件摘要均与开发基线`372e350`一致。 + +脱敏事实与源码摘要见[核验数据](evidence/trading_remediation_20261116.json)。该时点全局买入暂停和执行暂停均为false;4%网格、退出纪律和多源择时开启。它们是核验当时的值,后续发布必须重新读取。 + +## 杰普特的预算与退出 + +11月5日10:13:03保存的提议`PRP_20261105_688025SH_OPEN`,研判为UNAVAILABLE,原文为“研判总等待时间已到,转人工确认”。建议档位是试探仓、目标1%、单批;基本面看多、技术面无读数。 + +提议同时保存了数量调整说明:“目标15,695元买不足一手,抬到一手200股(4.00%)”。参考价313.90元,实际提议200股、62780元,恰为原试探金额四倍。人工采纳后10:13:53落下200股父指令。批次1455开仓价313.760元,当前已关闭。 + +这一问题已能从真实提议直接确认,不再只是根据盈亏反推:降档后的数量仍可借机械档预算补足最低申报数量,人工确认批准的是这个已经放大的计划。整改必须在数量计算和采纳入口同时拦截,不能仅改建议文字。 + +11月6日退出事件分别发生于09:36:29.308和09:36:43.993,相隔14.685秒。两条均来自intraday,置信度均为0.95,signal_id和msg_id不同。后一条原文包含“午后反弹至298”,却标记为上午09:36。保存的退出状态计数为2,09:37:03创建200股整仓退出指令。 + +旧去重函数会合并近时跨流消息,但同一来源只检查事件编号是否相同。因而这两条不同编号的同源消息被计成两次,并不证明有两次独立的市场观察。文本时序矛盾也已在原保存内容中确认;完整模型输入和观察窗口仍未取得,不能据此断言具体是哪项上游输入或模板造成,也不能以事后反弹评价当时止损一定错误。 + +11月11日后已切换多源合成;本次不把旧版退出案例算进新版效果,也不重放旧消息改变已完成交易。 + +## 四方的库存与重复准入 + +当前真实未平批次为: + +| 批次 | 日期 | 类型 | 剩余数量 | 实际买入成本 | +| --- | --- | --- | ---: | ---: | +| 1456 | 11月6日 | 底仓 | 700股 | 31.15元 | +| 1458 | 11月9日 | 网格增量 | 1000股 | 31.37元 | +| 1460 | 11月10日 | 网格增量 | 1000股 | 31.37元 | + +11月9日09:47:06第一笔网格父指令没有保存新版grid_entry和grid_params。历史理由使用旧错位档号,不能把文案中的30.744当作实际成交价。其实际批次成本为31.37元,旧卖单最终CANCELLED。 + +11月10日09:40:05第二笔父指令`STR20261110094004_601126SH_g`明确记录:现价31.37,买入线31.372,相邻卖出线32.627,数量1000股,档距4%。它与第一笔来自同一策略`STR_20261109_601126SH_188403`。 + +当前库存投影中,第一笔entry_level为空,第二笔为31.372;两笔实际成本均31.37,卖出目标均32.63。说明旧批次虽继续占用资金,却没有占用新网格的买入档。新代码的防重条件只匹配entry_level,故这次实际第二笔路径符合重复准入的定位。 + +2%与4%在原中枢31.39、下沿30.116、上沿32.825下确实共用31.372买入线。整改应根据真实未平库存限制新增机会,而不是按新版档号把旧仓当作无库存。现有2700股、成本和历史委托必须完整保留。 + +## 首次转弱与后续等待 + +四方保存的首次合成看空时间为11月12日13:56:48,支持组为当日09:30的日线QRS和13:56:48的v3f;主力资金组为UNKNOWN。09:26核验时仍为STOP_BUY、count=1,网格below_floor为true。 + +按当前独立性检查,仅继续更新v3f而没有第二个新的支持组,不能形成第二次确认。此事实支持对“停买后等待多久、何时转人工复核”进行专项设计,不能把旧QRS重新发布当新观察,也不能未经验证改为第一次转弱直接清仓。 + +## 本轮边界 + +本轮核对了原提议、父指令与实际批次的一致性,未重新计算整个账户收益、逐条审计所有QMT成交回报,亦未取得两次模型判断的完整输入。当前交易根因证据足以指导预算与网格整改;退出语义的上游根因及新版确认路径仍列后续工作。 diff --git a/scripts/run_tests.py b/scripts/run_tests.py index ee8ffbc..7d561a6 100644 --- a/scripts/run_tests.py +++ b/scripts/run_tests.py @@ -6,7 +6,7 @@ test_batch54_units.py 用户候选来源、到期核验、权限、显式取用、技术面补读及评级排序透传 (10例) -4%网格真实批次、账本核销顺序、零散成交合并、三种报价边界、退出升级及迁移事务专项50例。 +4%网格真实批次、核销顺序、报价边界、迁移事务与跨版本库存重复准入专项62例。 成交修正专项:批次投影36例、传输与执行保护29例、服务事务36例、 普通成交凭据与仓储13例、网格更正版本与并发20例。 diff --git a/scripts/test_grid_execution.py b/scripts/test_grid_execution.py index b47692c..a9080c7 100644 --- a/scripts/test_grid_execution.py +++ b/scripts/test_grid_execution.py @@ -107,6 +107,105 @@ class EvaluationTests(unittest.TestCase): st=strategy(dict(center=10,lower=8,upper=12,step_pct=.04,per_lot=100,max_capital=10000));state={'filled_levels':{},'invested':0} self.assertIsNone(evaluate(st,state,pos(0),9.1));self.assertEqual(state['last_band'],2) + +class InventoryOpportunityTests(unittest.TestCase): + """11月9日至10日四方实际价位重放,只有内存批次,不发真实委托。""" + def setUp(self): + self.params = dict(center=31.39, lower=30.116, upper=32.825, + step_pct=.04, per_lot=1000, max_capital=67730.61) + self.st = strategy(self.params) + self.st['ts_code'] = '601126.SH' + self.lots, self.parents = records(1000, 31.37) + self.lots[0].update(ts_code='601126.SH', open_date='2026-11-09') + self.parents['buy']['ts_code'] = '601126.SH' + + def state(self): + return {**core.project(self.st, self.lots, self.parents), 'last_band': 1} + + def test_sifang_legacy_inventory_cannot_buy_same_line_after_migration(self): + state = self.state() + before = copy.deepcopy(state['filled_levels']) + self.assertIsNone(evaluate(self.st, state, pos(1000, 700), 31.37)) + self.assertEqual(state['filled_levels'], before) + self.assertEqual(state['invested'], 31370) + + def test_parameter_revision_preserves_original_observation(self): + self.parents['buy']['progress'].update( + grid_params={**self.params, 'step_pct': .02}, + grid_entry=31.372, grid_exit=31.999) + row = self.state()['filled_levels']['1'] + self.assertIsNone(row['entry_level']) + self.assertEqual(row['original_entry_level'], 31.372) + self.assertTrue(core.buy_blocker({'1': row}, self.params, 31.372)) + + def test_percent_boundary_is_rounded_down_not_up(self): + filled = self.state()['filled_levels'] + self.assertTrue(core.buy_blocker(filled, self.params, 30.12)) + self.assertEqual(core.buy_blocker(filled, self.params, 30.11), '') + + def test_lower_existing_lot_controls_the_next_opportunity(self): + filled = self.state()['filled_levels'] + filled['2'] = {'qty': 100, 'price': 30} + self.assertTrue(core.buy_blocker(filled, self.params, 29)) + self.assertEqual(core.buy_blocker(filled, self.params, 28.8), '') + + def test_absolute_spacing_overrides_percent_spacing(self): + filled = self.state()['filled_levels'] + params = {**self.params, 'step': 1} + self.assertTrue(core.buy_blocker(filled, params, 30.38)) + self.assertEqual(core.buy_blocker(filled, params, 30.37), '') + + def test_partial_sale_does_not_release_remaining_inventory(self): + self.lots[0]['qty'] = 100 + self.assertIsNone(evaluate(self.st, self.state(), pos(100), 31.37)) + + def test_fully_booked_sale_allows_same_price_after_new_crossing(self): + self.lots[0]['qty'] = 0 + state = self.state() + decision = evaluate(self.st, state, pos(0), 31.37) + self.assertEqual((decision['side'], decision['qty'], decision['limit_price']), + ('buy', 1000, 31.37)) + + def test_changed_center_and_quantity_do_not_release_inventory(self): + for changes in ({'center': 31.4}, {'per_lot': 500}, {'step_pct': .02}): + params = {**self.params, **changes} + self.assertTrue(core.buy_blocker(self.state()['filled_levels'], params, 31.37)) + + def test_restart_and_roll_day_preserve_inventory_limit(self): + state = self.state() + state.pop('last_band') + self.assertIsNone(evaluate(self.st, state, pos(1000), 31.40)) + runner._roll_day(state, pos(1000), 20261110) + self.assertIsNone(evaluate(self.st, state, pos(1000), 31.37)) + + def test_invalid_cost_and_spacing_cannot_allow_new_risk(self): + for cost in (0, -1, float('nan'), float('inf')): + with self.assertRaises(ValueError): + core.buy_blocker({'1': {'qty': 100, 'price': cost}}, self.params, 30) + for pct in (0, -1, 1, float('nan'), float('inf')): + with self.assertRaises(ValueError): + core.buy_blocker(self.state()['filled_levels'], {**self.params, 'step_pct': pct}, 30) + + def test_old_prepared_buy_rechecked_against_newly_booked_inventory(self): + empty = {'last_band': 1, 'filled_levels': {}, 'invested': 0} + decision = evaluate(self.st, empty, pos(0), 31.37) + with patch.object(settings, 'INFRA_STRATEGY_BOOK_SCHEMA', False), \ + patch.object(runner, '_current_strategy', return_value=nullcontext(self.st)), \ + patch.object(pms_repo, 'list_instructions', return_value=[]), \ + patch.object(pms_repo, 'get_position', return_value=pos(1000)), \ + patch.object(service, 'snapshot', return_value=self.state()), \ + patch.object(runner, '_buypause_codes', return_value=set()), \ + patch.object(runner, '_macro_gate_active', return_value=False), \ + patch.object(pms_repo, 'insert_instruction') as insert: + with self.assertRaisesRegex(ValueError, '已有未卖出的网格仓'): + runner._emit_instruction(self.st, decision) + insert.assert_not_called() + + def test_existing_profit_sale_keeps_original_target_and_quantity(self): + decision = evaluate(self.st, self.state(), pos(1000, 1000), 32.63) + self.assertEqual((decision['side'], decision['qty'], decision['limit_price']), + ('sell', 1000, 32.63)) + class ProtectionTests(unittest.TestCase): def test_buy_pause_read_error_is_unknown_not_clear(self): with patch.object(strategy_service,'buypause_map',side_effect=RuntimeError('db')):