"""策略轮换执行适配,共用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 enqueue_with_reservation(payload, parent_id): """所有策略共用一个账户锁;出口行与预算保留同一事务提交。""" from app.services import rotation_service as service 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('委托没有可核实的策略父指令') book = parent['book_id'] 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 and o['side'] != side 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(funds['received_at'] > o['final_at'] for o in terminal)) if settled and funds['seq'] > account['epoch_seq']: account.update(epoch_seq=funds['seq'], available=float(available), orders={}, market_value=float(parsed.get('market_value') or 0)) 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 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 if dispatcher.mode() != 'ws': raise ValueError('轮换自动执行只支持唯一WS通道,不使用shadow成交认领') parent = intent['id'] book = intent['book_id'] with books.scope(book), db.transaction(): 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': 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, 'deadline': now.date().isoformat(), 'is_command': True, 'children': []}) elif existing['status'] not in ('PROPOSED', 'DISPATCHED'): return {'instruction_id': parent, 'state': '已有终态记录,次日重新规划'} child = parent+'_D01' 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=intent['qty'], 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['children'] = [{'ref': child, 'qty': intent['qty'], 'limit': price, 'mode': 'ws', 'ymd': int(now.strftime('%Y%m%d')), 'at': now.strftime('%H:%M:%S'), 'valid_until': '10:00'}] pms_repo.update_instruction(parent, status='DISPATCHED', dispatch_ref=child, progress=progress) return {'instruction_id': parent, 'book_id': book, 'result': result} 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 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 {} 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']) qmt_repo.inbox_mark([seq], processed=qmt_repo.PROC_BOOKED, note='按策略归属原子入账') return True