tradingSystem/app/services/rotation_execution.py

385 lines
23 KiB
Python
Raw Normal View History

"""策略轮换执行适配,共用PMS出口和账户资金串行保护。"""
import datetime as dt
from app.core import strategy_book as books, micro_rotation as core, ws_codec
from app.db import session as db
from app.repo import pms_repo, qmt_repo, rotation_repo as store
from app.services import param_store
from config.settings import settings
TERMINAL = qmt_repo.FINAL
def _later_snapshot(received_at, final_at):
from app.core.execution_policy import local_now
try:
received = received_at if isinstance(received_at, dt.datetime) else dt.datetime.fromisoformat(str(received_at))
final = final_at if isinstance(final_at, dt.datetime) else dt.datetime.fromisoformat(str(final_at))
return local_now(received) > local_now(final)
except (TypeError, ValueError):
return False
def release_settled_reservations(account, funds):
"""逐单释放已确认撤回的未成交金额,不重复扣QMT冻结、不预支卖出收入。"""
from app.services import execution_orders
from app.repo import execution_repo
if execution_repo.pending_corrections():
return
initial = account.setdefault('original_reservations', {})
for iid, held in list(account['orders'].items()):
initial.setdefault(iid, float(held))
order = qmt_repo.get_order(iid)
if not order or order['status'] not in TERMINAL or not _later_snapshot(funds.get('received_at'), execution_orders.settlement_cutoff(order)):
continue
settled = execution_orders.replacement_state(order['parent_id'])
if settled['pending_trades'] or settled['booked'] < settled['confirmed_filled'] or settled['missing_orders']:
continue
qty, done, price = int(order['qty']), execution_orders.effective_filled(order), float(order['limit_price'])
original = initial[iid]
if qty <= 0 or not 0 <= done <= qty or price <= 0 or original+1e-6 < qty*price:
raise ValueError('委托原始资金保留不可核实')
# 成交价不会高于买入限价。已成部分保留原限价本金和整笔原费用,避免低估占用。
retained = min(original, done*price+max(0., original-qty*price)) if done else 0.
account['orders'][iid] = retained
def _reservation_fills_booked(orders):
from app.services import execution_orders
from app.repo import execution_repo
if execution_repo.pending_corrections():
return False
for order in orders:
state = execution_orders.replacement_state(order['parent_id'])
if state['pending_trades'] or state['booked'] < state['confirmed_filled'] or state['missing_orders']:
return False
return True
def enqueue_with_reservation(payload, parent_id):
"""所有策略共用一个账户锁;出口行与预算保留同一事务提交。"""
from app.services import rotation_service as service, execution_orders
with store.locked('account-execution', 'funds_reservation', {'epoch_seq': 0, 'available': 0., 'orders': {}}) as account:
code, side, qty, price = payload['ts_code'], payload['side'], int(payload['qty']), float(payload['limit_price'])
old = qmt_repo.get_order(payload['instruction_id'])
if old:
if any(old[k] != v for k, v in [('parent_id', parent_id), ('ts_code', code), ('side', side), ('qty', qty)]) or abs(float(old['limit_price'])-price) > 1e-8:
raise ValueError('委托幂等键与内容冲突')
return
if param_store.get_bool('PMS_GLOBAL_EXEC_HALT', False):
raise ValueError('用户暂停全部执行')
with books.scope('all'):
parent = pms_repo.get_instruction(parent_id)
if not parent:
raise ValueError('委托没有可核实的策略父指令')
blocked = execution_orders.correction_blocker(parent, ts_code=code, side=side)
if blocked:
raise ValueError(blocked)
book = parent['book_id']
if side == 'buy':
from app.services.buy_budget import check_order
with books.scope(book):
check_order(parent, payload)
if side == 'buy':
if param_store.get_bool('PMS_GLOBAL_BUY_HALT', False):
raise ValueError('用户暂停买入')
reason = service.strategy_buy_blocker(book, parent)
if reason:
raise ValueError(reason)
# 同名股票全账户互斥。最多读取10001行,达到上限拒绝继续,避免截断漏检查。
active = db.fetch_all("SELECT * FROM pms_qmt_order WHERE status NOT IN ('FILLED','CANCELLED','REJECTED','EXPIRED','SEND_FAILED','ABORTED') ORDER BY id LIMIT 10001")
if len(active) > 10000:
raise ValueError('在途委托超出核验上限')
if any(o['ts_code'] == code for o in active):
raise ValueError('同名股票仍有在途委托,等待终态及成交入账')
if side == 'sell':
with books.scope(book):
position = pms_repo.get_position(code) or {}
booked = sum(int(o['qty'])-int(o.get('cum_qty') or 0) for o in active if o['ts_code'] == code and o['side'] == 'sell')
from app.services.ledger_service import positions_source
source = positions_source()
physical = next((r for r in source['rows'] if r['ts_code'] == code), {})
if source['source'] != 'ws':
raise ValueError('实际可卖量需要新鲜QMT快照')
physical_available = int(physical.get('available_qty', physical.get('avail_qty', 0)) or 0)
# 已受理卖单通常已从QMT可卖量扣除。只额外预留尚未发出的数量。
unsent = sum(int(o['qty'])-int(o.get('cum_qty') or 0) for o in active if o['ts_code'] == code and o['side'] == 'sell' and o['status'] in ('QUEUED','SENDING','SENT'))
available = min(int(position.get('avail_qty') or 0)-booked, physical_available-unsent)
if qty > max(0, available):
raise ValueError('账户或策略实际可卖数量不足')
if qty != int(position.get('total_qty') or 0) and (qty < core.min_qty(code) or qty % 100):
raise ValueError('部分卖出数量不符合交易单位')
else:
with books.scope(books.MICRO if book == books.ORIGINAL else books.ORIGINAL):
other = pms_repo.get_position(code) or {}
if int(other.get('total_qty') or 0) > 0:
raise ValueError('同名股票旧策略持仓尚未卖清')
if qty < core.min_qty(code) or qty % 100:
raise ValueError('买入数量不符合交易单位')
funds = qmt_repo.latest_snapshot('funds')
if not funds or funds.get('age_sec') is None or not 0 <= funds['age_sec'] <= 60:
raise ValueError('账户资金快照超过一分钟或不可用')
parsed = ws_codec.parse_funds_snapshot(funds['payload'])
available = parsed.get('available_cash')
if available is None:
raise ValueError('账户可用现金不可核实')
reserved = account['orders']
terminal = []
for iid in reserved:
order = qmt_repo.get_order(iid)
if order and order['status'] in TERMINAL and order.get('final_at'):
terminal.append(order)
# 全部委托终态,且新的资金快照晚于全部终态,才开启新预算周期。
settled = not reserved or (len(terminal) == len(reserved)
and all(_later_snapshot(funds['received_at'], execution_orders.settlement_cutoff(o)) for o in terminal)
and _reservation_fills_booked(terminal))
if settled and funds['seq'] > account['epoch_seq']:
account.update(epoch_seq=funds['seq'], available=float(available), orders={}, original_reservations={}, market_value=float(parsed.get('market_value') or 0))
else:
release_settled_reservations(account, funds)
costs = service.configuration()['costs']
demand = qty*price+core.fee(qty*price, 'buy', **costs)
remaining = account['available']-sum(account['orders'].values())
reserve_cash = param_store.get_float('PMS_TOTAL_SCALE', 0)*param_store.get_float('PMS_CASH_RESERVE', 0)
cash_limit = min(float(available), remaining)-reserve_cash
# 不另扣QMT冻结现金,不额外增加卖出回笼。
scale = param_store.get_float('PMS_TOTAL_SCALE', 0)
cap = scale*param_store.get_float('PMS_PORTFOLIO_CAP', 0)
mv = parsed.get('market_value')
if mv is None:
mv = parsed.get('total_market_value')
if mv is None:
raise ValueError('账户总市值不可核实')
# 委托未终态前保守保留本预算周期新增市值;不借旧行情绕过全账户上限。
cap_remaining = min(cap-float(mv), cap-float(account.get('market_value', mv))-sum(account['orders'].values()))
if demand > cash_limit+1e-8 or qty*price > cap_remaining+1e-8:
raise ValueError('账户可用现金或全账户仓位预算不足')
from app.services import rotation_accounting
live, _ = store.get('state:live')
phase = (live or {}).get('rotation', {}).get('phase', 'original')
rotation_accounting.reserve_book(book, demand, active, phase)
account['orders'][payload['instruction_id']] = demand
account.setdefault('original_reservations', {})[payload['instruction_id']] = demand
qmt_repo.enqueue_order(instruction_id=payload['instruction_id'], parent_id=parent_id,
ts_code=code, side=side, qty=qty, limit_price=price, valid_until=payload['valid_until'],
intent=payload['intent'], note=payload.get('note'))
def send_intent(intent, price, now, snapshot_version):
from app.services import dispatcher, execution_orders
if dispatcher.mode() != 'ws':
raise ValueError('轮换自动执行只支持唯一WS通道,不使用shadow成交认领')
parent = intent['id']
book = intent['book_id']
with books.scope(book), db.transaction():
blocker = execution_orders.code_settlement_blocker(intent['ts_code'])
if blocker:
raise ValueError(blocker)
existing = pms_repo.get_instruction(parent)
if not existing:
command = 'ROT_CMD_'+book+'_'+now.strftime('%Y%m%d')
if not pms_repo.get_command(command):
pms_repo.insert_command(command_id=command, cmd_class='task', cmd_type='ROTATE_MICRO',
ts_code=None, params={'rule_version': intent.get('rule_version', core.RULE), 'snapshot_version': snapshot_version},
status='EXECUTING', issued_by='rotation', note='微盘与原策略账户轮换')
plan = 'P_'+parent
action = 'OPEN' if intent['side'] == 'buy' else 'EXIT'
pms_repo.insert_plans([{'plan_id': plan, 'command_id': command, 'ts_code': intent['ts_code'],
'action': action, 'qty': intent['qty'], 'amount': intent['qty']*price,
'deadline': now.date(), 'status': 'EXECUTING', 'reason': intent['reason']}])
pms_repo.insert_instruction(instruction_id=parent, origin_type='plan', origin_id=plan,
ts_code=intent['ts_code'], action=action, side=intent['side'], qty=intent['qty'],
window_tdays=1, progress={'execution_profile': 'rotation', 'snapshot_version': snapshot_version,
'rule_version': intent.get('rule_version', core.RULE),
'deadline': now.date().isoformat(), 'is_command': True, 'children': []})
elif existing['status'] not in ('PROPOSED', 'DISPATCHED'):
return {'instruction_id': parent, 'state': '已有终态记录,次日重新规划'}
elif (existing.get('progress') or {}).get('cancel_requested'):
return {'instruction_id': parent, 'state': '父指令正在撤销,停止重挂', 'pending': True}
settled = execution_orders.replacement_state(parent)
if not settled.get('ready'):
return {'instruction_id': parent, 'state': '旧委托或成交尚未结清', 'pending': True}
quantity = min(int(intent['qty']), int(settled.get('remaining', intent['qty'])))
if quantity <= 0:
return {'instruction_id': parent, 'state': '目标数量已完成'}
retry_reason = execution_orders.retry_reason(parent, now=now)
if retry_reason:
return {'instruction_id': parent, 'state': retry_reason, 'pending': True}
child = execution_orders.next_child_id(parent)
result = dispatcher.dispatch(instruction_id=child, parent_id=parent, intent='OPEN' if intent['side'] == 'buy' else 'EXIT',
ts_code=intent['ts_code'], side=intent['side'], qty=quantity, limit_price=price,
valid_until=(now.replace(hour=10, minute=0, second=0, microsecond=0) if now.hour < 10 else min(now+dt.timedelta(minutes=10), now.replace(hour=14, minute=57, second=0, microsecond=0))), note=intent['reason'])
if not result.get('ok'):
raise ValueError('轮换委托未下发:'+str(result.get('error')))
progress = (pms_repo.get_instruction(parent) or {}).get('progress', {})
progress.setdefault('children', []).append({'ref': child, 'qty': quantity, 'limit': price,
'mode': 'ws', 'ymd': int(now.strftime('%Y%m%d')), 'at': now.strftime('%H:%M:%S'), 'valid_until': '10:00'})
progress['quote'] = intent.get('quote_basis')
pms_repo.update_instruction(parent, status='DISPATCHED', dispatch_ref=child, progress=progress)
return {'instruction_id': parent, 'book_id': book, 'result': result}
def pending_instructions():
"""父记录与真实出口合并,父单本地结束不能隐去仍活动的子委托。"""
with books.scope('all'):
parents = pms_repo.list_instructions(statuses=('PROPOSED', 'RULE_PASSED', 'JUDGE_PASSED', 'DISPATCHED', 'WAIT_REVIEW'), limit=10000)
by_id = {p['instruction_id']: p for p in parents}
children = qmt_repo.list_orders(statuses=qmt_repo.LIVE, limit=10001)
if len(children) > 10000:
raise ValueError('在途委托超出核验上限')
for child in children:
pid = child['parent_id']
if pid not in by_id:
parent = pms_repo.get_instruction(pid)
if not parent:
raise ValueError('活动子委托的策略归属无法核实')
by_id[pid] = parent
return list(by_id.values())
def suspended_rejections(now):
"""只消费当日已落库的精确停牌拒绝码,不凭行情缺失或柜台错误文字递补。"""
from app.services import execution_orders
day = now.date()
rows = db.fetch_all("SELECT * FROM pms_qmt_order WHERE status='REJECTED' "
"AND reject_code='SUSPENDED' AND side='buy' AND final_at>=:start AND final_at<:end "
"ORDER BY id DESC LIMIT 10001", {'start': day, 'end': day+dt.timedelta(days=1)})
if len(rows) > 10000:
raise ValueError('当日停牌拒单超过核验上限')
result = {}
with books.scope('all'):
for order in rows:
if order.get('reject_code') != 'SUSPENDED' or order.get('status') != 'REJECTED' or int(order.get('cum_qty') or 0):
continue
parent = pms_repo.get_instruction(order['parent_id'])
progress = (parent or {}).get('progress') or {}
if not parent or parent.get('book_id') != books.MICRO or int(parent.get('exec_qty') or 0) or progress.get('execution_profile') != 'rotation' or progress.get('rule_version') != core.EXECUTION_RULE or str(progress.get('deadline')) != day.isoformat():
continue
settled = execution_orders.replacement_state(parent['instruction_id'])
if settled['ready'] and settled['confirmed_filled'] == 0:
result[order['ts_code']] = 'QMT已确认停牌拒单SUSPENDED,委托'+order['instruction_id']
return result
def refresh_orders(parents, quotes, now, snapshot_version, target, unavailable):
"""v3只调整已授权剩余量。撤改先等旧子单终态,个股等待不阻塞整篮。"""
from app.services import execution_orders, executor
results, waits = [], []
for parent in parents:
progress = parent.get('progress') or {}
if progress.get('execution_profile') != 'rotation' or progress.get('rule_version') != core.EXECUTION_RULE:
continue
code, side = parent['ts_code'], parent['side']
if str(progress.get('deadline')) != now.date().isoformat():
continue
with books.scope(parent['book_id']):
if side == 'buy' and (code in unavailable or code not in target):
result = executor.cancel_instruction(parent['instruction_id'], reason='微盘成员不可交易或已调出', arbiter='system')
waits.append(code+'撤单确认后处理剩余席位')
if not result.get('ok'):
waits.append(code+'撤单请求待重试')
continue
if progress.get('cancel_requested'):
continue
children = qmt_repo.orders_for_parent(parent['instruction_id'])
live = [c for c in children if c['status'] not in TERMINAL]
quote = quotes.get(code, {})
priced = execution_orders.quote_order(side, quote, now)
if priced.get('action') != 'FIRE':
waits.append(code+str(priced.get('reason') or '执行报价不可用'))
if live:
try:
with db.savepoint():
execution_orders.request_replace(parent['instruction_id'], '行情不可核实,撤清后等待恢复', now=now)
except Exception as exc:
waits.append(code+'缺行情撤单待重试:'+type(exc).__name__)
continue
if live:
# 仍可按现价成交的限价不反复撤掉,保留原排队位置。
changed = any((side == 'buy' and float(c['limit_price']) < priced['limit_price']) or
(side == 'sell' and float(c['limit_price']) > priced['limit_price']) for c in live)
if not changed or not execution_orders.should_replace(parent, priced, now=now):
continue
try:
with db.savepoint():
execution_orders.request_replace(parent['instruction_id'], '微盘限价随有效行情更新', now=now)
except Exception as exc:
waits.append(code+'改价撤单待重试:'+type(exc).__name__)
continue
waits.append(code+'改价撤单待终态')
continue
status = execution_orders.replacement_state(parent['instruction_id'])
if not status.get('ready') or int(status.get('remaining') or 0) <= 0:
continue
intent = {'id': parent['instruction_id'], 'book_id': parent['book_id'], 'ts_code': code,
'side': side, 'qty': int(parent['qty']), 'reason': '微盘剩余数量继续执行',
'rule_version': core.EXECUTION_RULE, 'quote_basis': {**quote, **priced}}
try:
with db.savepoint():
results.append(send_intent(intent, priced['limit_price'], now, snapshot_version))
except Exception as exc:
waits.append(code+'重挂待重试:'+type(exc).__name__)
return results, waits
def cancel_expired(now):
from app.services import executor
with books.scope('all'):
rows = pms_repo.list_instructions(statuses=('PROPOSED', 'DISPATCHED'), limit=10000)
for row in rows:
p = row.get('progress') or {}
if p.get('execution_profile') != 'rotation':
continue
deadline = str(p.get('deadline') or '')
if deadline < now.date().isoformat() or (deadline == now.date().isoformat() and now.hour >= 10):
with books.scope(row['book_id']):
result = executor.cancel_instruction(row['instruction_id'], reason='轮换当日执行窗口结束', arbiter='system')
if not result.get('ok'):
raise RuntimeError('轮换委托撤销未确认')
def book_trade(act, fees, seq):
"""策略归属、批次、费用、指令进度和inbox标记同成同败。"""
from app.services import ledger_service as ledger
from app.services import trade_lineage
parent = act.get('instruction_id')
if not parent:
raise ValueError('逐笔成交缺少父指令归属')
with store.account_guard(), db.transaction():
inbox = db.fetch_one('SELECT processed FROM pms_qmt_inbox WHERE seq=:seq FOR UPDATE', {'seq': seq})
if not inbox or inbox['processed'] != 0:
return False
with books.scope('all'):
instruction = pms_repo.get_instruction(parent)
if not instruction or instruction.get('book_id') not in (books.ORIGINAL, books.MICRO):
raise ValueError('父指令策略归属不可核实')
if instruction['ts_code'] != act['ts_code'] or instruction['side'] != ('buy' if act['kind'] == 'BUY' else 'sell'):
raise ValueError('成交证券或方向与父指令冲突')
if int(instruction['exec_qty'])+int(act['qty']) > int(instruction['qty']):
raise ValueError('成交超过父指令剩余数量,等待核对')
with books.scope(instruction['book_id']):
before = pms_repo.get_position(act['ts_code']) or {}
lots_before = trade_lineage.lots(act['ts_code'])
qty = int(act['qty'])
if act['kind'] != 'BUY' and qty > int(before.get('total_qty') or 0):
raise ValueError('卖出成交超出所属策略持仓,等待核对')
ledger._apply_action(act)
after = pms_repo.get_position(act['ts_code']) or {}
delta = qty if act['kind'] == 'BUY' else -qty
if int(after.get('total_qty') or 0) != int(before.get('total_qty') or 0)+delta:
raise ValueError('成交数量未完整入账')
fresh = pms_repo.get_instruction(parent)
if int(fresh['exec_qty']) != int(instruction['exec_qty'])+qty:
raise ValueError('指令成交进度未原子更新')
for f in fees:
pms_repo.insert_cash_flow(ymd=int(dt.datetime.now().strftime('%Y%m%d')), kind='FEE',
amount=f['amount'], ts_code=f['ts_code'], estimated=f['estimated'],
trade_no=f['trade_no'], instruction_id=f['instruction_id'], note=f['note'])
from app.services.rotation_accounting import record_fill
record_fill(instruction['book_id'], 'buy' if act['kind'] == 'BUY' else 'sell',
qty*float(act['price']), -sum(float(f['amount']) for f in fees), str(seq))
ledger.recompute_position(act['ts_code'])
trade_lineage.record(act, instruction, seq, lots_before, trade_lineage.lots(act['ts_code']))
if not qmt_repo.inbox_mark([seq], processed=qmt_repo.PROC_BOOKED, note='按策略归属原子入账'):
raise RuntimeError('逐笔成交入账标记未写入')
return True