"""成交更正的纯投影器。原账可完整重演时才返回修改计划,不访问数据库或发出动作。""" from copy import deepcopy from datetime import date, datetime, timezone, timedelta from decimal import Decimal, InvalidOperation, ROUND_HALF_UP import re from app.core.cushion import sell_allocation from app.core.recon import parent_instruction_id TZ = timezone(timedelta(hours=8)) PRICE_UNIT, MONEY_UNIT = Decimal('0.001'), Decimal('0.01') LOT_TYPES = {'BASE', 'FILL', 'ADD', 'DCA', 'T0'} LOT_FIELDS = ('qty', 'closed_qty', 'open_price', 'close_avg_price', 'realized_pnl', 'status') MAX_QTY = 2147483647 def _need(value, reason): if not value: raise ValueError(reason) def _decimal(value, label): _need(value is not None and not isinstance(value, bool), label+'缺失或格式错误') try: number = Decimal(str(value)) except (InvalidOperation, TypeError, ValueError): raise ValueError(label+'不是有效数字') from None _need(number.is_finite(), label+'必须为有限数字') return number def _quantize(value, unit): try: return value.quantize(unit, rounding=ROUND_HALF_UP) except InvalidOperation: raise ValueError('成交数字超出可核验精度') from None def _qty(value, label='成交数量'): number = _decimal(value, label) _need(0 <= number <= MAX_QTY and number == number.to_integral_value(), label+'须为非负整数') return int(number) def _identity(value, label): number = _decimal(value, label) _need(0 < number < 2 ** 63 and number == number.to_integral_value(), label+'须为正整数') return int(number) def _price(value): number = _decimal(value, '成交价格') _need(0 <= number < Decimal('10000000') and number == _quantize(number, PRICE_UNIT), '成交价格须非负且不超过三位小数') return number def _money(value): number = _quantize(value, MONEY_UNIT) _need(abs(number) < Decimal('1000000000000'), '成交金额超出账本范围') return number def _economic(payload): _need(isinstance(payload, dict), '成交字段须为对象') qty, price = _qty(payload.get('qty')), _price(payload.get('price')) amount = _decimal(payload.get('amount'), '成交金额') _need(amount >= 0 and amount == _money(amount), '成交金额须非负且不超过两位小数') _need(qty == 0 or price > 0, '非零成交数量必须有正价格') _need(amount == _money(price * qty), '成交数量、价格与金额不一致') return {'qty': qty, 'price': price, 'amount': amount} def _day(value, label): try: if isinstance(value, datetime): return _stamp(value).date() if isinstance(value, date): return value return date.fromisoformat(str(value)) except (TypeError, ValueError): raise ValueError(label+'缺失或格式错误') from None def _stamp(value): try: stamp = value if isinstance(value, datetime) else datetime.fromisoformat(str(value).replace('Z', '+00:00')) return stamp.replace(tzinfo=TZ) if stamp.tzinfo is None else stamp.astimezone(TZ) except (TypeError, ValueError): raise ValueError('入账时间缺失或格式错误') from None def _trade_day(row): raw = row['original_payload'].get('trade_time') if raw is None: # 只有服务层显式提供的真实交易日可替代原时间,不拿接收日猜交易日。 return _day(row.get('trade_day'), '真实交易日期') try: value = _decimal(raw, '成交时间') _need(value > 0 and value == value.to_integral_value(), '成交时间须为正毫秒整数') return datetime.fromtimestamp(int(value) / 1000, TZ).date() except (OverflowError, OSError, ValueError): raise ValueError('真实成交时间不可核验') from None def _code(value): value = str(value or '').strip().upper() _need(re.fullmatch(r'(?:60\d{4}\.SH|68\d{4}\.SH|00\d{4}\.SZ|30\d{4}\.SZ)', value), '更正范围须为沪深股票') return value def _parent(payload): value = payload.get('parent_id') or parent_instruction_id(payload.get('instruction_id')) _need(isinstance(value, str) and value, '成交缺少父指令归属') return value def _fields(lot): qty, closed = _qty(lot.get('qty'), '批次剩余数量'), _qty(lot.get('closed_qty'), '批次已平数量') close = lot.get('close_avg_price') close = _price(close) if close is not None else None pnl = _decimal(lot.get('realized_pnl'), '批次已实现盈亏') _need(pnl == _money(pnl), '批次盈亏金额精度不可核验') status = str(lot.get('status') or '') _need(status in ('OPEN', 'CLOSED'), '批次状态不可核验') return {'qty': qty, 'closed_qty': closed, 'open_price': _price(lot.get('open_price')), 'close_avg_price': close, 'realized_pnl': pnl, 'status': status} def _export(fields): out = dict(fields) for key in ('open_price', 'close_avg_price'): if out.get(key) is not None: out[key] = format(_quantize(out[key], PRICE_UNIT), 'f') if 'realized_pnl' in out: out['realized_pnl'] = format(_money(out['realized_pnl']), 'f') return out def _export_trade(value): return {'qty': value['qty'], 'price': format(_quantize(value['price'], PRICE_UNIT), 'f'), 'amount': format(_money(value['amount']), 'f')} def _prepare(lots, trades, lot_links): _need(isinstance(lots, list) and lots and isinstance(trades, list) and trades, '缺少完整批次或逐笔成交') codes, books, by_lot = set(), set(), {} for lot in lots: _need(isinstance(lot, dict), '批次格式错误') lid = _identity(lot.get('id'), '批次编号') _need(lid > 0 and lid not in by_lot, '批次编号缺失或重复') _need(lot.get('lot_type') in LOT_TYPES, '存在RECON或未知批次,不能自动更正') _need(lot.get('instruction_id'), '存在外部批次,不能自动更正') _need(not lot.get('archived_at'), '已归档批次不能自动更正') _fields(lot) _day(lot.get('open_date'), '批次开仓日期') codes.add(_code(lot.get('ts_code'))) if 'book_id' in lot: _need(lot['book_id'] in ('original', 'micro30'), '批次策略归属不可核验') books.add(lot['book_id']) by_lot[lid] = lot rows, seqs, numbers = [], set(), set() for source in trades: _need(isinstance(source, dict), '逐笔记录格式错误') row = deepcopy(source) payload = row.get('payload') original = row.get('original_payload', payload) _need(isinstance(payload, dict) and isinstance(original, dict), '缺少原始或有效成交') _economic(payload) _need(_economic(original)['qty'] > 0, '原始成交数量必须为正数') seq = _identity(row.get('seq'), '入账序号') no = payload.get('trade_no') _need(seq > 0 and seq not in seqs and isinstance(no, str) and no and no not in numbers, '成交序号或成交号缺失、重复') _need(payload.get('side') in ('buy', 'sell'), '成交方向不可核验') _need(all(payload.get(k) == original.get(k) for k in ('trade_no', 'ts_code', 'side', 'instruction_id', 'broker_order_id')) and _parent(payload) == _parent(original), '有效成交与原成交身份冲突') _need(not row.get('archived_at') and not payload.get('archived_at'), '已归档成交不能自动更正') codes.add(_code(payload.get('ts_code'))) for owner in (row, payload, original): if 'book_id' in owner: _need(owner['book_id'] in ('original', 'micro30'), '成交策略归属不可核验') books.add(owner['book_id']) seqs.add(seq); numbers.add(no) row.update(seq=seq, payload=payload, original_payload=original) rows.append(row) _need(len(codes) == 1, '更正输入混有不同证券') _need(len(books) <= 1 and books <= {'original', 'micro30'}, '更正输入混有不同或未知策略账本') links = dict(lot_links or {}) buy_numbers = {r['payload']['trade_no'] for r in rows if r['payload']['side'] == 'buy'} _need(set(links) <= buy_numbers, '批次映射包含不属于本次证据的成交') used, mapped = set(), {} for row in rows: current, original = row['payload'], row['original_payload'] if current['side'] != 'buy': continue no = current['trade_no'] if no in links: lid = _identity(links[no], '映射批次编号') candidates = [by_lot[lid]] if lid in by_lot else [] else: value = _economic(original) stamp = _stamp(row.get('processed_at')) candidates = [l for l in lots if l['instruction_id'] == _parent(original) and _qty(l['qty']) + _qty(l['closed_qty']) == value['qty'] and _price(l['open_price']) == value['price'] and _stamp(l.get('created_at')) == stamp] _need(len(candidates) == 1, '原始买入成交不能唯一映射批次') lot = candidates[0] lid = int(lot['id']) _need(lid not in used and lot['instruction_id'] == _parent(current), '成交批次重复使用或父指令不符') used.add(lid); mapped[no] = lid _need(used == set(by_lot), '批次缺少完整逐笔来源,不能假设零基线') return sorted(rows, key=lambda r: r['seq']), by_lot, mapped def _project(rows, by_lot, mapped): state = {lid: {'qty': 0, 'closed_qty': 0, 'open_price': Decimal(0), 'close_avg_price': None, 'realized_pnl': Decimal(0), 'status': 'CLOSED'} for lid in by_lot} allocations, t_profit = [], Decimal(0) for row in rows: p = row['payload']; value = _economic(p) qty, price = value['qty'], value['price'] if p['side'] == 'buy': state[mapped[p['trade_no']]].update(qty=qty, open_price=price, status='OPEN' if qty else 'CLOSED') _need(sum(l['qty'] for l in state.values()) <= MAX_QTY, '逐笔持仓数量超出账本范围') continue candidates = [{'lot_id': lid, 'lot_type': by_lot[lid]['lot_type'], 'qty': l['qty'], 'open_date': int(_day(by_lot[lid]['open_date'], '批次开仓日期').strftime('%Y%m%d'))} for lid, l in sorted(state.items(), key=lambda pair: (str(by_lot[pair[0]]['open_date']), pair[0])) if l['qty'] > 0] trade_t_profit = Decimal(0) for allocation in sell_allocation(candidates, qty): lid, take = allocation['lot_id'], allocation['qty'] lot = state[lid] old_pnl = lot['realized_pnl'] if by_lot[lid]['lot_type'] == 'T0': trade_t_profit += (price - lot['open_price']) * take closed = lot['closed_qty'] + take # MySQL逐笔UPDATE会把均价写入三位列、盈亏写入两位列,下一笔使用这些已落库值。 lot['close_avg_price'] = _quantize(((lot['close_avg_price'] or Decimal(0)) * lot['closed_qty'] + price * take) / closed, PRICE_UNIT) lot['realized_pnl'] = _money(old_pnl + (price - lot['open_price']) * take) lot.update(qty=lot['qty'] - take, closed_qty=closed) lot['status'] = 'OPEN' if lot['qty'] else 'CLOSED' allocations.append({'seq': row['seq'], 'trade_no': p['trade_no'], 'lot_id': lid, 'qty': take, 'price': format(_quantize(price, PRICE_UNIT), 'f'), 'realized_pnl_delta': format(lot['realized_pnl'] - old_pnl, '.2f')}) # 持仓T收益是在一笔卖出全部T批核销后合计写入两位列,与逐批盈亏分别舍入不同。 t_profit = _money(t_profit + trade_t_profit) _need(sum(l['qty'] for l in state.values()) <= MAX_QTY, '更正后持仓数量超出账本范围') return state, allocations, t_profit def _round_boundaries(rows): """记录清零后再次买入的真实仓次边界;零量更正也保留原买入身份。""" balance, bought = 0, False boundaries, before = [], {} for row in rows: p, seq = row['payload'], row['seq'] qty = _qty(p['qty']) before[seq] = balance if p['side'] == 'buy': if bought and balance == 0 and qty > 0: boundaries.append(seq) bought = True balance += qty else: balance -= qty return boundaries, before def compile_correction(*, lots, trades, target_trade_no, after, today, lot_links=None): """证明当前有效逐笔与批次一致,再替换一笔并重演;费用与策略资金由服务层另行核验。 trades中的payload为已应用既有更正后的有效成交,original_payload保留原始trade。 lot_links为{trade_no: lot_id};没有明确映射的旧数据须按原父指令、数量、价格、时间唯一配对。 原始trade_time为毫秒;缺失时只接受服务层显式提供的真实trade_day,不用接收时间猜测。 """ rows, by_lot, mapped = _prepare(lots, trades, lot_links) current, allocations_before, t_before = _project(rows, by_lot, mapped) for lid, lot in by_lot.items(): _need(_fields(lot) == current[lid], '当前批次与有效逐笔重演不一致,拒绝更正') targets = [r for r in rows if r['payload']['trade_no'] == target_trade_no] _need(len(targets) == 1, '目标成交缺失或不唯一') target = targets[0] boundaries, _ = _round_boundaries(rows) _need(not any(target['seq'] < boundary for boundary in boundaries), '目标成交之后已清零并建立新仓,旧轮次更正须人工核对,不能回灌当前仓次') _need(isinstance(after, dict) and set(after) == {'qty', 'price', 'amount'}, '更正仅接受数量、价格和金额,不能修改成交身份或费用') old, new = _economic(target['payload']), _economic(after) current_day, actual_day = _day(today, '核验日期'), _trade_day(target) _need(actual_day <= current_day, '成交日期在未来,拒绝更正') quantity_delta = new['qty'] - old['qty'] if quantity_delta: book_day = (_day(by_lot[mapped[target_trade_no]]['open_date'], '批次开仓日期') if target['payload']['side'] == 'buy' else _day(target['book_day'], '原入账日期') if target.get('book_day') else _stamp(target.get('processed_at')).date()) _need(book_day == actual_day, '原成交跨日入账,数量更正的T+1基线待核实') target['payload'].update(after) projected, allocations, t_after = _project(rows, by_lot, mapped) _, revised_before = _round_boundaries(rows) _need(all(revised_before[boundary] == 0 for boundary in boundaries), '更正会使旧清零边界带库存进入新仓,须人工核对') changed, exported = [], [] for lid in sorted(by_lot): old_fields, new_fields = _export(current[lid]), _export(projected[lid]) exported.append(dict(deepcopy(by_lot[lid]), **new_fields)) if old_fields != new_fields: changed.append({'id': lid, 'before': old_fields, 'after': new_fields}) def net(state): return sum((l['qty'] * l['open_price'] - l['realized_pnl'] for l in state.values()), Decimal(0)) side = target['payload']['side'] avail_delta = -quantity_delta if side == 'sell' else quantity_delta if actual_day < current_day else 0 cash_delta = (new['amount'] - old['amount']) * (1 if side == 'sell' else -1) return {'lots': exported, 'changes': changed, 'lot_links': mapped, 'allocations': allocations, 'allocations_before': allocations_before, 'qty_before': sum(l['qty'] for l in current.values()), 'qty_after': sum(l['qty'] for l in projected.values()), 'amount_before': format(old['amount'], '.2f'), 'amount_after': format(new['amount'], '.2f'), 'cash_delta': format(cash_delta, '.2f'), 'avail_delta': avail_delta, 't_profit_delta': format(t_after - t_before, '.2f'), 't_profit_before': format(t_before, '.2f'), 't_profit_after': format(t_after, '.2f'), 'net_cost_before': format(net(current), 'f'), 'net_cost_after': format(net(projected), 'f'), 'trade_before': _export_trade(old), 'trade_after': _export_trade(new)}