实现异步研判和限价撤换及微盘递补
This commit is contained in:
parent
b798c1f38e
commit
9bc94eb4b3
|
|
@ -327,3 +327,16 @@ docker compose run --rm --no-deps pms-web python scripts/probe_bionic.py --base
|
|||
- `reanchored`(是否做了除权重锚)、`in_pool`(是否在夜间分析池)、`quality`(数据质量,OK 才用)、`bars_used`、`last_bar_date`、`bars_lag`。
|
||||
|
||||
**持仓管理系统侧怎么用。** 存进 pms_tech_daily 保留 40 个交易日;相位合成与五个阈值见该系统台账 006。四种无读数情形(没有当天行、读数超过两个交易日、未除权重锚、数据质量非 OK)一律弃权,绝不折成看空。本接口不改变研判请求体,研判闸的请求一字不变。
|
||||
|
||||
|
||||
## 2026年11月3日:异步研判与执行依据扩展
|
||||
|
||||
旧接口和字段保留。新接口必须先在bionic发布,PMS才能打开`PMS_JUDGE_ASYNC_ENABLED`及`PMS_EXEC_REPRICE_ENABLED`。
|
||||
|
||||
- 提交:`POST /api/intraday/pms_judge_submit`。原请求追加`request_id`、带时区的`deadline_at`和可选的`evidence_fingerprint`。
|
||||
- 查询:`GET /api/intraday/pms_judge_result/{request_id}`。返回`PENDING/PASS/REJECT/UNAVAILABLE`,携带请求编号、证据指纹、截止时间、缓存标记及理由。请求编号不可绑定第二份证据。PENDING不表示授权,也不立即建人工提议。
|
||||
- PMS先持久化请求再登记。一次网络请求默认3秒,含排队的总截止默认120秒。短暂传输故障在截止前继续查原编号;超时转既有人工确认路径,同证据失败不自动重新投模。成功结果短期复用前仍过当前规则闸。
|
||||
- `pms_exec.execution_basis`含`version/quality/continuation/reason/generated_at/valid_until/source_date/source`。`CONTINUE`仅说明已有授权机会未撤销,不构成首次准入。`STOP`表示新增买入机会明确撤销,`UNKNOWN`表示缺乏有效事实。已授权卖出不以看空或离场结论作为停止卖出的理由。缺字段、无时区和过期均不能继续授权报价。
|
||||
- 执行依据刷新是独立短任务,已挂委托也检查失效。报价不等待模型重新算区间;每次报价记录实际行情来源及时间。当前确认可用源为分钟行情,盘口和停牌状态未知时不得编造。
|
||||
|
||||
完整状态及验收边界见[实施规格](docs/执行报价与异步研判实施规格_2026-11-03.md)。
|
||||
|
|
|
|||
|
|
@ -2612,3 +2612,10 @@ QMT11:53:36至11:54:04短暂中断,自动重连恢复,外部原因未核实
|
|||
新增第7图,固定热度0.80及每边30基点,复用已有结果展示1至10日:单日完整41日样本、单日共同池24日和历史至少5次共同池24日。单日41日样本H1/H5/H10平均每批净收益分别为−0.88%/−1.40%/−3.40%;分别标明时间窗口,不将样本变化归因于频次。前六张PNG逐字节不变。保留原图集文件路径,更新开发入口。
|
||||
|
||||
开发机macOS核验时间2026-10-30T13:24:29.443371+08:00。全部436行绘图数据、源摘要、引用与Python语法通过;独立Chrome验证7图加载、新旧目录跳转、明细展开及430像素无页面横向溢出,外部请求和页面错误均为0;新增图与说明页面已目视。当前状态见research/strategy_chart_review_20261030/manifest.json。本次补充已完成,没有新回测、数据库访问、参数修改或部署;原有114行DEVLOG和10行复盘台账保持未暂存。
|
||||
|
||||
|
||||
## 2026-11-03 执行报价与异步研判实现
|
||||
|
||||
用户确认实施。使用独立codex工作树,保留原工作树未提交文件。新增异步研判持久化、共同撤单确认后重挂、原始执行依据追溯及微盘v3冻结备选。取消、过期和部分成交均按实际出口回报及入账收尾;逐单释放已核实的未成交占用。已授权卖出不因看空依据被反向阻断。
|
||||
|
||||
实现与启用条件见docs/执行报价与异步研判实施规格_2026-11-03.md。首次本地全量发现一处旧取消测试预期,与新pending/confirmed语义不符,已交专项修正。后续将合并155已发布源并重新全量验证。当前没有新功能启用、没有测试下单,也未操作188真实PMS。
|
||||
|
|
|
|||
|
|
@ -499,3 +499,10 @@ docker compose --profile ws up -d pms-ws && docker compose logs -f pms-ws
|
|||
## 开发约定
|
||||
|
||||
工作流程和授权边界统一维护在[AGENTS.md](AGENTS.md),技术限制在[后端目录规则](app/AGENTS.md)及相应子目录中。部署、QMT联调和测试按[开发入口](docs/开发入口.md)选择资料,不在本节重复维护,也不再要求每次通读三份设计文档。
|
||||
|
||||
|
||||
### 执行报价与异步研判(2026年11月3日)
|
||||
|
||||
新增可独立开启的异步研判和限价撤换流程。bionic负责机会判断,PMS按新鲜行情报价。撤单确认、累计成交入账之前,旧单继续占用数量和资金。微盘v3将首选与有序备选一并冻结;单股受阻不会中止其他成员。旧v2快照与规则保持兼容。
|
||||
|
||||
新开关默认关闭,运行值通过ParamStore读取。接口、开启条件和验证记录见[实施规格](docs/执行报价与异步研判实施规格_2026-11-03.md)。没有实时盘口时明确使用分钟行情降级,不承诺即时成交。
|
||||
|
|
|
|||
|
|
@ -0,0 +1,111 @@
|
|||
"""执行依据与报价的纯规则。未知事实不能授权买入,不推测盘口。"""
|
||||
from datetime import datetime, timezone, timedelta, date
|
||||
from decimal import Decimal, ROUND_CEILING, ROUND_FLOOR
|
||||
import math
|
||||
import re
|
||||
|
||||
BEIJING = timezone(timedelta(hours=8))
|
||||
|
||||
|
||||
def supported_code(code):
|
||||
return bool(re.fullmatch(r'(?:60\d{4}\.SH|68\d{4}\.SH|00\d{4}\.SZ|30\d{4}\.SZ)', str(code or '').upper()))
|
||||
|
||||
|
||||
def local_now(now=None):
|
||||
value = now or datetime.now(BEIJING)
|
||||
return value.replace(tzinfo=BEIJING) if value.tzinfo is None else value.astimezone(BEIJING)
|
||||
|
||||
|
||||
def timestamp(value):
|
||||
try:
|
||||
parsed = datetime.fromisoformat(str(value).replace('Z', '+00:00'))
|
||||
return parsed.astimezone(BEIJING) if parsed.tzinfo is not None else None
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
|
||||
|
||||
def positive(value):
|
||||
try:
|
||||
number = float(value)
|
||||
return number if math.isfinite(number) and number > 0 else 0.
|
||||
except (TypeError, ValueError):
|
||||
return 0.
|
||||
|
||||
|
||||
def basis_reason(basis, now=None, max_source_days=7):
|
||||
"""空串表示依据有效;拒绝无来源、无版本、无时区和过期事实。"""
|
||||
if not isinstance(basis, dict):
|
||||
return '执行依据缺失'
|
||||
if basis.get('continuation') == 'STOP':
|
||||
return '机会已撤销:' + str(basis.get('reason') or '上游明确停止')
|
||||
if basis.get('quality') != 'VALID' or basis.get('continuation') != 'CONTINUE':
|
||||
return '执行依据不可用或机会状态未知:' + str(basis.get('reason') or '')
|
||||
if not str(basis.get('version') or '').strip() or not str(basis.get('source') or '').strip():
|
||||
return '执行依据缺少版本或来源'
|
||||
generated, until = timestamp(basis.get('generated_at')), timestamp(basis.get('valid_until'))
|
||||
current = local_now(now)
|
||||
if not generated or not until or not generated <= current < until:
|
||||
return '执行依据时间无效或已过期'
|
||||
try:
|
||||
source_day = date.fromisoformat(str(basis.get('source_date')))
|
||||
except (ValueError, TypeError):
|
||||
return '执行依据缺少有效数据日期'
|
||||
if source_day > generated.date() or not 0 <= (current.date()-source_day).days <= max_source_days:
|
||||
return '执行依据数据日期超出有效范围'
|
||||
return ''
|
||||
|
||||
|
||||
def price_order(*, side, quote, now=None, user_limit=None, anchor_price=None, params=None):
|
||||
"""按对手盘报价;分钟行情只能显式降级,并始终遵守用户价格边界。"""
|
||||
prm, current = params or {}, local_now(now)
|
||||
def wait(why):
|
||||
return {'action': 'WAIT', 'limit_price': None, 'reason': why, 'degraded': False}
|
||||
if side not in ('buy', 'sell') or not isinstance(quote, dict):
|
||||
return wait('委托方向或行情无效')
|
||||
if quote.get('halted'):
|
||||
return wait('证券停牌,不报价')
|
||||
kind, source = quote.get('kind'), str(quote.get('source') or '')
|
||||
at = timestamp(quote.get('as_of'))
|
||||
if not source or not at or at.date() != current.date():
|
||||
return wait('行情缺少当日来源或明确时间')
|
||||
age = (current-at).total_seconds()
|
||||
minute = kind == 'minute'
|
||||
max_age = float(prm.get('minute_max_age_sec', 90) if minute else prm.get('quote_max_age_sec', 15))
|
||||
if age < 0 or age > max_age:
|
||||
return wait('行情已过期或时间超前')
|
||||
low, high, last = (positive(quote.get(k)) for k in ('limit_down', 'limit_up', 'last'))
|
||||
if not low or not high or low > high or not last:
|
||||
return wait('行情价格或当日价格上下限不可核实')
|
||||
tick = .01
|
||||
if minute:
|
||||
if not prm.get('allow_minute', True):
|
||||
return wait('没有实时盘口,分钟行情降级未启用')
|
||||
offset = max(0., float(prm.get('minute_slippage_pct', .002)))
|
||||
proposed = last*(1+offset if side == 'buy' else 1-offset)
|
||||
note = '分钟行情降级报价(无实时盘口)'
|
||||
elif kind == 'level1':
|
||||
counter = positive(quote.get('ask' if side == 'buy' else 'bid'))
|
||||
bid, ask = positive(quote.get('bid')), positive(quote.get('ask'))
|
||||
if not counter or (bid and ask and bid > ask):
|
||||
return wait('对手盘口缺失或买卖价异常')
|
||||
proposed = counter + (1 if side == 'buy' else -1)*tick*max(0, int(prm.get('offset_ticks', 1)))
|
||||
note = '实时对手盘口报价'
|
||||
else:
|
||||
return wait('行情种类未经核实')
|
||||
anchor = positive(anchor_price) or last
|
||||
slippage = max(0., float(prm.get('max_slippage_pct', .01)))
|
||||
boundary = positive(user_limit)
|
||||
if user_limit is not None and not boundary:
|
||||
return wait('用户价格边界无效')
|
||||
if side == 'buy':
|
||||
ceiling = min(high, last*(1+slippage), boundary or high)
|
||||
price = Decimal(str(min(proposed, ceiling))).quantize(Decimal('.01'), rounding=ROUND_FLOOR)
|
||||
else:
|
||||
floor = max(low, last*(1-slippage), boundary or low)
|
||||
price = Decimal(str(max(proposed, floor))).quantize(Decimal('.01'), rounding=ROUND_CEILING)
|
||||
value = float(price)
|
||||
if not low <= value <= high:
|
||||
return wait('用户边界或报价超出当日合法价格范围')
|
||||
return {'action': 'FIRE', 'limit_price': value, 'reason': note,
|
||||
'degraded': minute, 'quote_source': source, 'quote_as_of': at.isoformat(),
|
||||
'quote_age_sec': age, 'anchor_price': anchor, 'user_limit': boundary or None}
|
||||
|
|
@ -4,9 +4,12 @@ import datetime as dt
|
|||
import hashlib
|
||||
import json
|
||||
import math
|
||||
import re
|
||||
|
||||
LEGACY_RULE = 'micro30-v1-20261027'
|
||||
RULE = 'micro50-v2-20261028'
|
||||
EXECUTION_RULE = 'micro50-v3-20261103'
|
||||
SUPPORTED_RULES = (RULE, EXECUTION_RULE)
|
||||
# 参数组成不可拆分的规则版本。运行版本由服务层经ParamStore选择并核对。
|
||||
SIZE = 50
|
||||
ENTRY_PERCENTILE = .08
|
||||
|
|
@ -16,17 +19,34 @@ RULE_PARAMETERS = {'entry_percentile': ENTRY_PERCENTILE, 'exit_percentile': EXIT
|
|||
PHASES = ('original', 'entering_micro', 'micro', 'returning_original')
|
||||
|
||||
|
||||
def rule_parameters(rule=RULE, candidate_pool_size=100):
|
||||
if rule not in SUPPORTED_RULES:
|
||||
raise ValueError('未知微盘规则版本')
|
||||
result = dict(RULE_PARAMETERS)
|
||||
if rule == EXECUTION_RULE:
|
||||
if not SIZE <= candidate_pool_size <= 500:
|
||||
raise ValueError('冻结候选数量须在50至500之间')
|
||||
result.update(candidate_pool_size=candidate_pool_size,
|
||||
substitution='zero_fill_only', pricing='marketable_limit')
|
||||
return result
|
||||
|
||||
|
||||
def mainland_code(code):
|
||||
return bool(isinstance(code, str) and re.fullmatch(r'(?:60\d{4}|68\d{4})\.SH|(?:00\d{4}|30\d{4})\.SZ', code))
|
||||
|
||||
|
||||
def initial_state():
|
||||
return {'phase': 'original', 'rule_version': RULE, 'cycle': 0, 'last_snapshot': None, 'entry_date': None,
|
||||
'exit_date': None, 'reason': None, 'target': [], 'entry_budget': 0., 'disabled': False,
|
||||
'entry_quantities': {}, 'consumed_entry': None, 'closed_date': None}
|
||||
|
||||
|
||||
def snapshot_check(value, execution_date, *, require_frozen=True):
|
||||
def snapshot_check(value, execution_date, *, require_frozen=True, expected_rule=None, candidate_pool_size=100):
|
||||
errors = []
|
||||
if not isinstance(value, dict):
|
||||
return ['没有监控快照']
|
||||
if value.get('rule_version') != RULE or value.get('basket_size') != SIZE or value.get('rule_parameters') != RULE_PARAMETERS:
|
||||
rule = value.get('rule_version')
|
||||
if rule not in SUPPORTED_RULES or (expected_rule and rule != expected_rule) or value.get('basket_size') != SIZE or value.get('rule_parameters') != rule_parameters(rule if rule in SUPPORTED_RULES else RULE, candidate_pool_size):
|
||||
errors.append('策略规则版本不一致')
|
||||
if value.get('effective_date') != execution_date or str(value.get('data_date', '')) >= execution_date:
|
||||
errors.append('快照日期不适用于本次执行')
|
||||
|
|
@ -34,6 +54,8 @@ def snapshot_check(value, execution_date, *, require_frozen=True):
|
|||
errors.append('执行快照尚未冻结')
|
||||
keys = ('rule_version', 'rule_parameters', 'basket_size', 'data_date', 'effective_date', 'signal_ready',
|
||||
'basket_ready', 'signal', 'members', 'source_hashes', 'errors')
|
||||
if rule == EXECUTION_RULE:
|
||||
keys += ('candidates', 'candidate_count')
|
||||
try:
|
||||
body = {k: value[k] for k in keys}
|
||||
digest = hashlib.sha256(json.dumps(body, sort_keys=True, ensure_ascii=False,
|
||||
|
|
@ -51,6 +73,14 @@ def snapshot_check(value, execution_date, *, require_frozen=True):
|
|||
errors.append(f'目标名单不是{SIZE}只唯一股票')
|
||||
if any(m.get('eligible') is not True or m.get('rank') != i+1 for i, m in enumerate(members)):
|
||||
errors.append('名单资格或排名不完整')
|
||||
if any(not mainland_code(m.get('code')) for m in members):
|
||||
errors.append('名单包含沪深范围外证券')
|
||||
if rule == EXECUTION_RULE:
|
||||
pool = value['candidates']
|
||||
if (not SIZE <= len(pool) <= candidate_pool_size or value['candidate_count'] != len(pool)
|
||||
or pool[:SIZE] != members or len({m['code'] for m in pool}) != len(pool)
|
||||
or any(m.get('eligible') is not True or m.get('rank') != i+1 or not mainland_code(m.get('code')) for i, m in enumerate(pool))):
|
||||
errors.append('冻结候选池数量、资格或顺序不完整')
|
||||
except (KeyError, ValueError, TypeError):
|
||||
errors.append('快照字段不完整或数字非法')
|
||||
return errors
|
||||
|
|
@ -73,7 +103,7 @@ def transition(state, snapshot, *, micro_qty, original_qty, budget, control='run
|
|||
if ready and signal.get('high') and s['phase'] in ('entering_micro', 'micro'):
|
||||
s.update(phase='returning_original', exit_date=snapshot['effective_date'], reason='微盘活跃度高位')
|
||||
if s['phase'] == 'returning_original' and micro_qty == 0 and not micro_pending:
|
||||
s.update(phase='original', target=[], entry_budget=0., entry_quantities={},
|
||||
s.update(phase='original', target=[], entry_budget=0., entry_quantities={}, buy_targets={}, execution_day=None,
|
||||
closed_date=(snapshot or {}).get('effective_date') or s.get('exit_date'))
|
||||
return s # 清完当天不立即重开下一轮。
|
||||
fresh_entry = snapshot and snapshot.get('snapshot_version') != s.get('consumed_entry') and snapshot.get('effective_date') != s.get('closed_date')
|
||||
|
|
@ -82,15 +112,62 @@ def transition(state, snapshot, *, micro_qty, original_qty, budget, control='run
|
|||
raise ValueError('旧微盘持仓未清完,不能再次入场')
|
||||
s.update(phase='entering_micro', cycle=s['cycle']+1, entry_date=snapshot['effective_date'],
|
||||
rule_version=snapshot['rule_version'],
|
||||
entry_budget=max(0., budget), entry_quantities={}, consumed_entry=snapshot['snapshot_version'],
|
||||
entry_budget=max(0., budget), entry_quantities={}, buy_targets={}, execution_day=None, consumed_entry=snapshot['snapshot_version'],
|
||||
reason='成长主线瓦解' if signal['growth_break'] else '微盘活跃度极低')
|
||||
if snapshot and snapshot.get('basket_ready') and s['phase'] in ('entering_micro', 'micro'):
|
||||
s['target'] = [r['code'] for r in snapshot['members']]
|
||||
if s.get('rule_version') != EXECUTION_RULE or s.get('execution_day') != snapshot['effective_date']:
|
||||
s['target'] = [r['code'] for r in snapshot['members']]
|
||||
if snapshot:
|
||||
s['last_snapshot'] = snapshot['snapshot_version']
|
||||
return s
|
||||
|
||||
|
||||
def resolve_targets(state, snapshot, *, micro=(), pending=(), unavailable=None):
|
||||
"""每日冻结排名只作确定性递补。已成交或尚有在途的成员始终占原席位。"""
|
||||
if state.get('rule_version') != EXECUTION_RULE or state['phase'] not in ('entering_micro', 'micro') or not snapshot.get('basket_ready'):
|
||||
return []
|
||||
day = snapshot['effective_date']
|
||||
if state.get('execution_day') != day:
|
||||
state.update(execution_day=day, target=[m['code'] for m in snapshot['members']],
|
||||
skipped_candidates={}, substitutions=[])
|
||||
owned = {r['ts_code'] for r in micro if int(r.get('total_qty') or 0) > 0}
|
||||
active = {r['ts_code'] for r in pending}
|
||||
blocked = unavailable or {}
|
||||
skipped = state.setdefault('skipped_candidates', {})
|
||||
target = list(state['target'])
|
||||
waits = []
|
||||
for code in target:
|
||||
if code in blocked:
|
||||
if code in owned or code in active:
|
||||
waits.append(code+'已成交或委托未结清,保留席位')
|
||||
else:
|
||||
skipped.setdefault(code, str(blocked[code]))
|
||||
# 同一天不恢复已经跳过的成员。候选也须确认无持仓、无在途才可跳过。
|
||||
kept = [c for c in target if c not in skipped or c in owned or c in active]
|
||||
removed = [c for c in target if c not in kept]
|
||||
pool = snapshot['candidates']
|
||||
for member in pool:
|
||||
if len(kept) >= SIZE:
|
||||
break
|
||||
code = member['code']
|
||||
if code in kept or code in skipped:
|
||||
continue
|
||||
if code in blocked and code not in owned and code not in active:
|
||||
skipped[code] = str(blocked[code])
|
||||
continue
|
||||
kept.append(code)
|
||||
previous = removed.pop(0) if removed else None
|
||||
state.setdefault('substitutions', []).append({'from': previous, 'to': code,
|
||||
'rank': member['rank'], 'reason': skipped.get(previous, '补充未完成席位'),
|
||||
'snapshot_version': snapshot['snapshot_version']})
|
||||
state['target'] = kept
|
||||
state['target_shortfall'] = max(0, SIZE-len(kept))
|
||||
state['buy_targets'] = {c: t for c, t in state.get('buy_targets', {}).items() if c in kept}
|
||||
if len(kept) < SIZE:
|
||||
waits.append('候选已用尽,未完成席位'+str(SIZE-len(kept)))
|
||||
return waits
|
||||
|
||||
|
||||
def board(code):
|
||||
if code.startswith('68'):
|
||||
return 'STAR'
|
||||
|
|
@ -168,7 +245,11 @@ def orders(state, snapshot, *, original, micro, quotes, cash, account_mv, cap_am
|
|||
available = max(0., min(cash-reserve_cash, cap_amount-account_mv))
|
||||
# 入场期间保留既定每股资金目标,以便原策略分批回款后继续完成建仓。
|
||||
missing = sorted(c for c in target if c not in micro_by)
|
||||
candidates = sorted(target) if phase == 'entering_micro' else missing
|
||||
objectives = state.setdefault('buy_targets', {})
|
||||
objectives = {c: t for c, t in objectives.items() if c in target}
|
||||
state['buy_targets'] = objectives
|
||||
candidates = sorted(target) if phase == 'entering_micro' else sorted(set(missing)|{
|
||||
c for c, t in objectives.items() if int(t['qty']) > int(micro_by.get(c, {}).get('total_qty') or 0)})
|
||||
per_new = available/len(missing) if missing else 0.
|
||||
for c in candidates:
|
||||
if c in original_by or c in active_codes or c in blacklisted:
|
||||
|
|
@ -184,15 +265,27 @@ def orders(state, snapshot, *, original, micro, quotes, cash, account_mv, cap_am
|
|||
quantities = state.setdefault('entry_quantities', {})
|
||||
# 首次有行情时固定数量,部分成交只补剩余,不随价格下跌重新等权。
|
||||
if c not in quantities:
|
||||
quantities[c] = buy_qty(c, price, state['entry_budget']/SIZE, costs)
|
||||
target_qty = buy_qty(c, price, state['entry_budget']/SIZE, costs)
|
||||
if target_qty:
|
||||
quantities[c] = target_qty
|
||||
if c not in quantities:
|
||||
waits.append(c+'份额不足最小交易单位及费用')
|
||||
continue
|
||||
remaining_qty = max(0, quantities[c]-owned_qty)
|
||||
desired = remaining_qty*price+fee(remaining_qty*price, 'buy', **costs) if remaining_qty else 0.
|
||||
else:
|
||||
desired = per_new
|
||||
if c not in objectives:
|
||||
target_qty = buy_qty(c, price, per_new, costs)
|
||||
if not target_qty:
|
||||
waits.append(c+'可用资金不足最小交易单位及费用')
|
||||
continue
|
||||
objectives[c] = {'qty': target_qty, 'snapshot_version': snapshot['snapshot_version'],
|
||||
'effective_date': snapshot['effective_date']}
|
||||
remaining_qty = max(0, int(objectives[c]['qty'])-owned_qty)
|
||||
desired = remaining_qty*price+fee(remaining_qty*price, 'buy', **costs) if remaining_qty else 0.
|
||||
budget = min(available, desired)
|
||||
qty = buy_qty(c, price, budget, costs)
|
||||
if phase == 'entering_micro':
|
||||
qty = min(qty, remaining_qty)
|
||||
qty = min(qty, remaining_qty)
|
||||
if qty:
|
||||
result.append({'book_id': 'micro30', 'ts_code': c, 'side': 'buy', 'qty': qty, 'reason': '初始建仓补齐' if phase == 'entering_micro' else '名单调入'})
|
||||
available -= qty*price+fee(qty*price, 'buy', **costs)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
"""执行依据只更新独立JSON字段,避免刷新任务覆盖委托进度。"""
|
||||
import json
|
||||
from app.db import session as db
|
||||
|
||||
|
||||
def save_basis(instruction_id, value):
|
||||
affected = db.execute("UPDATE pms_instruction SET progress_json=JSON_SET("
|
||||
"COALESCE(progress_json,JSON_OBJECT()),'$.execution_basis_cache',JSON_EXTRACT(:body,'$')) "
|
||||
"WHERE instruction_id=:iid", {'iid': instruction_id, 'body': json.dumps(value, ensure_ascii=False)})
|
||||
if not affected:
|
||||
raise RuntimeError('执行依据缓存没有写入父指令')
|
||||
|
||||
|
||||
def pending_trades(child_ids):
|
||||
if not child_ids:
|
||||
return 0
|
||||
args = {'p': 0, 'orphan': 3}
|
||||
keys = []
|
||||
for index, iid in enumerate(child_ids):
|
||||
key = 'c'+str(index)
|
||||
args[key] = iid
|
||||
keys.append(':'+key)
|
||||
row = db.fetch_one("SELECT COUNT(*) AS n FROM pms_qmt_inbox WHERE msg_type='trade' "
|
||||
"AND processed IN (:p,:orphan) AND corr_id IN ("+','.join(keys)+')', args)
|
||||
return int((row or {}).get('n') or 0)
|
||||
|
|
@ -470,7 +470,19 @@ def update_instruction(instruction_id: str, *, status=None, exec_qty=None, exec_
|
|||
sets.append("dispatch_ref = :dr")
|
||||
p["dr"] = dispatch_ref
|
||||
if progress is not None:
|
||||
sets.append("progress_json = :pg")
|
||||
# 独立刷新任务仅通过 execution_repo 写缓存;旧的整份进度不能覆盖新依据。
|
||||
# 同理,异步撤单/撤换标记不能被不知情的执行回写抹掉。显式传键可更新标记。
|
||||
body = ':pg'
|
||||
for key in ('cancel_requested', 'replacement_requested'):
|
||||
if key not in progress:
|
||||
body = ("CASE WHEN JSON_CONTAINS_PATH(COALESCE(progress_json,JSON_OBJECT()),"
|
||||
f"'one','$.{key}') THEN JSON_SET(JSON_EXTRACT({body},'$'),"
|
||||
f"'$.{key}',JSON_EXTRACT(progress_json,'$.{key}')) ELSE {body} END")
|
||||
body = ("CASE WHEN JSON_CONTAINS_PATH(COALESCE(progress_json,JSON_OBJECT()),"
|
||||
"'one','$.execution_basis_cache') THEN JSON_SET(JSON_EXTRACT("+body+",'$'),"
|
||||
"'$.execution_basis_cache',JSON_EXTRACT(progress_json,'$.execution_basis_cache')) "
|
||||
'ELSE '+body+' END')
|
||||
sets.append('progress_json = '+body)
|
||||
p["pg"] = _dumps(progress)
|
||||
guard = " AND status IN ('PROPOSED','RULE_PASSED','JUDGE_PASSED','DISPATCHED')" if status == 'DISPATCHED' else ''
|
||||
# 下发后的旧进度回写不能把已撤销指令复活。真实迟到成交仍由成交入口记账。
|
||||
|
|
|
|||
|
|
@ -418,7 +418,7 @@ def claim_order(instruction_id: str) -> bool:
|
|||
|
||||
|
||||
def mark_sent(instruction_id: str) -> int:
|
||||
return execute("UPDATE pms_qmt_order SET status = 'SENT', sent_at = :ts, "
|
||||
return execute("UPDATE pms_qmt_order SET status = CASE WHEN status = 'SENDING' THEN 'SENT' ELSE status END, sent_at = :ts, "
|
||||
"send_attempts = send_attempts + 1, updated_at = :ts "
|
||||
"WHERE instruction_id = :iid", {"iid": instruction_id, "ts": _NOW()})
|
||||
|
||||
|
|
|
|||
|
|
@ -244,7 +244,11 @@ def intraday_exec():
|
|||
"""
|
||||
from app.services import executor, proposal_service, strategy_runner
|
||||
r = {"materialized": executor.materialize_plans()}
|
||||
r["proposals"] = proposal_service.scan_and_route()
|
||||
if not param_store.get_bool('PMS_JUDGE_ASYNC_ENABLED', False):
|
||||
r["proposals"] = proposal_service.scan_and_route()
|
||||
if param_store.get_bool('PMS_EXEC_REPRICE_ENABLED', False):
|
||||
from app.services import execution_basis
|
||||
r['execution_basis'] = execution_basis.monitor()
|
||||
r["strategies"] = strategy_runner.tick() # 个股交易方案: 触发即发短窗口指令, 由下面 run_tick 执行
|
||||
# 过期收口改为每分钟一次 (原来只在 15:10 日结跑) —— 让过了窗口/deadline 的单当即作废,
|
||||
# 不再多活一整天堵在「今日在办」、也不会隔天还替它试单 (2026-08-12 用户定的"实时"口径)。
|
||||
|
|
@ -254,6 +258,35 @@ def intraday_exec():
|
|||
return r
|
||||
|
||||
|
||||
@celery_app.task(name="pms.execution_cancel_confirm")
|
||||
@guard(trade_day=False, session=False, respect_exec_halt=False)
|
||||
@_exclusive("INFRA_LOCK_EXEC_CANCEL_CONFIRM", ttl_sec=55)
|
||||
def execution_cancel_confirm():
|
||||
"""暂停或收盘后继续核实既有撤单;此任务绝不报价或发新单。"""
|
||||
from app.services import execution_basis
|
||||
return execution_basis.monitor(cancel_only=True)
|
||||
|
||||
|
||||
@celery_app.task(name="pms.proposal_scan")
|
||||
@guard(trade_day=True, session=True)
|
||||
@_exclusive("INFRA_LOCK_PROPOSAL_SCAN", ttl_sec=230)
|
||||
def proposal_scan():
|
||||
if not param_store.get_bool('PMS_JUDGE_ASYNC_ENABLED', False):
|
||||
return {'skipped': '异步研判未开启'}
|
||||
from app.services import proposal_service
|
||||
return proposal_service.scan_and_route()
|
||||
|
||||
|
||||
@celery_app.task(name="pms.execution_basis_refresh")
|
||||
@guard(trade_day=True, session=True)
|
||||
@_exclusive("INFRA_LOCK_EXEC_BASIS_REFRESH", ttl_sec=115)
|
||||
def execution_basis_refresh():
|
||||
if not param_store.get_bool('PMS_EXEC_REPRICE_ENABLED', False):
|
||||
return {'skipped': '独立执行依据刷新未开启'}
|
||||
from app.services import execution_basis
|
||||
return execution_basis.refresh()
|
||||
|
||||
|
||||
@celery_app.task(name="pms.signal_digest")
|
||||
@guard(trade_day=True, session=True)
|
||||
@_exclusive("INFRA_LOCK_SIGNAL_DIGEST", ttl_sec=115)
|
||||
|
|
@ -372,6 +405,9 @@ def rotation_tick():
|
|||
|
||||
|
||||
celery_app.conf.beat_schedule = {
|
||||
'execution_cancel_confirm': {'task': 'pms.execution_cancel_confirm', 'schedule': crontab(minute='*'), 'options': {'expires': 50}},
|
||||
'proposal_scan': {'task': 'pms.proposal_scan', 'schedule': crontab(minute='*'), 'options': {'expires': 50}},
|
||||
'execution_basis_refresh': {'task': 'pms.execution_basis_refresh', 'schedule': crontab(minute='*'), 'options': {'expires': 50}},
|
||||
'rotation_tick': {'task': 'pms.rotation_tick', 'schedule': crontab(minute='*'), 'options': {'expires': 50}},
|
||||
# 技术面取数 (2026-09-11): 决策系统夜扫实测 00:38 完, 06:30 留足余量; 在计划拉取 (08:40) 之前。
|
||||
"tech_pull": {"task": "pms.tech_pull", "schedule": crontab(hour=6, minute=30)},
|
||||
|
|
|
|||
|
|
@ -309,6 +309,7 @@ def plan_command(cmd: dict) -> dict:
|
|||
cancel_r = _cancel_marked_instructions(items)
|
||||
cancelled = cancel_r["cancelled"]
|
||||
cancel_failed = cancel_r["failed"]
|
||||
cancel_pending = cancel_r.get('pending') or []
|
||||
|
||||
# 命令覆盖冲突 (设计铁律「命令至上」): 全额清仓类命令一下达, 立刻停掉这些票的**买入侧**
|
||||
# —— 撤活跃策略 (网格/做T/跟踪) 与待你确认的买入提议, 别再边卖边买。在途买单已由上面
|
||||
|
|
@ -345,6 +346,8 @@ def plan_command(cmd: dict) -> dict:
|
|||
if cancel_failed:
|
||||
notes.insert(0, f"⚠ {len(cancel_failed)} 条在途指令**没撤掉, 仍在下游挂着**: "
|
||||
+ "; ".join(f"{x['instruction_id']}({x['why']})" for x in cancel_failed[:5]))
|
||||
if cancel_pending:
|
||||
notes.insert(0, f'{len(cancel_pending)} 条委托已请求撤单,等待QMT终态及成交入账确认')
|
||||
if stop_r.get("strategies") or stop_r.get("proposals") or stop_r.get("instructions"):
|
||||
notes.insert(0, f"清仓覆盖冲突: 已撤策略 {len(stop_r['strategies'])} 条、"
|
||||
f"驳回买入提议 {len(stop_r['proposals'])} 条、"
|
||||
|
|
@ -365,6 +368,7 @@ def plan_command(cmd: dict) -> dict:
|
|||
# 有一堆候选、全被同一道闸拒了。不聚合出来就得去翻 rejects 原始清单。
|
||||
"reject_summary": pl.summarize_rejects(rejects),
|
||||
"cancelled_instructions": cancelled,
|
||||
'cancel_pending': cancel_pending,
|
||||
"cancel_failed": cancel_failed,
|
||||
"stopped_buyside": stop_r or {}}
|
||||
|
||||
|
|
@ -529,11 +533,15 @@ def cancel(command_id: str) -> dict:
|
|||
if cmd["status"] not in cs.ACTIVE_TASK_STATES:
|
||||
return {"ok": False, "message": f"命令处于 {cmd['status']}, 不可撤销"}
|
||||
|
||||
progress = dict(cmd.get('progress') or {})
|
||||
progress['cancel_requested'] = True
|
||||
if not pms_repo.update_command(command_id, progress=progress):
|
||||
return {'ok': False, 'message': '命令停止执行标记写入失败'}
|
||||
n_plan = pms_repo.cancel_plans_of_command(command_id)
|
||||
# 匹配在途指令用**全部**方案 (不带状态过滤): 上一次撤销若只撤了一半, 方案已进
|
||||
# CANCELLED, 按状态过滤会把剩下的在途指令漏成孤儿。
|
||||
plan_ids = {p["plan_id"] for p in pms_repo.list_plans(command_id=command_id)}
|
||||
cancelled, failed = [], []
|
||||
cancelled, failed, pending = [], [], []
|
||||
for ins in pms_repo.list_instructions(statuses=list(LIVE_INSTR), limit=500):
|
||||
if ins.get("origin_type") == "plan" and ins.get("origin_id") in plan_ids:
|
||||
try:
|
||||
|
|
@ -541,7 +549,9 @@ def cancel(command_id: str) -> dict:
|
|||
ins["instruction_id"], reason=f"撤销命令 {command_id}") or {}
|
||||
except Exception as e:
|
||||
r = {"ok": False, "error": f"{type(e).__name__}: {e}"}
|
||||
if r.get("ok"):
|
||||
if r.get('ok') and r.get('pending'):
|
||||
pending.append(ins['instruction_id'])
|
||||
elif r.get("ok"):
|
||||
cancelled.append(ins["instruction_id"])
|
||||
else:
|
||||
failed.append({"instruction_id": ins["instruction_id"],
|
||||
|
|
@ -555,6 +565,9 @@ def cancel(command_id: str) -> dict:
|
|||
f"仍在下游挂着**: "
|
||||
+ "; ".join(f"{x['instruction_id']}({x['why']})" for x in failed[:5])
|
||||
+ " —— 命令保持在途继续跟踪, 处理后可再次撤销")}
|
||||
if pending:
|
||||
return {'ok': True, 'pending': pending, 'cancelled': cancelled,
|
||||
'message': f'命令 {command_id} 已停止新下发,{len(pending)} 条委托等待撤单确认'}
|
||||
pms_repo.update_command(command_id, status=cs.ST_CANCELLED, done_at=datetime.now())
|
||||
return {"ok": True, "cancelled": cancelled,
|
||||
"message": f"命令 {command_id} 已撤销 (作废方案 {n_plan} 条, "
|
||||
|
|
@ -607,6 +620,10 @@ def refresh_progress(command_id=None) -> dict:
|
|||
out = []
|
||||
today = datetime.now().date()
|
||||
for c in [x for x in cmds if x]:
|
||||
if (c.get('progress') or {}).get('cancel_requested'):
|
||||
result = cancel(c['command_id'])
|
||||
out.append({'command_id': c['command_id'], 'cancellation': result})
|
||||
continue
|
||||
plans = pms_repo.list_plans(command_id=c["command_id"])
|
||||
done_amt = 0.0
|
||||
for p in plans:
|
||||
|
|
@ -711,14 +728,16 @@ def _cancel_marked_instructions(items: list) -> dict:
|
|||
命令点名的条数, 不是真撤掉的条数。
|
||||
"""
|
||||
from app.services import executor
|
||||
ok, failed = [], []
|
||||
ok, failed, pending = [], [], []
|
||||
for it in items or []:
|
||||
iid = it.get("cancel_instruction_id")
|
||||
if not iid:
|
||||
continue
|
||||
try:
|
||||
r = executor.cancel_instruction(iid, reason="命令撤销在途指令") or {}
|
||||
if r.get("ok"):
|
||||
if r.get('ok') and r.get('pending'):
|
||||
pending.append(iid)
|
||||
elif r.get("ok"):
|
||||
ok.append(iid)
|
||||
else:
|
||||
failed.append({"instruction_id": iid,
|
||||
|
|
@ -728,7 +747,7 @@ def _cancel_marked_instructions(items: list) -> dict:
|
|||
failed.append({"instruction_id": iid, "why": f"{type(e).__name__}: {e}"})
|
||||
if failed:
|
||||
logger.error("[命令] %s 条在途指令没撤掉, **它们仍在下游挂着**: %s", len(failed), failed)
|
||||
return {"cancelled": ok, "failed": failed}
|
||||
return {"cancelled": ok, "failed": failed, 'pending': pending}
|
||||
|
||||
|
||||
def _codes_with_live_plans() -> list:
|
||||
|
|
@ -917,7 +936,9 @@ def _stop_buyside_for_exit(codes: list, command_id: str) -> dict:
|
|||
iid = i.get("instruction_id")
|
||||
try:
|
||||
r = executor.cancel_instruction(iid, reason="清仓命令下达: 撤该票在途买入 (命令覆盖冲突)") or {}
|
||||
if r.get("ok"):
|
||||
if r.get('ok') and r.get('pending'):
|
||||
out.setdefault('pending_instructions', []).append(iid)
|
||||
elif r.get("ok"):
|
||||
out["instructions"].append(iid)
|
||||
else:
|
||||
# 下游没撤成: 指令仍在途, 单独列出来别吞 (与 _cancel_marked_instructions 同纪律)
|
||||
|
|
@ -1046,7 +1067,10 @@ def cleanup_exited_positions(limit: int = 500) -> dict:
|
|||
for ins in buys:
|
||||
try:
|
||||
r = executor.cancel_instruction(ins["instruction_id"], reason="清仓完成清场: 撤在途建仓") or {}
|
||||
if r.get("ok"):
|
||||
if r.get('ok') and r.get('pending'):
|
||||
err_codes.add(ins.get('ts_code'))
|
||||
out.setdefault('pending_instructions', []).append(ins['instruction_id'])
|
||||
elif r.get("ok"):
|
||||
per_code[ins["ts_code"]]["instructions"].append(ins["instruction_id"])
|
||||
else:
|
||||
err_codes.add(ins.get("ts_code"))
|
||||
|
|
|
|||
|
|
@ -141,6 +141,12 @@ def dispatch(*, instruction_id: str, ts_code: str, side: str, qty: int, limit_pr
|
|||
"""递一手给下游。返回 {ok, ref, mode, note, error}; ok=False 时 executor 不改指令状态。"""
|
||||
m = mode()
|
||||
try:
|
||||
from app.repo import pms_repo
|
||||
from app.services import execution_orders, execution_basis
|
||||
pid = _parent_of(instruction_id, parent_id)
|
||||
parent = pms_repo.get_instruction(pid)
|
||||
if parent and (parent.get('progress') or {}).get('cancel_requested'):
|
||||
return {'ok': False, 'ref': None, 'mode': m, 'note': '', 'error': '父指令撤销处理中,禁止继续下发'}
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA and str(side).lower() == 'buy':
|
||||
from app.core import strategy_book
|
||||
from app.services.rotation_service import strategy_buy_blocker
|
||||
|
|
@ -162,9 +168,37 @@ def dispatch(*, instruction_id: str, ts_code: str, side: str, qty: int, limit_pr
|
|||
if blocked:
|
||||
return {'ok': False, 'ref': None, 'mode': m, 'note': '', 'error': blocked}
|
||||
if m == MODE_WS:
|
||||
return _ws(instruction_id=instruction_id, ts_code=ts_code, side=side, qty=qty,
|
||||
limit_price=limit_price, valid_until=valid_until,
|
||||
intent=intent, parent_id=parent_id, note=note)
|
||||
with execution_orders.account_guard():
|
||||
if execution_orders.enabled() and not qmt_repo.get_order(instruction_id):
|
||||
from app.core.execution_policy import supported_code
|
||||
if not supported_code(ts_code):
|
||||
raise ValueError('执行范围仅限沪深合资格证券')
|
||||
parent = pms_repo.get_instruction(pid)
|
||||
if not parent or parent.get('status') not in ('PROPOSED', 'RULE_PASSED', 'JUDGE_PASSED', 'DISPATCHED'):
|
||||
raise ValueError('父指令未获准或已结束')
|
||||
progress = parent.get('progress') or {}
|
||||
if progress.get('cancel_requested'):
|
||||
raise ValueError('父指令正在撤销')
|
||||
state = execution_orders.replacement_state(pid)
|
||||
if not state['ready'] or qty > state['remaining']:
|
||||
raise ValueError('旧子单未确认结束、成交未入账或数量超过剩余目标')
|
||||
retry_wait = execution_orders.retry_reason(pid)
|
||||
if retry_wait:
|
||||
raise ValueError(retry_wait)
|
||||
if execution_basis.uses_basis(parent):
|
||||
reason = execution_basis.cache_reason(progress)
|
||||
if reason:
|
||||
raise ValueError(reason)
|
||||
reason = execution_orders.code_settlement_blocker(ts_code)
|
||||
if reason:
|
||||
raise ValueError(reason)
|
||||
boundary = progress.get('user_limit_price', parent.get('limit_price'))
|
||||
if boundary is not None and ((side == 'buy' and float(limit_price) > float(boundary)) or
|
||||
(side == 'sell' and float(limit_price) < float(boundary))):
|
||||
raise ValueError('报价突破用户指定价格边界')
|
||||
return _ws(instruction_id=instruction_id, ts_code=ts_code, side=side, qty=qty,
|
||||
limit_price=limit_price, valid_until=valid_until,
|
||||
intent=intent, parent_id=parent_id, note=note)
|
||||
return _shadow(instruction_id, side)
|
||||
except Exception as e:
|
||||
logger.exception("下发失败 %s", instruction_id)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,11 @@
|
|||
卖出区间, 盘中只回答「现价在不在区间内 + 限价挂哪里」, 不做任何新的盘中判断
|
||||
(2026-08-03 用户定的原则: 提前计算为主、盘中监控为辅, 可以接受买不上)。
|
||||
|
||||
分工与降级 (三条, 都是纪律不是实现细节):
|
||||
以下分工与降级描述兼容模式。开启 PMS_EXEC_REPRICE_ENABLED 后,独立任务刷新
|
||||
版本化执行依据,本模块只读取缓存与行情报价;依据未知或过期时等待,不退本地研判。
|
||||
PMS_EXEC_QMT_PRICE_MONITOR 模式则维持指定区间边缘限价,接口故障时等待补齐。
|
||||
|
||||
兼容模式的分工与降级:
|
||||
1. **本地检查先行**: 配额/兜底/停牌/一字板/不追高(当日涨幅) 由 core.exec_timing 的
|
||||
hard_gate 先判, 命中就不咨询 —— 这些检查始终留在 PMS 本地。尤其 14:45 兜底:
|
||||
到点必须完成, 决策系统说什么都不算。
|
||||
|
|
@ -122,6 +126,10 @@ def decide(*, side: str, action: str, ts_code: str, now, day: dict, params: dict
|
|||
prog 由调用方传入指令的 progress dict, 咨询结果/失败冷却会写进 prog["exec_advice"],
|
||||
随调用方既有的落表动作持久化; 传 None 则本轮结论不缓存 (dry_run 语义)。
|
||||
"""
|
||||
if param_store.get_bool('PMS_EXEC_REPRICE_ENABLED', False):
|
||||
return _execution_quote(side=side, action=action, ts_code=ts_code, now=now,
|
||||
quota=quota, fired_today=fired_today, prog=prog or {},
|
||||
instruction_limit=instruction_limit, urgent=urgent)
|
||||
if param_store.get_bool("PMS_EXEC_QMT_PRICE_MONITOR", False):
|
||||
return _qmt_limit(side=side, action=action, ts_code=ts_code, now=now, day=day,
|
||||
params=params, is_last_day=is_last_day, urgent=urgent,
|
||||
|
|
@ -174,6 +182,35 @@ def decide(*, side: str, action: str, ts_code: str, now, day: dict, params: dict
|
|||
return d
|
||||
|
||||
|
||||
def _execution_quote(*, side, action, ts_code, now, quota, fired_today, prog,
|
||||
instruction_limit, urgent):
|
||||
"""新执行环只读取版本化缓存和行情,绝不在这里同步咨询。"""
|
||||
from app.services import execution_basis, execution_orders, market
|
||||
left = max(0, int(quota)-int(fired_today))
|
||||
def wait(why):
|
||||
return {'action': et.ACT_WAIT, 'qty_hint': left, 'limit_price': None,
|
||||
'reason': why, 'forced': False, 'source': '执行报价', 'qmt_price_monitor': True}
|
||||
if prog.get('cancel_requested'):
|
||||
return wait('父指令撤单处理中')
|
||||
if not left or not et.in_session(et.hm_to_min(now)):
|
||||
return wait('本轮没有可发数量或不在执行时段')
|
||||
parent = {'progress': {**prog, 'urgent': urgent}, 'side': side}
|
||||
if execution_basis.uses_basis(parent):
|
||||
why = execution_basis.cache_reason(prog, now)
|
||||
if why:
|
||||
return wait(why)
|
||||
quote = market.execution_quote(ts_code, now=now)
|
||||
result = execution_orders.quote_order(side, quote, now,
|
||||
user_limit=prog.get('user_limit_price', instruction_limit),
|
||||
anchor_price=prog.get('execution_anchor_price'))
|
||||
prog['execution_quote'] = result
|
||||
if result.get('action') == et.ACT_FIRE:
|
||||
prog.setdefault('execution_anchor_price', result['anchor_price'])
|
||||
result.update(qty_hint=left, forced=False, source='执行报价', qmt_price_monitor=True,
|
||||
basis_version=((prog.get('execution_basis_cache') or {}).get('basis') or {}).get('version'))
|
||||
return result
|
||||
|
||||
|
||||
def _qmt_limit(*, side, action, ts_code, now, day, params, is_last_day, urgent,
|
||||
fired_today, quota, pos, tdays_left, prog, instruction_limit):
|
||||
"""计划已确定后只定限价,不等现价进入区间;数据故障不能转成追价买入。"""
|
||||
|
|
|
|||
|
|
@ -0,0 +1,132 @@
|
|||
"""独立刷新执行依据;在途监控不做HTTP,也不因配额已占用而跳过。"""
|
||||
import time
|
||||
from datetime import timedelta
|
||||
from app.core import execution_policy as policy, strategy_book as books
|
||||
from app.repo import pms_repo, execution_repo
|
||||
from app.services import param_store, execution_orders
|
||||
|
||||
LIVE = ('PROPOSED', 'RULE_PASSED', 'JUDGE_PASSED', 'DISPATCHED')
|
||||
|
||||
|
||||
def uses_basis(parent):
|
||||
progress = parent.get('progress') or {}
|
||||
# STOP 表达看空或离场,不能反向禁止已经获准的减仓和退出。
|
||||
return (parent.get('side') == 'buy'
|
||||
and progress.get('execution_profile') != 'rotation'
|
||||
and not (progress.get('origin') == 'strategy' and progress.get('stype') in ('GRID', 'TRAIL')))
|
||||
|
||||
|
||||
def cache_reason(progress, now=None):
|
||||
cache = (progress or {}).get('execution_basis_cache') or {}
|
||||
return policy.basis_reason(cache.get('basis'), now,
|
||||
max_source_days=max(0, param_store.get_int('PMS_EXEC_BASIS_SOURCE_MAX_DAYS', 7)))
|
||||
|
||||
|
||||
def refresh(now=None):
|
||||
"""短请求有整轮预算。失败留痕,但不会把旧依据时间重新延长。"""
|
||||
from app.services import exec_advisor, market
|
||||
current = policy.local_now(now)
|
||||
out = {'enabled': execution_orders.enabled(), 'refreshed': [], 'errors': [], 'skipped': []}
|
||||
if not out['enabled']:
|
||||
return out
|
||||
deadline = time.monotonic()+max(1, param_store.get_int('PMS_EXEC_BASIS_REFRESH_BUDGET_SEC', 20))
|
||||
with books.scope('all'):
|
||||
parents = pms_repo.list_instructions(statuses=list(LIVE), limit=10000)
|
||||
parents.sort(key=lambda p: ((p.get('progress') or {}).get('execution_basis_cache') or {}).get('fetched_at', ''))
|
||||
for parent in parents:
|
||||
if not policy.supported_code(parent.get('ts_code')) or not uses_basis(parent) or (parent.get('progress') or {}).get('cancel_requested'):
|
||||
continue
|
||||
cache = dict((parent.get('progress') or {}).get('execution_basis_cache') or {})
|
||||
due = policy.timestamp(cache.get('refresh_after'))
|
||||
if due and current < due:
|
||||
continue
|
||||
left = deadline-time.monotonic()
|
||||
if left < 1:
|
||||
out['skipped'].append('本轮执行依据刷新预算用尽')
|
||||
break
|
||||
fresh = dict(cache)
|
||||
try:
|
||||
if not exec_advisor.available():
|
||||
raise ValueError('执行依据来源未配置')
|
||||
with books.scope(parent.get('book_id') or books.ORIGINAL):
|
||||
position = pms_repo.get_position(parent['ts_code']) or {}
|
||||
day = market.day_snapshot(parent['ts_code'])
|
||||
payload = {'direction': 'PMS_EXEC', 'ts_code': parent['ts_code'],
|
||||
'side': parent['side'], 'action': parent.get('action'),
|
||||
'qty_left': max(0, int(parent['qty'])-int(parent.get('exec_qty') or 0)),
|
||||
'now': current.strftime('%H:%M'), 'day': day,
|
||||
'refs': {k: position.get(k+'_ref') for k in ('support', 'pressure', 'stop')},
|
||||
'position': {k: position.get(k) for k in ('total_qty', 'avail_qty', 'avg_cost')},
|
||||
'is_last_day': False, 'tdays_left': None}
|
||||
response = exec_advisor._post(exec_advisor.base_url()+param_store.get('PMS_EXEC_PATH', '/api/intraday/pms_exec'),
|
||||
payload, max(1, min(param_store.get_int('PMS_EXEC_TIMEOUT_SEC', 8), int(left))))
|
||||
if not isinstance(response, dict):
|
||||
raise ValueError('执行依据响应格式无效')
|
||||
basis = response.get('execution_basis')
|
||||
# 明确未知或停止立即覆盖旧事实。只有传输失败可保留尚在有效期内的旧依据。
|
||||
fresh = {'basis': basis if isinstance(basis, dict) else {},
|
||||
'observed': response.get('observed') or {}, 'error': ''}
|
||||
reason = policy.basis_reason(fresh['basis'], current,
|
||||
max_source_days=max(0, param_store.get_int('PMS_EXEC_BASIS_SOURCE_MAX_DAYS', 7)))
|
||||
if reason:
|
||||
fresh['error'] = reason
|
||||
except Exception as exc:
|
||||
fresh['error'] = str(exc)[:300]
|
||||
out['errors'].append({'instruction_id': parent['instruction_id'], 'error': fresh['error']})
|
||||
fresh['fetched_at'] = current.isoformat()
|
||||
fresh['refresh_after'] = (current+timedelta(seconds=max(1,
|
||||
param_store.get_int('PMS_EXEC_BASIS_REFRESH_SEC', 60)))).isoformat()
|
||||
with books.scope(parent.get('book_id') or books.ORIGINAL):
|
||||
execution_repo.save_basis(parent['instruction_id'], fresh)
|
||||
out['refreshed'].append(parent['instruction_id'])
|
||||
return out
|
||||
|
||||
|
||||
def monitor(now=None, cancel_only=False):
|
||||
from app.services import executor, market
|
||||
current = policy.local_now(now)
|
||||
out = {'cancelled': [], 'replacing': [], 'errors': []}
|
||||
with books.scope('all'):
|
||||
parents = pms_repo.list_instructions(statuses=list(LIVE)+['CANCELLED', 'EXPIRED'], limit=10000)
|
||||
for parent in parents:
|
||||
if not policy.supported_code(parent.get('ts_code')):
|
||||
continue
|
||||
try:
|
||||
with books.scope(parent.get('book_id') or books.ORIGINAL):
|
||||
progress = parent.get('progress') or {}
|
||||
if progress.get('cancel_requested') or parent.get('status') in ('CANCELLED', 'EXPIRED'):
|
||||
result = executor.cancel_instruction(parent['instruction_id'],
|
||||
reason=(progress.get('cancel_requested') or {}).get('reason', '继续确认撤单'), arbiter='system')
|
||||
if not result.get('ok'):
|
||||
raise ValueError(result.get('error') or result.get('message'))
|
||||
out['cancelled'].append(result)
|
||||
continue
|
||||
if cancel_only:
|
||||
continue
|
||||
if not execution_orders.enabled() or progress.get('execution_profile') == 'rotation':
|
||||
continue
|
||||
state = execution_orders.replacement_state(parent['instruction_id'])
|
||||
if not state['active']:
|
||||
continue
|
||||
reason = cache_reason(progress, current) if uses_basis(parent) else ''
|
||||
if reason:
|
||||
result = execution_orders.request_replace(parent['instruction_id'], reason, current)
|
||||
out['replacing'].append({'instruction_id': parent['instruction_id'], 'reason': reason, 'pending': result['pending']})
|
||||
continue
|
||||
try:
|
||||
quote = market.execution_quote(parent['ts_code'], now=current)
|
||||
desired = execution_orders.quote_order(parent['side'], quote, current,
|
||||
user_limit=progress.get('user_limit_price', parent.get('limit_price')),
|
||||
anchor_price=progress.get('execution_anchor_price'))
|
||||
except Exception as exc:
|
||||
desired = {'action': 'WAIT', 'reason': '行情读取失败,等待恢复:'+str(exc)[:200]}
|
||||
if desired.get('action') != 'FIRE':
|
||||
result = execution_orders.request_replace(parent['instruction_id'], desired['reason'], current)
|
||||
out['replacing'].append({'instruction_id': parent['instruction_id'], 'reason': desired['reason'], 'pending': result['pending']})
|
||||
continue
|
||||
if execution_orders.should_replace(parent, desired, current):
|
||||
result = execution_orders.request_replace(parent['instruction_id'], desired['reason'], current)
|
||||
out['replacing'].append({'instruction_id': parent['instruction_id'], 'reason': desired['reason'], 'pending': result['pending']})
|
||||
except Exception as exc:
|
||||
out['errors'].append({'instruction_id': parent['instruction_id'], 'error': str(exc)[:300]})
|
||||
return out
|
||||
|
|
@ -27,6 +27,9 @@ def queued_buy_blocker(row):
|
|||
parent = pms_repo.get_instruction(row.get('parent_id'))
|
||||
if not parent or parent.get('book_id') not in (books.ORIGINAL, books.MICRO):
|
||||
raise RuntimeError('委托的策略来源无法核实')
|
||||
reason = _queued_basis_check(parent)
|
||||
if reason:
|
||||
return reason
|
||||
if (parent.get('progress') or {}).get('execution_profile') == 'rotation' and param_store.get('PMS_ROTATION_CONTROL', 'run') == 'pause_all':
|
||||
return '用户暂停轮换全部执行'
|
||||
if row.get('side') != 'buy':
|
||||
|
|
@ -38,12 +41,22 @@ def queued_buy_blocker(row):
|
|||
return reason
|
||||
with books.scope(parent['book_id']):
|
||||
return _queued_parent_check(row, parent)
|
||||
if row.get('side') != 'buy':
|
||||
return None
|
||||
parent = pms_repo.get_instruction(row.get('parent_id'))
|
||||
reason = _queued_basis_check(parent) if parent else None
|
||||
if reason or row.get('side') != 'buy':
|
||||
return reason
|
||||
return _queued_parent_check(row, parent)
|
||||
|
||||
|
||||
def _queued_basis_check(parent):
|
||||
if (parent.get('progress') or {}).get('cancel_requested'):
|
||||
return '父指令已请求撤销,未发出的委托作废'
|
||||
from app.services import execution_orders, execution_basis
|
||||
if execution_orders.enabled() and execution_basis.uses_basis(parent):
|
||||
return execution_basis.cache_reason(parent.get('progress') or {}) or None
|
||||
return None
|
||||
|
||||
|
||||
def _queued_parent_check(row, parent):
|
||||
if not parent:
|
||||
raise RuntimeError('买入来源指令无法核实,暂不发送')
|
||||
|
|
@ -51,6 +64,9 @@ def _queued_parent_check(row, parent):
|
|||
return '清仓已终止原买入安排'
|
||||
if parent.get('status') not in LIVE:
|
||||
return '来源买入指令已结束,未发出的委托作废'
|
||||
reason = _queued_basis_check(parent)
|
||||
if reason:
|
||||
return reason
|
||||
return buy_blocker(row['ts_code'])
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,151 @@
|
|||
"""共用报价及撤换。撤单请求不释放数量,实际终态及成交入账后才可重报。"""
|
||||
from contextlib import nullcontext
|
||||
from app.core import execution_policy as policy, strategy_book as books
|
||||
from app.repo import pms_repo, qmt_repo, execution_repo
|
||||
from app.services import param_store
|
||||
from config.settings import settings
|
||||
|
||||
|
||||
def enabled():
|
||||
return param_store.get_bool('PMS_EXEC_REPRICE_ENABLED', False)
|
||||
|
||||
|
||||
def account_guard():
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
from app.repo import rotation_repo
|
||||
return rotation_repo.account_guard()
|
||||
return nullcontext()
|
||||
|
||||
|
||||
def quote_order(side, quote, now=None, user_limit=None, anchor_price=None):
|
||||
return policy.price_order(side=side, quote=quote, now=now, user_limit=user_limit,
|
||||
anchor_price=anchor_price, params={
|
||||
'allow_minute': param_store.get_bool('PMS_EXEC_ALLOW_MINUTE_QUOTE', True),
|
||||
'quote_max_age_sec': param_store.get_int('PMS_EXEC_QUOTE_MAX_AGE_SEC', 15),
|
||||
'minute_max_age_sec': param_store.get_int('PMS_EXEC_MINUTE_MAX_AGE_SEC', 90),
|
||||
'offset_ticks': param_store.get_int('PMS_EXEC_PRICE_OFFSET_TICKS', 1),
|
||||
'max_slippage_pct': param_store.get_float('PMS_EXEC_MAX_SLIPPAGE_PCT', .01),
|
||||
'minute_slippage_pct': param_store.get_float('PMS_EXEC_MINUTE_SLIPPAGE_PCT', .002)})
|
||||
|
||||
|
||||
def replacement_state(parent_id):
|
||||
with books.scope('all'):
|
||||
parent = pms_repo.get_instruction(parent_id)
|
||||
if not parent:
|
||||
raise ValueError('父指令不存在')
|
||||
orders = qmt_repo.orders_for_parent(parent_id)
|
||||
filled, live, seen = 0, [], set()
|
||||
for order in orders:
|
||||
iid = order['instruction_id']
|
||||
if iid in seen:
|
||||
raise ValueError('重复子单编号')
|
||||
seen.add(iid)
|
||||
qty, done = int(order['qty']), int(order.get('cum_qty') or 0)
|
||||
if qty <= 0 or done < 0 or done > qty:
|
||||
raise ValueError('子单累计成交数量异常')
|
||||
if order.get('status') == 'FILLED' and done != qty:
|
||||
raise ValueError('全成子单累计成交不完整')
|
||||
filled += done
|
||||
if order.get('status') not in qmt_repo.FINAL:
|
||||
live.append(order)
|
||||
booked, target = int(parent.get('exec_qty') or 0), int(parent['qty'])
|
||||
if max(booked, filled) > target:
|
||||
raise ValueError('已成交数量超出父指令,等待核对')
|
||||
pending_trades = execution_repo.pending_trades(list(seen))
|
||||
remaining = max(0, target-max(booked, filled))
|
||||
missing = [c.get('ref') for c in (parent.get('progress') or {}).get('children', [])
|
||||
if c.get('ref') and c.get('ref') not in seen]
|
||||
ready = not live and booked >= filled and not pending_trades and not missing
|
||||
return {'ready': ready, 'pending': not ready, 'confirmed_filled': max(booked, filled),
|
||||
'remaining': remaining, 'active': live, 'orders': orders,
|
||||
'pending_trades': pending_trades, 'booked': booked, 'missing_orders': missing}
|
||||
|
||||
|
||||
def code_settlement_blocker(ts_code):
|
||||
"""跨父指令与策略检查迟到成交,不因旧父指令本地终态而再次建仓。"""
|
||||
with books.scope('all'):
|
||||
parents = pms_repo.list_instructions(ts_code=ts_code, limit=10001, include_archived=True)
|
||||
if len(parents) >= 10001:
|
||||
return '该股历史指令超过核验上限,等待核对'
|
||||
for parent in parents:
|
||||
state = replacement_state(parent['instruction_id'])
|
||||
if state['pending_trades'] or state['booked'] < state['confirmed_filled'] or state['missing_orders']:
|
||||
return '该股已有成交或旧委托尚未核实入账'
|
||||
return ''
|
||||
|
||||
|
||||
def next_child_id(parent_id):
|
||||
numbers = [0]
|
||||
prefix = parent_id+'_D'
|
||||
for row in qmt_repo.orders_for_parent(parent_id):
|
||||
iid = row['instruction_id']
|
||||
if not iid.startswith(prefix) or not iid[len(prefix):].isdigit():
|
||||
raise ValueError('父指令存在无法核实序号的子单')
|
||||
numbers.append(int(iid[len(prefix):]))
|
||||
return prefix+str(max(numbers)+1).zfill(2)
|
||||
|
||||
|
||||
def retry_reason(parent_id, now=None):
|
||||
"""拒单和发送失败退避;普通到期及正常撤换不消耗此预算。"""
|
||||
history = qmt_repo.orders_for_parent(parent_id)
|
||||
if not history or history[-1].get('status') not in ('REJECTED', 'SEND_FAILED'):
|
||||
return ''
|
||||
last = history[-1]
|
||||
code, count = last.get('reject_code'), 0
|
||||
for row in reversed(history):
|
||||
if row.get('status') not in ('REJECTED', 'SEND_FAILED') or row.get('reject_code') != code:
|
||||
break
|
||||
count += 1
|
||||
at = last.get('final_at') or last.get('sent_at') or last.get('created_at')
|
||||
if not hasattr(at, 'tzinfo'):
|
||||
at = policy.timestamp(at)
|
||||
if at is None:
|
||||
return '拒单时间不可核实,等待核对'
|
||||
base = max(1, param_store.get_int('PMS_EXEC_REJECT_COOLDOWN_SEC', 60))
|
||||
cap = max(base, param_store.get_int('PMS_EXEC_REJECT_MAX_COOLDOWN_SEC', 900))
|
||||
wait = min(cap, base*2**min(max(0, count-1), 8))
|
||||
left = wait-(policy.local_now(now)-policy.local_now(at)).total_seconds()
|
||||
return f'委托连续拒绝或发送失败,退避等待 {max(0, int(left))} 秒:{code or "原因见回执"}' if left > 0 else ''
|
||||
|
||||
|
||||
def request_replace(parent_id, reason, now=None):
|
||||
from app.services import dispatcher
|
||||
with account_guard(), books.scope('all'):
|
||||
parent = pms_repo.get_instruction(parent_id)
|
||||
if not parent:
|
||||
raise ValueError('父指令不存在')
|
||||
progress = dict(parent.get('progress') or {})
|
||||
if not progress.get('replacement_requested'):
|
||||
progress['replacement_requested'] = {'at': policy.local_now(now).isoformat(), 'reason': reason}
|
||||
if not pms_repo.update_instruction(parent_id, progress=progress):
|
||||
raise RuntimeError('撤换请求没有写入父指令')
|
||||
result = dispatcher.cancel(instruction_id=parent_id, dispatch_ref=parent.get('dispatch_ref'))
|
||||
if not result.get('ok'):
|
||||
raise RuntimeError('撤换请求未成功落表:'+str(result.get('error')))
|
||||
state = replacement_state(parent_id)
|
||||
return {'requested': True, **state}
|
||||
|
||||
|
||||
def should_replace(parent, desired, now=None):
|
||||
"""仅比较同轮报价;失败重试次数和间隔由实际子单历史决定。"""
|
||||
if desired.get('action') != 'FIRE':
|
||||
return False
|
||||
orders = qmt_repo.orders_for_parent(parent['instruction_id'])
|
||||
live = [o for o in orders if o.get('status') not in qmt_repo.FINAL]
|
||||
if not live or any(o.get('cancel_state') in ('REQUESTED', 'SENT') for o in live):
|
||||
return False
|
||||
cap = max(0, param_store.get_int('PMS_EXEC_REPRICE_MAX_ATTEMPTS', 0))
|
||||
if cap and len(orders) >= cap:
|
||||
return False
|
||||
gap = max(1, param_store.get_int('PMS_EXEC_REPRICE_TICKS', 2))*.01
|
||||
min_age = max(0, param_store.get_int('PMS_EXEC_REPRICE_MIN_SEC', 60))
|
||||
current = policy.local_now(now)
|
||||
for order in live:
|
||||
at = order.get('sent_at') or order.get('created_at')
|
||||
if at is None:
|
||||
return False
|
||||
if not hasattr(at, 'tzinfo'):
|
||||
at = policy.timestamp(at)
|
||||
if at is None or (current-policy.local_now(at)).total_seconds() < min_age:
|
||||
return False
|
||||
return any(abs(float(o['limit_price'])-desired['limit_price'])+1e-8 >= gap for o in live)
|
||||
|
|
@ -190,6 +190,25 @@ def run_tick(*, now=None, dry_run: bool = False) -> dict:
|
|||
is_last = tdays_left <= 1
|
||||
|
||||
try:
|
||||
if prog.get('cancel_requested'):
|
||||
if not dry_run:
|
||||
cancelled = cancel_instruction(ins['instruction_id'],
|
||||
reason=prog['cancel_requested'].get('reason', '继续确认撤单'), arbiter='system')
|
||||
if not cancelled.get('ok'):
|
||||
out['errors'].append(cancelled.get('error') or cancelled.get('message'))
|
||||
continue
|
||||
from app.services import execution_orders
|
||||
if execution_orders.enabled() and dispatcher.mode() == dispatcher.MODE_WS:
|
||||
replacement = execution_orders.replacement_state(ins['instruction_id'])
|
||||
if not replacement['ready']:
|
||||
out['waited'].append({'instruction_id': ins['instruction_id'], 'code': code,
|
||||
'reason': '等待旧子单终态及实际成交入账'})
|
||||
continue
|
||||
retry_wait = execution_orders.retry_reason(ins['instruction_id'], now)
|
||||
if retry_wait:
|
||||
out['waited'].append({'instruction_id': ins['instruction_id'], 'code': code,
|
||||
'reason': retry_wait})
|
||||
continue
|
||||
from app.services import execution_conflicts
|
||||
if ins.get('action') == 'EXIT' and side == 'sell' and not dry_run:
|
||||
cleanup = execution_conflicts.enforce_exit(ins)
|
||||
|
|
@ -372,7 +391,9 @@ def run_tick(*, now=None, dry_run: bool = False) -> dict:
|
|||
# parent_id 让子单能回指父指令, note 原样落到 QMT 侧供人工看盘。
|
||||
# shadow 模式忽略这三项, 传着不碍事 —— 两种模式共用同一个调用点。
|
||||
res = dispatcher.dispatch(
|
||||
instruction_id=_child_id(ins["instruction_id"], len(children) + 1),
|
||||
instruction_id=(execution_orders.next_child_id(ins['instruction_id'])
|
||||
if execution_orders.enabled() and dispatcher.mode() == dispatcher.MODE_WS
|
||||
else _child_id(ins["instruction_id"], len(children) + 1)),
|
||||
parent_id=ins["instruction_id"], intent=ins.get("action"),
|
||||
ts_code=code, side=side, qty=qty, limit_price=d["limit_price"],
|
||||
note=d.get("reason"),
|
||||
|
|
@ -387,6 +408,7 @@ def run_tick(*, now=None, dry_run: bool = False) -> dict:
|
|||
"valid_until": et._fmt(dl_min),
|
||||
"forced": d.get("forced", False), "reason": d["reason"]})
|
||||
prog["children"] = children
|
||||
prog['replacement_requested'] = None
|
||||
prog.setdefault("dispatched_at", now.strftime("%Y-%m-%d %H:%M:%S"))
|
||||
prog["last_decision"] = {"at": now.strftime("%H:%M"), "action": et.ACT_FIRE,
|
||||
"reason": d["reason"], "source": d.get("source")}
|
||||
|
|
@ -431,6 +453,14 @@ def sweep_windows(*, now=None) -> dict:
|
|||
for ins in pms_repo.list_instructions(statuses=list(LIVE), limit=300):
|
||||
try:
|
||||
prog = dict(ins.get("progress") or {})
|
||||
if prog.get('cancel_requested'):
|
||||
cancellation = cancel_instruction(ins['instruction_id'],
|
||||
reason=prog['cancel_requested'].get('reason', '继续确认撤单'), arbiter='system')
|
||||
if not cancellation.get('ok'):
|
||||
out['errors'].append(cancellation.get('error') or cancellation.get('message'))
|
||||
elif cancellation.get('confirmed') and cancellation.get('status') == ST_EXPIRED:
|
||||
out['expired'].append(ins['instruction_id'])
|
||||
continue
|
||||
exec_qty = int(ins.get("exec_qty") or 0)
|
||||
remaining = int(ins["qty"]) - exec_qty
|
||||
if ins.get("origin_type") == "plan" and ins.get("origin_id"):
|
||||
|
|
@ -453,9 +483,14 @@ def sweep_windows(*, now=None) -> dict:
|
|||
continue
|
||||
prog["window_verdict"] = v
|
||||
if v["verdict"] == "EXPIRED":
|
||||
pms_repo.update_instruction(ins["instruction_id"], status=ST_EXPIRED,
|
||||
progress=prog)
|
||||
out["expired"].append(ins["instruction_id"])
|
||||
if not pms_repo.update_instruction(ins["instruction_id"], progress=prog):
|
||||
raise RuntimeError('窗口到期状态未写入')
|
||||
cancellation = cancel_instruction(ins['instruction_id'], reason=v['note'],
|
||||
arbiter='system', final_status=ST_EXPIRED)
|
||||
if not cancellation.get('ok'):
|
||||
out['errors'].append(cancellation.get('error') or cancellation.get('message'))
|
||||
elif cancellation.get('confirmed') and cancellation.get('status') == ST_EXPIRED:
|
||||
out['expired'].append(ins['instruction_id'])
|
||||
else:
|
||||
pms_repo.update_instruction(ins["instruction_id"], progress=prog)
|
||||
out["partial"].append({"instruction_id": ins["instruction_id"],
|
||||
|
|
@ -471,36 +506,62 @@ def sweep_windows(*, now=None) -> dict:
|
|||
return out
|
||||
|
||||
|
||||
def cancel_instruction(instruction_id: str, reason: str = "页面人工撤销", *, arbiter="user") -> dict:
|
||||
ins = pms_repo.get_instruction(instruction_id)
|
||||
if not ins:
|
||||
return {"ok": False, "error": "指令不存在"}
|
||||
if ins["status"] not in LIVE:
|
||||
return {"ok": False, "error": f"指令处于 {ins['status']}, 不可撤销"}
|
||||
r = dispatcher.cancel(instruction_id=instruction_id,
|
||||
dispatch_ref=ins.get("dispatch_ref")) or {}
|
||||
# **下游没撤成就不能在本端标 CANCELLED。** 原来 r 拿到手却从不检查, 一律回「已撤销」
|
||||
# 并把父指令置终态 —— 而终态之后没有任何东西再跟踪它, 下游那张委托继续挂着、继续成交,
|
||||
# 成交回来还会因为找不到在途父指令而变成孤儿。撤不掉就保持在途, 让它继续被择时/收口/
|
||||
# 对账看见, 由人再处理。
|
||||
if not r.get("ok"):
|
||||
logger.error("[撤单] 下游未受理 %s: %s —— 指令保持在途, 下游委托可能仍挂着",
|
||||
instruction_id, r.get("error") or r)
|
||||
return {"ok": False, "downstream": r, "instruction_id": instruction_id,
|
||||
"message": f"指令 {instruction_id} **未能撤销**: "
|
||||
f"{r.get('error') or '下游未受理'} —— 指令仍在途, "
|
||||
f"请在 QMT 侧确认该委托是否还挂着"}
|
||||
# 进终态前把成交进度回写方案 (与 ledger._settle_instruction 同一条纪律, 2026-08-28)
|
||||
if ins.get("origin_type") == "plan" and ins.get("origin_id"):
|
||||
try:
|
||||
pms_repo.update_plan(ins["origin_id"], filled_qty=int(ins.get("exec_qty") or 0))
|
||||
except Exception as e:
|
||||
logger.warning("撤销指令进度回写失败 %s: %s", instruction_id, e)
|
||||
pms_repo.update_instruction(instruction_id, status=ST_CANCELLED)
|
||||
pms_repo.insert_ledger(ts_code=ins["ts_code"], action=ins.get("action"), arbiter=arbiter,
|
||||
verdict="REJECT", price_at=0, ref_id=instruction_id, reason=reason,
|
||||
hard_numbers=(ins.get('progress') or {}).get('plan') or {})
|
||||
return {"ok": True, "downstream": r, "message": f"指令 {instruction_id} 已撤销"}
|
||||
def cancel_instruction(instruction_id: str, reason: str = "页面人工撤销", *, arbiter="user",
|
||||
final_status=ST_CANCELLED) -> dict:
|
||||
"""先停止新下发,再请求撤子单;只有实际终态和入账确认后结束父指令。"""
|
||||
from app.services import execution_orders
|
||||
try:
|
||||
if final_status not in (ST_CANCELLED, ST_EXPIRED):
|
||||
raise ValueError('撤单后的父指令终态无效')
|
||||
with execution_orders.account_guard():
|
||||
ins = pms_repo.get_instruction(instruction_id)
|
||||
if not ins:
|
||||
return {"ok": False, "error": "指令不存在"}
|
||||
if ins['status'] in (ST_CANCELLED, ST_CONFIRMED, ST_EXPIRED):
|
||||
if dispatcher.mode() != dispatcher.MODE_WS or execution_orders.replacement_state(instruction_id)['ready']:
|
||||
return {"ok": True, "confirmed": True, "pending": False,
|
||||
"status": ins['status'],
|
||||
"message": f"指令 {instruction_id} 已结束"}
|
||||
elif ins['status'] not in LIVE:
|
||||
return {"ok": False, "error": f"指令处于 {ins['status']}, 不可撤销"}
|
||||
progress = dict(ins.get('progress') or {})
|
||||
if not progress.get('cancel_requested'):
|
||||
progress['cancel_requested'] = {'reason': reason, 'arbiter': arbiter,
|
||||
'at': datetime.now().isoformat(),
|
||||
'final_status': ST_EXPIRED if ins['status'] == ST_EXPIRED else final_status}
|
||||
if not pms_repo.update_instruction(instruction_id, progress=progress):
|
||||
raise RuntimeError('父指令停止下发标记未写入')
|
||||
result = dispatcher.cancel(instruction_id=instruction_id,
|
||||
dispatch_ref=ins.get('dispatch_ref')) or {}
|
||||
if not result.get('ok'):
|
||||
return {'ok': False, 'downstream': result, 'instruction_id': instruction_id,
|
||||
'message': '撤单请求未成功,父指令停止新增下发并等待重试'}
|
||||
if dispatcher.mode() == dispatcher.MODE_WS:
|
||||
state = execution_orders.replacement_state(instruction_id)
|
||||
if not state['ready']:
|
||||
return {'ok': True, 'confirmed': False, 'pending': True,
|
||||
'downstream': result, 'instruction_id': instruction_id,
|
||||
'message': f'指令 {instruction_id} 已请求撤单,等待委托终态与成交入账确认'}
|
||||
fresh = pms_repo.get_instruction(instruction_id) or ins
|
||||
done = int(fresh.get('exec_qty') or 0)
|
||||
if fresh.get('origin_type') == 'plan' and fresh.get('origin_id'):
|
||||
pms_repo.update_plan(fresh['origin_id'], filled_qty=done)
|
||||
requested_status = ((fresh.get('progress') or {}).get('cancel_requested') or {}).get('final_status', ST_CANCELLED)
|
||||
status = ST_CONFIRMED if done >= int(fresh['qty']) else requested_status
|
||||
if status not in (ST_CONFIRMED, ST_CANCELLED, ST_EXPIRED):
|
||||
raise ValueError('撤单确认的父指令终态无效')
|
||||
if not pms_repo.update_instruction(instruction_id, status=status):
|
||||
raise RuntimeError('父指令撤单确认未落表')
|
||||
pms_repo.insert_ledger(ts_code=fresh['ts_code'], action=fresh.get('action'),
|
||||
arbiter=arbiter, verdict='REJECT', price_at=0, ref_id=instruction_id,
|
||||
reason=reason, hard_numbers=(fresh.get('progress') or {}).get('plan') or {})
|
||||
return {'ok': True, 'confirmed': True, 'pending': False, 'downstream': result,
|
||||
'status': status,
|
||||
'message': f'指令 {instruction_id} 已全额成交' if status == ST_CONFIRMED else f'指令 {instruction_id} 撤单已确认'}
|
||||
except Exception as exc:
|
||||
logger.exception('撤单确认失败 %s', instruction_id)
|
||||
return {'ok': False, 'pending': True, 'confirmed': False, 'instruction_id': instruction_id,
|
||||
'error': str(exc), 'message': '撤单状态待核实:'+str(exc)}
|
||||
|
||||
|
||||
# ================================================================ 内部
|
||||
|
|
|
|||
|
|
@ -257,6 +257,9 @@ def request(candidate: dict, context: dict = None, *, timeout: int = None,
|
|||
to = int(timeout or param_store.get_int("PMS_JUDGE_TIMEOUT", 90))
|
||||
url = base_url() + (param_store.get("PMS_JUDGE_PATH", "/api/intraday/pms_judge") or "")
|
||||
payload = jsonable(payload) # 数据库类型 (Decimal/日期) 换成 JSON 认得的, 见 jsonable
|
||||
if param_store.get_bool('PMS_JUDGE_ASYNC_ENABLED', False):
|
||||
from app.services import judge_async
|
||||
return judge_async.request(candidate, payload, base_url())
|
||||
try:
|
||||
import requests
|
||||
r = requests.post(url, json=payload, timeout=to)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,148 @@
|
|||
"""研判请求先持久化,短请求登记或查询;不在执行循环等待模型。"""
|
||||
import copy
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import uuid
|
||||
from datetime import timedelta
|
||||
|
||||
from app.core.execution_policy import local_now, timestamp
|
||||
from app.repo import rotation_repo as repo
|
||||
from app.services import param_store
|
||||
|
||||
logger = logging.getLogger('pms.judge_async')
|
||||
KIND = 'judge_request'
|
||||
|
||||
|
||||
def digest(value):
|
||||
return hashlib.sha256(json.dumps(value, sort_keys=True, ensure_ascii=False,
|
||||
separators=(',', ':'), allow_nan=False).encode()).hexdigest()
|
||||
|
||||
|
||||
def identity(candidate, payload):
|
||||
"""研究内容和实际持仓变更才换请求;价格、资金、候选排名每跳重核。"""
|
||||
hard = candidate.get('hard_numbers') or {}
|
||||
research = hard.get('plan_basis')
|
||||
if research is None:
|
||||
research = {k: v for k, v in payload.get('hard_numbers', {}).items()
|
||||
if k in ('score', 'theme', 'tier', 'upside', 'heat', 'sector', 'verdict', 'reasons',
|
||||
'missing', 'risk', 'events_text', 'pricing_text', 'news_text',
|
||||
'company_review_text', 'intraday_buy_signal', 'intraday_buy_reason',
|
||||
'logic_state', 'support', 'pressure', 'ref_source')}
|
||||
position = (payload.get('context') or {}).get('position') or {}
|
||||
return digest({'research': research, 'position': {k:position.get(k) for k in
|
||||
('total_qty', 'base_qty', 'add_qty', 'dca_qty', 'avg_cost', 'support_ref', 'pressure_ref', 'stop_ref')},
|
||||
'book': candidate.get('book'), 'source': hard.get('candidate_source'),
|
||||
'stage': {k: hard.get(k) for k in ('dca_stage', 'stage', 'dca_count', 'trigger', 'logic_state')},
|
||||
# OPEN数量由PMS实时复核,持仓增持的模型证据还包含本次动作数量。
|
||||
'action_qty': candidate.get('qty') if payload.get('action') != 'OPEN' else None})
|
||||
|
||||
|
||||
def unavailable(reason, **extra):
|
||||
return {'verdict': 'UNAVAILABLE', 'reason': reason, 'degraded': True,
|
||||
'confidence': None, 'raw': extra or None}
|
||||
|
||||
|
||||
def pending(doc):
|
||||
return {'verdict': 'PENDING', 'reason': '研判已登记,等待结果,其他执行继续',
|
||||
'degraded': False, 'confidence': None,
|
||||
'raw': {'request_id': doc['request_id'], 'deadline_at': doc['deadline_at']}}
|
||||
|
||||
|
||||
def _result(doc, candidate):
|
||||
from app.services.judge import _map_verdict
|
||||
if doc.get('reask'):
|
||||
candidate['reask'] = copy.deepcopy(doc['reask'])
|
||||
return _map_verdict(doc['result'])
|
||||
|
||||
|
||||
def request(candidate, payload, base, now=None):
|
||||
"""同一逻辑请求重启后续查;网络故障重发原编号,过期不补发模型。"""
|
||||
current = local_now(now)
|
||||
code = str(payload.get('ts_code') or '').upper()
|
||||
if not re.fullmatch(r'(?:60[0-9]{4}\.SH|68[0-9]{4}\.SH|00[0-9]{4}\.SZ|30[0-9]{4}\.SZ)', code):
|
||||
return unavailable('研判证券不在沪深支持范围')
|
||||
scope = {'day': str(current.date()), 'code': code, 'action': payload.get('action'),
|
||||
'book': candidate.get('book'),
|
||||
'source': (candidate.get('hard_numbers') or {}).get('candidate_source')}
|
||||
key = 'judge:' + digest(scope)
|
||||
material = identity(candidate, payload)
|
||||
saved = None
|
||||
try:
|
||||
with repo.locked(key, KIND, {}) as doc:
|
||||
stale = (doc.get('result', {}).get('verdict') in ('PASS', 'REJECT')
|
||||
and current >= timestamp(doc['cache_until']))
|
||||
force_new = payload.get('force_refresh') and not doc.get('forced')
|
||||
if not doc or doc.get('material') != material or stale or force_new:
|
||||
doc.clear()
|
||||
deadline = current + timedelta(seconds=param_store.get_int('PMS_JUDGE_ASYNC_DEADLINE_SEC', 120))
|
||||
doc.update(scope=scope, request_id='PMS_'+uuid.uuid4().hex, payload=copy.deepcopy(payload),
|
||||
material=material, fingerprint=digest(payload), deadline_at=deadline.isoformat(),
|
||||
cache_until=(deadline+timedelta(seconds=param_store.get_int('PMS_JUDGE_ASYNC_CACHE_SEC',120))).isoformat(),
|
||||
created_at=current.isoformat(), forced=bool(payload.get('force_refresh')),
|
||||
reask=copy.deepcopy(candidate.get('reask')), submitted=False)
|
||||
if doc.get('result'):
|
||||
doc['delivered'] = True
|
||||
return _result(doc, candidate)
|
||||
if current >= timestamp(doc['deadline_at']):
|
||||
doc['result'] = {'verdict':'UNAVAILABLE', 'reason':'研判总等待时间已到,转人工确认', 'error_code':'EXPIRED'}
|
||||
doc['delivered'] = True
|
||||
return _result(doc, candidate)
|
||||
# 短租约避免同一请求被两个扫描同时轮询。网络不在数据库事务内。
|
||||
lease = timestamp(doc.get('poll_until'))
|
||||
if lease and current < lease:
|
||||
return pending(doc)
|
||||
doc['poll_until'] = (current+timedelta(seconds=param_store.get_int('PMS_JUDGE_ASYNC_HTTP_TIMEOUT',3)+2)).isoformat()
|
||||
saved = copy.deepcopy(doc)
|
||||
import requests
|
||||
timeout = min(param_store.get_int('PMS_JUDGE_ASYNC_HTTP_TIMEOUT',3),
|
||||
max(.1,(timestamp(saved['deadline_at'])-current).total_seconds()))
|
||||
if saved['submitted']:
|
||||
response = requests.get(base+'/api/intraday/pms_judge_result/'+saved['request_id'], timeout=timeout)
|
||||
else:
|
||||
response = requests.post(base+'/api/intraday/pms_judge_submit', timeout=timeout,
|
||||
json=dict(saved['payload'], request_id=saved['request_id'], deadline_at=saved['deadline_at'],
|
||||
evidence_fingerprint=saved['fingerprint']))
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
finished = local_now() if now is None else current
|
||||
with repo.locked(key, KIND, {}) as doc:
|
||||
if doc.get('request_id') != saved['request_id']:
|
||||
return unavailable('研究依据已变化,本次旧结果已作废', error_code='SUPERSEDED')
|
||||
doc.pop('poll_until', None)
|
||||
if finished >= timestamp(doc['deadline_at']):
|
||||
data = {'verdict':'UNAVAILABLE', 'reason':'结果到达时已超过截止时间,转人工确认', 'error_code':'EXPIRED'}
|
||||
elif (not isinstance(data,dict) or data.get('request_id') != doc['request_id']
|
||||
or (data.get('verdict') in ('PASS','REJECT','PENDING') and
|
||||
data.get('evidence_fingerprint') != doc['fingerprint'])):
|
||||
data = {'verdict':'UNAVAILABLE', 'reason':'研判结果编号或证据不匹配,转人工确认', 'error_code':'INVALID_RESPONSE'}
|
||||
if data.get('error_code') == 'NOT_FOUND':
|
||||
doc['submitted'] = False # 同编号重试,不另造一个模型任务。
|
||||
return pending(doc)
|
||||
doc['submitted'] = True
|
||||
if data.get('verdict') == 'PENDING':
|
||||
return pending(doc)
|
||||
doc['result'] = data
|
||||
doc['completed_at'] = finished.isoformat()
|
||||
doc['cache_until'] = (finished+timedelta(seconds=param_store.get_int('PMS_JUDGE_ASYNC_CACHE_SEC',120))).isoformat()
|
||||
doc['delivered'] = True
|
||||
return _result(doc, candidate)
|
||||
except Exception as exc:
|
||||
logger.warning('研判登记或查询暂不可用 [%s]: %s', code, type(exc).__name__)
|
||||
# 已可靠登记且尚未过期,传输失败保留轮询;否则在途提议会挡住结果消费。
|
||||
if saved is not None and local_now(now) < timestamp(saved['deadline_at']):
|
||||
return pending(saved)
|
||||
return unavailable('研判登记或查询暂不可用,转人工确认', error_code=type(exc).__name__)
|
||||
|
||||
|
||||
def resumable_reasks(now=None):
|
||||
"""仅让已经登记的重问穿过旧模型驳回,不越过用户拒绝、在途或规则闸。"""
|
||||
today = str(local_now(now).date())
|
||||
values = {}
|
||||
for row in repo.recent(KIND,1000):
|
||||
doc = row['value']
|
||||
if doc.get('scope',{}).get('day') == today and doc.get('reask') and not doc.get('delivered'):
|
||||
scope = doc['scope']
|
||||
values[(scope['code'],scope['action'])] = copy.deepcopy(doc['reask'])
|
||||
return values
|
||||
|
|
@ -113,6 +113,38 @@ def day_snapshot(ts_code: str) -> dict:
|
|||
return {}
|
||||
|
||||
|
||||
|
||||
def execution_quote(code, now=None, limits=None):
|
||||
"""执行用分钟行情。没有盘口或停牌证据时保留未知,不把昨收冒充现价。"""
|
||||
import re
|
||||
from app.core.execution_policy import local_now
|
||||
code = str(code or '').strip().upper()
|
||||
if not re.fullmatch(r'(?:60[0-9]{4}\.SH|68[0-9]{4}\.SH|00[0-9]{4}\.SZ|30[0-9]{4}\.SZ)', code):
|
||||
return {}
|
||||
current = local_now(now)
|
||||
bars = _bars(code)
|
||||
if not bars:
|
||||
return {}
|
||||
last = bars[-1]
|
||||
try:
|
||||
at = datetime.fromisoformat(str(last.get('time') or '').replace('Z', '+00:00'))
|
||||
# 已核实源分钟线time为北京时间;必须带完整日期,不能用今天补造旧行情。
|
||||
at = local_now(at)
|
||||
price = float(last.get('close') or 0)
|
||||
if price <= 0:
|
||||
return {}
|
||||
except (ValueError, TypeError):
|
||||
return {}
|
||||
if limits is None:
|
||||
rows = fetch_all('SELECT up_limit,down_limit FROM gp_stk_limit '
|
||||
'WHERE trade_date=:day AND ts_code=:code',
|
||||
{'day': current.date().isoformat(), 'code': code})
|
||||
limits = rows[0] if rows else {}
|
||||
return {'kind': 'minute', 'source': 'tushare:rt_min:1MIN',
|
||||
'as_of': at.isoformat(), 'last': price, 'bid': None, 'ask': None,
|
||||
'limit_up': (limits or {}).get('up_limit'),
|
||||
'limit_down': (limits or {}).get('down_limit'), 'halted': None}
|
||||
|
||||
# ================================================================ 参考位
|
||||
def _shard_tables(months: int = 4) -> list:
|
||||
now = datetime.now()
|
||||
|
|
|
|||
|
|
@ -40,6 +40,28 @@ SECRET_KEYS = ("PMS_QMT_SIGN_SEED_HEX", "PMS_QMT_PEER_PUBKEY_B64", "PMS_SESSION_
|
|||
|
||||
# 运行态开关: key -> (默认值, 类型, 说明)
|
||||
RUNTIME_EXTRA = {
|
||||
'PMS_EXEC_REJECT_COOLDOWN_SEC': (60, int, '拒单后的首次退避秒数,不限制正常改价'),
|
||||
'PMS_EXEC_REJECT_MAX_COOLDOWN_SEC': (900, int, '重复拒单退避最大秒数'),
|
||||
'PMS_EXEC_REPRICE_ENABLED': (False, bool, '已授权委托按新鲜行情撤单确认后重挂;先发布上游执行依据接口'),
|
||||
'PMS_EXEC_BASIS_REFRESH_SEC': (60, int, '执行依据刷新间隔,秒'),
|
||||
'PMS_EXEC_BASIS_REFRESH_BUDGET_SEC': (20, int, '每轮依据刷新时间预算,秒'),
|
||||
'PMS_EXEC_BASIS_SOURCE_MAX_DAYS': (7, int, '执行依据原始数据最大自然日龄'),
|
||||
'PMS_EXEC_QUOTE_MAX_AGE_SEC': (15, int, '实时盘口最大允许时龄,秒'),
|
||||
'PMS_EXEC_ALLOW_MINUTE_QUOTE': (True, bool, '允许明确留痕的分钟行情降级报价;不冒充盘口'),
|
||||
'PMS_EXEC_MINUTE_MAX_AGE_SEC': (90, int, '分钟行情最大允许时龄,秒'),
|
||||
'PMS_EXEC_PRICE_OFFSET_TICKS': (1, int, '相对对手盘增加的最小价格单位数'),
|
||||
'PMS_EXEC_REPRICE_MIN_SEC': (60, int, '两次撤换委托的最小间隔,秒'),
|
||||
'PMS_EXEC_REPRICE_TICKS': (2, int, '触发撤换的最小价差单位数'),
|
||||
'PMS_EXEC_REPRICE_MAX_ATTEMPTS': (0, int, '窗口内撤换次数上限,0表示不限;始终受命令和时段约束'),
|
||||
'PMS_EXEC_MAX_SLIPPAGE_PCT': (0.01, float, '相对本次新鲜行情的最大报价偏移,不锁定首笔价格'),
|
||||
'PMS_EXEC_MINUTE_SLIPPAGE_PCT': (0.002, float, '分钟行情降级限价偏移比例'),
|
||||
'PMS_ROTATION_CANDIDATE_POOL_SIZE': (100, int, '微盘v3冻结候选总数,包含首选和有序备选'),
|
||||
'PMS_JUDGE_ASYNC_ENABLED': (False, bool, '异步登记研判并轮询结果,超时转人工;须先发布上游'),
|
||||
'PMS_JUDGE_ASYNC_HTTP_TIMEOUT': (3, int, '异步提交或查询单次网络超时,秒'),
|
||||
'PMS_JUDGE_ASYNC_DEADLINE_SEC': (120, int, '含排队时间的研判总截止秒数'),
|
||||
'PMS_JUDGE_ASYNC_CACHE_SEC': (120, int, '相同研究依据结果可复用时间,秒'),
|
||||
'PMS_JUDGE_ASYNC_SCAN_BUDGET_SEC': (20, int, '每轮异步研判登记与查询总预算,秒'),
|
||||
|
||||
'PMS_ROTATION_MODE': ('observe', str, '微盘轮换模式:observe观察、paper独立纸面、auto自动。自动须通过验收门槛'),
|
||||
'PMS_ROTATION_SIM_DIRECT': (False, bool, '155模拟仓直接启用,取消观察及纸面等待;保留账本、资金、数据、权限和用户控制检查'),
|
||||
'PMS_ROTATION_MONITOR_URL': ('', str, '微盘监控只读接口根地址,留空停用拉取'),
|
||||
|
|
@ -507,6 +529,25 @@ def set_param(key: str, value, updated_by: str = "user") -> dict:
|
|||
|
||||
|
||||
_RANGES = {
|
||||
'PMS_EXEC_REJECT_COOLDOWN_SEC': (1, 3600),
|
||||
'PMS_EXEC_REJECT_MAX_COOLDOWN_SEC': (1, 86400),
|
||||
'PMS_EXEC_BASIS_REFRESH_SEC': (0, 3600),
|
||||
'PMS_EXEC_BASIS_REFRESH_BUDGET_SEC': (0, 3600),
|
||||
'PMS_EXEC_BASIS_SOURCE_MAX_DAYS': (0, 30),
|
||||
'PMS_EXEC_QUOTE_MAX_AGE_SEC': (0, 3600),
|
||||
'PMS_EXEC_MINUTE_MAX_AGE_SEC': (0, 3600),
|
||||
'PMS_EXEC_PRICE_OFFSET_TICKS': (0, 3600),
|
||||
'PMS_EXEC_REPRICE_MIN_SEC': (0, 3600),
|
||||
'PMS_EXEC_REPRICE_TICKS': (0, 3600),
|
||||
'PMS_EXEC_REPRICE_MAX_ATTEMPTS': (0, 3600),
|
||||
'PMS_ROTATION_CANDIDATE_POOL_SIZE': (50, 500),
|
||||
'PMS_JUDGE_ASYNC_HTTP_TIMEOUT': (1, 10),
|
||||
'PMS_JUDGE_ASYNC_DEADLINE_SEC': (10, 180),
|
||||
'PMS_JUDGE_ASYNC_CACHE_SEC': (0, 3600),
|
||||
'PMS_JUDGE_ASYNC_SCAN_BUDGET_SEC': (0, 3600),
|
||||
'PMS_EXEC_MAX_SLIPPAGE_PCT': (0, 0.1),
|
||||
'PMS_EXEC_MINUTE_SLIPPAGE_PCT': (0, 0.05),
|
||||
|
||||
"PMS_PORTFOLIO_CAP": (0, 1), "PMS_STOCK_CAP": (0, 1), "PMS_STOCK_TARGET_DEFAULT": (0, 1),
|
||||
"PMS_CASH_RESERVE": (0, 1), "PMS_CUSHION_SOLID": (0, 1), "PMS_TRIM_GIVEBACK": (0, 1),
|
||||
"PMS_DCA_MAX_RATIO": (0, 1), "PMS_SECTOR_MAX_RATIO": (0, 1), "PMS_T0_RATIO_MAX": (0, 0.3334),
|
||||
|
|
|
|||
|
|
@ -269,6 +269,8 @@ def refresh_one(p, *, now=None, deadline=None):
|
|||
td.ymd() < param_store.get_int('PMS_BRAKE_UNTIL', 0), now, True, out,
|
||||
review_only=True, deadline=deadline, prepared_verdict=verdict)
|
||||
if not result:
|
||||
if any(r.get('by') == 'judge_pending' for r in out.get('skipped', [])):
|
||||
return _pending_response(p, '研究更新已登记,等待研判结果', 'judge_pending')
|
||||
return _failed_recheck(p, out)
|
||||
hn = c['hard_numbers']
|
||||
hn.update({k:hard[k] for k in ('requested_by','requested_at') if k in hard})
|
||||
|
|
|
|||
|
|
@ -129,6 +129,9 @@ def scan_and_route(*, now=None, dry_run: bool = False) -> dict:
|
|||
_unlocked = reask_service.evaluate_all(_jk, now)
|
||||
except Exception as e:
|
||||
logger.warning("[新建仓] 解锁判定出错 (本轮当日闸照旧全挡): %s", e)
|
||||
if param_store.get_bool('PMS_JUDGE_ASYNC_ENABLED', False):
|
||||
from app.services import judge_async
|
||||
_unlocked.update(judge_async.resumable_reasks(now))
|
||||
for _k in _unlocked:
|
||||
_jk.pop(_k, None)
|
||||
out["reask_unlocked"] = [{"ts_code": c, "action": a, "why": v.get("why"),
|
||||
|
|
@ -231,8 +234,10 @@ def scan_and_route(*, now=None, dry_run: bool = False) -> dict:
|
|||
|
||||
# 研判的时间预算从这一刻起算。**先跑已有持仓的四类, 再跑新建仓** —— 预算真用尽时,
|
||||
# 被推到下一跳的一定是新建仓, 已有持仓的动作行为与 2026-08-06 之前一致。
|
||||
deadline = time.monotonic() + max(
|
||||
0, param_store.get_int("PMS_JUDGE_TICK_BUDGET_SEC", 150))
|
||||
budget = (param_store.get_int('PMS_JUDGE_ASYNC_SCAN_BUDGET_SEC', 20)
|
||||
if param_store.get_bool('PMS_JUDGE_ASYNC_ENABLED', False)
|
||||
else param_store.get_int('PMS_JUDGE_TICK_BUDGET_SEC', 150))
|
||||
deadline = time.monotonic() + max(0, budget)
|
||||
|
||||
for group_index, group in enumerate((list(scanned['candidates']), open_cands)):
|
||||
if group_index == 1 and not dry_run:
|
||||
|
|
@ -889,9 +894,17 @@ def _route_one(c, view, params, stock_params, brake_active, now, dry_run, out,
|
|||
previous = None
|
||||
# 即使上一跳因版本变化放弃自动采纳,下一跳也不能复用该票旧批准的缓存。
|
||||
_force = _force or needs_new_judgement(c.get('hard_numbers') or {}, previous)
|
||||
verdict = judge.request(c, context={"position": _judge_ctx(pos),
|
||||
"recent_ledger": _recent_ledger(code)},
|
||||
force=_force or bool(c.get('_plan_recheck')), reask_text=_rtext)
|
||||
if dry_run and not review_only and param_store.get_bool('PMS_JUDGE_ASYNC_ENABLED', False):
|
||||
verdict = {'verdict': judge.UNAVAILABLE, 'degraded': True, 'confidence': None,
|
||||
'reason': '试算不登记异步研判,实际处理时再请求'}
|
||||
else:
|
||||
verdict = judge.request(c, context={"position": _judge_ctx(pos),
|
||||
"recent_ledger": _recent_ledger(code)},
|
||||
force=_force or bool(c.get('_plan_recheck')), reask_text=_rtext)
|
||||
if verdict.get('verdict') == 'PENDING':
|
||||
out['skipped'].append({'ts_code': code, 'action': action, 'by': 'judge_pending',
|
||||
'why': verdict['reason'], 'request': verdict.get('raw')})
|
||||
return
|
||||
# 新建仓的低把握驳回改交人 (2026-09-07, 第二道保险)。择时决策系统的提示词已改成
|
||||
# 「证据不足以判断 → 不可用」, 但模型未必每次守得住; 它给的把握度是现成的读数,
|
||||
# 低于阈值的驳回按「不可用」处理 —— 进人的待确认队列, 不记驳回、不杀提议。
|
||||
|
|
@ -1166,7 +1179,9 @@ def _judge_budget_left(deadline) -> bool:
|
|||
"""
|
||||
if deadline is None:
|
||||
return True
|
||||
need = max(1, param_store.get_int("PMS_JUDGE_TIMEOUT", 90))
|
||||
need = max(1, param_store.get_int('PMS_JUDGE_ASYNC_HTTP_TIMEOUT', 3)
|
||||
if param_store.get_bool('PMS_JUDGE_ASYNC_ENABLED', False)
|
||||
else param_store.get_int("PMS_JUDGE_TIMEOUT", 90))
|
||||
return (time.monotonic() + need) <= deadline
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,46 @@ 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
|
||||
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'), order.get('final_at')):
|
||||
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']), int(order.get('cum_qty') or 0), 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
|
||||
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
|
||||
|
|
@ -36,8 +76,8 @@ def enqueue_with_reservation(payload, parent_id):
|
|||
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 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 {}
|
||||
|
|
@ -76,9 +116,13 @@ def enqueue_with_reservation(payload, parent_id):
|
|||
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))
|
||||
settled = not reserved or (len(terminal) == len(reserved)
|
||||
and all(_later_snapshot(funds['received_at'], o['final_at']) 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={}, market_value=float(parsed.get('market_value') or 0))
|
||||
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())
|
||||
|
|
@ -101,24 +145,28 @@ def enqueue_with_reservation(payload, parent_id):
|
|||
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
|
||||
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': core.RULE, 'snapshot_version': snapshot_version},
|
||||
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'
|
||||
|
|
@ -128,22 +176,138 @@ def send_intent(intent, price, now, snapshot_version):
|
|||
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': '已有终态记录,次日重新规划'}
|
||||
child = parent+'_D01'
|
||||
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=intent['qty'], limit_price=price,
|
||||
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['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'}]
|
||||
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'), 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'):
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ def documents_ready():
|
|||
def configuration():
|
||||
return {'mode': str(param_store.get('PMS_ROTATION_MODE', 'observe')),
|
||||
'rule_version': str(param_store.get('PMS_ROTATION_RULE_VERSION', core.RULE)),
|
||||
'candidate_pool_size': param_store.get_int('PMS_ROTATION_CANDIDATE_POOL_SIZE', 100),
|
||||
'control': str(param_store.get('PMS_ROTATION_CONTROL', 'run')),
|
||||
'url': str(param_store.get('PMS_ROTATION_MONITOR_URL', '')).rstrip('/'),
|
||||
'boards': set(str(param_store.get('PMS_MICRO30_ALLOWED_BOARDS', '')).split(','))-{''},
|
||||
|
|
@ -33,7 +34,7 @@ def configuration():
|
|||
|
||||
def pull(execution_date=None):
|
||||
config = configuration()
|
||||
if config['rule_version'] != core.RULE:
|
||||
if config['rule_version'] not in core.SUPPORTED_RULES:
|
||||
raise ValueError('运行规则与已部署代码不一致')
|
||||
day = execution_date or dt.datetime.now(TZ).date().isoformat()
|
||||
if not config['url']:
|
||||
|
|
@ -43,7 +44,7 @@ def pull(execution_date=None):
|
|||
if len(response.content) > 1024*1024:
|
||||
raise ValueError('监控快照超过大小限制')
|
||||
value = response.json()
|
||||
errors = core.snapshot_check(value, day, require_frozen=False)
|
||||
errors = core.snapshot_check(value, day, require_frozen=False, expected_rule=config['rule_version'], candidate_pool_size=config.get('candidate_pool_size', 100))
|
||||
if errors:
|
||||
raise ValueError(';'.join(errors))
|
||||
return value
|
||||
|
|
@ -54,7 +55,7 @@ def activation_errors():
|
|||
return ['本次策略只开放155模拟实例']
|
||||
if not settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
return ['策略账本迁移尚未启用']
|
||||
if configuration()['rule_version'] != core.RULE:
|
||||
if configuration()['rule_version'] not in core.SUPPORTED_RULES:
|
||||
return ['运行规则与已部署版本不一致']
|
||||
if param_store.get_bool('PMS_ROTATION_SIM_DIRECT', False):
|
||||
# 用户授权155模拟仓直接运行。该授权不伪造历史验收或交易日记录。
|
||||
|
|
@ -70,7 +71,7 @@ def activation_errors():
|
|||
if any(not math.isfinite(v) or v < 0 for v in config['costs'].values()):
|
||||
errors.append('交易费用参数无效')
|
||||
return errors
|
||||
evidence, _ = store.get('validation:'+core.RULE)
|
||||
evidence, _ = store.get('validation:'+configuration()['rule_version'])
|
||||
evidence = evidence or {}
|
||||
required = ('backtest', 'capacity', 'fees', 'board_permissions', 'migration', 'regression', 'browser', 'fill_attribution', 'fund_reservation')
|
||||
labels = dict(backtest='回测', capacity='成交容量', fees='交易费用', board_permissions='板块权限',
|
||||
|
|
@ -78,8 +79,8 @@ def activation_errors():
|
|||
errors = [labels[k]+'尚未验收' for k in required if not evidence.get(k)]
|
||||
observed = store.recent('observed_day', 1000)
|
||||
paper = store.recent('paper_day', 1000)
|
||||
dates = sorted({r['value']['date'] for r in observed if r['value'].get('rule_version') == core.RULE})
|
||||
paper_dates = sorted({r['value']['date'] for r in paper if r['value'].get('rule_version') == core.RULE and r['value'].get('healthy')})
|
||||
dates = sorted({r['value']['date'] for r in observed if r['value'].get('rule_version') == configuration()['rule_version']})
|
||||
paper_dates = sorted({r['value']['date'] for r in paper if r['value'].get('rule_version') == configuration()['rule_version'] and r['value'].get('healthy')})
|
||||
if len(dates) < 3:
|
||||
errors.append('有效观察不足3个交易日')
|
||||
if len(paper_dates) < 10 or (len(dates) >= 3 and any(d <= dates[2] for d in paper_dates[:10])):
|
||||
|
|
@ -93,7 +94,7 @@ def signal_history(end, limit=60):
|
|||
if day > dt.datetime.now(TZ).date():
|
||||
raise ValueError('趋势日期不能晚于今天')
|
||||
config = configuration()
|
||||
if config['rule_version'] != core.RULE:
|
||||
if config['rule_version'] not in core.SUPPORTED_RULES:
|
||||
raise ValueError('运行规则与趋势代码不一致')
|
||||
url = config['url']
|
||||
if not url:
|
||||
|
|
@ -105,7 +106,7 @@ def signal_history(end, limit=60):
|
|||
raise ValueError('趋势数据超过大小限制')
|
||||
value = response.json()
|
||||
rows = value.get('records')
|
||||
if value.get('rule_version') != core.RULE or value.get('rule_parameters') != core.RULE_PARAMETERS or not isinstance(rows, list) or len(rows) > n:
|
||||
if value.get('rule_version') != config['rule_version'] or value.get('rule_parameters') != core.rule_parameters(config['rule_version'], config.get('candidate_pool_size', 100)) or not isinstance(rows, list) or len(rows) > n:
|
||||
raise ValueError('趋势规则或格式不符')
|
||||
previous = ''
|
||||
for row in rows:
|
||||
|
|
@ -124,7 +125,7 @@ def signal_history(end, limit=60):
|
|||
def status():
|
||||
config = configuration()
|
||||
out = {'mode': config['mode'], 'control': config['control'], 'rule_version': config['rule_version'],
|
||||
'rule_parameters': dict(core.RULE_PARAMETERS) if config['rule_version'] == core.RULE else {},
|
||||
'rule_parameters': core.rule_parameters(config['rule_version'], config.get('candidate_pool_size', 100)) if config['rule_version'] in core.SUPPORTED_RULES else {},
|
||||
'simulation_direct': not settings.PMS_TABLE_PREFIX and param_store.get_bool('PMS_ROTATION_SIM_DIRECT', False),
|
||||
'basket_size': core.SIZE, 'schema_ready': settings.INFRA_STRATEGY_BOOK_SCHEMA, 'documents_ready': documents_ready(),
|
||||
'phase': 'original', 'snapshot': None, 'errors': [], 'activation_errors': []}
|
||||
|
|
@ -138,8 +139,8 @@ def status():
|
|||
out.update(state=value, phase=value.get('rotation', {}).get('phase', 'original'))
|
||||
out['activation_errors'] = activation_errors()
|
||||
out['recent'] = store.recent('execution', 30)
|
||||
out['observed_days'] = len({r['value']['date'] for r in store.recent('observed_day', 1000) if r['value'].get('rule_version') == core.RULE})
|
||||
out['paper_days'] = len({r['value']['date'] for r in store.recent('paper_day', 1000) if r['value'].get('rule_version') == core.RULE and r['value'].get('healthy')})
|
||||
out['observed_days'] = len({r['value']['date'] for r in store.recent('observed_day', 1000) if r['value'].get('rule_version') == configuration()['rule_version']})
|
||||
out['paper_days'] = len({r['value']['date'] for r in store.recent('paper_day', 1000) if r['value'].get('rule_version') == configuration()['rule_version'] and r['value'].get('healthy')})
|
||||
out['daily'] = store.recent('paper_day' if config['mode'] == 'paper' else 'live_day', 500)
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA and config['mode'] != 'paper':
|
||||
try:
|
||||
|
|
@ -162,7 +163,7 @@ def control(*, mode=None, action=None, actor='user'):
|
|||
if mode in ('paper', 'auto') and not documents_ready():
|
||||
raise ValueError('观察与纸面记录表尚未建立')
|
||||
if mode == 'paper' and not (not settings.PMS_TABLE_PREFIX and param_store.get_bool('PMS_ROTATION_SIM_DIRECT', False)):
|
||||
days = {r['value']['date'] for r in store.recent('observed_day', 1000) if r['value'].get('rule_version') == core.RULE}
|
||||
days = {r['value']['date'] for r in store.recent('observed_day', 1000) if r['value'].get('rule_version') == configuration()['rule_version']}
|
||||
if len(days) < 3:
|
||||
raise ValueError('有效观察不足3个交易日,暂不进入纸面运行')
|
||||
if mode == 'auto':
|
||||
|
|
@ -257,6 +258,21 @@ def _limits(day):
|
|||
return {r['ts_code']: r for r in rows}
|
||||
|
||||
|
||||
def _unavailable_candidates(snapshot, quotes, budget, boards, costs):
|
||||
"""仅明确停牌或静态交易资格问题递补;行情缺失和价格落后都不是停牌。"""
|
||||
unavailable = {}
|
||||
for member in snapshot.get('candidates', snapshot.get('members', [])):
|
||||
code = member['code']
|
||||
quote = quotes.get(code, {})
|
||||
if quote.get('halted') is True:
|
||||
unavailable[code] = '行情来源明确标记停牌'
|
||||
elif core.board(code) not in boards:
|
||||
unavailable[code] = '账户没有该板块权限'
|
||||
elif budget > 0 and not core.buy_qty(code, float(member.get('close') or 0), budget/core.SIZE, costs):
|
||||
unavailable[code] = '单股预算不足最低交易单位及费用'
|
||||
return unavailable
|
||||
|
||||
|
||||
def _micro_unsettled(pending):
|
||||
"""本地撤销不等于下游终态;未入账成交也不能视为退出已完成。"""
|
||||
if any(r['book_id'] == books.MICRO for r in pending) or qmt_repo.inbox_pending_count():
|
||||
|
|
@ -297,7 +313,7 @@ def _tick(now=None, *, prepare_only=False):
|
|||
day = now.date().isoformat()
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA and config['mode'] == 'auto':
|
||||
cancel_control_orders(config['control'])
|
||||
if config['mode'] == 'auto' and settings.INFRA_STRATEGY_BOOK_SCHEMA and now.hour*60+now.minute >= 600:
|
||||
if config['mode'] == 'auto' and settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
from app.services.rotation_execution import cancel_expired
|
||||
cancel_expired(now)
|
||||
if td.calendar_degraded(day) or not td.is_trade_day(day):
|
||||
|
|
@ -309,7 +325,7 @@ def _tick(now=None, *, prepare_only=False):
|
|||
value, errors = None, []
|
||||
try:
|
||||
value = pull(day)
|
||||
errors = core.snapshot_check(value, day)
|
||||
errors = core.snapshot_check(value, day, expected_rule=config['rule_version'], candidate_pool_size=config.get('candidate_pool_size', 100))
|
||||
except Exception as exc:
|
||||
errors = ['监控暂不可用:'+type(exc).__name__]
|
||||
if errors:
|
||||
|
|
@ -319,7 +335,7 @@ def _tick(now=None, *, prepare_only=False):
|
|||
if value:
|
||||
store.put_once('snapshot:'+day+':'+value['snapshot_version'], 'snapshot', value)
|
||||
if value['signal_ready'] and value['basket_ready']:
|
||||
store.put_once('observed:'+day, 'observed_day', {'date': day, 'rule_version': core.RULE, 'snapshot_version': value['snapshot_version']})
|
||||
store.put_once('observed:'+day, 'observed_day', {'date': day, 'rule_version': config['rule_version'], 'snapshot_version': value['snapshot_version']})
|
||||
if config['mode'] == 'observe':
|
||||
return {'mode': 'observe', 'snapshot': value, 'blocked': errors}
|
||||
exit_pending = exit_pending or bool(value and value.get('signal_ready') and value.get('signal', {}).get('high') and saved.get('rotation', {}).get('phase') in ('entering_micro', 'micro'))
|
||||
|
|
@ -363,12 +379,17 @@ def _tick(now=None, *, prepare_only=False):
|
|||
if funds['source'] != 'ws':
|
||||
raise ValueError('真实资金快照不可用')
|
||||
cash = funds['cash_avail']
|
||||
with books.scope('all'):
|
||||
pending = pms_repo.list_instructions(statuses=LIVE, limit=10000)
|
||||
codes = {r['ts_code'] for r in original+micro}|{r['code'] for r in value['members']}
|
||||
from app.services.rotation_execution import pending_instructions
|
||||
pending = pending_instructions()
|
||||
codes = {r['ts_code'] for r in original+micro}|{r['code'] for r in value.get('candidates', value['members'])}
|
||||
quotes = {c: _quote(c, now) for c in codes}
|
||||
missing = [r['ts_code'] for r in original+micro if not quotes.get(r['ts_code'])]
|
||||
if missing and not exit_pending:
|
||||
if config['mode'] == 'auto' and config['rule_version'] == core.EXECUTION_RULE:
|
||||
from app.services.rotation_execution import refresh_orders
|
||||
# 估值不完整时不增加风险,已发v3单先撤清,未知行情不触发递补。
|
||||
refresh_orders(pending, {}, now, value['snapshot_version'],
|
||||
set(state.get('rotation', {}).get('target') or []), {})
|
||||
state['blocked'] = ['持仓行情不完整,无法核算全账户仓位']+missing
|
||||
return {'blocked': state['blocked']}
|
||||
account_mv = sum(r['total_qty']*quotes.get(r['ts_code'], {}).get('price', 0) for r in original+micro)
|
||||
|
|
@ -379,10 +400,47 @@ def _tick(now=None, *, prepare_only=False):
|
|||
cap = scale*param_store.get_float('PMS_PORTFOLIO_CAP', 0.)
|
||||
reserve = scale*param_store.get_float('PMS_CASH_RESERVE', 0.)
|
||||
budget = max(0., min(cap, cash+account_mv-reserve))
|
||||
boards = config['boards'] if config['mode'] == 'auto' else {'MAIN', 'STAR', 'CHINEXT'}
|
||||
execution_quotes = {}
|
||||
buy_prices = {}
|
||||
quote_errors = []
|
||||
limits = None
|
||||
if config['rule_version'] == core.EXECUTION_RULE and not prepare_only:
|
||||
from app.services import execution_orders
|
||||
try:
|
||||
limits = _limits(day)
|
||||
except Exception as exc:
|
||||
limits = {}
|
||||
quote_errors.append('当日限制价查询失败:'+type(exc).__name__)
|
||||
for c in codes:
|
||||
try:
|
||||
execution_quotes[c] = market.execution_quote(c, now=now, limits=limits.get(c, {}))
|
||||
except Exception as exc:
|
||||
# 查询失败是未知,不能把故障改写成该股不可交易的资格结论。
|
||||
execution_quotes[c] = {}
|
||||
quote_errors.append(c+'执行行情查询失败:'+type(exc).__name__)
|
||||
for c, execution_quote in execution_quotes.items():
|
||||
priced = execution_orders.quote_order('buy', execution_quote, now)
|
||||
if priced.get('action') == 'FIRE':
|
||||
buy_prices[c] = {**quotes.get(c, {}), 'price': priced['limit_price']}
|
||||
unavailable = _unavailable_candidates(value, execution_quotes, budget, boards, config['costs'])
|
||||
if config['mode'] == 'auto' and config['rule_version'] == core.EXECUTION_RULE and not prepare_only:
|
||||
from app.services.rotation_execution import suspended_rejections
|
||||
unavailable.update(suspended_rejections(now))
|
||||
fresh_entry = state['rotation']['phase'] == 'original' and config['control'] == 'run' and value.get('signal_ready') and value.get('basket_ready') and not value['signal'].get('high') and (value['signal'].get('low') or value['signal'].get('growth_break'))
|
||||
if fresh_entry:
|
||||
# 先排除确定性整体不可执行,不能先清旧仓再发现根本没有建仓预算。
|
||||
static_issues = core.preflight(value['members'], budget, boards, config['costs'])
|
||||
if config['rule_version'] == core.EXECUTION_RULE:
|
||||
static_issues = ['没有可执行的初始建仓候选或预算'] if budget <= 0 or not any(m['code'] not in unavailable for m in value['candidates']) else []
|
||||
if static_issues:
|
||||
state['blocked'] = static_issues
|
||||
return {'blocked': static_issues}
|
||||
s = core.transition(state['rotation'], value, micro_qty=sum(r['total_qty'] for r in micro),
|
||||
original_qty=sum(r['total_qty'] for r in original), budget=budget, control=config['control'],
|
||||
micro_pending=config['mode'] == 'auto' and exit_pending and _micro_unsettled(pending))
|
||||
state['rotation'] = s
|
||||
target_waits = core.resolve_targets(s, value, micro=micro, pending=pending, unavailable=unavailable)
|
||||
if s['phase'] == 'returning_original' and not s.get('exit_date'):
|
||||
s['exit_date'] = day
|
||||
store.stage_locked(key, state)
|
||||
|
|
@ -403,26 +461,47 @@ def _tick(now=None, *, prepare_only=False):
|
|||
return {'blocked': state['blocked']}
|
||||
from app.services import command_service
|
||||
black = command_service.blacklist()
|
||||
intents, waits = core.orders(s, value, original=original, micro=micro, quotes=quotes,
|
||||
refreshed, refresh_waits = [], []
|
||||
if config['mode'] == 'auto' and s.get('rule_version') == core.EXECUTION_RULE and config['control'] == 'run':
|
||||
from app.services.rotation_execution import refresh_orders
|
||||
refreshed, refresh_waits = refresh_orders(pending, execution_quotes, now, value['snapshot_version'], set(s['target']) if s['phase'] in ('entering_micro', 'micro') else set(), unavailable)
|
||||
if s['phase'] == 'entering_micro' and s.get('rule_version') == core.EXECUTION_RULE and not any(c in buy_prices and c not in unavailable for c in s['target']):
|
||||
# 先完成已有单的撤销确认,避免全部停牌时卡住递补;不新卖原仓筹资。
|
||||
state['blocked'] = quote_errors+target_waits+refresh_waits+['目标成员当前没有可核实执行报价,暂不卖出原策略筹资']
|
||||
return {'blocked': state['blocked'], 'phase': s['phase'], 'results': refreshed}
|
||||
planning_quotes = {**quotes, **buy_prices}
|
||||
intents, waits = core.orders(s, value, original=original, micro=micro, quotes=planning_quotes,
|
||||
cash=cash, account_mv=account_mv, cap_amount=cap, reserve_cash=reserve,
|
||||
pending=pending, control=config['control'], blacklisted=black, costs=config['costs'])
|
||||
# 纸面档明确采用全沪深权限假设,不把它当实际账户权限判收。
|
||||
boards = config['boards'] if config['mode'] == 'auto' else {'MAIN', 'STAR', 'CHINEXT'}
|
||||
issues = core.preflight(value['members'], budget, boards, config['costs']) if value['basket_ready'] else ['名单未就绪']
|
||||
if issues:
|
||||
if issues and s.get('rule_version') != core.EXECUTION_RULE:
|
||||
intents = [r for r in intents if r['side'] == 'sell']
|
||||
waits.extend(issues)
|
||||
limits = _limits(day)
|
||||
done = []
|
||||
if s.get('rule_version') == core.EXECUTION_RULE:
|
||||
intents = [r for r in intents if r['side'] == 'sell' or r['ts_code'] not in unavailable]
|
||||
waits.extend(quote_errors+target_waits+refresh_waits)
|
||||
if limits is None:
|
||||
limits = _limits(day)
|
||||
done = list(refreshed)
|
||||
for i, intent in enumerate(intents):
|
||||
c, side = intent['ts_code'], intent['side']
|
||||
quote = quotes.get(c, {})
|
||||
price = quote.get('price')
|
||||
lim = limits.get(c)
|
||||
if s.get('rule_version') == core.EXECUTION_RULE:
|
||||
from app.services import execution_orders
|
||||
priced = execution_orders.quote_order(side, execution_quotes.get(c, {}), now)
|
||||
if priced.get('action') != 'FIRE':
|
||||
waits.append(c+str(priced.get('reason') or '执行报价不可用'))
|
||||
continue
|
||||
price = priced['limit_price']
|
||||
intent['quote_basis'] = {**execution_quotes.get(c, {}), **priced}
|
||||
intent['rule_version'] = core.EXECUTION_RULE
|
||||
if not price or not lim or not lim.get('up_limit') or not lim.get('down_limit'):
|
||||
waits.append(c+'行情或限制价不完整')
|
||||
continue
|
||||
if (side == 'buy' and price >= float(lim['up_limit'])) or (side == 'sell' and price <= float(lim['down_limit'])):
|
||||
if s.get('rule_version') != core.EXECUTION_RULE and ((side == 'buy' and price >= float(lim['up_limit'])) or (side == 'sell' and price <= float(lim['down_limit']))):
|
||||
waits.append(c+'涨跌停阻止成交')
|
||||
continue
|
||||
rows = original if intent['book_id'] == 'original' else micro
|
||||
|
|
@ -431,7 +510,9 @@ def _tick(now=None, *, prepare_only=False):
|
|||
intent['qty'] = min(intent['qty'], int(pos.get('avail_qty') or 0))
|
||||
if not intent['qty']:
|
||||
continue
|
||||
token = hashlib.sha256(json.dumps([day, s['cycle'], intent, len(state.get('account', {}).get('fills', {}))], sort_keys=True).encode()).hexdigest()[:24]
|
||||
# 报价是可变执行信息,不进入父任务幂等键。
|
||||
identity = {k: v for k, v in intent.items() if k != 'quote_basis'}
|
||||
token = hashlib.sha256(json.dumps([day, s['cycle'], identity, len(state.get('account', {}).get('fills', {}))], sort_keys=True).encode()).hexdigest()[:24]
|
||||
intent['id'] = 'ROT_'+token
|
||||
if config['mode'] == 'paper':
|
||||
core.apply_paper_fill(account, intent, price, day, config['costs'],
|
||||
|
|
@ -445,7 +526,7 @@ def _tick(now=None, *, prepare_only=False):
|
|||
done.append(send_intent(intent, price, now, value['snapshot_version']))
|
||||
except Exception as exc:
|
||||
waits.append(c+'委托待重试:'+type(exc).__name__)
|
||||
if s['phase'] == 'entering_micro' and not original and all(any(p['ts_code'] == c and p['total_qty'] >= s.get('entry_quantities', {}).get(c, 0) for p in micro) for c in s['target']) and not pending:
|
||||
if s['phase'] == 'entering_micro' and len(s['target']) == core.SIZE and not original and all(any(p['ts_code'] == c and p['total_qty'] >= s.get('entry_quantities', {}).get(c, 0) for p in micro) for c in s['target']) and not pending:
|
||||
s['phase'] = 'micro'
|
||||
state.update(last_tick=now.isoformat(), blocked=waits+errors, last_intents=intents, last_results=done,
|
||||
signal_healthy=bool(value.get('signal_ready') and value.get('basket_ready')),
|
||||
|
|
@ -542,7 +623,7 @@ def settle_day(now=None):
|
|||
from app.services.rotation_accounting import view
|
||||
result = view()
|
||||
healthy = result['ready'] and result['cash_difference'] is not None and abs(result['cash_difference']) <= .02 and reconcile_books()['ok']
|
||||
record = {'date': day, 'rule_version': core.RULE, 'healthy': healthy, **result}
|
||||
record = {'date': day, 'rule_version': config['rule_version'], 'healthy': healthy, **result}
|
||||
store.put_once('live_day:'+day, 'live_day', record)
|
||||
return record
|
||||
if config['mode'] != 'paper':
|
||||
|
|
@ -571,7 +652,7 @@ def settle_day(now=None):
|
|||
values[book] += p['qty']*q['price']
|
||||
healthy = bool(not missing and state.get('signal_healthy') and str(state.get('last_tick','')).startswith(day) and config['control'] != 'pause_all')
|
||||
metrics = capital.metrics(account['capital'], values) if account.get('capital') and not missing else None
|
||||
record = {'date': day, 'rule_version': core.RULE, 'healthy': healthy, 'missing_prices': missing,
|
||||
record = {'date': day, 'rule_version': config['rule_version'], 'healthy': healthy, 'missing_prices': missing,
|
||||
'metrics': metrics, 'fees': account['fees'], 'snapshot_version': state['rotation'].get('last_snapshot')}
|
||||
if metrics:
|
||||
state['metrics'] = metrics
|
||||
|
|
|
|||
|
|
@ -208,7 +208,9 @@ def cancel_by_code(ts_code: str, reason: str = "") -> dict:
|
|||
iid = i.get("instruction_id")
|
||||
try:
|
||||
r = executor.cancel_instruction(iid, reason=why, arbiter="system") or {}
|
||||
if r.get("ok"):
|
||||
if r.get('ok') and r.get('pending'):
|
||||
out.setdefault('pending_instructions', []).append(iid)
|
||||
elif r.get("ok"):
|
||||
out["instructions"].append(iid)
|
||||
else:
|
||||
out["ok"] = False
|
||||
|
|
|
|||
|
|
@ -675,6 +675,18 @@ class WsRunner:
|
|||
n, json.dumps(pl, ensure_ascii=False)[:500])
|
||||
self._maybe_degrade_ack(pl)
|
||||
else:
|
||||
row = await _db(qmt_repo.get_order, iid) or {}
|
||||
if wsc.is_final(str(row.get('status') or '').upper()):
|
||||
logger.warning('[reject] %s 已终态,拒绝消息不回退原委托状态', iid)
|
||||
return
|
||||
if code == 'ALREADY_FINAL' or pl.get('cancel_id') or row.get('cancel_state') in ('REQUESTED', 'SENT'):
|
||||
# 撤单被拒不等于原委托被拒,不能借此释放剩余数量。
|
||||
final_status = str(pl.get('status') or pl.get('current_status') or '').upper()
|
||||
if wsc.is_final(final_status) and pl.get('cum_qty') is not None:
|
||||
await self._on_order_update(iid, {**pl, 'status': final_status, 'final': True})
|
||||
else:
|
||||
logger.error('[撤单拒绝] %s %s,等待原委托终态及累计成交核实', iid, code)
|
||||
return
|
||||
n = await _db(qmt_repo.update_order, iid, status=qmt_repo.OS_REJECTED,
|
||||
reject_code=code[:32],
|
||||
reject_reason=str(pl.get("reason") or "")[:300],
|
||||
|
|
@ -722,6 +734,13 @@ class WsRunner:
|
|||
status = str(pl.get("status") or "").upper()
|
||||
row = await _db(qmt_repo.get_order, iid) or {}
|
||||
cur = str(row.get("status") or "").upper()
|
||||
cumulative = pl.get('cum_qty')
|
||||
if cumulative is None or int(cumulative) < int(row.get('cum_qty') or 0) or int(cumulative) > int(row.get('qty') or 0):
|
||||
logger.error('[order_update] %s 累计成交缺失、回退或超量,保留原状态', iid)
|
||||
return
|
||||
if status == wsc.ST_FILLED and int(cumulative) != int(row.get('qty') or 0):
|
||||
logger.error('[order_update] %s 全成状态缺少完整累计成交,保留原状态', iid)
|
||||
return
|
||||
if wsc.is_final(cur) and status != cur:
|
||||
# §5.4/§7.1: 终态唯一、不可再变、只推一次。真收到第二条终态说明对端有 bug ——
|
||||
# 此时**保住第一条**: 账本记的是"到底成交了多少", 被一条迟到的 CANCELLED 覆盖掉
|
||||
|
|
|
|||
|
|
@ -17,6 +17,29 @@ from pydantic_settings import BaseSettings, SettingsConfigDict
|
|||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8", extra="ignore")
|
||||
|
||||
# 异步研判与委托报价;业务运行值统一从ParamStore读取。
|
||||
PMS_EXEC_REJECT_COOLDOWN_SEC: int = 60
|
||||
PMS_EXEC_REJECT_MAX_COOLDOWN_SEC: int = 900
|
||||
PMS_EXEC_REPRICE_ENABLED: bool = False
|
||||
PMS_EXEC_BASIS_REFRESH_SEC: int = 60
|
||||
PMS_EXEC_BASIS_REFRESH_BUDGET_SEC: int = 20
|
||||
PMS_EXEC_BASIS_SOURCE_MAX_DAYS: int = 7
|
||||
PMS_EXEC_QUOTE_MAX_AGE_SEC: int = 15
|
||||
PMS_EXEC_ALLOW_MINUTE_QUOTE: bool = True
|
||||
PMS_EXEC_MINUTE_MAX_AGE_SEC: int = 90
|
||||
PMS_EXEC_PRICE_OFFSET_TICKS: int = 1
|
||||
PMS_EXEC_REPRICE_MIN_SEC: int = 60
|
||||
PMS_EXEC_REPRICE_TICKS: int = 2
|
||||
PMS_EXEC_REPRICE_MAX_ATTEMPTS: int = 0
|
||||
PMS_EXEC_MAX_SLIPPAGE_PCT: float = 0.01
|
||||
PMS_EXEC_MINUTE_SLIPPAGE_PCT: float = 0.002
|
||||
PMS_ROTATION_CANDIDATE_POOL_SIZE: int = 100
|
||||
PMS_JUDGE_ASYNC_ENABLED: bool = False
|
||||
PMS_JUDGE_ASYNC_HTTP_TIMEOUT: int = 3
|
||||
PMS_JUDGE_ASYNC_DEADLINE_SEC: int = 120
|
||||
PMS_JUDGE_ASYNC_CACHE_SEC: int = 120
|
||||
PMS_JUDGE_ASYNC_SCAN_BUDGET_SEC: int = 20
|
||||
|
||||
# 数据库基础设施能力位。迁移核验完成后才在部署环境打开,不是交易开关。
|
||||
INFRA_STRATEGY_BOOK_SCHEMA: bool = False
|
||||
INFRA_ROTATION_DOCUMENT_SCHEMA: bool = False
|
||||
|
|
|
|||
|
|
@ -297,3 +297,10 @@
|
|||
**依据。** 用户明确选定8%,不把此前5%或10%的回测收益冒充8%的结果。新阈值与原算法逐日核对,旧冻结快照按版本保留。
|
||||
|
||||
**预期与复核。** 目标名单50只,当前历史分位8.33%不触发低位入场。部署后复核实际页面、数据健康和QMT账实一致;次日08:50核验新版本自然冻结,09:30以后按原执行窗口处理自然信号。无新观察期或纸面期要求,188不变。对应bionic台账006。
|
||||
|
||||
|
||||
## 2026-11-03 执行报价与异步研判
|
||||
|
||||
决定:按用户确认方案,将机会判断与限价执行分开。模型价格区间保留参考用途;盘中改价不同步排队重问模型。改价必须先撤单确认并核实累计成交入账,才释放剩余数量。微盘冻结有序备选,明确停牌等不可交易成员仅零成交时递补,单股受阻不停止其他成员。
|
||||
|
||||
依据:用户说明QMT主要接受限价成交或撤单重挂路径。预期是减少过时限价与模型排队导致的策略执行偏差,不宣称收益提升。复核以断网回归、实际配置、可追溯来源和自然委托回报分别记证。关联bionic台账007及同日实施规格;数据缺失或版本不匹配时不擅自开启。
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
# 执行报价与异步研判实施规格
|
||||
|
||||
2026年11月3日。用户已确认实施。本次只针对155模拟PMS及其依赖的bionic接口和微盘监控。188真实PMS不在范围内。
|
||||
|
||||
## 目标
|
||||
|
||||
bionic判断机会是否成立,PMS管理授权、数量、预算和报价,QMT接受限价委托并返回实际结果。改价不逐次等待大模型。用户指定的价格边界优先。微盘成交优先,明确不可交易的未建仓成员按冻结候选顺序递补。
|
||||
|
||||
## 实施内容与接口
|
||||
|
||||
1. bionic为支撑压力保存来源、原始生成时间、数据日期和版本。失败、零置信度、不可追溯的沿用结果不能冒充有效模型价格。区间仍作为参考提供,不把重查接口称作模型重算。机会继续有效、已撤销、未知分别表达;监控缺失与没有离场结论分开。
|
||||
2. `pms_exec`保留兼容字段,新增`execution_basis`:`version`、`quality`(VALID/UNAVAILABLE)、`continuation`(CONTINUE/STOP/UNKNOWN)、`reason`、`generated_at`、`valid_until`、`source_date`、`source`。时间使用带时区的ISO格式。STOP用于明确的回避或离场,约束新增买入;已授权卖出仍执行其数量、可卖量和用户限价。UNKNOWN不能当作CONTINUE。旧消费者仍能使用旧字段,新消费者严格校验新事实。
|
||||
3. 新增异步研判接口`POST /api/intraday/pms_judge_submit`及`GET /api/intraday/pms_judge_result/{request_id}`。提交请求携带原研判字段、`request_id`和`deadline_at`。返回`verdict=PENDING`表示未完成,终态沿用PASS/REJECT/UNAVAILABLE,并回传请求编号及证据指纹。入口先读缓存、合并同证据在途请求;任务带过期时间,开始和发布结果前检查截止时间。迟到结果不能覆盖新版本。一次任务最多一次模型调用。旧同步接口保留兼容但复用同一调度入口。
|
||||
4. PMS将提议研判与委托执行分开调度。异步等待只保存等待状态,不当作拒绝或自动通过。轮次重新检查用户命令、候选版本和规则后才消费结果。执行依据另行刷新,委托循环只读取有效缓存。未配置新能力时保持兼容;开启后不静默回退到长时间同步研判。
|
||||
5. 委托撤换共用出口。撤单中继续占用资金和数量。终态及累计成交核实后才能发下一笔。父指令取消与子单撤单确认分开。已有买入委托的依据失效也要监控并撤销,不能因全部数量在途而跳过检查。成交先入账,重复和迟到回报幂等。
|
||||
6. 成交优先报价使用有效实时盘口,买入参考卖盘、卖出参考买盘,并遵守交易单位、价格合法性、用户边界及预算。盘口来源需明确核实;没有盘口不能伪称已实现实时对手价。若仅具备分钟行情,必须明确降级口径及新鲜度,不擅自假造盘口。报价规则由ParamStore读取并留痕。
|
||||
7. 微盘冻结有序后备池,与首选采用相同资格和排序。沪深入口排除北交所。未成交且确认不可交易才能递补;价格落后先改价。部分成交保留席位,当日递补不换回,次日依据新版本处理。个股受阻不阻塞其他个股,全账户故障仍守成。旧仓和旧在途继续占用预算。每日调入也保存未完成目标。
|
||||
|
||||
## 验收
|
||||
|
||||
- 模型失败的兜底价格被识别为无效,沿用价格保留原始时间。日期、版本和质量缺失不得自动通过。
|
||||
- 异步重复提交只产生一个有效任务,队列过期不调用模型,迟到结果不能被新证据消费。人工确认降级仍有效。
|
||||
- 研判不可用不阻塞已有授权委托的执行循环。依据刷新超时保留明确状态,缓存超过有效期不授权新买入。
|
||||
- 撤单同时成交、部分成交、重复回报、进程恢复和跨日不超买;用户固定价格不被改价突破。
|
||||
- 微盘停牌递补、价格变化重挂、部分成交占席位、候选耗尽、资金不足、恢复交易不来回换,以及退出优先通过隔离测试。
|
||||
- 两仓本地断网检查、155候选镜像断网全量回归、实际配置及自然回执分别记录。不制造信号或测试委托。
|
||||
|
||||
## 验证价格区间
|
||||
|
||||
保留当时可见的版本化依据与报价结果,为比较区间等待和成交优先提供同口径数据。比较成交率、等待、费用、成交后变化及未成交机会成本。没有历史冻结依据时不倒填、不用重算覆盖值冒充历史。工程测试通过不宣称策略收益改善。
|
||||
|
||||
## 开启与回退
|
||||
|
||||
新能力默认关闭,接口先兼容发布,再核对155实际数据条件开启。授权以当前会话为准,不重复设置旧观察期。回退停止新增能力,保留账本、委托与回报;在途撤换不得因切换开关丢失。已消费的新微盘轮次保持版本一致,不能直接换回旧规则。
|
||||
|
||||
|
||||
## 实施时核实的现实边界
|
||||
|
||||
2026年11月3日盘中只读核验155:运行模式为模拟ws,微盘处于auto/run、v2版本。当前可读取分钟行情,时间为北京时间完整日期;没有已接入的盘口或停牌事实表。停牌递补采用协议明确的SUSPENDED自然拒单,零成交且回报入账核清后生效,不发送探测委托。
|
||||
|
||||
当日600000.SH的gp_stk_limit未到,前一交易日有记录。执行不得借昨日涨跌停价代替今日。新报价的启用前必须核验当日报价和限制价覆盖,不能把本地假数据通过视为现网可交易。旧在手微盘v2不得硬改版本,v3只能配合匹配的新冻结和轮次。
|
||||
|
||||
正常夜扫对MAINTAIN的缺来源或到期记录转入一次真实研究,不从盘中改价触发模型。模型失败仍保持UNKNOWN;等待一晚不保证所有标的都取得有效来源。
|
||||
|
||||
撤单中的父任务保留状态;独立撤单确认任务在全局暂停和非交易时段也可收尾,不能报价或发新单。窗口到期也先撤清后结束。逐单释放经新资金快照和成交入账确认的未成交预留,其他在途继续占用。其他买单仍在途期间,新卖出回款可能保守等待当前预算周期结束,后续可以另行优化,不能重复累加资金。
|
||||
|
|
@ -5,6 +5,9 @@
|
|||
运行: docker compose run --rm pms-web python scripts/run_tests.py
|
||||
|
||||
包含 (例数按 2026-10-26 隔离回归实跑校正):
|
||||
test_batch57_units.py 异步请求持久化、截止时间、旧结果隔离、失败续查及行情来源 (16例)
|
||||
test_batch56_units.py 微盘v3冻结候选、按序递补与部分成交保护 (例数见回归输出)
|
||||
test_batch55_units.py 共同撤换状态、报价、依据监控与取消确认 (例数见回归输出)
|
||||
test_batch50_units.py QMT限价等待、价格来源故障、用户暂停、配额防重和试算隔离 (14例)
|
||||
test_batch49_units.py 持仓分组、同名股票与批次归属、未迁移及缺数展示 (8例)
|
||||
test_batch48_units.py 策略账本隔离、轮换优先级、交易单位、模拟直接启用、读数及微盘50规则换版 (55例)
|
||||
|
|
@ -208,6 +211,7 @@ SUITES = ["test_core_units.py", "test_batch2_units.py", "test_batch3_units.py",
|
|||
"test_batch48_units.py",
|
||||
"test_batch49_units.py",
|
||||
"test_batch50_units.py",
|
||||
"test_batch55_units.py", "test_batch56_units.py", "test_batch57_units.py",
|
||||
"test_page_copy_guard.py",
|
||||
"test_page_enum_guard.py",
|
||||
"test_page_wiring_guard.py",
|
||||
|
|
|
|||
|
|
@ -0,0 +1,359 @@
|
|||
"""执行依据、报价、撤换及迟到成交的断网回归。"""
|
||||
import unit_test_guard
|
||||
import asyncio
|
||||
import copy
|
||||
import unittest
|
||||
from datetime import datetime, timedelta
|
||||
from unittest.mock import patch
|
||||
from test_wiring import install_fakes
|
||||
from app.core import execution_policy as policy, ws_codec
|
||||
from app.repo import pms_repo, qmt_repo, execution_repo
|
||||
from app.services import execution_orders as orders, execution_basis as basis
|
||||
from app.services import exec_advisor, executor, dispatcher, market, execution_conflicts, param_store
|
||||
from app.ws.runner import WsRunner
|
||||
|
||||
NOW = datetime(2026, 11, 3, 10, 30, tzinfo=policy.BEIJING)
|
||||
CODE = '600000.SH'
|
||||
|
||||
|
||||
def fact(**extra):
|
||||
return {'version': 'v1', 'quality': 'VALID', 'continuation': 'CONTINUE',
|
||||
'reason': '已有授权未撤销', 'generated_at': (NOW-timedelta(hours=4)).isoformat(),
|
||||
'valid_until': (NOW+timedelta(minutes=5)).isoformat(), 'source_date': '2026-11-02',
|
||||
'source': 'model:traceable', **extra}
|
||||
|
||||
|
||||
def quote(**extra):
|
||||
return {'kind': 'level1', 'source': 'test-level1', 'as_of': NOW.isoformat(),
|
||||
'last': 10., 'bid': 9.99, 'ask': 10.01, 'limit_down': 9., 'limit_up': 11.,
|
||||
'halted': False, **extra}
|
||||
|
||||
|
||||
class ExecutionTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.fake = install_fakes(prices={CODE: 10.}, params={
|
||||
'PMS_EXEC_REPRICE_ENABLED': True, 'PMS_EXEC_IMPL': 'A',
|
||||
'PMS_EXEC_API_BASE': 'http://fake', 'PMS_DISPATCH_MODE': 'ws',
|
||||
'PMS_TOTAL_SCALE': 2000000})
|
||||
self.fake.qmt.set_channel()
|
||||
real_now = policy.local_now
|
||||
patch.object(policy, 'local_now', side_effect=lambda now=None: real_now(now or NOW)).start()
|
||||
patch.object(market, 'execution_quote', side_effect=lambda *a, **k: quote(), create=True).start()
|
||||
patch.object(executor.td, 'ymd', return_value=20261103).start()
|
||||
patch.object(executor.td, 'trade_days_left', return_value=1).start()
|
||||
self.addCleanup(patch.stopall)
|
||||
self.parent()
|
||||
|
||||
def parent(self, iid='P', **extra):
|
||||
self.fake.insert_instruction(instruction_id=iid, origin_type='strategy', origin_id='S',
|
||||
ts_code=CODE, side='buy', action='OPEN', qty=600, status='DISPATCHED',
|
||||
progress={'is_command': True, 'children': [], 'deadline': '2026-11-03',
|
||||
'execution_basis_cache': {'basis': fact()}}, **extra)
|
||||
return self.fake.instructions[iid]
|
||||
|
||||
def child(self, status='ACCEPTED', done=0, iid='P_D01', qty=600):
|
||||
self.fake.qmt.enqueue_order(instruction_id=iid, parent_id='P', ts_code=CODE,
|
||||
side='buy', qty=qty, limit_price=10., valid_until=int((NOW+timedelta(minutes=10)).timestamp()*1000))
|
||||
row = self.fake.qmt.orders[iid]
|
||||
row.update(status=status, cum_qty=done, created_at=NOW-timedelta(minutes=2))
|
||||
self.fake.instructions['P']['progress']['children'].append({'ref': iid, 'qty': qty,
|
||||
'ymd': 20261103, 'mode': 'ws'})
|
||||
return row
|
||||
|
||||
def decide(self):
|
||||
parent = self.fake.instructions['P']
|
||||
return exec_advisor.decide(side='buy', action='OPEN', ts_code=CODE, now=NOW,
|
||||
day={'price': 10}, params={}, is_last_day=True, quota=600,
|
||||
prog=parent['progress'])
|
||||
|
||||
def test_basis_requires_provenance_time_and_expiry(self):
|
||||
self.assertEqual(policy.basis_reason(fact(), NOW), '')
|
||||
for override in ({'version': None}, {'source': ''}, {'source_date': None},
|
||||
{'generated_at': '2026-11-03T06:30:00'},
|
||||
{'valid_until': NOW.isoformat()}, {'continuation': 'UNKNOWN'},
|
||||
{'quality': 'UNAVAILABLE'}):
|
||||
self.assertTrue(policy.basis_reason(fact(**override), NOW))
|
||||
|
||||
def test_stop_wins_even_invalid_quality(self):
|
||||
self.assertIn('已撤销', policy.basis_reason(fact(quality='UNAVAILABLE', continuation='STOP'), NOW))
|
||||
|
||||
def test_quote_counterparty_and_user_limits(self):
|
||||
self.assertEqual(orders.quote_order('buy', quote(), NOW)['limit_price'], 10.02)
|
||||
self.assertEqual(orders.quote_order('sell', quote(), NOW)['limit_price'], 9.98)
|
||||
self.assertEqual(orders.quote_order('buy', quote(), NOW, user_limit=9.95)['limit_price'], 9.95)
|
||||
self.assertEqual(orders.quote_order('sell', quote(), NOW, user_limit=10.05)['limit_price'], 10.05)
|
||||
|
||||
def test_quote_follows_current_price_not_first_order_anchor(self):
|
||||
result = orders.quote_order('buy', quote(last=10.8, ask=10.81, bid=10.79), NOW, anchor_price=10.)
|
||||
self.assertEqual(result['limit_price'], 10.82)
|
||||
|
||||
def test_quote_requires_fresh_source_and_legal_range(self):
|
||||
for override in ({'source': ''}, {'as_of': (NOW-timedelta(seconds=16)).isoformat()},
|
||||
{'as_of': (NOW+timedelta(seconds=1)).isoformat()},
|
||||
{'limit_up': None}, {'ask': None}, {'halted': True}):
|
||||
self.assertEqual(orders.quote_order('buy', quote(**override), NOW)['action'], 'WAIT')
|
||||
self.assertEqual(orders.quote_order('buy', quote(), NOW, user_limit=8)['action'], 'WAIT')
|
||||
|
||||
def test_minute_degradation_is_explicit_and_switchable(self):
|
||||
value = quote(kind='minute', source='minute-cache', bid=None, ask=None)
|
||||
result = orders.quote_order('buy', value, NOW)
|
||||
self.assertTrue(result['degraded'])
|
||||
self.assertIn('无实时盘口', result['reason'])
|
||||
self.fake.params['PMS_EXEC_ALLOW_MINUTE_QUOTE'] = False
|
||||
param_store._cache['loaded'] = False
|
||||
self.assertEqual(orders.quote_order('buy', value, NOW)['action'], 'WAIT')
|
||||
|
||||
def test_execution_uses_cache_without_http(self):
|
||||
with patch.object(exec_advisor, '_post', side_effect=AssertionError('执行环禁止HTTP')):
|
||||
self.assertEqual(self.decide()['action'], 'FIRE')
|
||||
self.fake.instructions['P']['progress']['execution_basis_cache']['basis']['continuation'] = 'UNKNOWN'
|
||||
self.assertEqual(self.decide()['action'], 'WAIT')
|
||||
|
||||
def test_refresh_preserves_children_and_records_new_version(self):
|
||||
self.child()
|
||||
with patch.object(exec_advisor, '_post', return_value={'execution_basis': fact(version='v2')}):
|
||||
result = basis.refresh(NOW)
|
||||
self.assertEqual(result['refreshed'], ['P'])
|
||||
progress = self.fake.instructions['P']['progress']
|
||||
self.assertEqual(len(progress['children']), 1)
|
||||
self.assertEqual(progress['execution_basis_cache']['basis']['version'], 'v2')
|
||||
|
||||
def test_refresh_failure_does_not_extend_basis_expiry(self):
|
||||
with patch.object(exec_advisor, '_post', side_effect=TimeoutError('timeout')):
|
||||
result = basis.refresh(NOW)
|
||||
self.assertTrue(result['errors'])
|
||||
progress = self.fake.instructions['P']['progress']
|
||||
self.assertEqual(basis.cache_reason(progress, NOW), '')
|
||||
self.assertTrue(basis.cache_reason(progress, NOW+timedelta(minutes=6)))
|
||||
|
||||
def test_refresh_unknown_replaces_previous_continue(self):
|
||||
with patch.object(exec_advisor, '_post', return_value={'execution_basis': fact(continuation='UNKNOWN')}):
|
||||
basis.refresh(NOW)
|
||||
self.assertEqual(self.decide()['action'], 'WAIT')
|
||||
|
||||
def test_monitor_cancels_full_inflight_on_stop_without_http(self):
|
||||
row = self.child()
|
||||
self.fake.instructions['P']['progress']['execution_basis_cache']['basis'] = fact(continuation='STOP')
|
||||
with patch.object(exec_advisor, '_post', side_effect=AssertionError('监控禁止HTTP')):
|
||||
result = basis.monitor(NOW)
|
||||
self.assertEqual(result['errors'], [])
|
||||
self.assertEqual(row['cancel_state'], 'REQUESTED')
|
||||
self.assertEqual(self.fake.instructions['P']['status'], 'DISPATCHED')
|
||||
self.assertFalse(orders.replacement_state('P')['ready'])
|
||||
|
||||
def test_stop_blocks_buy_but_allows_authorized_nonurgent_sell(self):
|
||||
parent = self.fake.instructions['P']
|
||||
parent['progress']['execution_basis_cache']['basis'] = fact(continuation='STOP')
|
||||
self.assertEqual(self.decide()['action'], 'WAIT')
|
||||
parent.update(side='sell', action='EXIT')
|
||||
result = exec_advisor.decide(side='sell', action='EXIT', ts_code=CODE, now=NOW,
|
||||
day={'price': 10}, params={}, is_last_day=True, quota=600,
|
||||
prog=parent['progress'], urgent=False)
|
||||
self.assertEqual(result['action'], 'FIRE')
|
||||
self.assertEqual(result['limit_price'], 9.98)
|
||||
with patch.object(exec_advisor, '_post', side_effect=AssertionError('卖出不刷新买入继续依据')):
|
||||
self.assertEqual(basis.refresh(NOW)['refreshed'], [])
|
||||
|
||||
def test_stop_does_not_cancel_or_block_existing_sell(self):
|
||||
parent = self.fake.instructions['P']
|
||||
parent.update(side='sell', action='TRIM')
|
||||
parent['progress']['execution_basis_cache']['basis'] = fact(continuation='STOP')
|
||||
row = self.child()
|
||||
row.update(side='sell', limit_price=9.98)
|
||||
self.assertFalse(execution_conflicts.queued_buy_blocker(row))
|
||||
result = basis.monitor(NOW)
|
||||
self.assertEqual(result['errors'], [])
|
||||
self.assertEqual(result['replacing'], [])
|
||||
self.assertEqual(row['cancel_state'], 'NONE')
|
||||
|
||||
def test_cancel_only_never_quotes_or_starts_new_replacements(self):
|
||||
row = self.child()
|
||||
with patch.object(market, 'execution_quote', side_effect=AssertionError('仅确认撤单不允许报价')):
|
||||
result = basis.monitor(NOW, cancel_only=True)
|
||||
self.assertEqual(result['errors'], [])
|
||||
self.assertEqual(row['cancel_state'], 'NONE')
|
||||
self.assertTrue(executor.cancel_instruction('P')['pending'])
|
||||
row.update(status='CANCELLED')
|
||||
confirmed = basis.monitor(NOW, cancel_only=True)
|
||||
self.assertEqual(confirmed['errors'], [])
|
||||
self.assertEqual(self.fake.instructions['P']['status'], 'CANCELLED')
|
||||
|
||||
def test_missing_quote_cancels_old_order_and_waits_for_fresh_quote(self):
|
||||
row = self.child()
|
||||
with patch.object(market, 'execution_quote', return_value={}):
|
||||
result = basis.monitor(NOW)
|
||||
self.assertEqual(result['errors'], [])
|
||||
self.assertEqual(row['cancel_state'], 'REQUESTED')
|
||||
self.assertEqual(len(self.fake.qmt.orders), 1)
|
||||
|
||||
def test_quote_exception_requests_cancel_without_false_confirmation(self):
|
||||
row = self.child()
|
||||
with patch.object(market, 'execution_quote', side_effect=RuntimeError('limit snapshot unavailable')):
|
||||
result = basis.monitor(NOW)
|
||||
self.assertEqual(result['errors'], [])
|
||||
self.assertIn('行情读取失败', result['replacing'][0]['reason'])
|
||||
self.assertTrue(result['replacing'][0]['pending'])
|
||||
self.assertEqual(row['cancel_state'], 'REQUESTED')
|
||||
self.assertEqual(self.fake.instructions['P']['status'], 'DISPATCHED')
|
||||
|
||||
def test_partial_cancel_requires_cumulative_fill_booked(self):
|
||||
self.child(status='CANCELLED', done=200)
|
||||
self.assertFalse(orders.replacement_state('P')['ready'])
|
||||
self.fake.instructions['P']['exec_qty'] = 200
|
||||
state = orders.replacement_state('P')
|
||||
self.assertTrue(state['ready'])
|
||||
self.assertEqual(state['remaining'], 400)
|
||||
|
||||
def test_unprocessed_or_held_trade_blocks_replacement(self):
|
||||
self.child(status='EXPIRED')
|
||||
self.fake.qmt.put_trade(seq=1, instruction_id='P_D01', ts_code=CODE, side='buy', qty=100, price=10)
|
||||
self.assertFalse(orders.replacement_state('P')['ready'])
|
||||
self.fake.qmt.inbox[1]['processed'] = 3
|
||||
self.assertTrue(orders.code_settlement_blocker(CODE))
|
||||
|
||||
def test_child_number_recovers_from_outbox_not_progress(self):
|
||||
self.child(status='EXPIRED', iid='P_D04')
|
||||
self.fake.instructions['P']['progress']['children'] = []
|
||||
self.assertEqual(orders.next_child_id('P'), 'P_D05')
|
||||
|
||||
def test_parent_cancel_waits_then_confirms_and_never_refires(self):
|
||||
row = self.child()
|
||||
first = executor.cancel_instruction('P')
|
||||
self.assertTrue(first['ok'] and first['pending'])
|
||||
self.assertEqual(self.fake.instructions['P']['status'], 'DISPATCHED')
|
||||
self.assertEqual(executor.run_tick(now=NOW)['fired'], [])
|
||||
row.update(status='CANCELLED')
|
||||
second = executor.cancel_instruction('P')
|
||||
self.assertTrue(second['confirmed'])
|
||||
self.assertEqual(self.fake.instructions['P']['status'], 'CANCELLED')
|
||||
|
||||
def test_fill_during_cancel_ends_confirmed(self):
|
||||
row = self.child()
|
||||
self.assertTrue(executor.cancel_instruction('P')['pending'])
|
||||
row.update(status='FILLED', cum_qty=600)
|
||||
self.fake.instructions['P']['exec_qty'] = 600
|
||||
self.assertTrue(executor.cancel_instruction('P')['confirmed'])
|
||||
self.assertEqual(self.fake.instructions['P']['status'], 'CONFIRMED')
|
||||
|
||||
def test_window_expiry_waits_for_terminal_and_booked_fill(self):
|
||||
parent = self.fake.instructions['P']
|
||||
parent['progress']['is_command'] = False
|
||||
row = self.child()
|
||||
with patch.object(executor.td, 'trade_days_left', return_value=0):
|
||||
first = executor.sweep_windows(now=NOW)
|
||||
self.assertEqual(first['errors'], [])
|
||||
self.assertEqual(first['expired'], [])
|
||||
self.assertEqual(parent['status'], 'DISPATCHED')
|
||||
self.assertEqual(row['cancel_state'], 'REQUESTED')
|
||||
row.update(status='CANCELLED', cum_qty=200)
|
||||
self.assertEqual(executor.sweep_windows(now=NOW)['expired'], [])
|
||||
self.assertEqual(parent['status'], 'DISPATCHED')
|
||||
parent['exec_qty'] = 200
|
||||
final = executor.sweep_windows(now=NOW)
|
||||
self.assertEqual(final['errors'], [])
|
||||
self.assertEqual(final['expired'], ['P'])
|
||||
self.assertEqual(parent['status'], 'EXPIRED')
|
||||
|
||||
def test_cancel_monitor_recovers_old_expired_parent_with_live_child(self):
|
||||
row = self.child()
|
||||
self.fake.instructions['P']['status'] = 'EXPIRED'
|
||||
result = basis.monitor(NOW, cancel_only=True)
|
||||
self.assertEqual(result['errors'], [])
|
||||
self.assertTrue(result['cancelled'][0]['pending'])
|
||||
self.assertEqual(row['cancel_state'], 'REQUESTED')
|
||||
row['status'] = 'CANCELLED'
|
||||
result = basis.monitor(NOW, cancel_only=True)
|
||||
self.assertTrue(result['cancelled'][0]['confirmed'])
|
||||
self.assertEqual(self.fake.instructions['P']['status'], 'EXPIRED')
|
||||
|
||||
def test_dispatch_rechecks_inflight_and_cached_basis(self):
|
||||
self.child()
|
||||
result = dispatcher.dispatch(instruction_id='P_D02', parent_id='P', ts_code=CODE,
|
||||
side='buy', qty=600, limit_price=10., valid_until=NOW+timedelta(minutes=10))
|
||||
self.assertFalse(result['ok'])
|
||||
self.assertEqual(len(self.fake.qmt.orders), 1)
|
||||
|
||||
def test_reprice_zero_attempt_cap_keeps_following_with_frequency_guard(self):
|
||||
row = self.child()
|
||||
self.fake.params['PMS_EXEC_REPRICE_MAX_ATTEMPTS'] = 0
|
||||
self.assertTrue(orders.should_replace(self.fake.instructions['P'], {'action': 'FIRE', 'limit_price': 10.1}, NOW))
|
||||
row['created_at'] = NOW
|
||||
self.assertFalse(orders.should_replace(self.fake.instructions['P'], {'action': 'FIRE', 'limit_price': 10.1}, NOW))
|
||||
|
||||
def test_queued_order_rechecks_stop_before_sending(self):
|
||||
row = self.child()
|
||||
self.fake.instructions['P']['progress']['execution_basis_cache']['basis'] = fact(continuation='STOP')
|
||||
self.assertTrue(execution_conflicts.queued_buy_blocker(row))
|
||||
|
||||
def test_progress_write_preserves_asynchronously_refreshed_cache(self):
|
||||
old = copy.deepcopy(self.fake.instructions['P']['progress'])
|
||||
execution_repo.save_basis('P', {'basis': fact(version='v2')})
|
||||
pms_repo.update_instruction('P', progress=old)
|
||||
self.assertEqual(self.fake.instructions['P']['progress']['execution_basis_cache']['basis']['version'], 'v2')
|
||||
|
||||
def test_cancel_reject_does_not_release_original_order(self):
|
||||
row = self.child()
|
||||
row['cancel_state'] = 'SENT'
|
||||
runner = WsRunner()
|
||||
with patch.object(qmt_repo, 'update_order', side_effect=AssertionError('不应把撤单拒绝写成原单拒绝')):
|
||||
asyncio.run(runner._apply_side_effects(ws_codec.T_REJECT,
|
||||
{'instruction_id': 'P_D01', 'code': 'ALREADY_FINAL'}, {}))
|
||||
self.assertEqual(row['status'], 'ACCEPTED')
|
||||
|
||||
def test_cumulative_quantity_never_regresses(self):
|
||||
row = self.child(status='PARTIAL', done=200)
|
||||
runner = WsRunner()
|
||||
with patch.object(qmt_repo, 'update_order', side_effect=AssertionError('累计成交不得回退')):
|
||||
asyncio.run(runner._on_order_update('P_D01', {'status': 'PARTIAL', 'cum_qty': 100}))
|
||||
self.assertEqual(row['cum_qty'], 200)
|
||||
|
||||
def test_old_local_cancelled_parent_does_not_claim_broker_confirmation(self):
|
||||
row = self.child()
|
||||
self.fake.instructions['P']['status'] = 'CANCELLED'
|
||||
result = executor.cancel_instruction('P')
|
||||
self.assertTrue(result['pending'])
|
||||
self.assertFalse(result['confirmed'])
|
||||
self.assertEqual(row['cancel_state'], 'REQUESTED')
|
||||
|
||||
def test_remaining_is_reissued_only_after_partial_fill_booked(self):
|
||||
row = self.child(status='CANCELLED', done=200)
|
||||
first = executor.run_tick(now=NOW)
|
||||
self.assertEqual(first['fired'], [])
|
||||
self.fake.instructions['P']['exec_qty'] = 200
|
||||
second = executor.run_tick(now=NOW)
|
||||
self.assertEqual([(i['qty'], i['limit']) for i in second['fired']], [(400, 10.02)], second)
|
||||
self.assertEqual(self.fake.qmt.orders['P_D02']['qty'], 400)
|
||||
self.assertEqual(row['cum_qty'], 200)
|
||||
|
||||
def test_reject_backoff_does_not_block_normal_expiry(self):
|
||||
row = self.child(status='REJECTED')
|
||||
row.update(final_at=NOW, reject_code='BAD_PARAM')
|
||||
self.assertTrue(orders.retry_reason('P', NOW))
|
||||
self.assertFalse(orders.retry_reason('P', NOW+timedelta(seconds=61)))
|
||||
row['status'] = 'EXPIRED'
|
||||
self.assertFalse(orders.retry_reason('P', NOW))
|
||||
|
||||
def test_command_pending_does_not_mark_cancelled_until_terminal(self):
|
||||
from app.services import command_service
|
||||
self.fake.insert_command(command_id='C', cmd_class='task', cmd_type='OPEN_STOCK',
|
||||
ts_code=CODE, params={}, status='EXECUTING', progress={})
|
||||
self.fake.insert_plans([{'plan_id': 'PLAN', 'command_id': 'C', 'ts_code': CODE,
|
||||
'action': 'OPEN', 'qty': 600, 'status': 'EXECUTING', 'amount': 6000}])
|
||||
self.fake.instructions['P'].update(origin_type='plan', origin_id='PLAN')
|
||||
row = self.child()
|
||||
pending = command_service.cancel('C')
|
||||
self.assertEqual(pending['pending'], ['P'])
|
||||
self.assertEqual(self.fake.commands['C']['status'], 'EXECUTING')
|
||||
row.update(status='CANCELLED')
|
||||
command_service.refresh_progress('C')
|
||||
self.assertEqual(self.fake.commands['C']['status'], 'CANCELLED')
|
||||
|
||||
def test_unsupported_exchange_never_refreshes_upstream(self):
|
||||
self.fake.instructions['P']['ts_code'] = '999999.BJ'
|
||||
with patch.object(exec_advisor, '_post', side_effect=AssertionError('排除交易所不得取数')) as post:
|
||||
self.assertEqual(basis.refresh(NOW)['refreshed'], [])
|
||||
self.assertEqual(post.call_count, 0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main(verbosity=2)
|
||||
|
|
@ -0,0 +1,406 @@
|
|||
"""微盘冻结后备池、持久补齐及真实在途防重。所有输入为内存数据。"""
|
||||
import unit_test_guard # noqa: F401
|
||||
import copy
|
||||
from contextlib import contextmanager, ExitStack, nullcontext
|
||||
import datetime as dt
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from app.core import micro_rotation as m
|
||||
|
||||
|
||||
def snapshot(day='2026-11-03', size=100, rule=m.EXECUTION_RULE, low=True):
|
||||
rows = [{'code': f'{600000+i}.SH', 'rank': i+1, 'eligible': True, 'close': 10.,
|
||||
'total_mv_wan': i+1} for i in range(size)]
|
||||
value = {'rule_version': rule, 'rule_parameters': m.rule_parameters(rule),
|
||||
'basket_size': 50, 'data_date': '2026-11-02', 'effective_date': day,
|
||||
'signal_ready': True, 'basket_ready': True,
|
||||
'signal': {'date': '2026-11-02', 'ready': True, 'low': low, 'high': False, 'growth_break': False},
|
||||
'members': rows[:50], 'source_hashes': {'stocks': 'fixed'}, 'errors': []}
|
||||
if rule == m.EXECUTION_RULE:
|
||||
value.update(candidates=rows, candidate_count=len(rows))
|
||||
value['snapshot_version'] = hashlib.sha256(json.dumps(value, sort_keys=True, ensure_ascii=False,
|
||||
separators=(',', ':')).encode()).hexdigest()
|
||||
value['frozen'] = True
|
||||
return value
|
||||
|
||||
|
||||
def state(snap=None):
|
||||
snap = snap or snapshot()
|
||||
return m.transition(m.initial_state(), snap, micro_qty=0, original_qty=0, budget=100000)
|
||||
|
||||
|
||||
class SnapshotTests(unittest.TestCase):
|
||||
def test_v2_remains_compatible_without_candidates(self):
|
||||
snap = snapshot(rule=m.RULE)
|
||||
self.assertNotIn('candidates', snap)
|
||||
self.assertEqual(m.snapshot_check(snap, '2026-11-03'), [])
|
||||
|
||||
def test_v3_contract_and_frozen_hash(self):
|
||||
snap = snapshot()
|
||||
self.assertEqual(m.snapshot_check(snap, '2026-11-03', expected_rule=m.EXECUTION_RULE), [])
|
||||
snap['candidates'][70]['close'] = 20
|
||||
self.assertIn('快照摘要不一致', m.snapshot_check(snap, '2026-11-03'))
|
||||
|
||||
def test_candidate_order_and_mainland_required(self):
|
||||
for change in ('rank', 'code'):
|
||||
snap = snapshot()
|
||||
snap['candidates'][70][change] = 1 if change == 'rank' else '920001.BJ'
|
||||
self.assertIn('冻结候选池数量、资格或顺序不完整', m.snapshot_check(snap, '2026-11-03'))
|
||||
|
||||
def test_rule_mismatch_cannot_be_silent(self):
|
||||
self.assertIn('策略规则版本不一致', m.snapshot_check(snapshot(), '2026-11-03', expected_rule=m.RULE))
|
||||
old = state(snapshot(rule=m.RULE))
|
||||
with self.assertRaises(ValueError):
|
||||
m.transition(old, snapshot(), micro_qty=100, original_qty=0, budget=100000)
|
||||
|
||||
def test_short_pool_is_explicit_and_valid(self):
|
||||
snap = snapshot(size=51)
|
||||
self.assertEqual(m.snapshot_check(snap, '2026-11-03'), [])
|
||||
|
||||
|
||||
class TargetTests(unittest.TestCase):
|
||||
def test_halted_zero_fill_uses_next_rank(self):
|
||||
snap = snapshot(); s = state(snap)
|
||||
m.resolve_targets(s, snap, unavailable={'600000.SH': '停牌'})
|
||||
self.assertNotIn('600000.SH', s['target'])
|
||||
self.assertIn('600050.SH', s['target'])
|
||||
self.assertEqual(len(s['target']), 50)
|
||||
self.assertEqual(s['substitutions'][0]['from'], '600000.SH')
|
||||
|
||||
def test_partial_fill_keeps_seat(self):
|
||||
snap = snapshot(); s = state(snap)
|
||||
m.resolve_targets(s, snap, micro=[{'ts_code': '600000.SH', 'total_qty': 100}],
|
||||
unavailable={'600000.SH': '停牌'})
|
||||
self.assertIn('600000.SH', s['target'])
|
||||
self.assertNotIn('600050.SH', s['target'])
|
||||
|
||||
def test_cancel_pending_keeps_seat(self):
|
||||
snap = snapshot(); s = state(snap)
|
||||
m.resolve_targets(s, snap, pending=[{'ts_code': '600000.SH'}], unavailable={'600000.SH': '停牌'})
|
||||
self.assertIn('600000.SH', s['target'])
|
||||
m.resolve_targets(s, snap, pending=[], unavailable={'600000.SH': '停牌'})
|
||||
self.assertNotIn('600000.SH', s['target'])
|
||||
|
||||
def test_recovery_and_restart_do_not_switch_back_same_day(self):
|
||||
snap = snapshot(); s = state(snap)
|
||||
m.resolve_targets(s, snap, unavailable={'600000.SH': '停牌'})
|
||||
s = json.loads(json.dumps(s))
|
||||
s = m.transition(s, snap, micro_qty=0, original_qty=0, budget=100000)
|
||||
m.resolve_targets(s, snap, unavailable={})
|
||||
self.assertNotIn('600000.SH', s['target'])
|
||||
self.assertIn('600050.SH', s['target'])
|
||||
|
||||
def test_next_day_returns_to_new_frozen_rank(self):
|
||||
snap = snapshot(); s = state(snap)
|
||||
m.resolve_targets(s, snap, unavailable={'600000.SH': '停牌'})
|
||||
tomorrow = snapshot(day='2026-11-04')
|
||||
s = m.transition(s, tomorrow, micro_qty=100, original_qty=0, budget=100000)
|
||||
m.resolve_targets(s, tomorrow, unavailable={})
|
||||
self.assertIn('600000.SH', s['target'])
|
||||
self.assertNotIn('600050.SH', s['target'])
|
||||
|
||||
def test_price_lag_does_not_change_target(self):
|
||||
snap = snapshot(); s = state(snap)
|
||||
m.resolve_targets(s, snap, unavailable={})
|
||||
self.assertEqual(s['target'], [r['code'] for r in snap['members']])
|
||||
|
||||
def test_pool_exhaustion_does_not_remove_good_members(self):
|
||||
snap = snapshot(size=51); s = state(snap)
|
||||
waits = m.resolve_targets(s, snap, unavailable={'600000.SH': '停牌', '600001.SH': '停牌'})
|
||||
self.assertEqual(len(s['target']), 49)
|
||||
self.assertEqual(s['target_shortfall'], 1)
|
||||
self.assertTrue(waits)
|
||||
|
||||
def test_incomplete_daily_member_is_still_topped_up(self):
|
||||
snap = snapshot(); s = state(snap); s['phase'] = 'micro'
|
||||
held = [{'ts_code': r['code'], 'total_qty': 100} for r in snap['members'][1:]]
|
||||
kwargs = dict(original=[], quotes={'600000.SH': {'price': 10}}, cash=5000,
|
||||
account_mv=0, cap_amount=100000)
|
||||
orders, _ = m.orders(s, snap, micro=held, **kwargs)
|
||||
wanted = orders[0]['qty']
|
||||
held.append({'ts_code': '600000.SH', 'total_qty': 100})
|
||||
s = json.loads(json.dumps(s))
|
||||
orders, _ = m.orders(s, snap, micro=held, **kwargs)
|
||||
self.assertEqual(orders[0]['qty'], wanted-100)
|
||||
self.assertEqual(s['buy_targets']['600000.SH']['qty'], wanted)
|
||||
|
||||
def test_daily_target_does_not_expand_after_price_fall(self):
|
||||
snap = snapshot(); s = state(snap); s['phase'] = 'micro'
|
||||
s['buy_targets'] = {'600000.SH': {'qty': 300, 'snapshot_version': 'old'}}
|
||||
held = [{'ts_code': r['code'], 'total_qty': 100} for r in snap['members']]
|
||||
orders, _ = m.orders(s, snap, original=[], micro=held,
|
||||
quotes={'600000.SH': {'price': 1}}, cash=10000, account_mv=0, cap_amount=100000)
|
||||
self.assertEqual(orders[0]['qty'], 200)
|
||||
|
||||
def test_old_positions_do_not_create_cash(self):
|
||||
snap = snapshot(); s = state(snap)
|
||||
orders, _ = m.orders(s, snap, original=[{'ts_code': '000001.SZ', 'total_qty': 100}], micro=[],
|
||||
quotes={'000001.SZ': {'price': 10}, **{r['code']: {'price': 10} for r in snap['members']}},
|
||||
cash=0, account_mv=1000, cap_amount=100000)
|
||||
self.assertTrue(orders)
|
||||
self.assertTrue(all(r['side'] == 'sell' for r in orders))
|
||||
|
||||
def test_high_exit_overrides_unfinished_entry(self):
|
||||
snap = snapshot(); s = state(snap); snap['signal']['high'] = True
|
||||
s = m.transition(s, snap, micro_qty=100, original_qty=0, budget=100000)
|
||||
self.assertEqual(s['phase'], 'returning_original')
|
||||
before = copy.deepcopy(s)
|
||||
m.resolve_targets(s, snap, unavailable={'600000.SH': '停牌'})
|
||||
self.assertEqual(s, before)
|
||||
|
||||
|
||||
class ServiceBoundaries(unittest.TestCase):
|
||||
def prepare(self, snap, funds, scale, *, prepare_only=True, fail_limits=False,
|
||||
missing_position=False, refresh=None):
|
||||
from app.services import rotation_service as r, rotation_execution as execution, execution_orders, command_service
|
||||
stored = {'rotation': m.initial_state()}
|
||||
config = {'mode': 'auto', 'control': 'run', 'rule_version': snap['rule_version'],
|
||||
'candidate_pool_size': 100, 'boards': {'MAIN','CHINEXT','STAR'}, 'costs': {}}
|
||||
@contextmanager
|
||||
def locked(*args):
|
||||
yield stored
|
||||
with ExitStack() as stack:
|
||||
for obj, name, value in (
|
||||
(r, 'configuration', config), (r, 'documents_ready', True), (r, 'activation_errors', []),
|
||||
(r, 'pull', snap), (r, 'reconcile_books', {'ok': True, 'diffs': []}),
|
||||
(r, 'positions', [{'ts_code':'600000.SH','total_qty':100}] if missing_position else []),
|
||||
(r, '_quote', {} if missing_position else {'price': 10}),
|
||||
(r, '_limits', {}), (r.market, 'execution_quote', {}),
|
||||
(execution_orders, 'quote_order', {'action':'WAIT','reason':'没有当日限制价'}),
|
||||
(r.td, 'calendar_degraded', False), (r.td, 'is_trade_day', True),
|
||||
(r.portfolio, 'cash_view', {'source': 'ws', 'cash_avail': funds}),
|
||||
(r.store, 'get', (stored, 0)), (r.store, 'put_once', None),
|
||||
(r, 'cancel_control_orders', None), (execution, 'cancel_expired', None),
|
||||
(execution, 'suspended_rejections', {}),
|
||||
(r.pms_repo, 'active_strategies', []), (command_service, 'blacklist', []),
|
||||
(execution, 'pending_instructions', [])):
|
||||
stack.enter_context(patch.object(obj, name, return_value=value))
|
||||
stack.enter_context(patch.object(execution, 'refresh_orders',
|
||||
side_effect=refresh or (lambda *a, **kw: ([], []))))
|
||||
if fail_limits:
|
||||
stack.enter_context(patch.object(r, '_limits', side_effect=RuntimeError('in-memory query failure')))
|
||||
stack.enter_context(patch.object(r.store, 'locked', side_effect=locked))
|
||||
stage = stack.enter_context(patch.object(r.store, 'stage_locked'))
|
||||
stack.enter_context(patch.object(r.settings, 'INFRA_STRATEGY_BOOK_SCHEMA', True))
|
||||
stack.enter_context(patch.object(r.param_store, 'get_bool', return_value=False))
|
||||
stack.enter_context(patch.object(r.param_store, 'get_float', side_effect=lambda k,d=0: {
|
||||
'PMS_TOTAL_SCALE': scale, 'PMS_PORTFOLIO_CAP': 1, 'PMS_CASH_RESERVE': 0}.get(k,d)))
|
||||
result = r._tick(dt.datetime(2026, 11, 3, 9, 31, tzinfo=r.TZ), prepare_only=prepare_only)
|
||||
return stored, result, stage.call_count
|
||||
|
||||
def test_static_failure_prevents_committing_entry(self):
|
||||
stored, result, writes = self.prepare(snapshot(rule=m.RULE), 500, 500)
|
||||
self.assertEqual(stored['rotation']['phase'], 'original')
|
||||
self.assertIn('blocked', result)
|
||||
self.assertEqual(writes, 0)
|
||||
|
||||
def test_v3_zero_budget_does_not_clear_old_strategy(self):
|
||||
stored, result, writes = self.prepare(snapshot(), 0, 100000)
|
||||
self.assertEqual(stored['rotation']['phase'], 'original')
|
||||
self.assertEqual(writes, 0)
|
||||
|
||||
def test_no_today_limits_blocks_funding_sales(self):
|
||||
from app.services import rotation_execution as execution
|
||||
with patch.object(execution,'send_intent') as send:
|
||||
stored, result, writes = self.prepare(snapshot(),100000,100000,prepare_only=False)
|
||||
self.assertIn('blocked',result)
|
||||
self.assertTrue(any('不卖出原策略' in reason for reason in result['blocked']))
|
||||
send.assert_not_called()
|
||||
|
||||
def test_v3_single_expensive_member_does_not_block_other_members(self):
|
||||
snap = snapshot(); snap['members'][0]['close'] = 10000
|
||||
body = {k:v for k,v in snap.items() if k not in ('snapshot_version','frozen')}
|
||||
snap['snapshot_version'] = hashlib.sha256(json.dumps(body, sort_keys=True, ensure_ascii=False,
|
||||
separators=(',', ':')).encode()).hexdigest()
|
||||
stored, result, writes = self.prepare(snap, 100000, 100000)
|
||||
self.assertEqual(stored['rotation']['phase'], 'entering_micro')
|
||||
self.assertNotIn('600000.SH', stored['rotation']['target'])
|
||||
self.assertIn('600050.SH', stored['rotation']['target'])
|
||||
self.assertEqual(writes, 1)
|
||||
|
||||
def test_limit_query_failure_still_refreshes_existing_orders(self):
|
||||
calls=[]
|
||||
def refresh(*args):
|
||||
calls.append(args)
|
||||
return [], []
|
||||
stored,result,_=self.prepare(snapshot(),100000,100000,prepare_only=False,
|
||||
fail_limits=True,refresh=refresh)
|
||||
self.assertEqual(len(calls),1)
|
||||
self.assertTrue(any('限制价查询失败' in reason for reason in result['blocked']))
|
||||
self.assertEqual(stored['rotation'].get('skipped_candidates'),{})
|
||||
|
||||
def test_missing_holding_quote_cancels_during_prepare_phase(self):
|
||||
calls=[]
|
||||
def refresh(*args):
|
||||
calls.append(args)
|
||||
return [], []
|
||||
_,result,writes=self.prepare(snapshot(),100000,100000,missing_position=True,refresh=refresh)
|
||||
self.assertIn('blocked',result)
|
||||
self.assertEqual(writes,0)
|
||||
self.assertEqual(len(calls),1)
|
||||
self.assertEqual(calls[0][1],{})
|
||||
|
||||
def test_unknown_quote_is_not_a_suspension(self):
|
||||
from app.services.rotation_service import _unavailable_candidates
|
||||
out = _unavailable_candidates(snapshot(), {'600000.SH': {}}, 100000,
|
||||
{'MAIN', 'CHINEXT', 'STAR'}, {})
|
||||
self.assertEqual(out, {})
|
||||
|
||||
def test_explicit_halt_affects_only_one_member(self):
|
||||
from app.services.rotation_service import _unavailable_candidates
|
||||
out = _unavailable_candidates(snapshot(), {'600000.SH': {'halted': True}}, 100000,
|
||||
{'MAIN', 'CHINEXT', 'STAR'}, {})
|
||||
self.assertEqual(set(out), {'600000.SH'})
|
||||
|
||||
def test_live_child_survives_terminal_parent_in_pending(self):
|
||||
from app.services import rotation_execution as r
|
||||
parent = {'instruction_id': 'old', 'book_id': 'micro30', 'ts_code': '600000.SH', 'status': 'CANCELLED'}
|
||||
with patch.object(r.pms_repo, 'list_instructions', return_value=[]), \
|
||||
patch.object(r.pms_repo, 'get_instruction', return_value=parent), \
|
||||
patch.object(r.qmt_repo, 'list_orders', return_value=[{'parent_id': 'old'}]):
|
||||
self.assertEqual(r.pending_instructions(), [parent])
|
||||
|
||||
def test_unknown_live_child_owner_blocks(self):
|
||||
from app.services import rotation_execution as r
|
||||
with patch.object(r.pms_repo, 'list_instructions', return_value=[]), \
|
||||
patch.object(r.pms_repo, 'get_instruction', return_value=None), \
|
||||
patch.object(r.qmt_repo, 'list_orders', return_value=[{'parent_id': 'unknown'}]):
|
||||
with self.assertRaises(ValueError):
|
||||
r.pending_instructions()
|
||||
|
||||
def send_existing(self, settled, cancel=False):
|
||||
from app.services import rotation_execution as r, execution_orders as e, dispatcher
|
||||
parent = {'instruction_id':'p', 'book_id':'micro30', 'ts_code':'600000.SH', 'side':'buy',
|
||||
'qty':300, 'exec_qty':100, 'status':'DISPATCHED',
|
||||
'progress':{'children':[{'ref':'p_D01'}], **({'cancel_requested':True} if cancel else {})}}
|
||||
with patch.object(r.db, 'transaction', side_effect=nullcontext), \
|
||||
patch.object(r.pms_repo, 'get_instruction', return_value=parent), \
|
||||
patch.object(r.pms_repo, 'update_instruction', return_value=1), \
|
||||
patch.object(e, 'code_settlement_blocker', return_value=''), \
|
||||
patch.object(e, 'replacement_state', return_value=settled), \
|
||||
patch.object(e, 'retry_reason', return_value=''), \
|
||||
patch.object(e, 'next_child_id', return_value='p_D02'), \
|
||||
patch.object(dispatcher, 'mode', return_value='ws'), \
|
||||
patch.object(dispatcher, 'dispatch', return_value={'ok':True}) as send:
|
||||
result = r.send_intent({'id':'p','book_id':'micro30','ts_code':'600000.SH','side':'buy',
|
||||
'qty':300,'reason':'补齐'},10,dt.datetime(2026,11,3,9,40),'snapshot')
|
||||
return result, send.call_args
|
||||
|
||||
def test_replacement_waits_until_cancellation_and_booking_finish(self):
|
||||
result, sent = self.send_existing({'ready':False,'remaining':200})
|
||||
self.assertTrue(result['pending'])
|
||||
self.assertIsNone(sent)
|
||||
|
||||
def test_replacement_only_submits_unfilled_quantity(self):
|
||||
result, sent = self.send_existing({'ready':True,'remaining':200})
|
||||
self.assertEqual(sent.kwargs['qty'],200)
|
||||
self.assertEqual(sent.kwargs['instruction_id'],'p_D02')
|
||||
|
||||
def test_user_cancelled_parent_never_reopens(self):
|
||||
result, sent = self.send_existing({'ready':True,'remaining':200},cancel=True)
|
||||
self.assertIsNone(sent)
|
||||
|
||||
def test_partial_fill_after_cancel_still_keeps_seat(self):
|
||||
snap=snapshot(); s=state(snap)
|
||||
m.resolve_targets(s,snap,pending=[{'ts_code':'600000.SH'}],unavailable={'600000.SH':'停牌'})
|
||||
m.resolve_targets(s,snap,micro=[{'ts_code':'600000.SH','total_qty':100}],unavailable={'600000.SH':'停牌'})
|
||||
self.assertIn('600000.SH',s['target'])
|
||||
self.assertEqual(len(s['target']),50)
|
||||
|
||||
def test_missing_quote_cancels_live_child_without_substituting(self):
|
||||
from app.services import rotation_execution as r, execution_orders as e, executor
|
||||
parent={'instruction_id':'p','book_id':'micro30','ts_code':'600000.SH','side':'buy','qty':100,
|
||||
'progress':{'execution_profile':'rotation','rule_version':m.EXECUTION_RULE,'deadline':'2026-11-03'}}
|
||||
with patch.object(r.qmt_repo,'orders_for_parent',return_value=[{'status':'ACCEPTED'}]), \
|
||||
patch.object(e,'quote_order',return_value={'action':'WAIT','reason':'行情缺失'}), \
|
||||
patch.object(e,'request_replace',return_value={'pending':True}) as replace, \
|
||||
patch.object(executor,'cancel_instruction') as cancel, \
|
||||
patch.object(r.db,'savepoint',side_effect=nullcontext):
|
||||
result,waits=r.refresh_orders([parent],{},dt.datetime(2026,11,3,9,40),'v',{'600000.SH'},{})
|
||||
replace.assert_called_once()
|
||||
cancel.assert_not_called()
|
||||
self.assertEqual(result,[])
|
||||
self.assertTrue(waits)
|
||||
|
||||
def test_missing_quote_without_live_child_only_waits(self):
|
||||
from app.services import rotation_execution as r, execution_orders as e
|
||||
parent={'instruction_id':'p','book_id':'micro30','ts_code':'600000.SH','side':'buy','qty':100,
|
||||
'progress':{'execution_profile':'rotation','rule_version':m.EXECUTION_RULE,'deadline':'2026-11-03'}}
|
||||
with patch.object(r.qmt_repo,'orders_for_parent',return_value=[]), \
|
||||
patch.object(e,'quote_order',return_value={'action':'WAIT','reason':'行情缺失'}), \
|
||||
patch.object(e,'request_replace') as replace:
|
||||
r.refresh_orders([parent],{},dt.datetime(2026,11,3,9,40),'v',{'600000.SH'},{})
|
||||
replace.assert_not_called()
|
||||
|
||||
|
||||
class ReservationTests(unittest.TestCase):
|
||||
def release(self, *, filled=0, fresh=True, booked=True):
|
||||
from app.services import rotation_execution as r, execution_orders as e
|
||||
now=dt.datetime(2026,11,3,9,40)
|
||||
account={'available':10010.,'orders':{'a':1005.,'b':9005.}}
|
||||
orders={'a':{'instruction_id':'a','parent_id':'pa','status':'CANCELLED',
|
||||
'qty':1000,'cum_qty':filled,'limit_price':1.,'final_at':now},
|
||||
'b':{'instruction_id':'b','parent_id':'pb','status':'ACCEPTED',
|
||||
'qty':9000,'cum_qty':0,'limit_price':1.,'final_at':None}}
|
||||
funds={'received_at':now+dt.timedelta(seconds=1 if fresh else -1)}
|
||||
state={'pending_trades':0 if booked else 1,'booked':filled if booked else 0,
|
||||
'confirmed_filled':filled,'missing_orders':[]}
|
||||
with patch.object(r.qmt_repo,'get_order',side_effect=orders.get), \
|
||||
patch.object(e,'replacement_state',return_value=state):
|
||||
r.release_settled_reservations(account,funds)
|
||||
r.release_settled_reservations(account,funds)
|
||||
return account
|
||||
|
||||
def test_cancelled_one_releases_budget_while_others_live(self):
|
||||
account=self.release()
|
||||
self.assertEqual(account['orders'],{'a':0.,'b':9005.})
|
||||
self.assertEqual(account['available']-sum(account['orders'].values()),1005.)
|
||||
|
||||
def test_partial_cancel_keeps_filled_principal_and_original_fee(self):
|
||||
account=self.release(filled=100)
|
||||
self.assertEqual(account['orders'],{'a':105.,'b':9005.})
|
||||
self.assertEqual(account['original_reservations']['a'],1005.)
|
||||
|
||||
def test_old_funds_snapshot_cannot_release(self):
|
||||
self.assertEqual(self.release(fresh=False)['orders']['a'],1005.)
|
||||
|
||||
def test_unbooked_trade_cannot_release(self):
|
||||
self.assertEqual(self.release(filled=100,booked=False)['orders']['a'],1005.)
|
||||
|
||||
|
||||
class SuspensionEvidenceTests(unittest.TestCase):
|
||||
def evidence(self, code='SUSPENDED', filled=0, ready=True, deadline='2026-11-03'):
|
||||
from app.services import rotation_execution as r, execution_orders as e
|
||||
order={'instruction_id':'p_D01','parent_id':'p','ts_code':'600000.SH','status':'REJECTED',
|
||||
'reject_code':code,'cum_qty':filled}
|
||||
parent={'instruction_id':'p','book_id':'micro30','exec_qty':filled,
|
||||
'progress':{'execution_profile':'rotation','rule_version':m.EXECUTION_RULE,'deadline':deadline}}
|
||||
with patch.object(r.db,'fetch_all',return_value=[order]), \
|
||||
patch.object(r.pms_repo,'get_instruction',return_value=parent), \
|
||||
patch.object(e,'replacement_state',return_value={'ready':ready,'confirmed_filled':filled}):
|
||||
return r.suspended_rejections(dt.datetime(2026,11,3,9,40))
|
||||
|
||||
def test_exact_suspended_zero_fill_can_substitute(self):
|
||||
result=self.evidence()
|
||||
self.assertIn('600000.SH',result)
|
||||
self.assertIn('p_D01',result['600000.SH'])
|
||||
|
||||
def test_generic_errors_and_price_errors_never_substitute(self):
|
||||
for code in ('BROKER_ERROR','BAD_PARAM','UNKNOWN_CODE','LIMIT_UP','INTERNAL','INSUFFICIENT_CASH'):
|
||||
self.assertEqual(self.evidence(code=code),{})
|
||||
|
||||
def test_partial_or_pending_never_substitutes(self):
|
||||
self.assertEqual(self.evidence(filled=100),{})
|
||||
self.assertEqual(self.evidence(ready=False),{})
|
||||
|
||||
def test_previous_day_rejection_does_not_carry_forward(self):
|
||||
self.assertEqual(self.evidence(deadline='2026-11-02'),{})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
@ -0,0 +1,174 @@
|
|||
"""异步研判的持久化、迟到隔离与真实行情来源边界。"""
|
||||
import unit_test_guard
|
||||
import contextlib
|
||||
import copy
|
||||
import unittest
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from unittest.mock import patch, Mock
|
||||
from test_wiring import install_fakes
|
||||
from app.services import judge_async as ja, market, judge
|
||||
|
||||
NOW = datetime(2026,11,3,10,0,tzinfo=timezone(timedelta(hours=8)))
|
||||
|
||||
|
||||
class AsyncJudgeTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.fake = install_fakes(params={'PMS_JUDGE_ASYNC_ENABLED':True})
|
||||
self.docs = {}
|
||||
@contextlib.contextmanager
|
||||
def locked(key, kind, initial):
|
||||
value = copy.deepcopy(self.docs.get(key, initial))
|
||||
yield value
|
||||
self.docs[key] = value
|
||||
patch.object(ja.repo,'locked',locked).start()
|
||||
patch.object(ja.repo,'recent',side_effect=lambda *a:[{'value':v} for v in self.docs.values()]).start()
|
||||
self.post = patch('requests.post',side_effect=self.submit).start()
|
||||
self.get = patch('requests.get',side_effect=self.poll).start()
|
||||
self.state = 'PENDING'
|
||||
self.c = {'ts_code':'600000.SH','action':'OPEN','qty':100,'hard_numbers':{'plan_basis':{'logic':'stable'}}}
|
||||
self.payload = {'direction':'PMS_JUDGE','ts_code':'600000.SH','action':'OPEN','qty':100,
|
||||
'hard_numbers':{'price':10}, 'context':{'position':{'total_qty':0}}}
|
||||
self.addCleanup(patch.stopall)
|
||||
|
||||
def response(self, data):
|
||||
return Mock(json=lambda:data,raise_for_status=lambda:None)
|
||||
|
||||
def submit(self, url, json, **kw):
|
||||
self.sent = json
|
||||
return self.answer()
|
||||
|
||||
def poll(self, *a, **kw):
|
||||
return self.answer()
|
||||
|
||||
def answer(self):
|
||||
return self.response({'verdict':self.state, 'request_id':self.sent['request_id'],
|
||||
'evidence_fingerprint':self.sent['evidence_fingerprint'],'reason':'test'})
|
||||
|
||||
def ask(self, now=NOW):
|
||||
return ja.request(self.c, self.payload, 'http://stub', now)
|
||||
|
||||
def test_register_then_poll_without_duplicate_model(self):
|
||||
self.assertEqual(self.ask()['verdict'],'PENDING')
|
||||
self.state='PASS'
|
||||
self.assertEqual(self.ask(NOW+timedelta(seconds=10))['verdict'],'PASS')
|
||||
self.assertEqual((self.post.call_count,self.get.call_count),(1,1))
|
||||
|
||||
def test_price_and_quantity_noise_reuses_original_evidence(self):
|
||||
self.ask()
|
||||
self.payload['hard_numbers']['price']=10.8
|
||||
self.payload['qty']=300
|
||||
self.ask(NOW+timedelta(seconds=10))
|
||||
self.assertEqual(self.post.call_count,1)
|
||||
self.assertEqual(self.sent['hard_numbers']['price'],10)
|
||||
|
||||
def test_changed_research_creates_new_request(self):
|
||||
self.ask(); old=self.sent['request_id']
|
||||
self.c['hard_numbers']['plan_basis']['logic']='changed'
|
||||
self.ask(NOW+timedelta(seconds=10))
|
||||
self.assertNotEqual(self.sent['request_id'],old)
|
||||
|
||||
def test_changed_actual_holding_creates_new_request(self):
|
||||
self.ask();old=self.sent['request_id']
|
||||
self.payload['context']['position']['total_qty']=100
|
||||
self.ask(NOW+timedelta(seconds=10))
|
||||
self.assertNotEqual(self.sent['request_id'],old)
|
||||
|
||||
def test_deadline_includes_queue_and_never_polls_late(self):
|
||||
self.ask();self.state='PASS'
|
||||
result=self.ask(NOW+timedelta(seconds=121))
|
||||
self.assertEqual(result['verdict'],'UNAVAILABLE')
|
||||
self.get.assert_not_called()
|
||||
|
||||
def test_wrong_fingerprint_never_authorizes(self):
|
||||
self.ask()
|
||||
self.get.side_effect=lambda *a,**kw:self.response({'verdict':'PASS',
|
||||
'request_id':self.sent['request_id'],'evidence_fingerprint':'wrong'})
|
||||
self.assertEqual(self.ask(NOW+timedelta(seconds=10))['verdict'],'UNAVAILABLE')
|
||||
|
||||
def test_cached_result_is_reused_without_http(self):
|
||||
self.state='PASS';self.ask();self.ask(NOW+timedelta(seconds=10))
|
||||
self.assertEqual(self.post.call_count,1);self.get.assert_not_called()
|
||||
|
||||
def test_late_old_response_cannot_replace_new_request(self):
|
||||
self.ask()
|
||||
def replace(*a,**kw):
|
||||
for doc in self.docs.values():
|
||||
doc['request_id']='new_request'
|
||||
self.state='PASS'
|
||||
return self.answer()
|
||||
self.get.side_effect=replace
|
||||
self.assertEqual(self.ask(NOW+timedelta(seconds=10))['verdict'],'UNAVAILABLE')
|
||||
self.assertEqual(next(iter(self.docs.values()))['request_id'],'new_request')
|
||||
|
||||
def test_reask_survives_next_scan_and_retains_manual_confirmation(self):
|
||||
self.c['reask']={'why':'changed','hits':['price']}
|
||||
self.payload['force_refresh']=True
|
||||
self.ask()
|
||||
self.assertIn(('600000.SH','OPEN'),ja.resumable_reasks(NOW))
|
||||
self.c.pop('reask');self.payload.pop('force_refresh');self.state='PASS'
|
||||
self.ask(NOW+timedelta(seconds=10))
|
||||
self.assertIn('reask',self.c)
|
||||
self.assertFalse(ja.resumable_reasks(NOW))
|
||||
|
||||
def test_beijing_code_does_not_reach_storage_or_network(self):
|
||||
self.payload['ts_code']='920001.BJ'
|
||||
self.assertEqual(self.ask()['verdict'],'UNAVAILABLE')
|
||||
self.post.assert_not_called();self.assertEqual(self.docs,{})
|
||||
|
||||
def test_persist_failure_never_sends(self):
|
||||
with patch.object(ja.repo,'locked',side_effect=RuntimeError('db')):
|
||||
self.assertEqual(self.ask()['verdict'],'UNAVAILABLE')
|
||||
self.post.assert_not_called()
|
||||
|
||||
def test_network_timeout_keeps_pending_and_original_request(self):
|
||||
import requests
|
||||
self.post.side_effect=requests.Timeout('test')
|
||||
self.assertEqual(self.ask()['verdict'],'PENDING')
|
||||
old=next(iter(self.docs.values()))['request_id']
|
||||
self.post.side_effect=self.submit
|
||||
self.ask(NOW+timedelta(seconds=10))
|
||||
self.assertEqual(self.sent['request_id'],old)
|
||||
|
||||
def test_expired_request_does_not_requeue_same_evidence(self):
|
||||
self.ask();self.ask(NOW+timedelta(seconds=121))
|
||||
self.assertEqual(self.ask(NOW+timedelta(seconds=601))['verdict'],'UNAVAILABLE')
|
||||
self.assertEqual(self.post.call_count,1)
|
||||
|
||||
def test_async_dry_run_never_registers(self):
|
||||
from app.services import proposal_service as ps
|
||||
c={'ts_code':'600000.SH','action':'OPEN','side':'buy','qty':100,'price':10,
|
||||
'reason':'test','hard_numbers':{},'judge_required':True}
|
||||
out={k:[] for k in ('skipped','rejected','queued','executed')}
|
||||
with patch.object(ps,'check_candidate_rules',return_value={'passed':True}), \
|
||||
patch.object(ps.judge,'request',side_effect=AssertionError('试算不得登记')), \
|
||||
patch.object(ps,'_recent_ledger',return_value=[]):
|
||||
ps._route_one(c,{'positions':[]},{},{},False,NOW,True,out)
|
||||
self.assertFalse(self.docs)
|
||||
|
||||
def test_pending_route_is_neither_pass_nor_manual_proposal(self):
|
||||
from app.services import proposal_service as ps
|
||||
c={'ts_code':'600000.SH','action':'OPEN','side':'buy','qty':100,'price':10,
|
||||
'reason':'test','hard_numbers':{},'judge_required':True}
|
||||
out={k:[] for k in ('skipped','rejected','queued','executed')}
|
||||
with patch.object(ps,'check_candidate_rules',return_value={'passed':True}), \
|
||||
patch.object(ps.judge,'request',return_value={'verdict':'PENDING','reason':'waiting'}), \
|
||||
patch.object(ps,'_recent_ledger',return_value=[]):
|
||||
ps._route_one(c,{'positions':[]},{},{},False,NOW,False,out)
|
||||
self.assertEqual(out['skipped'][0]['by'],'judge_pending')
|
||||
self.assertEqual(out['queued']+out['executed']+out['rejected'],[])
|
||||
|
||||
|
||||
class QuoteTests(unittest.TestCase):
|
||||
def test_minute_quote_keeps_unknown_halt_and_no_invented_level1(self):
|
||||
with patch.object(market,'_bars',return_value=[{'time':'2026-11-03 09:59:00','close':10}]):
|
||||
q=market.execution_quote('600000.SH',NOW,{'up_limit':11,'down_limit':9})
|
||||
self.assertEqual(q['kind'],'minute');self.assertIsNone(q['halted'])
|
||||
self.assertIsNone(q['ask']);self.assertEqual(q['as_of'],'2026-11-03T09:59:00+08:00')
|
||||
|
||||
def test_missing_date_cannot_be_filled_with_today(self):
|
||||
with patch.object(market,'_bars',return_value=[{'time':'09:59:00','close':10}]):
|
||||
self.assertEqual(market.execution_quote('600000.SH',NOW,{}),{})
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
unittest.main()
|
||||
|
|
@ -250,7 +250,8 @@ class FakeRepo:
|
|||
self.instructions[kw["instruction_id"]] = kw
|
||||
return 1
|
||||
|
||||
def list_instructions(self, *, statuses=None, side=None, ts_code=None, limit=300):
|
||||
def list_instructions(self, *, statuses=None, side=None, ts_code=None, limit=300,
|
||||
include_archived=False):
|
||||
out = [i for i in self.instructions.values()
|
||||
if (not statuses or i["status"] in statuses)
|
||||
and (not side or i.get("side") == side)
|
||||
|
|
@ -275,6 +276,12 @@ class FakeRepo:
|
|||
return 0
|
||||
if status == 'DISPATCHED' and i.get('status') not in ('PROPOSED', 'RULE_PASSED', 'JUDGE_PASSED', 'DISPATCHED'):
|
||||
return 0
|
||||
if progress is not None:
|
||||
progress = dict(progress)
|
||||
previous = i.get('progress') or {}
|
||||
for key in ('execution_basis_cache', 'cancel_requested', 'replacement_requested'):
|
||||
if key in previous and (key == 'execution_basis_cache' or key not in progress):
|
||||
progress[key] = previous[key]
|
||||
for k, v in (("status", status), ("exec_qty", exec_qty),
|
||||
("exec_avg_price", exec_avg_price), ("dispatch_ref", dispatch_ref),
|
||||
("progress", progress)):
|
||||
|
|
@ -663,6 +670,15 @@ def install_fakes(prices=None, positions=None, params=None, high5=None, prev_clo
|
|||
"request_cancel", "get_order", "orders_for_parent", "orders_for_fill", "inbox_pending", "inbox_mark",
|
||||
"inbox_orphan_count", "latest_snapshot"):
|
||||
setattr(qmt_repo, _n, getattr(fake.qmt, _n))
|
||||
from app.repo import execution_repo
|
||||
def save_basis(iid, value):
|
||||
if iid not in fake.instructions:
|
||||
raise RuntimeError('执行依据缓存没有写入父指令')
|
||||
fake.instructions[iid].setdefault('progress', {})['execution_basis_cache'] = value
|
||||
execution_repo.save_basis = save_basis
|
||||
execution_repo.pending_trades = lambda ids: sum(1 for row in fake.qmt.inbox.values()
|
||||
if row.get('msg_type') == 'trade' and row.get('processed') in (0, 3)
|
||||
and (row.get('payload') or {}).get('instruction_id') in ids)
|
||||
fake.params.update(params or {})
|
||||
for p in (positions or []):
|
||||
base = {"ts_code": p["ts_code"], "status": "HOLDING", "frozen_reason": "NONE",
|
||||
|
|
@ -777,7 +793,7 @@ def _():
|
|||
names = set(sch.celery_app.conf.beat_schedule)
|
||||
assert names == {"plan_pull", "premarket", "command_poll", "replay_fills",
|
||||
"intraday_exec", "signal_digest", "t0_close", "daily_settle",
|
||||
"daily_report", "rotation_tick",
|
||||
"daily_report", "rotation_tick", "proposal_scan", "execution_basis_refresh", "execution_cancel_confirm",
|
||||
"macro_scan", # 2026-08-18 +宏观择时 09:35
|
||||
"strategy_attach", # 2026-08-25 +策略自动挂载 09:40
|
||||
"nav_snapshot", # 2026-08-28 +公示净值快照 15:20
|
||||
|
|
@ -786,7 +802,7 @@ def _():
|
|||
for t in ("pms.premarket", "pms.command_poll", "pms.replay_fills", "pms.daily_settle",
|
||||
"pms.daily_report", "pms.t0_close", "pms.intraday_exec", "pms.signal_digest",
|
||||
"pms.plan_pull", "pms.macro_scan", "pms.strategy_attach", "pms.nav_snapshot",
|
||||
"pms.tech_pull", "pms.rotation_tick"):
|
||||
"pms.tech_pull", "pms.rotation_tick", "pms.proposal_scan", "pms.execution_basis_refresh", "pms.execution_cancel_confirm"):
|
||||
assert t in tasks, t
|
||||
assert sch.in_session(datetime(2026, 7, 27, 10, 0)) is True
|
||||
assert sch.in_session(datetime(2026, 7, 27, 12, 0)) is False
|
||||
|
|
@ -1699,7 +1715,7 @@ def _():
|
|||
assert dispatcher.channel_status()["orphan_held"] == 1
|
||||
|
||||
|
||||
@case("下发通道·影子回执 / ws 出口队列 / 进程与连接两级降级 / 撤销走本地置状态")
|
||||
@case("下发通道·影子回执 / ws出口与连接降级 / 撤单请求与终态确认分离")
|
||||
def _():
|
||||
from datetime import datetime as _dtm
|
||||
from app.services import dispatcher, executor, param_store
|
||||
|
|
@ -1760,7 +1776,22 @@ def _():
|
|||
status="DISPATCHED", progress={"children": []})
|
||||
r = executor.cancel_instruction("INS_C")
|
||||
assert r["ok"] and fake.instructions["INS_C"]["status"] == "CANCELLED", r
|
||||
assert executor.cancel_instruction("INS_C")["ok"] is False
|
||||
assert executor.cancel_instruction("INS_C")["confirmed"] is True
|
||||
assert param_store.set_param('PMS_DISPATCH_MODE', 'ws')['ok']
|
||||
fake.insert_instruction(instruction_id='INS_PENDING', origin_type='strategy', origin_id='S',
|
||||
ts_code='600000.SH', action='TRIM', side='sell', qty=1000,
|
||||
status='DISPATCHED', progress={'children': []})
|
||||
sent = _send('INS_PENDING_D01')
|
||||
assert sent['ok'], sent
|
||||
pending = executor.cancel_instruction('INS_PENDING')
|
||||
assert pending['ok'] and pending['pending'] and not pending['confirmed'], pending
|
||||
assert fake.instructions['INS_PENDING']['status'] == 'DISPATCHED'
|
||||
fake.qmt.orders['INS_PENDING_D01'].update(status='CANCELLED', cum_qty=200)
|
||||
assert executor.cancel_instruction('INS_PENDING')['pending'], '部分成交未入账不能确认撤单结束'
|
||||
fake.instructions['INS_PENDING']['exec_qty'] = 200
|
||||
final = executor.cancel_instruction('INS_PENDING')
|
||||
assert final['confirmed'] and not final['pending'], final
|
||||
assert fake.instructions['INS_PENDING']['status'] == 'CANCELLED'
|
||||
|
||||
|
||||
def _prop_fakes(**kw):
|
||||
|
|
|
|||
Loading…
Reference in New Issue