diff --git a/app/repo/downstream_repo.py b/app/repo/downstream_repo.py index f9ef531..da0e64b 100644 --- a/app/repo/downstream_repo.py +++ b/app/repo/downstream_repo.py @@ -142,7 +142,7 @@ def _float(v): # ================================================================ trading_order -def sim_buy_fill_evidence(child_id, broker_id): +def sim_fill_evidence(child_id, broker_id): """仅模拟委托的源记录;不改下游数据库。""" if T_ORDER != 'trading_order' or T_POSITION != 'trading_position': raise ValueError('不能从非模拟表读取补账证据') diff --git a/app/services/trade_correction.py b/app/services/trade_correction.py index 96642e2..6af3cbb 100644 --- a/app/services/trade_correction.py +++ b/app/services/trade_correction.py @@ -125,7 +125,7 @@ def _pair(row): return payload, amendment, originals, amendments -def _load_evidence(original, order, parent): +def _load_evidence(original, order, parent, *, recovery=None): """分别读取单表,在服务层按本端父指令归属组合完整证据。""" code, book = parent['ts_code'], parent['book_id'] orders = repo.orders_for_code(code) @@ -136,6 +136,10 @@ def _load_evidence(original, order, parent): continue pid = child.get('parent_id') current = parents.get(pid) or pms_repo.get_instruction(pid) + if recovery and not current and child.get('status') in qmt_repo.FINAL and child.get('final_at'): + finalized = _stamp(child['final_at']) + if finalized < recovery['cutover'] and pid not in recovery['lot_parents']: + continue _need(current and current.get('book_id') in ('original', 'micro30'), '同股历史委托策略归属不完整') _need(current.get('ts_code') == code and current.get('side') == child.get('side'), '历史父子委托身份冲突') parents[pid] = current @@ -146,6 +150,8 @@ def _load_evidence(original, order, parent): rows = repo.trades_for_orders(list(by_child)) trades, links = [], {} for row in rows: + if recovery and row['seq'] == recovery.get('balancing_trade_seq') and row['processed'] == qmt_repo.PROC_PENDING: + continue if row['processed'] == qmt_repo.PROC_PENDING: raise Waiting('同股所属策略仍有原成交等待入账') _need(row['processed'] == qmt_repo.PROC_BOOKED, '同股所属策略存在未核实的原成交') @@ -204,8 +210,8 @@ def _position_fields(position, projection): return result -def _verify_physical(code, book, projection, received_at): - if projection['qty_before'] == projection['qty_after']: +def _verify_physical(code, book, projection, received_at, *, balancing_sell=0): + if projection['qty_before'] == projection['qty_after'] and not balancing_sell: return snap = qmt_repo.latest_snapshot('positions') if not snap or snap.get('age_sec') is None or not 0 <= snap['age_sec'] <= 60: @@ -233,7 +239,7 @@ def _verify_physical(code, book, projection, received_at): with books.scope('all'): positions = [r for r in pms_repo.list_positions() if r['ts_code'] == code] _need(all(r.get('book_id') in ('original', 'micro30') for r in positions), '同股持仓策略归属不可核实') - total = sum(_q(r['total_qty']) for r in positions if r['book_id'] != book)+projection['qty_after'] + total = sum(_q(r['total_qty']) for r in positions if r['book_id'] != book)+projection['qty_after']-balancing_sell _need(total == physical, '更正后双策略数量与QMT实际持仓不一致') @@ -332,7 +338,7 @@ def _order_fields(order, amended, quantity, received): return fields -def _apply(row): +def _apply(row, *, recovery=None): state = qmt_repo.get_state() or {} watermark = state.get('last_seq') if not isinstance(watermark, int) or watermark < int(row['seq']): @@ -369,7 +375,8 @@ def _apply(row): and parent.get('side') == payload['side'], '父指令证券、方向或策略归属不可核实') book, code = parent['book_id'], parent['ts_code'] with books.scope(book): - orders, parents, trades, links = _load_evidence(original, order, parent) + options = {'recovery': recovery} if recovery else {} + orders, parents, trades, links = _load_evidence(original, order, parent, **options) lots = pms_repo.list_lots(code, status=None, limit=20001) _need(len(lots) < 20001, '批次证据超过核验上限') projection = core.compile_correction(lots=lots, trades=trades, target_trade_no=no, @@ -395,7 +402,8 @@ def _apply(row): '父指令执行量与旧逐笔合计不符或更正后超量') quantity_changed = _q(effective['qty']) != _q(payload['after']['qty']) received = max(_stamp(row['received_at']), _stamp(amendment['received_at'])) - _verify_physical(code, book, projection, received) + physical_options = {'balancing_sell': recovery['balancing_sell']} if recovery else {} + _verify_physical(code, book, projection, received, **physical_options) cash_options = {'verified_price_only': True} if price_only_overfill else {} cash_before, cash_after = _cash_projection(original, book, payload['side'], projection, am, order, **cash_options) projected_orders = [{**r, **order_fields} if r['instruction_id'] == iid else r for r in orders] diff --git a/scripts/reconcile_qmt_closed_sells.py b/scripts/reconcile_qmt_closed_sells.py new file mode 100644 index 0000000..9126fa0 --- /dev/null +++ b/scripts/reconcile_qmt_closed_sells.py @@ -0,0 +1,145 @@ +"""155模拟仓:在同一事务复核并补齐2026年11月19日两组已平仓卖出事实。""" +import argparse +import json +from datetime import date +from decimal import Decimal + +from reconcile_qmt_late_buys import check_scope, digest, require, raw_values +from app.core import recon, strategy_book as books, ws_codec +from app.repo import downstream_repo, pms_repo, qmt_repo, rotation_repo, trade_correction_repo +from app.services import ledger_service, param_store, rotation_execution, rotation_service, trade_correction + + +def data(seq, kind, code): + row = trade_correction_repo.get_inbox(seq) + require(row and row['msg_type'] == kind and row['payload']['ts_code'] == code + and row['payload']['side'] == 'sell', '卖出证据身份不匹配') + return row + + +def source(payload, effective): + child, broker, code = (payload[key] for key in ('instruction_id','broker_order_id','ts_code')) + orders, logs = downstream_repo.sim_fill_evidence(child, broker) + require(len(orders) == len(logs) == 1, 'QMT原始委托或逐笔缺失、重复') + order, record = orders[0], logs[0] + log = json.loads(record['extra_data']) + require(order['order_id'] == child and order['qmt_order_id'] == record['order_id'] == broker + and order['order_side'] == log['trade_direction'] == 'sell' + and all(downstream_repo.to_dot(row['stock_code']) == code for row in (order, record)) + and log['trade_no'] == payload['trade_no'] + and raw_values({'qty': log['traded_volume'], 'price': log['traded_price'], + 'amount': log['traded_amount']}) == raw_values(effective) + and int(order['filled_quantity']) == int(effective['qty']), 'QMT原始卖出与有效逐笔不互证') + return order, record + + +def plan(): + require(not qmt_repo.list_orders(statuses=qmt_repo.LIVE, limit=1), '有在途委托,不能恢复已平仓卖出') + snapshot = ledger_service.positions_source() + require(snapshot['source'] == 'ws' and 0 <= snapshot['age_sec'] <= 60 + and rotation_service.reconcile_books()['ok'], '先核实完整的QMT实际持仓并消除已有数量差') + funds = qmt_repo.latest_snapshot('funds') + require(funds and 0 <= funds['age_sec'] <= 60, 'QMT资金快照过期') + physical = ws_codec.parse_funds_snapshot(funds['payload']) + capital, _ = rotation_repo.get('capital:live') + cutover = trade_correction._stamp(capital['started_at']) + expected_cash = Decimal(str(physical['available_cash'])) + Decimal(str(physical['frozen_cash'])) + expected_delta = expected_cash - sum(Decimal(str(value)) for value in capital['cash'].values()) + require(physical['frozen_cash'] == 0, '仍有资金冻结,暂缓补账') + evidence, amount = [], Decimal(0) + for code, correction_seqs, late_seq in (('601126.SH', (1965760,1965780),1965841), ('002979.SZ',(1965803,),None)): + corrections = [data(seq, 'trade_correction', code) for seq in correction_seqs] + require(all(row['processed'] in (qmt_repo.PROC_PENDING, qmt_repo.PROC_ORPHAN) for row in corrections) + and all(row['processed'] != qmt_repo.PROC_ORPHAN or '历史委托策略归属不完整' in row['process_note'] + for row in corrections), '卖出更正状态变化或另有阻塞') + payloads = [row['payload'] for row in corrections] + original = trade_correction_repo.get_trade(payloads[0]['trade_no']) + require(original and original['processed'] == qmt_repo.PROC_BOOKED, '原卖出未入账') + first, last = payloads[0], payloads[-1] + require(raw_values(original['payload']) == raw_values(first['before']) and + all(raw_values(previous['after']) == raw_values(current['before']) for previous,current in zip(payloads,payloads[1:])), + '更正记录并非连续版本') + raw = source(original['payload'], last['after']) + order = qmt_repo.get_order(first['instruction_id']) + with books.scope(books.ORIGINAL): + parent = pms_repo.get_instruction(order['parent_id']) + position = pms_repo.get_position(code) + lots = pms_repo.list_lots(code,status=None,limit=2001) + require(parent and parent['status'] == 'CONFIRMED' and parent['book_id'] == books.ORIGINAL + and position and position['status'] == 'CLOSED' and not position.get('archived_at') + and not position['total_qty'] and lots and len(lots) < 2001 + and all(lot['instruction_id'] and lot['status'] == 'CLOSED' and lot['open_date'] > cutover.date() for lot in lots), + '无法完整证明迁移后的当前平仓轮次') + late = data(late_seq,'trade',code) if late_seq else None + late_order = late_raw = final = None + quantity = int(first['before']['qty']) - int(last['after']['qty']) + if late: + late_trade = late['payload'] + late_order = qmt_repo.get_order(late_trade['instruction_id']) + require(late['processed'] == qmt_repo.PROC_PENDING and late_trade.get('desk_backfill') is True + and late_trade['fee'] is None and quantity == raw_values(late_trade)[0] + and late_order['parent_id'] == order['parent_id'] and late_order['broker_order_id'] == late_trade['broker_order_id'] + and late_order['status'] == 'CANCELLED' and int(late_order['cum_qty']) == 0, + '晚到卖出不是同一父指令可验证的补量') + late_raw = source(late_trade, late_trade) + final = [row for row in qmt_repo.inbox_list(msg_type='order_update',corr_id=late_order['instruction_id'],limit=51) + if row['seq'] > late_seq and row['payload'].get('final') is True] + require(len(final) == 1 and final[0]['payload']['status'] == 'EXPIRED' + and int(final[0]['payload']['cum_qty']) == quantity and final[0]['payload']['broker_order_id'] == late_trade['broker_order_id'] + and Decimal(str(final[0]['payload']['cum_avg_price'])) == raw_values(late_trade)[1] + and final[0]['payload']['leaves_qty'] == 0, '晚到卖出终态和真实累计量不一致') + amount += raw_values(late_trade)[2] + else: + require(quantity == 0, '仅价格更正不能改变持仓数量') + amount += raw_values(last['after'])[2] - raw_values(first['before'])[2] + evidence.append({'code':code,'corrections':corrections,'raw':raw,'late':late,'late_order':late_order, + 'late_raw':late_raw,'late_final':final,'position':position,'lots':lots,'parent':parent, + 'recovery':{'cutover':cutover,'lot_parents':sorted({lot['instruction_id'] for lot in lots}), + 'balancing_trade_seq':late_seq,'balancing_sell':quantity}}) + require(abs(expected_delta - amount) <= Decimal('.02'), '逐笔本金不能解释全部剩余资金差,不写入') + return {'evidence': evidence, 'cash_before':capital['cash'], 'qmt_cash':expected_cash, 'cash_delta':amount} + + +def apply(plan_result): + for item in plan_result['evidence']: + for event in item['corrections']: + if event['processed'] == qmt_repo.PROC_ORPHAN: + require(qmt_repo.inbox_mark([event['seq']], processed=qmt_repo.PROC_PENDING, + note='仅经已平仓轮次核实,重新处理原始更正'), '更正挂起标记未变更') + require(trade_correction._apply(trade_correction_repo.get_inbox(event['seq'],lock=True), + recovery=item['recovery']) == 'applied', '更正没有实际入账') + late = item['late'] + if late: + parent = item['parent'] + mapped = recon.map_trades_to_book([late], {parent['instruction_id']:parent['action']}) + require(len(mapped['actions']) == 1 and not mapped['alerts'] and not mapped['fees'], '晚到卖出映射异常') + action = dict(mapped['actions'][0], verified_trade_date=date(2026,11,19)) + require(rotation_execution.book_trade(action, [], late['seq']), '晚到卖出没有原子入账') + require(qmt_repo.update_order(item['late_order']['instruction_id'], cum_qty=action['qty'], + cum_avg_price=action['price'], leaves_qty=0), '晚到卖出累计量未保存') + rotation_repo.put_once('verified-closed-sell:20261119:'+item['code'], 'verified_closed_sell', item) + require(rotation_service.reconcile_books()['ok'], '已平仓数量核对未通过,全部回滚') + capital, _ = rotation_repo.get('capital:live') + cash = sum(Decimal(str(value)) for value in capital['cash'].values()) + require(abs(cash - plan_result['qmt_cash']) <= Decimal('.02'), '补记后资金仍与QMT不符,全部回滚') + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--apply-evidence-sha256') + requested = parser.parse_args().apply_evidence_sha256 + check_scope() + if not requested: + result = plan() + print(rotation_repo.encode({'sha256':digest(result), **result})) + return + with rotation_repo.account_guard(): + require(param_store.get_bool('PMS_GLOBAL_EXEC_HALT',False), '先暂停交易执行') + result = plan() + require(digest(result) == requested, '卖出证据或资金较预演变化,不写入') + apply(result) + print(rotation_repo.encode({'applied':len(result['evidence']),'cash_delta':result['cash_delta'],'sha256':requested})) + + +if __name__ == '__main__': + main() diff --git a/scripts/reconcile_qmt_late_buys.py b/scripts/reconcile_qmt_late_buys.py index 15e3170..3cb1242 100644 --- a/scripts/reconcile_qmt_late_buys.py +++ b/scripts/reconcile_qmt_late_buys.py @@ -24,7 +24,8 @@ def require(condition, message): def check_scope(): require(not settings.PMS_TABLE_PREFIX and settings.INFRA_STRATEGY_BOOK_SCHEMA and dispatcher.mode() == 'ws' and downstream_repo.T_ORDER == 'trading_order' - and downstream_repo.T_POSITION == 'trading_position', '仅允许在155模拟PMS运行') + and downstream_repo.T_POSITION == 'trading_position' + and param_store.get('PMS_MONITOR_WATCH_SCOPE') == 'pms-sim-155', '仅允许在155模拟PMS运行') def raw_values(payload): @@ -99,7 +100,7 @@ def plan(codes, trade_day): with books.scope('all'): parent = pms_repo.get_instruction(order['parent_id']) require(parent, '待核父指令不存在') - orders, logs = downstream_repo.sim_buy_fill_evidence(child, broker) + orders, logs = downstream_repo.sim_fill_evidence(child, broker) events = qmt_repo.inbox_list(msg_type='order_update', corr_id=child, limit=51) require(len(events) < 51, '同一子委托的回报超出完整检查上限') events = sorted((item for item in events if item['payload'].get('final') is True diff --git a/scripts/test_trade_correction_service.py b/scripts/test_trade_correction_service.py index c420b57..a71aed2 100644 --- a/scripts/test_trade_correction_service.py +++ b/scripts/test_trade_correction_service.py @@ -212,6 +212,30 @@ class ServiceTests(unittest.TestCase): self.assertEqual((lot['qty'],lot['realized_pnl']),(60,40.)) self.assertAlmostEqual(pms_repo.get_position(CODE)['avg_cost'],10.333) + def test_closed_recovery_excludes_only_unlinked_pre_cutover_terminal(self): + self.insert('pms_qmt_order',dict(id=90,instruction_id='PRE_D01',parent_id='PRE',ts_code=CODE, + side='buy',status='FILLED',final_at='2026-10-30T12:00:00+08:00')) + original=service.repo.get_trade('BUY') + order=qmt_repo.get_order('P_BUY_D01') + parent=pms_repo.get_instruction('P_BUY') + recovery={'cutover':NOW-dt.timedelta(days=3),'lot_parents':['P_BUY'],'balancing_sell':0} + with self.assertRaisesRegex(ValueError,'历史委托'): + service._load_evidence(original,order,parent) + rows, _, trades, _ = service._load_evidence(original,order,parent,recovery=recovery) + self.assertEqual([row['instruction_id'] for row in rows],['P_BUY_D01']) + self.assertEqual([row['seq'] for row in trades],[original['seq']]) + with self.assertRaisesRegex(ValueError,'历史委托'): + service._load_evidence(original,order,parent,recovery=dict(recovery,lot_parents=['PRE'])) + + def test_balancing_sell_requires_final_actual_quantity(self): + self.snapshot['payload']['items']=[] + projection={'qty_before':0,'qty_after':70} + with self.assertRaisesRegex(ValueError,'实际持仓不一致'): + service._verify_physical(CODE,'original',projection,NOW) + service._verify_physical(CODE,'original',projection,NOW,balancing_sell=70) + with self.assertRaisesRegex(ValueError,'实际持仓不一致'): + service._verify_physical(CODE,'original',projection,NOW,balancing_sell=69) + def test_other_book_same_code_is_not_modified(self): self.insert('pms_position',dict(id=2,book_id='micro30',ts_code=CODE,total_qty=50,avail_qty=50,status='HOLDING')) self.modify_payload(10,after=economic(150,'10'));self.modify_payload(11,cum_qty=150,cum_avg_price=10)