From 39d209a6e42860496a389520fec2a92c6c80d8aa Mon Sep 17 00:00:00 2001 From: baobao Date: Fri, 30 Oct 2026 16:19:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B6=E7=9B=98=E5=90=8E=E7=A6=81=E6=AD=A2?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=BD=93=E6=97=A5=E5=B7=B2=E8=BF=87=E6=9C=9F?= =?UTF-8?q?=E7=9A=84=E7=BB=AD=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/exec_advisor.py | 5 ++++- app/services/executor.py | 11 ++++++++++- scripts/run_tests.py | 2 +- scripts/test_batch50_units.py | 28 ++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/app/services/exec_advisor.py b/app/services/exec_advisor.py index ce9f7e4..782cfd3 100644 --- a/app/services/exec_advisor.py +++ b/app/services/exec_advisor.py @@ -191,7 +191,10 @@ def _qmt_limit(*, side, action, ts_code, now, day, params, is_last_day, urgent, return result(et.ACT_WAIT, "无有效行情或已停牌,等待核实") if left <= 0: return result(et.ACT_WAIT, et.quota_wait_reason(urgent, is_last_day)) - if not et.in_session(et.hm_to_min(now)): + now_min = et.hm_to_min(now) + if now_min >= et.CLOSE_MIN: + return result(et.ACT_WAIT, "已收盘,今日不再新增委托") + if not et.in_session(now_min): return result(et.ACT_WAIT, "非交易时段") strategy = (prog or {}).get("origin") == "strategy" and (prog or {}).get("stype") in ("GRID", "TRAIL") diff --git a/app/services/executor.py b/app/services/executor.py index b662f9a..b4135e6 100644 --- a/app/services/executor.py +++ b/app/services/executor.py @@ -367,13 +367,22 @@ def run_tick(*, now=None, dry_run: bool = False) -> dict: pms_repo.update_instruction(ins["instruction_id"], progress=prog) continue + dl_min = order_deadline(now, d, ttl_min=order_ttl) + if dl_min <= et.hm_to_min(now): + # 收盘边界可能刚收到旧单终态,不能因此再生成有效期已到的子单。 + reason = '已到委托截止时间,今日不再新增委托' + prog['last_decision'] = {'at': now.strftime('%H:%M'), 'action': et.ACT_WAIT, 'reason': reason} + if not dry_run: + pms_repo.update_instruction(ins['instruction_id'], progress=prog) + out['waited'].append({'instruction_id': ins['instruction_id'], 'code': code, + 'action': et.ACT_WAIT, 'reason': reason}) + continue if dry_run: out["fired"].append({"instruction_id": ins["instruction_id"], "code": code, "qty": qty, "limit": d["limit_price"], "reason": d["reason"], "dry_run": True}) continue - dl_min = order_deadline(now, d, ttl_min=order_ttl) # intent / parent_id / note 是 ws 通道要的 (协议 §4.2): intent 只作归类, # parent_id 让子单能回指父指令, note 原样落到 QMT 侧供人工看盘。 # shadow 模式忽略这三项, 传着不碍事 —— 两种模式共用同一个调用点。 diff --git a/scripts/run_tests.py b/scripts/run_tests.py index d83176c..368f808 100644 --- a/scripts/run_tests.py +++ b/scripts/run_tests.py @@ -7,7 +7,7 @@ 包含 (例数按 2026-10-26 隔离回归实跑校正): test_batch52_units.py 当日限价、撤单拒绝、迟到成交、发送恢复与真实SQL状态保护 (22例) test_batch51_units.py 跨日决策复核、撤单回报、数量约束、人工确认与真实SQL事务及完整选股接线 (36例) - test_batch50_units.py QMT限价等待、价格来源故障、用户暂停、配额防重和试算隔离 (14例) + test_batch50_units.py QMT限价等待、价格来源故障、用户暂停、配额防重、试算隔离及收盘边界 (17例) test_batch49_units.py 持仓分组、同名股票与批次归属、未迁移及缺数展示 (8例) test_batch48_units.py 策略账本隔离、轮换优先级、交易单位、模拟直接启用、读数及微盘50规则换版 (55例) test_batch47_units.py 账户资金去重、买入额度与账户展示和策略估值分离 (9 例) diff --git a/scripts/test_batch50_units.py b/scripts/test_batch50_units.py index a152fd1..8cf4471 100644 --- a/scripts/test_batch50_units.py +++ b/scripts/test_batch50_units.py @@ -94,6 +94,34 @@ class QmtPriceTests(unittest.TestCase): self.assertEqual(r['limit_price'], 30.02) self.assertFalse(r['forced']) + def test_close_boundary_cannot_submit_even_urgent_sell(self): + for now in ('15:00', '15:01', datetime(2026, 10, 28, 15, 0, 16)): + for side in ('buy', 'sell'): + self.assertEqual(self.decide(now=now, side=side, urgent=True)['action'], 'WAIT') + self.assertEqual(self.post.call_count, 0) + self.assertEqual(self.decide(now='14:59')['action'], 'FIRE') + + def test_expired_child_at_close_does_not_create_new_outbox(self): + self.parent() + self.fake.params['PMS_EXEC_QMT_DAY_ORDER'] = True + with patch.object(market, 'day_snapshot', return_value={'price': 31.1}): + self.assertEqual(len(executor.run_tick(now=NOW)['fired']), 1) + old = next(iter(self.fake.qmt.orders.values())); old.update(status='EXPIRED', cum_qty=0) + r = executor.run_tick(now=datetime(2026, 10, 28, 15, 0, 16)) + self.assertEqual(r['fired'], []) + self.assertIn('已收盘', str(r['waited'])) + self.assertEqual(len(self.fake.qmt.orders), 1) + + def test_executor_rejects_expired_deadline_even_when_advisor_fires(self): + self.parent() + with patch.object(market, 'day_snapshot', return_value={'price': 31.1}), \ + patch.object(ea, 'decide', return_value={'action': 'FIRE', 'qty_hint': 200, + 'limit_price': 30.02, 'reason': 'test', 'qmt_price_monitor': True}): + r = executor.run_tick(now=datetime(2026, 10, 28, 15, 0, 16)) + self.assertEqual(r['fired'], []) + self.assertIn('截止时间', str(r['waited'])) + self.assertEqual(self.fake.qmt.orders, {}) + def test_disabled_mode_retains_old_wait(self): self.fake.params['PMS_EXEC_QMT_PRICE_MONITOR'] = False self.assertEqual(self.decide(day={'price': 31.1})['action'], 'WAIT')