tradingSystem/scripts/reconcile_qmt_late_buys.py

153 lines
9.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""155模拟仓:凭原始逐笔、终态和实际持仓认领已成交的微盘超量买入。默认只读。"""
import argparse
import hashlib
import json
import sys
from datetime import date, datetime
from decimal import Decimal
from pathlib import Path
from zoneinfo import ZoneInfo
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from app.core import execution_policy, recon, strategy_book as books, ws_codec
from app.repo import downstream_repo, pms_repo, qmt_repo, rotation_repo
from app.services import dispatcher, ledger_service, param_store, rotation_execution, rotation_service
from config.settings import settings
def require(condition, message):
if not condition:
raise ValueError(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'
and param_store.get('PMS_MONITOR_WATCH_SCOPE') == 'pms-sim-155', '仅允许在155模拟PMS运行')
def raw_values(payload):
quantity, price, amount = (Decimal(str(payload[key])) for key in ('qty', 'price', 'amount'))
require(0 < quantity == int(quantity) and price > 0 and amount == quantity * price,
'成交数量、价格或金额异常')
return int(quantity), price, amount
def validate_trade(event, order, parent, order_rows, logs, terminal, difference, trade_day):
payload = event['payload']
code, child, broker = (payload[key] for key in ('ts_code', 'instruction_id', 'broker_order_id'))
quantity, price, _ = raw_values(payload)
trade_date = datetime.fromtimestamp(payload['trade_time'] / 1000, ZoneInfo('Asia/Shanghai')).date()
require(trade_date == trade_day and execution_policy.supported_code(code), '成交日期或市场不在本次范围')
require(event['processed'] == qmt_repo.PROC_PENDING and payload['side'] == 'buy'
and payload.get('desk_backfill') is True
and event['dedup_key'] == ws_codec.dedup_key('trade', payload), '收件身份或状态不可核实')
require(order['instruction_id'] == child and order['broker_order_id'] == broker
and order['ts_code'] == code and order['side'] == 'buy' and int(order['qty']) == quantity
and order['status'] in ('CANCELLED', 'EXPIRED') and int(order['cum_qty']) == 0,
'待补子委托与本地记录不符')
require(parent['instruction_id'] == order['parent_id'] == recon.parent_instruction_id(child)
and parent['book_id'] == books.MICRO and parent['ts_code'] == code and parent['side'] == 'buy'
and parent['status'] == 'CONFIRMED' and int(parent['exec_qty']) == int(parent['qty'])
and difference['book_qty'] + quantity == difference['ds_qty'], '父指令或账户持仓差异不吻合')
require(len(order_rows) == len(logs) == 1 and len(terminal) == 2, '原始委托、逐笔或终态证据不唯一')
source, log = order_rows[0], logs[0]
require(source['order_id'] == child and source['qmt_order_id'] == log['order_id'] == broker
and all(downstream_repo.to_dot(item['stock_code']) == code for item in (source, log))
and source['order_side'] == 'buy' and source['order_status'] in downstream_repo.FILLED_STATUSES
and int(source['order_quantity']) == int(source['filled_quantity']) == quantity, 'QMT原始委托与成交不互证')
body = json.loads(log['extra_data'])
require(body['trade_no'] == payload['trade_no'] and body['trade_direction'] == 'buy'
and raw_values({'qty': body['traded_volume'], 'price': body['traded_price'],
'amount': body['traded_amount']})[0] == quantity, 'QMT交易日志不能证明同一逐笔成交')
raw = body.get('before') if body.get('corrected') else {
'qty': body['traded_volume'], 'price': body['traded_price'], 'amount': body['traded_amount']}
require(raw_values(raw) == raw_values(payload), 'QMT原始成交价或成交款与收件不符')
earlier, later = terminal
first, last = earlier['payload'], later['payload']
require(all(item['instruction_id'] == child and item['broker_order_id'] == broker
and item['final'] is True and int(item['leaves_qty']) == 0 for item in (first, last))
and first['status'] == order['status'] and int(first['cum_qty']) == 0
and earlier['seq'] < event['seq'] < later['seq']
and last['status'] == 'FILLED' and last.get('reason') == 'desk_backfill' and int(last['cum_qty']) == quantity
and Decimal(str(last['cum_avg_price'])) == price, '缺少旧终态和其后全成的配套原始回报')
return {'trade_seq': event['seq'], 'trade': payload, 'old_terminal': first, 'filled_terminal': last,
'terminal_seqs': [earlier['seq'], later['seq']], 'order': order, 'parent': parent,
'source_order': source, 'source_log': log, 'position_difference': difference}
def plan(codes, trade_day):
require(not qmt_repo.list_orders(statuses=qmt_repo.LIVE, limit=1), '仍有在途委托,不进行补账')
snapshot = qmt_repo.latest_snapshot('positions') or {}
require(snapshot.get('age_sec', 9999) <= 60, 'QMT实际持仓超过一分钟或不可用')
result = rotation_service.reconcile_books()
require(result.get('source') == 'ws' and not result['errors'], '没有有效QMT实际持仓')
differences = {row['ts_code']: row for row in result['diffs']}
require(set(differences) == set(codes), '实际差异名单与指定范围不一致')
rows = qmt_repo.inbox_pending(limit=501, msg_type='trade')
require(len(rows) < 501, '待入账成交超出完整检查上限')
selected = [row for row in rows if (row.get('payload') or {}).get('ts_code') in differences]
require(len(selected) == len(codes) and {row['payload']['ts_code'] for row in selected} == set(codes),
'并非每个差异均可唯一匹配待入账逐笔')
evidence = []
for row in selected:
payload = row['payload']
child, broker, code = (payload[key] for key in ('instruction_id', 'broker_order_id', 'ts_code'))
order = qmt_repo.get_order(child)
require(order, '待核子委托不存在')
with books.scope('all'):
parent = pms_repo.get_instruction(order['parent_id'])
require(parent, '待核父指令不存在')
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
and not item['payload'].get('amended')), key=lambda item: item['seq'])
evidence.append(validate_trade(row, order, parent, orders, logs, events, differences[code], trade_day))
return evidence
def apply(evidence, trade_day):
for item in evidence:
order, parent, payload = (item[key] for key in ('order', 'parent', 'trade'))
mapped = recon.map_trades_to_book([{'seq': item['trade_seq'], 'payload': payload}],
{parent['instruction_id']: parent['action']})
require(len(mapped['actions']) == 1 and not mapped['alerts'], '无法唯一映射原逐笔成交')
act = {**mapped['actions'][0], 'verified_trade_date': trade_day}
require(rotation_execution.book_trade(act, mapped['fees'], item['trade_seq'], late_buy_evidence=item),
'收件已变更,未重复补账')
require(qmt_repo.update_order(order['instruction_id'], cum_qty=act['qty'],
cum_avg_price=act['price'], leaves_qty=0), '未保存实成数量')
final = rotation_service.reconcile_books()
require(final['ok'] and final.get('source') == 'ws', '补记后仍存在数量差异,全部回滚')
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--day', required=True, type=date.fromisoformat)
parser.add_argument('--codes', required=True, nargs='+')
parser.add_argument('--apply-evidence-sha256')
args = parser.parse_args()
check_scope()
require(len(set(args.codes)) == len(args.codes), '股票范围不能重复')
if not args.apply_evidence_sha256:
evidence = plan(args.codes, args.day)
print(rotation_repo.encode({'sha256': digest(evidence), 'evidence': evidence}))
return
with rotation_repo.account_guard():
require(param_store.get_bool('PMS_GLOBAL_EXEC_HALT', False), '实际写入前必须暂停执行')
evidence = plan(args.codes, args.day)
require(digest(evidence) == args.apply_evidence_sha256, '现场证据与只读备份不同')
apply(evidence, args.day)
print(rotation_repo.encode({'applied': len(evidence), 'evidence_sha256': digest(evidence)}))
def digest(evidence):
return hashlib.sha256(rotation_repo.encode(evidence).encode()).hexdigest()
if __name__ == '__main__':
main()