Compare commits
3 Commits
4b34f42d9c
...
4f50e924e5
| Author | SHA1 | Date |
|---|---|---|
|
|
4f50e924e5 | |
|
|
165d25217f | |
|
|
06cfb0ac24 |
|
|
@ -3280,3 +3280,7 @@ a44c994于10:53:29仅发布155的web,镜像pms:proposal-copy-a44c994。开发
|
|||
开发机90组隔离禁网全量通过,含20项资金状态及25项关注发布测试。拟定向更新155 web及worker(worker仅传递原始登记时刻),按实际角色镜像构建并继续候选断网验收;此记录时尚未部署。项目规则补充来源周期、交易时段和不可重置的等待上限。
|
||||
|
||||
13:09补记:午后13:01:30新增603087自然接入;最初baseline_unavailable并非仍在补历史,准备实际11:53已完成,13:00属于有效分钟映射之外,13:03:31六路已全部evaluated。13:05旧版提示自然消失,不把上游自然恢复归功于部署。web89组、worker87组候选断网回归通过后,13:08:30温和排空并仅重建这两个服务,镜像fund-delay-cac3ba3;其他28容器不变、私有配置保持。13:09自然名单携带原始登记时刻并被web读取,15只优先股票及132只告警池实时资金可用、提示为空。连续确认1942098、数量和现金差零,无缺口或在途,HTTP200。午休反例使用实际响应离线回放,未制造新增关注或测试信号;下一次自然新关注等待不冒充已发生。完整回执见[接续](docs/资金提示预期延迟与故障区分_2026-11-18.md)。
|
||||
|
||||
## 2026-11-18 18:16 北京时间,交易规则与实际入口对齐
|
||||
|
||||
交易基线 `4b34f42`,实现 `06cfb0a/165d252`。修复逐票资格、自主与跨日批准、择时收件时钟、人工止损观察、策略独占、分批数量、微盘退出和两个账本的最终禁买保护。开发机干净提交断网 92 组通过,指纹 `383510093af5`;155 与内网 Git 的 SSH 握手关闭,未验证或发布候选镜像。交易员可从[修复、历史与判收](docs/交易规则修复与历史复盘_2026-11-18.md)查设计依据和自然验收位置。
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@
|
|||
- 业务参数走`services/param_store.py`;`config/settings.py`只是业务初值和基础设施配置。新密钥同时登记`SECRET_KEYS`和相应测试,不进入参数快照。
|
||||
- 资金冻结、在途买入、卖出释放及策略划拨使用既有统一占用口径。新增执行路径不能另算一份余额;划拨不是盈利。
|
||||
- 关键写入和下发函数可能返回`ok=False`或影响0行而不抛异常,必须检查返回值并处理失败。新软失败函数同步登记`test_batch10_units.py`的`SOFT_FAIL`。
|
||||
- 同一授权存在自动、人工、复核或恢复入口时,共用业务事实。涉及共享额度或独占策略的最终授权在同一短事务内重读,批准与可执行指令同成同败。测试覆盖在两阶段之间改变授权、部分成功后重试,以及应继续允许的反向操作。
|
||||
- 内部股票代码使用Tushare点式,例如`600000.SH`;访问上游时在边界转换。改`core/ws_codec.py`后验证协议签名向量,不能只看连接成功。
|
||||
- 修改接口先查对应契约:QMT用[协议](../QMT_WS_PROTOCOL.md),bionic用[接口说明](../BIONIC_PMS_INTERFACE.md),上游选股用[计划接口](../UPSTREAM_PLAN_API.md)。架构定位见[README](../README.md)。
|
||||
|
||||
|
|
|
|||
|
|
@ -206,7 +206,7 @@ def preflight(members, budget, allowed_boards, costs):
|
|||
|
||||
|
||||
def orders(state, snapshot, *, original, micro, quotes, cash, account_mv, cap_amount,
|
||||
reserve_cash=0., pending=(), control='run', blacklisted=(), costs=None):
|
||||
reserve_cash=0., pending=(), control='run', blacklisted=(), costs=None, buy_restrictions=None):
|
||||
"""只形成意图,不把计划卖出计入现金。调用者落表后再执行。"""
|
||||
from app.core.planner import plan_reduce_exposure
|
||||
costs = costs or {}
|
||||
|
|
@ -252,6 +252,9 @@ def orders(state, snapshot, *, original, micro, quotes, cash, account_mv, cap_am
|
|||
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 buy_restrictions and c in buy_restrictions:
|
||||
waits.append(c+':'+buy_restrictions[c])
|
||||
continue
|
||||
if c in original_by or c in active_codes or c in blacklisted:
|
||||
waits.append(c+'旧仓、在途委托或黑名单阻止买入')
|
||||
continue
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ def valid(value, now, *, since=None):
|
|||
if not start or at < start:
|
||||
return None
|
||||
key = value.get('key', '')
|
||||
if len(key) != 64 or any(c not in '0123456789abcdef' for c in key):
|
||||
if not isinstance(key, str) or len(key) != 64 or any(c not in '0123456789abcdef' for c in key):
|
||||
return None
|
||||
groups = value.get('groups')
|
||||
if not isinstance(groups, dict) or set(groups) != set(GROUPS):
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import time
|
|||
import threading
|
||||
import contextlib
|
||||
import contextvars
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from sqlalchemy import create_engine, text
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
|
@ -34,6 +35,20 @@ _lock = threading.Lock()
|
|||
_TRANSACTION = contextvars.ContextVar('pms_transaction', default=None)
|
||||
|
||||
|
||||
@dataclass
|
||||
class TransactionState:
|
||||
local: dict = field(default_factory=dict)
|
||||
after_commit: set = field(default_factory=set)
|
||||
|
||||
|
||||
_state = contextvars.ContextVar('pms_transaction_state', default=None)
|
||||
|
||||
|
||||
def transaction_state():
|
||||
"""供事务参与者保存私有读数和登记提交成功后执行的缓存失效操作。"""
|
||||
return _state.get()
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def transaction():
|
||||
"""跨多条单表语句的原子事务。只在显式业务编排中启用。"""
|
||||
|
|
@ -41,12 +56,17 @@ def transaction():
|
|||
if existing is not None:
|
||||
yield existing
|
||||
return
|
||||
state = TransactionState()
|
||||
with get_engine('proxy').begin() as connection:
|
||||
token = _TRANSACTION.set(connection)
|
||||
state_token = _state.set(state)
|
||||
try:
|
||||
yield connection
|
||||
finally:
|
||||
_state.reset(state_token)
|
||||
_TRANSACTION.reset(token)
|
||||
for callback in state.after_commit:
|
||||
callback()
|
||||
|
||||
@contextlib.contextmanager
|
||||
def savepoint():
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ PMS 自有表数据访问 (pms_*, 153 代理侧, 严格单表)
|
|||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from contextlib import contextmanager
|
||||
from datetime import datetime
|
||||
|
||||
from app.db.session import execute, execute_many, fetch_all, fetch_one
|
||||
|
|
@ -63,8 +64,9 @@ def set_param(key: str, value, updated_by: str = "user") -> int:
|
|||
{"k": key, "v": str(value), "by": updated_by, "ts": _NOW()})
|
||||
|
||||
|
||||
def get_param(key: str):
|
||||
r = fetch_one("SELECT param_value FROM pms_runtime_param WHERE param_key = :k", {"k": key})
|
||||
def get_param(key: str, *, for_update=False):
|
||||
sql = "SELECT param_value FROM pms_runtime_param WHERE param_key = :k"
|
||||
r = fetch_one(sql + (" FOR UPDATE" if for_update else ""), {"k": key})
|
||||
return r["param_value"] if r else None
|
||||
|
||||
|
||||
|
|
@ -91,7 +93,7 @@ def get_command(command_id: str):
|
|||
return _cmd_row(r) if r else None
|
||||
|
||||
|
||||
def list_commands(*, statuses=None, cmd_class=None, limit: int = 200,
|
||||
def list_commands(*, statuses=None, cmd_class=None, cmd_type=None, limit: int = 200,
|
||||
include_archived: bool = False) -> list:
|
||||
where, p = [], {"n": int(limit)}
|
||||
if statuses:
|
||||
|
|
@ -103,6 +105,9 @@ def list_commands(*, statuses=None, cmd_class=None, limit: int = 200,
|
|||
if cmd_class:
|
||||
where.append("cmd_class = :cls")
|
||||
p["cls"] = cmd_class
|
||||
if cmd_type:
|
||||
where.append("cmd_type = :cmd_type")
|
||||
p["cmd_type"] = cmd_type
|
||||
if not include_archived:
|
||||
where.append("archived_at IS NULL")
|
||||
sql = "SELECT * FROM pms_command"
|
||||
|
|
@ -170,7 +175,7 @@ def latest_effective(cmd_type: str, ts_code=None):
|
|||
return _cmd_row(r) if r else None
|
||||
|
||||
|
||||
def list_effective_stock_params(cmd_types=None) -> list:
|
||||
def list_effective_stock_params(cmd_types=None, *, ts_code=None) -> list:
|
||||
"""个股参数命令的当前值 (黑白名单/止损价/目标价/做T授权/冻结 的事实源)。"""
|
||||
sql = ("SELECT * FROM pms_command WHERE cmd_class = 'param' AND status = 'EFFECTIVE' "
|
||||
"AND ts_code IS NOT NULL")
|
||||
|
|
@ -181,7 +186,11 @@ def list_effective_stock_params(cmd_types=None) -> list:
|
|||
keys.append(f":t{i}")
|
||||
p[f"t{i}"] = t
|
||||
sql += f" AND cmd_type IN ({', '.join(keys)})"
|
||||
sql += " ORDER BY id DESC LIMIT 500"
|
||||
if ts_code is not None:
|
||||
sql += " AND ts_code = :code"
|
||||
p["code"] = ts_code
|
||||
# 其他股票的命令增加不能让仍然生效的旧冻结消失。
|
||||
sql += " ORDER BY id DESC"
|
||||
return [_cmd_row(r) for r in fetch_all(sql, p)]
|
||||
|
||||
|
||||
|
|
@ -275,6 +284,23 @@ def list_positions(*, only_open: bool = False) -> list:
|
|||
return fetch_all(sql)
|
||||
|
||||
|
||||
def strategy_guard(ts_code):
|
||||
"""双账本复用交易账户锁,旧账本按唯一证券持仓行串行化。"""
|
||||
from config.settings import settings
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
from app.repo import rotation_repo
|
||||
return rotation_repo.account_guard()
|
||||
return _position_guard(ts_code)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _position_guard(ts_code):
|
||||
from app.db.session import transaction
|
||||
with transaction():
|
||||
fetch_one("SELECT ts_code FROM pms_position WHERE ts_code=:code FOR UPDATE", {"code": ts_code})
|
||||
yield
|
||||
|
||||
|
||||
def get_position(ts_code: str):
|
||||
return fetch_one("SELECT * FROM pms_position WHERE ts_code = :code", {"code": ts_code})
|
||||
|
||||
|
|
@ -416,9 +442,9 @@ def insert_instruction(*, instruction_id, origin_type, origin_id, ts_code, actio
|
|||
"w": int(window_tdays), "st": status, "pg": _dumps(progress), "ts": now})
|
||||
|
||||
|
||||
def list_instructions(*, statuses=None, side=None, ts_code=None, limit: int = 300,
|
||||
def list_instructions(*, statuses=None, side=None, ts_code=None, limit: int | None = 300,
|
||||
include_archived: bool = False) -> list:
|
||||
where, p = [], {"n": int(limit)}
|
||||
where, p = [], {}
|
||||
if statuses:
|
||||
keys = []
|
||||
for i, s in enumerate(statuses):
|
||||
|
|
@ -436,7 +462,10 @@ def list_instructions(*, statuses=None, side=None, ts_code=None, limit: int = 30
|
|||
sql = "SELECT * FROM pms_instruction"
|
||||
if where:
|
||||
sql += " WHERE " + " AND ".join(where)
|
||||
sql += " ORDER BY id DESC LIMIT :n"
|
||||
sql += " ORDER BY id DESC"
|
||||
if limit is not None:
|
||||
sql += " LIMIT :n"
|
||||
p["n"] = int(limit)
|
||||
rows = fetch_all(sql, p)
|
||||
for r in rows:
|
||||
r["progress"] = _loads(r.get("progress_json"), {})
|
||||
|
|
@ -708,6 +737,23 @@ def system_decided_today(since) -> set:
|
|||
return {(r["ts_code"], r["action"]) for r in rows}
|
||||
|
||||
|
||||
@contextmanager
|
||||
def account_decision_guard():
|
||||
"""账户决策与临发额度共用同一实例的短事务锁。"""
|
||||
from app.db import session as db
|
||||
from config.settings import settings
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
from app.repo import rotation_repo
|
||||
with rotation_repo.account_guard():
|
||||
yield
|
||||
else:
|
||||
# 单账本模式无轮换文档表,基础 DDL 已建 ws_state 的唯一账户行。
|
||||
with db.transaction():
|
||||
if not db.fetch_one("SELECT id FROM pms_ws_state WHERE id=1 FOR UPDATE"):
|
||||
raise RuntimeError('无法锁定账户,本次自动建仓暂缓')
|
||||
yield
|
||||
|
||||
|
||||
def system_pass_count_today(since) -> int:
|
||||
"""今天系统自决**自动采纳**的新建仓只数, 供每日上限 PMS_SELF_DECIDE_DAILY_MAX 判断。
|
||||
|
||||
|
|
@ -718,7 +764,7 @@ def system_pass_count_today(since) -> int:
|
|||
"WHERE action = 'OPEN' AND verdict = 'PASS' AND decided_at >= :d "
|
||||
"AND reason LIKE :pfx",
|
||||
{"d": since, "pfx": SYSTEM_ACCEPT_REASON_PREFIX + "%"})
|
||||
return int((r or {}).get("n") or 0)
|
||||
return int(r["n"])
|
||||
|
||||
|
||||
def judge_rejected_open_rows(since) -> dict:
|
||||
|
|
|
|||
|
|
@ -48,6 +48,12 @@ def recent(kind, limit=100):
|
|||
return [{'id': r['doc_id'], 'value': json.loads(r['payload_json']), 'updated_at': str(r['updated_at'])} for r in rows]
|
||||
|
||||
|
||||
def document_ids(kind, *, limit):
|
||||
rows = db.fetch_all('SELECT doc_id FROM pms_rotation_document WHERE kind=:kind LIMIT :n',
|
||||
{'kind': kind, 'n': int(limit)})
|
||||
return {r['doc_id'] for r in rows}
|
||||
|
||||
|
||||
def updated_since(kind, since, limit=6001):
|
||||
rows = db.fetch_all('SELECT doc_id,payload_json FROM pms_rotation_document '
|
||||
'WHERE kind=:kind AND updated_at>=:since ORDER BY updated_at DESC LIMIT :n',
|
||||
|
|
|
|||
|
|
@ -140,6 +140,8 @@ def _brief(r):
|
|||
keep = {k: v for k, v in r.items()
|
||||
if k in ("ok", "fills", "actions", "planned", "failed", "diffs", "errors",
|
||||
"skipped", "avail_reset", "refs", "cursor", "date", "returned")}
|
||||
if 'timing_fusion' in r:
|
||||
keep['timing_fusion'] = r['timing_fusion'].get('status')
|
||||
if isinstance(keep.get("diffs"), list):
|
||||
keep["diffs"] = len(keep["diffs"])
|
||||
if isinstance(keep.get("errors"), list):
|
||||
|
|
@ -244,14 +246,22 @@ def replay_fills():
|
|||
|
||||
|
||||
@celery_app.task(name="pms.intraday_exec")
|
||||
@guard(trade_day=True, session=True)
|
||||
@guard(trade_day=True, session=True, respect_exec_halt=False)
|
||||
@_exclusive("INFRA_LOCK_INTRADAY_EXEC", ttl_sec=230)
|
||||
def intraday_exec():
|
||||
"""盘中执行: 方案转指令 → 择时出手 (规则闸终检 → 下发 → 记子单)。
|
||||
|
||||
含自主提议扫描 (FILL/ADD/DCA/TRIM 动作引擎 → 规则闸 → 研判闸 → 按档位分流)。
|
||||
"""
|
||||
from app.services import executor, proposal_service, strategy_runner
|
||||
from app.services import executor, proposal_service, strategy_runner, risk_exit
|
||||
observe_stops = risk_exit.supported() and not settings.PMS_TABLE_PREFIX
|
||||
halted = param_store.execution_halted() if observe_stops else param_store.get_bool('PMS_GLOBAL_EXEC_HALT', False)
|
||||
if halted:
|
||||
if not observe_stops:
|
||||
return {'skipped': 'exec_halt'}
|
||||
observed = risk_exit.scan(user_stops_only=True)
|
||||
return {'skipped': 'exec_halt', 'risk_exit': observed, 'ok': not observed['errors'],
|
||||
'errors': observed['errors']}
|
||||
r = {"materialized": executor.materialize_plans()}
|
||||
if not param_store.get_bool('PMS_JUDGE_ASYNC_ENABLED', False):
|
||||
from app.services import daily_buy_review
|
||||
|
|
@ -260,7 +270,6 @@ def intraday_exec():
|
|||
if param_store.get_bool('PMS_EXEC_REPRICE_ENABLED', False):
|
||||
from app.services import execution_basis
|
||||
r['execution_basis'] = execution_basis.monitor()
|
||||
from app.services import risk_exit
|
||||
r['risk_exit'] = risk_exit.scan() # 先收尾退出纪律,网格与买入执行不得越过停买状态。
|
||||
r["strategies"] = strategy_runner.tick() # 个股交易方案: 触发即发短窗口指令, 由下面 run_tick 执行
|
||||
# 过期收口改为每分钟一次 (原来只在 15:10 日结跑) —— 让过了窗口/deadline 的单当即作废,
|
||||
|
|
|
|||
|
|
@ -42,9 +42,10 @@ def active_commands() -> list:
|
|||
cs.ST_EFFECTIVE], limit=300)
|
||||
|
||||
|
||||
def effective_stock_params() -> dict:
|
||||
def effective_stock_params(*, cmd_types=STOCK_PARAM_TYPES, ts_code=None) -> dict:
|
||||
"""个股参数命令当前值: {ts_code: {frozen, t0, stop_price, target_price, black, white}}"""
|
||||
rows = pms_repo.list_effective_stock_params(STOCK_PARAM_TYPES)
|
||||
rows = (pms_repo.list_effective_stock_params(cmd_types, ts_code=ts_code) if ts_code else
|
||||
pms_repo.list_effective_stock_params(cmd_types))
|
||||
out = {}
|
||||
for r in sorted(rows, key=lambda x: x["id"]): # 旧 → 新, 后者覆盖前者
|
||||
code = r.get("ts_code")
|
||||
|
|
@ -169,14 +170,20 @@ def issue(cmd_type: str, params: dict, *, note=None, issued_by="user",
|
|||
|
||||
# 只有明确解冻才能恢复;新买入方案失败或旧命令重规划都不能顺带清掉风控停买。
|
||||
from app.services import risk_exit
|
||||
if cmd_type == 'UNFREEZE_STOCK' and risk_exit.supported():
|
||||
from config.settings import settings as infrastructure
|
||||
stock_restriction = cmd_type in ('FREEZE_STOCK', 'UNFREEZE_STOCK', 'BLACKLIST_ADD', 'BLACKLIST_REMOVE')
|
||||
simulated_restriction = stock_restriction and risk_exit.supported() and not infrastructure.PMS_TABLE_PREFIX
|
||||
if simulated_restriction or (cmd_type == 'UNFREEZE_STOCK' and risk_exit.supported()):
|
||||
from app.db import session as db
|
||||
from app.repo import rotation_repo
|
||||
guard = rotation_repo.account_guard if simulated_restriction else db.transaction
|
||||
try:
|
||||
with db.transaction():
|
||||
risk_exit.resume_buy(ts_code)
|
||||
with guard():
|
||||
if cmd_type == 'UNFREEZE_STOCK':
|
||||
risk_exit.resume_buy(ts_code)
|
||||
res = _apply_param(command_id, cmd_type, spec, norm)
|
||||
if not res.get('ok'):
|
||||
raise RuntimeError('解冻命令未生效,保留原停买状态')
|
||||
raise RuntimeError('单股命令未生效,保留原有约束')
|
||||
except (ValueError, RuntimeError) as exc:
|
||||
pms_repo.update_command(command_id, status=cs.ST_CANCELLED, note=str(exc))
|
||||
return {'ok': False, 'command_id': command_id, 'errors': [str(exc)]}
|
||||
|
|
@ -186,7 +193,6 @@ def issue(cmd_type: str, params: dict, *, note=None, issued_by="user",
|
|||
res = plan_command(pms_repo.get_command(command_id))
|
||||
res.setdefault("command_id", command_id)
|
||||
res["conflicts"] = conflicts
|
||||
from config.settings import settings as infrastructure
|
||||
if infrastructure.INFRA_STRATEGY_BOOK_SCHEMA and res.get('ok'):
|
||||
from app.services.rotation_service import on_user_command
|
||||
on_user_command(cmd_type, command_id, issued_by)
|
||||
|
|
@ -211,8 +217,9 @@ def _apply_param(command_id: str, cmd_type: str, spec: dict, norm: dict) -> dict
|
|||
applied = dict(norm)
|
||||
pms_repo.supersede_param_commands(cmd_type, ts_code, keep_command_id=command_id)
|
||||
_supersede_opposites(cmd_type, ts_code)
|
||||
pms_repo.update_command(command_id, status=cs.ST_EFFECTIVE, progress={"applied": applied},
|
||||
done_at=datetime.now())
|
||||
if not pms_repo.update_command(command_id, status=cs.ST_EFFECTIVE, progress={"applied": applied},
|
||||
done_at=datetime.now()):
|
||||
raise RuntimeError("参数命令未写入生效状态")
|
||||
return {"ok": True, "status": cs.ST_EFFECTIVE, "applied": applied, "plan": None}
|
||||
except Exception as e:
|
||||
logger.exception("参数命令生效失败")
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
"""跨日买入复核:停止旧单、等待真实回报、重走当日决策,再开放当日额度。"""
|
||||
from copy import deepcopy
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
import hashlib
|
||||
|
||||
from app.db.session import transaction
|
||||
|
|
@ -115,7 +115,8 @@ def _hold(ins, now, reason):
|
|||
previous = prog.get('daily_review') or {}
|
||||
if previous:
|
||||
prog['daily_review_history'] = (prog.get('daily_review_history', []) + [previous])[-20:]
|
||||
prog['daily_review'] = {'ymd': _day(now), 'status': 'pending', 'reason': reason}
|
||||
prog['daily_review'] = {'ymd': _day(now), 'status': 'pending', 'reason': reason,
|
||||
'entered_at': now.isoformat()}
|
||||
# 同一日人工维护后重核也不能复用旧执行价。
|
||||
for key in ('exec_advice', 'ref_lock', 'last_decision'):
|
||||
prog.pop(key, None)
|
||||
|
|
@ -193,6 +194,66 @@ def _queue(ins, result, now):
|
|||
return True
|
||||
|
||||
|
||||
def _finish_review(row, fresh, now):
|
||||
if not fresh['auto_exec']:
|
||||
return 'queued' if _queue(row, fresh, now) else None
|
||||
sd = fresh.get('self_decide') or {}
|
||||
if row['action'] == 'OPEN' and sd.get('decision') == 'accept':
|
||||
# 跨日续买和当日新建仓共用每日名额;只锁最后的核验、状态推进和审计。
|
||||
from app.services import proposal_service as ps
|
||||
with pms_repo.account_decision_guard():
|
||||
stopped = ps.locked_open_authorization({'self_decide': 'full'}, trial=sd.get('tier') == 'trial')
|
||||
if stopped:
|
||||
return 'waiting'
|
||||
maximum = param_store.locked_open_param('PMS_SELF_DECIDE_DAILY_MAX')
|
||||
why = ps._quota_confirm_reason(ps._auto_open_count(now), maximum)
|
||||
if why:
|
||||
manual = dict(fresh, candidate=dict(fresh['candidate'], confirm_why=why))
|
||||
return 'queued' if _queue(row, manual, now) else None
|
||||
return 'approved' if _approve(row, fresh, now) else None
|
||||
return 'approved' if _approve(row, fresh, now) else None
|
||||
|
||||
|
||||
def _queue_position(row):
|
||||
review = (row.get('progress') or {}).get('daily_review') or {}
|
||||
# 旧指令无排队时间时沿用创建时间。同一时刻未试过的先到,最后以数据库 id 稳定排序。
|
||||
last = review.get('last_attempt_at')
|
||||
stamp = last or review.get('entered_at') or str(row.get('created_at') or '').replace(' ', 'T')
|
||||
return stamp, int(review.get('attempts') or bool(last)), int(row.get('id') or 0)
|
||||
|
||||
|
||||
def _claim_review(row, now):
|
||||
"""在发起耗时研判之前原子移到队尾;服务重启或资料待补仍保留公平顺序。"""
|
||||
progress = deepcopy(row['progress'])
|
||||
review = progress['daily_review']
|
||||
# 与盘中调度的既有 230 秒租期一致。崩溃后可重新领取,迟到的旧结果不能覆盖新版本。
|
||||
if review.get('active_until', '') > now.isoformat():
|
||||
return None
|
||||
review['last_attempt_at'] = now.isoformat()
|
||||
review['active_until'] = (now + timedelta(seconds=230)).isoformat()
|
||||
review['attempts'] = int(review.get('attempts') or 0) + 1
|
||||
with transaction():
|
||||
if not pms_repo.compare_review_instruction(row, progress, 'WAIT_REVIEW'):
|
||||
return None
|
||||
return pms_repo.get_instruction(row['instruction_id'])
|
||||
|
||||
|
||||
def _release_review(claimed):
|
||||
"""只释放本轮领取的机会。批准、撤销和新一轮复核各自保留其结果。"""
|
||||
with transaction():
|
||||
current = pms_repo.get_instruction(claimed['instruction_id'])
|
||||
if not current or current['status'] != 'WAIT_REVIEW':
|
||||
return
|
||||
progress = deepcopy(current['progress'])
|
||||
state = progress.get('daily_review') or {}
|
||||
owner = claimed['progress']['daily_review']
|
||||
if any(state.get(key) != owner[key] for key in ('active_until', 'last_attempt_at', 'attempts')):
|
||||
return
|
||||
del state['active_until']
|
||||
if not pms_repo.compare_review_instruction(current, progress, 'WAIT_REVIEW'):
|
||||
raise RuntimeError('父指令同时发生变化,本次占用将到期释放')
|
||||
|
||||
|
||||
def process(*, now=None):
|
||||
from app.services import dispatcher, proposal_service, execution_orders
|
||||
now = now or datetime.now()
|
||||
|
|
@ -200,22 +261,25 @@ def process(*, now=None):
|
|||
if not param_store.get_bool('PMS_DAILY_BUY_REVIEW', False):
|
||||
return out
|
||||
# 先停完所有旧任务,再进行可能耗时的研判;未处理到的也不能从下发出口绕过。
|
||||
for row in pms_repo.list_instructions(statuses=list(LIVE), side='buy', limit=500):
|
||||
if not applies(row) or not blocker(row, now=now):
|
||||
for row in pms_repo.list_instructions(statuses=list(LIVE), side='buy', limit=None):
|
||||
if not applies(row):
|
||||
continue
|
||||
try:
|
||||
review = (row.get('progress') or {}).get('daily_review') or {}
|
||||
if row['status'] != 'WAIT_REVIEW' or review.get('ymd') != _day(now):
|
||||
held = _hold(row, now, blocker(row, now=now))
|
||||
if held:
|
||||
out['held'].append(row['instruction_id'])
|
||||
if row['status'] == 'WAIT_REVIEW' and review.get('ymd') == _day(now):
|
||||
continue
|
||||
reason = blocker(row, now=now)
|
||||
if reason and _hold(row, now, reason):
|
||||
out['held'].append(row['instruction_id'])
|
||||
except Exception as e:
|
||||
out['errors'].append(f"{row['instruction_id']}: {e}")
|
||||
reviewed = 0
|
||||
for row in pms_repo.list_instructions(statuses=['WAIT_REVIEW'], side='buy', limit=500):
|
||||
waiting = pms_repo.list_instructions(statuses=['WAIT_REVIEW'], side='buy', limit=None)
|
||||
for row in sorted(waiting, key=_queue_position):
|
||||
if not applies(row):
|
||||
continue
|
||||
iid = row['instruction_id']
|
||||
claimed = None
|
||||
try:
|
||||
if execution_orders.correction_blocker(row):
|
||||
out['waiting'].append(iid)
|
||||
|
|
@ -252,7 +316,13 @@ def process(*, now=None):
|
|||
out['waiting'].append(iid)
|
||||
continue
|
||||
if reviewed >= 1:
|
||||
break
|
||||
continue
|
||||
if state.get('ymd') != _day(now):
|
||||
continue
|
||||
claimed = _claim_review(row, now)
|
||||
if not claimed:
|
||||
continue
|
||||
row = claimed
|
||||
reviewed += 1
|
||||
result = proposal_service.scan_and_route(now=now, review_instruction=row)
|
||||
if result.get('errors') or not result.get('ok'):
|
||||
|
|
@ -271,9 +341,9 @@ def process(*, now=None):
|
|||
out['waiting'].append(iid)
|
||||
continue
|
||||
fresh = current
|
||||
changed = _approve(row, fresh, now) if fresh['auto_exec'] else _queue(row, fresh, now)
|
||||
if changed:
|
||||
out['approved' if fresh['auto_exec'] else 'queued'].append(iid)
|
||||
disposition = _finish_review(row, fresh, now)
|
||||
if disposition:
|
||||
out[disposition].append(iid)
|
||||
else:
|
||||
# 未通过不带昨日任务继续。后续条件转好仍由正常扫描产生新建议。
|
||||
reasons = [r.get('why') or ';'.join(r.get('failed') or [])
|
||||
|
|
@ -284,6 +354,12 @@ def process(*, now=None):
|
|||
out['ended'].append(iid)
|
||||
except Exception as e:
|
||||
out['errors'].append(f'{iid}: {e}')
|
||||
finally:
|
||||
if claimed:
|
||||
try:
|
||||
_release_review(claimed)
|
||||
except Exception as e:
|
||||
out['errors'].append(f'{iid}: 释放复核占用失败:{e}')
|
||||
return out
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -173,21 +173,35 @@ 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:
|
||||
with execution_orders.account_guard():
|
||||
from app.services import executor
|
||||
now = datetime.now()
|
||||
multi_day = executor.ordinary_days_left(parent, now)
|
||||
guard = pms_repo.account_decision_guard if multi_day else execution_orders.account_guard
|
||||
with guard():
|
||||
parent = pms_repo.get_instruction(pid)
|
||||
blocked = execution_orders.correction_blocker(parent, ts_code=ts_code, side=str(side).lower())
|
||||
if blocked:
|
||||
raise ValueError(blocked)
|
||||
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)
|
||||
reprice = execution_orders.enabled()
|
||||
new_order = (multi_day or reprice) and not qmt_repo.get_order(instruction_id)
|
||||
if new_order:
|
||||
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('父指令正在撤销')
|
||||
if multi_day and new_order:
|
||||
days = executor.ordinary_days_left(parent, now)
|
||||
if days:
|
||||
available = executor.dispatch_allowance(parent, now, days)
|
||||
if qty > available:
|
||||
reason = f'本轮原计划{qty}股,按最新成交和在途委托当日还可投{available}股,下一轮重新计算'
|
||||
return {'ok': False, 'deferred': True, 'ref': None, 'mode': m,
|
||||
'note': '', 'error': reason}
|
||||
if reprice and new_order:
|
||||
from app.core.execution_policy import supported_code
|
||||
if not supported_code(ts_code):
|
||||
raise ValueError('执行范围仅限沪深合资格证券')
|
||||
state = execution_orders.replacement_state(pid)
|
||||
if not state['ready'] or qty > state['remaining']:
|
||||
raise ValueError('旧子单未确认结束、成交未入账或数量超过剩余目标')
|
||||
|
|
|
|||
|
|
@ -109,6 +109,11 @@ def prepare_send(instruction_id):
|
|||
blocked = correction_blocker(parent, ts_code=order['ts_code'], side=order['side'])
|
||||
if blocked:
|
||||
return {'marked': False, 'blocked': blocked}
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA and not settings.PMS_TABLE_PREFIX:
|
||||
from app.services.rotation_service import order_control_blocker
|
||||
blocked = order_control_blocker(parent, order['side'])
|
||||
if blocked:
|
||||
return {'marked': False, 'blocked': blocked}
|
||||
return {'marked': qmt_repo.mark_send_attempt(instruction_id), 'blocked': ''}
|
||||
|
||||
|
||||
|
|
@ -152,14 +157,25 @@ def replacement_state(parent_id):
|
|||
'pending_trades': pending_trades, 'booked': booked, 'missing_orders': missing}
|
||||
|
||||
|
||||
def code_settlement_blocker(ts_code):
|
||||
def code_settlement_blocker(ts_code, *, manual_exit_day=None, manual_exit_requested_at=None):
|
||||
"""跨父指令与策略检查迟到成交,不因旧父指令本地终态而再次建仓。"""
|
||||
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:
|
||||
progress = parent.get('progress') or {}
|
||||
same_exit = (manual_exit_day and parent.get('book_id') == books.MICRO and parent.get('side') == 'sell'
|
||||
and progress.get('execution_profile') == 'rotation' and progress.get('deadline') == manual_exit_day)
|
||||
cancelled = progress.get('cancel_requested')
|
||||
if same_exit and cancelled and (not isinstance(cancelled, dict) or cancelled.get('arbiter') != 'system'):
|
||||
cancelled_at = _receipt_time(cancelled.get('at')) if isinstance(cancelled, dict) else None
|
||||
renewed_at = _receipt_time(manual_exit_requested_at)
|
||||
if not renewed_at or not cancelled_at or renewed_at <= cancelled_at:
|
||||
return '该股今日退出父单已被撤销,重新确认退出后才可继续'
|
||||
state = replacement_state(parent['instruction_id'])
|
||||
if same_exit and state.get('active'):
|
||||
return '该股旧退出委托仍在途,待撤成确认及成交入账'
|
||||
if state['pending_trades'] or state['booked'] < state['confirmed_filled'] or state['missing_orders']:
|
||||
return '该股已有成交或旧委托尚未核实入账'
|
||||
return ''
|
||||
|
|
|
|||
|
|
@ -180,7 +180,7 @@ def run_tick(*, now=None, dry_run: bool = False) -> dict:
|
|||
slices = param_store.get_int("PMS_EXEC_SLICES", 1)
|
||||
order_ttl = param_store.get_int("PMS_ORDER_TTL_MIN", 10) # 单个分片挂单有效期(交易分钟)
|
||||
brake_active = td.ymd() < param_store.get_int("PMS_BRAKE_UNTIL", 0)
|
||||
ymd_today = td.ymd()
|
||||
ymd_today = td.ymd(now)
|
||||
|
||||
for ins in instrs:
|
||||
out["checked"] += 1
|
||||
|
|
@ -237,16 +237,25 @@ def run_tick(*, now=None, dry_run: bool = False) -> dict:
|
|||
out['errors'].append(cancelled.get('message') or '清仓停止买入尚未完成')
|
||||
continue
|
||||
from app.services import daily_buy_review
|
||||
blocked = daily_buy_review.blocker(ins, now=now) or execution_conflicts.buy_blocker(code)
|
||||
review_block = daily_buy_review.blocker(ins, now=now)
|
||||
blocked = review_block or execution_conflicts.buy_blocker(code)
|
||||
if blocked:
|
||||
prog['last_decision'] = {'at': now.strftime('%H:%M'), 'action': 'WAIT', 'reason': blocked}
|
||||
if not dry_run:
|
||||
# 跨日复核自行保存等待与批准。不能以执行扫描较早读到的进度抹去排队和批准。
|
||||
if not dry_run and not review_block:
|
||||
prog['last_decision'] = {'at': now.strftime('%H:%M'), 'action': 'WAIT', 'reason': blocked}
|
||||
pms_repo.update_instruction(ins['instruction_id'], progress=prog)
|
||||
out['waited'].append({'instruction_id': ins['instruction_id'], 'code': code, 'reason': blocked})
|
||||
continue
|
||||
# 回放与下发是两个任务。出口已成交时,exec_qty 可能还没更新,
|
||||
# 不能把「已终态、不在途」误当成「父指令还有额度」。
|
||||
available = _parent_available(ins, children)
|
||||
if dispatcher.mode() == dispatcher.MODE_WS:
|
||||
remaining, available, filled_today, pending_today = _order_usage(ins, children, ymd_today)
|
||||
else:
|
||||
# 影子委托没有真实出口回报,只能把未核实的当日下发保留为在途。
|
||||
pending_today = _inflight_today(children, ymd_today)
|
||||
consumed = _consumed_today(children, ymd_today)
|
||||
filled_today = min(int(ins.get('exec_qty') or 0), max(0, consumed - pending_today))
|
||||
available = remaining
|
||||
if side == 'buy':
|
||||
available = min(available, daily_buy_review.allowance(ins, now=now))
|
||||
if available <= 0:
|
||||
|
|
@ -259,22 +268,15 @@ def run_tick(*, now=None, dry_run: bool = False) -> dict:
|
|||
continue
|
||||
day = market.day_snapshot(code)
|
||||
pos = _pos_of(view, code)
|
||||
quota = et.daily_quota(remaining, tdays_left, lot=lot_of(code),
|
||||
allow_odd_tail=(ins.get("action") == "EXIT"))
|
||||
# 当日配额的「今日已投放量」口径分两种:
|
||||
# 普通多日单 —— _consumed_today (今日已成交 + 今日在途), 给多日出手节流用。
|
||||
# 紧急单 / 窗口末日单 —— 只按「今日在途量」算 (_inflight_today)。这两类单的
|
||||
# 配额本来就等于全部剩余量, 而剩余量 = 委托量 − 累计成交, 已经扣过成交一次;
|
||||
# 若再从「已投放量」里把今日成交扣第二遍, 同一笔成交被扣两次, 当天成交越多,
|
||||
# 「可再投放 = 配额 − 已投放」越快变成负数 —— 于是发一两笔、成交回来之后就误判
|
||||
# 「当日配额已出完」, 当天不再补单, 连 14:45 强制兜底也被挡在这道判断前面,
|
||||
# 要等次日配额清零才继续。这正是 2026-08-19 紧急清仓剩三支拖到次日早盘的根因。
|
||||
# 只按在途量扣: 可再投放 = 剩余 − 在途, 永不重复扣, 紧急清仓当天会持续补单,
|
||||
# 父指令总量另由 _parent_available 限制。可卖量只能保护账户持仓,
|
||||
# 不能保护某次只卖一半的指令,更不能假定回放已及时更新 exec_qty。
|
||||
is_urgent = bool(prog.get("urgent"))
|
||||
fired_today = (_inflight_today(children, ymd_today) if (is_urgent or is_last)
|
||||
else _consumed_today(children, ymd_today))
|
||||
if is_urgent or is_last:
|
||||
# 紧急命令保留其指定窗口。末日配额等于全部剩余,仅再扣尚可成交的在途份额。
|
||||
quota = _day_quota(ins, remaining, tdays_left)
|
||||
fired_today = pending_today
|
||||
else:
|
||||
# 普通指令以日初未完成量分日。同一天的部分成交只消费一次当日配额。
|
||||
quota = _day_quota(ins, remaining + filled_today, tdays_left)
|
||||
fired_today = filled_today + pending_today
|
||||
lim_up, lim_down = _limit_flags(code, day)
|
||||
day_ctx = {**day, "support": pos.get("support_ref"),
|
||||
"limit_up": lim_up, "limit_down": lim_down,
|
||||
|
|
@ -325,6 +327,10 @@ def run_tick(*, now=None, dry_run: bool = False) -> dict:
|
|||
qty = slice_list[0] if slice_list else 0
|
||||
if side == "sell":
|
||||
qty = min(qty, int(pos.get("avail_qty") or 0))
|
||||
# 部分成交可能留下非整百额度。可申报的整百先出,卖出实际全部持仓时保留零股。
|
||||
sell_entire_position = side == 'sell' and qty == int(pos.get("total_qty") or 0)
|
||||
if qty >= et.LOT and not sell_entire_position:
|
||||
qty = qty // et.LOT * et.LOT
|
||||
budget_reason = None
|
||||
if side == 'buy' and qty > 0:
|
||||
from app.services.buy_budget import allowed_quantity
|
||||
|
|
@ -425,7 +431,11 @@ def run_tick(*, now=None, dry_run: bool = False) -> dict:
|
|||
valid_until=now.replace(hour=dl_min // 60, minute=dl_min % 60,
|
||||
second=0, microsecond=0))
|
||||
if not res.get("ok"):
|
||||
out["errors"].append(f"{ins['instruction_id']} 下发失败: {res.get('error')}")
|
||||
if res.get('deferred'):
|
||||
out['waited'].append({'instruction_id': ins['instruction_id'], 'code': code,
|
||||
'action': et.ACT_WAIT, 'reason': res['error']})
|
||||
else:
|
||||
out["errors"].append(f"{ins['instruction_id']} 下发失败: {res.get('error')}")
|
||||
continue
|
||||
|
||||
children.append({"ymd": ymd_today, "at": now.strftime("%H:%M:%S"), "qty": qty,
|
||||
|
|
@ -611,45 +621,73 @@ def _child_id(instruction_id: str, n: int) -> str:
|
|||
return f"{instruction_id}_D{n:02d}"
|
||||
|
||||
|
||||
def _parent_available(ins: dict, children: list) -> int:
|
||||
"""父指令总量减去已成交量与仍可成交量,不把通道回报当作账本入账。
|
||||
def _day_quota(parent, remaining, days_left):
|
||||
return et.daily_quota(remaining, days_left, lot=lot_of(parent['ts_code']),
|
||||
allow_odd_tail=parent.get('action') == 'EXIT')
|
||||
|
||||
已成交取父指令与全部出口子单累计成交的较大值,兼容任一方暂时领先。
|
||||
在途子单再预留尚未成交的数量;终态只释放未成交部分。必须跨日查询,
|
||||
也必须包含已入出口但没写回 progress 的子单。查询失败由调用方报错,禁止下发。
|
||||
"""
|
||||
from app.services import execution_orders
|
||||
if execution_orders.correction_blocker(ins):
|
||||
|
||||
def ordinary_days_left(parent, now):
|
||||
progress = (parent or {}).get('progress') or {}
|
||||
if progress.get('urgent') or progress.get('execution_profile') == 'rotation' or not progress.get('deadline'):
|
||||
return 0
|
||||
days = td.trade_days_left(progress['deadline'], now)
|
||||
return days if days > 1 else 0
|
||||
|
||||
|
||||
def dispatch_allowance(parent, now, days_left):
|
||||
remaining, available, filled, pending = _order_usage(
|
||||
parent, (parent.get('progress') or {}).get('children') or [], td.ymd(now))
|
||||
quota = _day_quota(parent, remaining + filled, days_left)
|
||||
return max(0, min(available, quota - filled - pending))
|
||||
|
||||
|
||||
def _order_usage(ins, children, today):
|
||||
"""同一批出口回报计算父单剩余、可投量、当日成交和当日在途,兼容进度漏记子单。"""
|
||||
from app.services import execution_orders
|
||||
target = int(ins['qty'])
|
||||
booked = int(ins.get('exec_qty') or 0)
|
||||
if dispatcher.mode() != dispatcher.MODE_WS:
|
||||
return max(0, target - booked)
|
||||
filled, pending = 0, 0
|
||||
child_days = {c['ref']: int(c.get('ymd') or 0) for c in children if c.get('ref')}
|
||||
filled = pending = filled_today = pending_today = 0
|
||||
seen = set()
|
||||
for order in qmt_repo.orders_for_parent(ins['instruction_id']):
|
||||
ref = order['instruction_id']
|
||||
if ref in seen:
|
||||
raise ValueError('同一父指令的出口子单重复,暂停下发')
|
||||
seen.add(ref)
|
||||
qty = int(order['qty'])
|
||||
done = execution_orders.effective_filled(order)
|
||||
status = str(order.get('status') or '').upper()
|
||||
live = 0 if status in qmt_repo.FINAL else int(order['qty']) - done
|
||||
filled += done
|
||||
if status not in qmt_repo.FINAL:
|
||||
pending += qty - done
|
||||
pending += live
|
||||
day = td.ymd(order['created_at']) if order.get('created_at') else child_days.get(ref)
|
||||
if day == today or not day:
|
||||
# 无法定位日期的旧单按可能占用今日份额计。其成交仍只扣一次,紧急退出仍可执行。
|
||||
filled_today += done
|
||||
pending_today += live
|
||||
for child in children:
|
||||
ref = child.get('ref')
|
||||
if ref in seen:
|
||||
continue
|
||||
# 查不到出口的既有下发,或切换模式前的人工委托,保守预留全部数量。
|
||||
qty = int(child['qty'])
|
||||
if qty <= 0:
|
||||
raise ValueError('既有子单数量异常,暂停下发')
|
||||
pending += qty
|
||||
if not child.get('ymd') or int(child['ymd']) == today:
|
||||
pending_today += qty
|
||||
if ref:
|
||||
seen.add(ref)
|
||||
return max(0, target - max(booked, filled) - pending)
|
||||
remaining = max(0, target - max(booked, filled))
|
||||
return remaining, max(0, remaining - pending), filled_today, pending_today
|
||||
|
||||
|
||||
def _parent_available(ins: dict, children: list) -> int:
|
||||
"""父单总量兼容尚未入账的成交、在途与漏记子单,成交待修正时关闭。"""
|
||||
from app.services import execution_orders
|
||||
if execution_orders.correction_blocker(ins):
|
||||
return 0
|
||||
if dispatcher.mode() != dispatcher.MODE_WS:
|
||||
return max(0, int(ins['qty']) - int(ins.get('exec_qty') or 0))
|
||||
return _order_usage(ins, children, None)[1]
|
||||
|
||||
|
||||
def _consumed_today(children: list, ymd_today: int) -> int:
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import time
|
|||
|
||||
from config.settings import settings
|
||||
from app.repo import pms_repo
|
||||
from app.db import session as db
|
||||
|
||||
logger = logging.getLogger("pms.params")
|
||||
|
||||
|
|
@ -121,13 +122,11 @@ RUNTIME_EXTRA = {
|
|||
# (它只回 ok=False 不抛异常, 调用点又把返回值丢了)。后果是 prev_ymd 永远读回 0,
|
||||
# 「连续 N 日」的按日推进彻底失效, 修复形同虚设。加表时**必须同步加这里**。
|
||||
"PMS_RECON_STREAK_YMD": (0, int, "上次推进连续天数的交易日 YYYYMMDD (同日不重复计数)"),
|
||||
# 2026-08-14: 上游信号告警面板的三个显示参数。原来只在 upstream_signals.py 里用 get_int
|
||||
# 兜底、没登记, 所以设置页调不到、改一次得重新部署。注册进来后可在页面实时调 (即时生效)。
|
||||
"PMS_UPSTREAM_ALERT_SCAN": (300, int, "告警扫描窗: 每轮从告警流取最近 N 条再分类限量 (窗要大于各类上限之和, 防 price_notice 等高频源把风控告警挤出最新N)"),
|
||||
"PMS_UPSTREAM_ALERT_PER_CAT": (20, int, "告警每类上限: 同一类型最多显示最新 N 条 (砍单类刷屏, 不挤别的类)"),
|
||||
"PMS_UPSTREAM_ALERT_MAX_AGE_MIN": (60, int, "告警时间窗(分钟): 超过此龄判为陈旧并在页面置灰标注(不删除, 风控不丢信息), 0=不限龄。与每类上限正交, 先按龄标记再每类限量"),
|
||||
# 2026-09-09: 点股票看它今天全部信号 (台账 055)。快照口径只留每类最新 N 条, 到下午
|
||||
# 一只活跃票早盘的信号早被挤掉; 按代码回扫是另一条路径, 上限单独可调。
|
||||
# 旧版扫描窗与每类条数保留作配置兼容,当前全场快照使用 PMS_UPSTREAM_DAY_SCAN。
|
||||
"PMS_UPSTREAM_ALERT_SCAN": (300, int, "历史兼容参数。当前告警按当天来源分页,扫描上限使用 PMS_UPSTREAM_DAY_SCAN"),
|
||||
"PMS_UPSTREAM_ALERT_PER_CAT": (20, int, "历史兼容参数。当前告警按当天来源分页"),
|
||||
"PMS_UPSTREAM_ALERT_MAX_AGE_MIN": (60, int, "告警超过此分钟数标为陈旧,仍可查看。0 表示不限龄"),
|
||||
# 点击单票查看当天信号时独立回扫,避免把单票浏览变成全场轮询。
|
||||
"PMS_UPSTREAM_BYCODE_SCAN": (5000, int, "单票信号回扫上限: 点开一只票时每条上游流最多倒序回扫 N 条 (分页 500/次)。只在点击时触发, 不进轮询; 调大更全但更慢"),
|
||||
# ── 2026-09-09 解锁重问 (台账 055): 研判驳回后的当日闸不再是死的, 输入确实变了就放一次。
|
||||
# 八个键一次定死, 不按复盘读数回调 —— 要调先记台账。
|
||||
|
|
@ -467,23 +466,55 @@ def _type_of(key: str):
|
|||
return str
|
||||
|
||||
|
||||
def _current_cache(*, force=False):
|
||||
state = db.transaction_state()
|
||||
if state is None:
|
||||
return _cache
|
||||
local = state.local.get("params")
|
||||
if local is None and not force and _cache["loaded"] and time.time() - _cache["at"] < CACHE_TTL:
|
||||
return _cache
|
||||
# 保存点内的读数不能沿用到其回滚或释放之后;写入过的外层事务始终看自己的表值。
|
||||
scope = db._TRANSACTION.get().get_nested_transaction()
|
||||
if local is None or local.get("scope") is not scope:
|
||||
local = state.local["params"] = {"scope": scope, "loaded": False, "at": 0, "data": {}, "error": None}
|
||||
return local
|
||||
|
||||
|
||||
def _refresh_cache(cache, now):
|
||||
try:
|
||||
rows = pms_repo.all_params()
|
||||
cache.update({"data": rows, "at": now, "error": None, "loaded": True})
|
||||
except Exception as exc:
|
||||
msg = f"{type(exc).__name__}: {exc}"
|
||||
changed = cache.get("error") != msg
|
||||
cache.update({"at": now, "error": msg, "loaded": True})
|
||||
if changed:
|
||||
logger.warning("参数表读取失败, 退回 settings 初值: %s", exc)
|
||||
|
||||
|
||||
def refresh(force: bool = False) -> dict:
|
||||
"""拉一次表值 (带 TTL 缓存)。连库失败不抛异常 —— 退回文件初值并记 error。"""
|
||||
"""事务内刚写的参数仅在本事务可见。进程共享缓存只从已提交的数据刷新。"""
|
||||
cache = _current_cache(force=force)
|
||||
now = time.time()
|
||||
if not force and _cache["loaded"] and now - _cache["at"] < CACHE_TTL:
|
||||
return _cache["data"]
|
||||
if cache["loaded"] and not force and now - cache["at"] < CACHE_TTL:
|
||||
return cache["data"]
|
||||
if db.transaction_state() is not None:
|
||||
# 共享快照可能正好到期或被另一线程失效,此处也必须改用私有读数。
|
||||
cache = _current_cache(force=True)
|
||||
_refresh_cache(cache, now)
|
||||
else:
|
||||
with _lock:
|
||||
_refresh_cache(cache, now)
|
||||
return cache["data"]
|
||||
|
||||
|
||||
def _invalidate_shared():
|
||||
with _lock:
|
||||
try:
|
||||
rows = pms_repo.all_params()
|
||||
_cache.update({"data": rows, "at": now, "error": None, "loaded": True})
|
||||
except Exception as e:
|
||||
msg = f"{type(e).__name__}: {e}"
|
||||
changed = _cache.get("error") != msg
|
||||
# 失败也要更新时间戳并置 loaded, 否则下一次 get() 立刻又去连库
|
||||
_cache.update({"at": now, "error": msg, "loaded": True})
|
||||
if changed: # 同一个错误只吼一次, 恢复或换错才再吼
|
||||
logger.warning("参数表读取失败, 退回 settings 初值: %s", e)
|
||||
return _cache["data"]
|
||||
_cache["loaded"] = False
|
||||
|
||||
|
||||
def _is_simulation_halt(key):
|
||||
return key == 'PMS_GLOBAL_EXEC_HALT' and settings.INFRA_STRATEGY_BOOK_SCHEMA and not settings.PMS_TABLE_PREFIX
|
||||
|
||||
|
||||
def get(key: str, default=None):
|
||||
|
|
@ -498,12 +529,13 @@ def get(key: str, default=None):
|
|||
t = _type_of(key)
|
||||
# 表读不到时, 安全开关按"拦"而不是按默认值放行 —— 见 FAIL_CLOSED 的注释。
|
||||
# 只在**表确实读失败**时生效; 表读到了而这个键没设过, 那是真的没设, 照常走默认值。
|
||||
if _cache.get("error") and key in FAIL_CLOSED and key not in rows:
|
||||
error = _current_cache().get("error")
|
||||
if error and key in FAIL_CLOSED and key not in rows:
|
||||
logger.error("[参数] 读不到 %s (参数表: %s) —— 按 fail-closed 取 %r, "
|
||||
"宁可多拦一轮也不放行", key, _cache["error"], FAIL_CLOSED[key])
|
||||
"宁可多拦一轮也不放行", key, error, FAIL_CLOSED[key])
|
||||
return FAIL_CLOSED[key]
|
||||
if key in rows:
|
||||
if key in LIMIT_FLAGS and not _valid_limit_flag(rows[key].get('param_value')):
|
||||
if (key in LIMIT_FLAGS or _is_simulation_halt(key)) and not _valid_limit_flag(rows[key].get('param_value')):
|
||||
return FAIL_CLOSED[key]
|
||||
try:
|
||||
return _coerce(rows[key]["param_value"], t)
|
||||
|
|
@ -516,6 +548,51 @@ def get(key: str, default=None):
|
|||
return default
|
||||
|
||||
|
||||
def execution_halted(*, locked=False):
|
||||
"""止损观察与执行分流必须看当前暂停命令;持账户锁时同时锁定参数行。"""
|
||||
key = 'PMS_GLOBAL_EXEC_HALT'
|
||||
try:
|
||||
raw = pms_repo.get_param(key, for_update=True) if locked else pms_repo.get_param(key)
|
||||
except Exception as exc:
|
||||
logger.warning('当前暂停命令读取失败,停止执行: %s', type(exc).__name__)
|
||||
return True
|
||||
if raw is None:
|
||||
return RUNTIME_EXTRA[key][0]
|
||||
if not _valid_limit_flag(raw):
|
||||
return True
|
||||
return _coerce(raw, bool)
|
||||
|
||||
|
||||
def current_rotation_param(key, *, locked=False):
|
||||
"""轮换开关决定是否允许下单,直接读取当前事务的运行参数。"""
|
||||
if key not in ('PMS_ROTATION_MODE', 'PMS_ROTATION_CONTROL'):
|
||||
raise ValueError('此参数不属于轮换执行控制')
|
||||
raw = pms_repo.get_param(key, for_update=locked)
|
||||
value = RUNTIME_EXTRA[key][0] if raw is None else raw
|
||||
allowed = ('observe', 'paper', 'auto') if key == 'PMS_ROTATION_MODE' else ('run', 'pause_new', 'pause_all', 'exit_disable')
|
||||
if value not in allowed:
|
||||
raise ValueError('轮换执行控制取值无效')
|
||||
return value
|
||||
|
||||
|
||||
def locked_open_param(key):
|
||||
"""建仓最终批准读取本事务内最新的授权,读取失败由调用方保持暂停。"""
|
||||
if key not in ('PMS_GLOBAL_BUY_HALT', 'PMS_SELF_DECIDE_DAILY_MAX', 'PMS_SELF_DECIDE',
|
||||
'PMS_SELF_DECIDE_TRIAL', 'PMS_AUTONOMY', 'PMS_OPEN_AUTONOMY',
|
||||
'PMS_OPEN_AUTO_EXEC_ON_VERDICT'):
|
||||
raise ValueError('此参数不属于建仓授权')
|
||||
raw = pms_repo.get_param(key, for_update=True)
|
||||
if raw is None:
|
||||
return getattr(settings, key) if hasattr(settings, key) else RUNTIME_EXTRA[key][0]
|
||||
typ = _type_of(key)
|
||||
if typ is bool and not _valid_limit_flag(raw):
|
||||
raise ValueError('当前建仓授权取值无效')
|
||||
value = int(raw) if typ is int else _coerce(raw, typ)
|
||||
if _range_check(key, value):
|
||||
raise ValueError('当前建仓授权取值超出有效范围')
|
||||
return value
|
||||
|
||||
|
||||
def get_float(key, default=0.0):
|
||||
try:
|
||||
return float(get(key, default))
|
||||
|
|
@ -553,6 +630,9 @@ def get_tuple_floats(key, default=(0.5, 0.25, 0.25)):
|
|||
def set_param(key: str, value, updated_by: str = "user") -> dict:
|
||||
"""页面改参入口。校验键名与类型后落表, 并立即失效缓存。"""
|
||||
editable = _editable_keys()
|
||||
simulation_halt = _is_simulation_halt(key)
|
||||
if simulation_halt and not _valid_limit_flag(value):
|
||||
return {'ok': False, 'error': '全局暂停执行必须明确选择开启或关闭'}
|
||||
if key in LIMIT_FLAGS and not _valid_limit_flag(value):
|
||||
return {'ok': False, 'error': '限制价开关必须为明确的布尔值'}
|
||||
if key not in editable and key not in RUNTIME_EXTRA and not key.startswith("PMS_SECTOR_CAP_"):
|
||||
|
|
@ -567,11 +647,23 @@ def set_param(key: str, value, updated_by: str = "user") -> dict:
|
|||
if bad:
|
||||
return {"ok": False, "error": bad}
|
||||
try:
|
||||
pms_repo.set_param(key, v, updated_by)
|
||||
# 包含参数行尚未创建的首个暂停命令,与模拟账户退出事务按同一账户锁排序。
|
||||
if simulation_halt:
|
||||
from app.repo import rotation_repo
|
||||
with rotation_repo.account_guard():
|
||||
pms_repo.set_param(key, v, updated_by)
|
||||
else:
|
||||
pms_repo.set_param(key, v, updated_by)
|
||||
except Exception as e:
|
||||
return {"ok": False, "error": f"写入失败: {type(e).__name__}: {e}"}
|
||||
_cache["loaded"] = False
|
||||
refresh(force=True)
|
||||
state = db.transaction_state()
|
||||
if state is None:
|
||||
_invalidate_shared()
|
||||
refresh(force=True)
|
||||
else:
|
||||
# 即使随后回滚保存点,也应从所在事务重新读表。失效动作在最外层提交后才可见。
|
||||
_current_cache(force=True)["loaded"] = False
|
||||
state.after_commit.add(_invalidate_shared)
|
||||
return {"ok": True, "key": key, "value": v}
|
||||
|
||||
|
||||
|
|
@ -707,7 +799,7 @@ def _range_check(key, v):
|
|||
def snapshot() -> dict:
|
||||
"""页面「参数设置」用: 每个可调参数的 当前值/来源/初值/说明。"""
|
||||
rows = refresh()
|
||||
out = {"params": [], "source_error": _cache.get("error")}
|
||||
out = {"params": [], "source_error": _current_cache().get("error")}
|
||||
for key, field in sorted(_editable_keys().items()):
|
||||
cur = get(key)
|
||||
out["params"].append({
|
||||
|
|
|
|||
|
|
@ -1210,7 +1210,13 @@ def candidates(*, held=(), black=(), route_by_verdict: bool = False, force=False
|
|||
显式传 True。要让命令驱动也按判决过滤, 改这里的调用方, 不改默认值。
|
||||
"""
|
||||
p = _params()
|
||||
plan = plan_override if plan_override is not None else (get_plan(force=force) if force else get_plan())
|
||||
if plan_override is not None:
|
||||
plan = plan_override
|
||||
elif recheck_code:
|
||||
# 已入选的股票只复核当前资格。普通榜单的返回上限仅用于发现新仓。
|
||||
plan = read_current_plan(code=recheck_code)
|
||||
else:
|
||||
plan = get_plan(force=True) if force else get_plan()
|
||||
route = bool(route_by_verdict)
|
||||
if not allow_user:
|
||||
plan = dict(plan, **{b: [r for r in plan.get(b, []) if not r.get('user_required')] for b in ('main', 'observe')})
|
||||
|
|
@ -1237,6 +1243,9 @@ def candidates(*, held=(), black=(), route_by_verdict: bool = False, force=False
|
|||
'recheck_code': recheck_code}
|
||||
if recheck_code:
|
||||
out['items'] = [r for r in out['items'] if r['ts_code'] == recheck_code]
|
||||
for row in out['items']:
|
||||
# 逐票回执带完整研究名次,本次没有全池候选排序。
|
||||
row.pop('selection_rank', None)
|
||||
consumption = {'kind': 'rechecked' if recheck_code else 'consumed', 'decision': plan.get('decision'),
|
||||
'params': effective, 'context': context, 'rank_mode': out['rank_mode'],
|
||||
'filtered': out['filtered'], 'order': [r['ts_code'] for r in out['items']]}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
from datetime import datetime, timedelta
|
||||
import logging
|
||||
import re
|
||||
from app.core.plan_revision import compare, fingerprint, portfolio_context, confirmation_revision
|
||||
from app.core.plan_revision import compare, fingerprint, portfolio_context, confirmation_revision, needs_new_judgement
|
||||
from app.services.plan_feed import PlanFeedError
|
||||
|
||||
logger = logging.getLogger('pms.proposal.prepare')
|
||||
|
|
@ -16,12 +16,9 @@ def current_basis(hard, code, proposal_id=None, *, record=True):
|
|||
feed = plan_feed
|
||||
if hard.get('candidate_source') == 'dividend':
|
||||
from app.services import dividend_feed as feed
|
||||
options = {'record': record}
|
||||
else:
|
||||
options = {'force': True} if record else {'record': False, 'plan_override': plan_feed.read_current_plan(code=code)}
|
||||
selected = feed.candidates(held=[x['ts_code'] for x in view['held']],
|
||||
black=[c for c, v in stock.items() if v.get('black')],
|
||||
route_by_verdict=bool(params.get('open_route_by_verdict')), recheck_code=code, **options)
|
||||
route_by_verdict=bool(params.get('open_route_by_verdict')), recheck_code=code, record=record)
|
||||
current = next((r for r in selected['items'] if r['ts_code'] == code), None)
|
||||
if not (selected.get('decision') or {}).get('id'):
|
||||
return {'ok': False, 'error': '当前研究资料暂不可用,恢复后自动更新建议', 'stage': 'source'}
|
||||
|
|
@ -281,10 +278,19 @@ def refresh_one(p, *, now=None, deadline=None):
|
|||
if not fresh:
|
||||
return _failed_recheck(p, out)
|
||||
c = fresh[0]
|
||||
if hard.get('candidate_source') != 'dividend':
|
||||
# 单票重评没有与其他候选竞争,原扫描的先后顺序仍在原消费记录中。
|
||||
for key in ('selection_rank', 'execution_rank', 'priority_reason'):
|
||||
c['hard_numbers'].pop(key, None)
|
||||
from app.services.buy_budget import from_hard
|
||||
previous_budget = from_hard(hard, p['ts_code'])
|
||||
if previous_budget is not None:
|
||||
c.setdefault('hard_numbers', {})['buy_budget'] = previous_budget
|
||||
# 重新评估取到的发布版可能刚在第一次核对后变化。旧批准只能复用于相同的研究材料。
|
||||
if not needs_model and needs_new_judgement(c['hard_numbers'], hard):
|
||||
needs_model = True
|
||||
if not ps._judge_budget_left(deadline):
|
||||
return _pending_response(p, '等待下一轮完成研究更新', 'budget')
|
||||
c['_plan_recheck'] = needs_model
|
||||
verdict = None if needs_model else _judgement(p)
|
||||
result = ps._route_one(c, check['view'], params, check['stock'],
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@
|
|||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import nullcontext
|
||||
import logging
|
||||
import time
|
||||
from datetime import datetime, timedelta
|
||||
|
|
@ -103,15 +104,9 @@ def scan_and_route(*, now=None, dry_run: bool = False, review_instruction=None,
|
|||
stock_params = command_service.effective_stock_params()
|
||||
mkt = _market_ctx(view["held"], now)
|
||||
params["_mkt"] = mkt # 规则闸要用同一份 MA5, 不再重取
|
||||
# 系统自决每日新建仓上限的基数: 今天已自动采纳几只 (评审账本按前缀数)。只读一次,
|
||||
# 本轮内 _route_one 每采纳一只再往 out["_sd_accepts"] 加, 两者相加即当日累计。off 时不查库。
|
||||
# 此读数供试算和初筛。实际批准前会在账户锁内重读并占用名额。
|
||||
if ae.self_decide_on(params):
|
||||
try:
|
||||
_t0 = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
params["_sd_base_count"] = pms_repo.system_pass_count_today(_t0)
|
||||
except Exception as e: # noqa: BLE001
|
||||
logger.warning("[系统自决] 读当日已采纳数失败 (本轮按 0 起算): %s", e)
|
||||
params["_sd_base_count"] = 0
|
||||
params["_sd_base_count"] = _auto_open_count(now)
|
||||
# 跳过三类: ①已有在途提议或指令的 ②今天已被规则闸拒过的 ③今天已被研判闸驳回的新建仓。
|
||||
# ② 是 2026-07-29 的教训 —— 组合已超总仓上限时, 16 只深亏票的补仓候选每分钟被拒
|
||||
# 一次, 一天往评审账本灌几千行一模一样的记录。闸门结论当天基本不会变, 记一次就够。
|
||||
|
|
@ -1069,7 +1064,10 @@ def _route_one(c, view, params, stock_params, brake_active, now, dry_run, out,
|
|||
# 重问放行也归自决 (试探仓): 先给它打上 decide_kind, 再交 _self_decide 判。
|
||||
if c.get("reask") and ae.self_decide_on(params) and not c.get("decide_kind"):
|
||||
c["decide_kind"] = ae.DK_REASK
|
||||
sd = _self_decide(c, verdict, params, params.get("_sd_base_count", 0) + out.get("_sd_accepts", 0))
|
||||
count = params.get("_sd_base_count", 0)
|
||||
if count is not None:
|
||||
count += out.get("_sd_accepts", 0)
|
||||
sd = _self_decide(c, verdict, params, count)
|
||||
if sd.get("would") and sd["would"] != "none":
|
||||
_con = _sd_consensus_brief(c)
|
||||
out.setdefault("self_decide_seen", []).append(
|
||||
|
|
@ -1081,8 +1079,6 @@ def _route_one(c, view, params, stock_params, brake_active, now, dry_run, out,
|
|||
sd_decision = sd.get("decision", "none")
|
||||
if sd_decision == "accept":
|
||||
auto_exec, force_queue = True, False
|
||||
if is_open:
|
||||
out["_sd_accepts"] = out.get("_sd_accepts", 0) + 1
|
||||
elif sd_decision == "decline":
|
||||
# 自动放弃: 落账 (评审方 system) + 追加 rejected by system + 当日不重评 + 返回
|
||||
if dry_run:
|
||||
|
|
@ -1125,6 +1121,8 @@ def _route_one(c, view, params, stock_params, brake_active, now, dry_run, out,
|
|||
'self_decide': sd}
|
||||
|
||||
if dry_run:
|
||||
if is_open and sd_decision == "accept":
|
||||
out["_sd_accepts"] = out.get("_sd_accepts", 0) + 1
|
||||
(out["executed"] if auto_exec else out["queued"]).append(
|
||||
{**_brief(c), "route": "auto" if auto_exec else "queue",
|
||||
"judge": verdict["verdict"], "dry_run": True,
|
||||
|
|
@ -1162,52 +1160,55 @@ def _route_one(c, view, params, stock_params, brake_active, now, dry_run, out,
|
|||
logger.warning("[自决清仓] 停买入侧有未成项 %s: %s", code, r["errors"])
|
||||
except Exception as e: # noqa: BLE001
|
||||
logger.warning("[自决清仓] 停买入侧失败 %s (清仓照落): %s", code, e)
|
||||
elif sd_accept and is_open:
|
||||
# 试探仓类采纳: 把数量重算成建议档 (弱基本面那类还带上紧止盈标记 advice.tight_trail)。
|
||||
_apply_advice_tier(c, c.setdefault("hard_numbers", {}), verdict,
|
||||
force_trial=(sd.get("tier") == "trial"))
|
||||
original_reason = c["reason"]
|
||||
if sd_accept:
|
||||
# 指令与账本理由一致。到价清仓把原句 (现价/目标价/股数) 接在后面, 复盘看得见到的是什么价 (09-14 评审补)。
|
||||
_why = sd.get("why") or c["reason"]
|
||||
if sd.get("rule") == ae.DK_TARGET_PRICE and c.get("reason"):
|
||||
_why = f"{_why};{c['reason']}"
|
||||
c["reason"] = _why
|
||||
iid = _make_instruction(c, price, now)
|
||||
if sd_accept:
|
||||
reason = c["reason"]
|
||||
_dc = params.get("_sd_base_count", 0) + out.get("_sd_accepts", 0)
|
||||
reason = _why
|
||||
hard = {**(c.get("hard_numbers") or {}),
|
||||
"self_decide": _self_decide_hard(c, verdict, sd, params, strat_cancelled, _dc,
|
||||
"self_decide": _self_decide_hard(c, verdict, sd, params, strat_cancelled, count,
|
||||
buys_cancelled=buys_cancelled)}
|
||||
else:
|
||||
reason = verdict.get("reason") or c["reason"]
|
||||
if auto_why:
|
||||
reason = f"判决候选自动执行: {reason}"
|
||||
hard = c["hard_numbers"]
|
||||
pms_repo.insert_ledger(ts_code=code, action=action,
|
||||
arbiter="judge" if c.get("judge_required") else "rule",
|
||||
verdict="PASS", price_at=price, hard_numbers=hard,
|
||||
ref_id=iid, reason=reason[:500])
|
||||
out["executed"].append({**_brief(c), "instruction_id": iid,
|
||||
"why": (sd.get("why") if sd_accept else
|
||||
("减持自动执行 (规则触发, 来源没有要求交人裁决)"
|
||||
if side == "sell"
|
||||
else (auto_why or ("新建仓档位 full" if is_open
|
||||
else "档位 full"))))})
|
||||
else:
|
||||
pid = _make_proposal(c, price, verdict)
|
||||
# 交人的原因按从具体到笼统取: 重问放行 → 上游标的硬风险 → 候选自带的 (关注判决等,
|
||||
# 见 action_engine.verdict_confirm_why) → 来源强制的 (研究证据走弱那类减持) →
|
||||
# 深档补仓那条老规矩 → 研判不可用 → 档位。
|
||||
# 重问放行仍排在最前 (2026-09-09 拍板): 它是这张提议最需要人知道的那件事
|
||||
# (曾被驳回、什么变了、可能买不上)。硬风险 (2026-09-10 加) 紧随其后 —— 两者
|
||||
# 实际上几乎不会同时出现: 上游标了硬风险的票判决多半不是「候选」, 走不到重问那一步,
|
||||
# 所以这个次序之争是空的, 不必为它推翻上一次拍板。
|
||||
why = (reask_why or risk_why or c.get("confirm_why") or src_why
|
||||
or ("深档补仓强制确认" if c.get("needs_user_confirm") else None)
|
||||
or ("研判不可用, 降级人工确认" if verdict.get("degraded") else None)
|
||||
or ("新建仓档位 propose_only" if is_open else "档位 propose_only"))
|
||||
out["queued"].append({**_brief(c), "proposal_id": pid, "why": why})
|
||||
result = _save_automatic_action(c, price, now, hard, reason, sd_accept, params, out, auto_why)
|
||||
if result.get('skip'):
|
||||
out['skipped'].append({**_brief(c), 'why': result['skip']})
|
||||
return
|
||||
if result.get('confirm'):
|
||||
c['confirm_why'] = result['confirm']
|
||||
# 恢复交易员看到的决定与原因;保存后的实际处置优先于初筛。
|
||||
c['reason'] = original_reason
|
||||
out['self_decide_seen'][-1].update(
|
||||
would='confirm', decision='confirm', tier=None, why=result['confirm'])
|
||||
else:
|
||||
if is_open and sd_accept:
|
||||
out["_sd_accepts"] = out.get("_sd_accepts", 0) + 1
|
||||
out["executed"].append({**_brief(c), "instruction_id": result['instruction_id'],
|
||||
"why": (sd.get("why") if sd_accept else
|
||||
("减持自动执行 (规则触发, 来源没有要求交人裁决)"
|
||||
if side == "sell"
|
||||
else (auto_why or ("新建仓档位 full" if is_open
|
||||
else "档位 full"))))})
|
||||
return
|
||||
pid = _make_proposal(c, price, verdict)
|
||||
# 交人的原因按从具体到笼统取: 重问放行 → 上游标的硬风险 → 候选自带的 (关注判决等,
|
||||
# 见 action_engine.verdict_confirm_why) → 来源强制的 (研究证据走弱那类减持) →
|
||||
# 深档补仓那条老规矩 → 研判不可用 → 档位。
|
||||
# 重问放行仍排在最前 (2026-09-09 拍板): 它是这张提议最需要人知道的那件事
|
||||
# (曾被驳回、什么变了、可能买不上)。硬风险 (2026-09-10 加) 紧随其后 —— 两者
|
||||
# 实际上几乎不会同时出现: 上游标了硬风险的票判决多半不是「候选」, 走不到重问那一步,
|
||||
# 所以这个次序之争是空的, 不必为它推翻上一次拍板。
|
||||
why = (reask_why or risk_why or c.get("confirm_why") or src_why
|
||||
or ("深档补仓强制确认" if c.get("needs_user_confirm") else None)
|
||||
or ("研判不可用, 降级人工确认" if verdict.get("degraded") else None)
|
||||
or ("新建仓档位 propose_only" if is_open else "档位 propose_only"))
|
||||
out["queued"].append({**_brief(c), "proposal_id": pid, "why": why})
|
||||
|
||||
|
||||
def _verdict_auto_exec_why(c, verdict):
|
||||
|
|
@ -1263,7 +1264,73 @@ def verify_open_basis(hard, code):
|
|||
return {'ok': False, 'error': f'当前计划版本无法核验:{type(e).__name__}'}
|
||||
|
||||
|
||||
def _make_instruction(c, price, now) -> str:
|
||||
def _auto_open_count(now):
|
||||
try:
|
||||
return pms_repo.system_pass_count_today(now.replace(hour=0, minute=0, second=0, microsecond=0))
|
||||
except Exception:
|
||||
logger.exception("无法核实今日自动建仓只数")
|
||||
return None
|
||||
|
||||
|
||||
def _quota_confirm_reason(count, maximum):
|
||||
if maximum and count is None:
|
||||
return "今日自动建仓只数暂时无法核实,交人裁决"
|
||||
if maximum and count >= maximum:
|
||||
return f"今日自动新建仓已达上限 {maximum} 只,本只转交人裁决"
|
||||
return None
|
||||
|
||||
|
||||
def locked_open_authorization(params, out=None, *, trial=False, verdict_override=False):
|
||||
"""模型等待期间用户可能改变授权;在短提交事务中验证当前开关。"""
|
||||
if param_store.execution_halted(locked=True) or param_store.locked_open_param('PMS_GLOBAL_BUY_HALT'):
|
||||
return '当前账户已暂停执行或买入,自动建仓暂缓'
|
||||
mode = param_store.locked_open_param('PMS_AUTONOMY')
|
||||
open_mode = param_store.locked_open_param('PMS_OPEN_AUTONOMY')
|
||||
if mode == AUTONOMY_OFF or open_mode == AUTONOMY_OFF:
|
||||
return '当前已关闭自主建仓,暂缓执行'
|
||||
if out and open_mode != (out.get('open_autonomy') or AUTONOMY_FULL):
|
||||
return '自主建仓档位已改变,下一轮按最新授权评估'
|
||||
if (params.get('self_decide') or 'off') != param_store.locked_open_param('PMS_SELF_DECIDE'):
|
||||
return '系统自决档位已改变,下一轮按最新授权评估'
|
||||
if trial and not param_store.locked_open_param('PMS_SELF_DECIDE_TRIAL'):
|
||||
return '试探仓自动采纳已关闭,下一轮重新评估'
|
||||
if verdict_override and not param_store.locked_open_param('PMS_OPEN_AUTO_EXEC_ON_VERDICT'):
|
||||
return '候选自动采纳已关闭,下一轮重新评估'
|
||||
return None
|
||||
|
||||
|
||||
def _save_automatic_action(c, price, now, hard, reason, sd_accept, params, out, auto_why):
|
||||
from app.services import buy_budget
|
||||
is_open = c['action'] == ae.A_OPEN
|
||||
with pms_repo.account_decision_guard() if is_open else nullcontext():
|
||||
if is_open:
|
||||
stopped = locked_open_authorization(params, out,
|
||||
trial=(hard.get('self_decide') or {}).get('tier') == '试探仓', verdict_override=bool(auto_why))
|
||||
if stopped:
|
||||
return {'skip': stopped}
|
||||
# 同一轮重试沿用同一指令号;另一次并发扫描也不能给在途买单重复建仓。
|
||||
iid = cs.make_instruction_id(td.ymd(now), c['ts_code'], c['action'], int(now.strftime('%H%M%S')))
|
||||
if pms_repo.get_instruction(iid) or pms_repo.list_instructions(
|
||||
ts_code=c['ts_code'], side='buy', statuses=buy_budget.LIVE, limit=1):
|
||||
return {'skip': '这只股票已有买入安排,本轮不重复建仓'}
|
||||
if sd_accept:
|
||||
maximum = param_store.locked_open_param('PMS_SELF_DECIDE_DAILY_MAX')
|
||||
current = _auto_open_count(now)
|
||||
hard['self_decide']['daily_max'] = maximum
|
||||
why = _quota_confirm_reason(current, maximum)
|
||||
if why:
|
||||
return {'confirm': why}
|
||||
hard['self_decide']['daily_count'] = current + 1 if current is not None else None
|
||||
iid = _make_instruction(c, price, now, account_locked=is_open)
|
||||
saved = pms_repo.insert_ledger(ts_code=c['ts_code'], action=c['action'],
|
||||
arbiter='judge' if c.get('judge_required') else 'rule',
|
||||
verdict='PASS', price_at=price, hard_numbers=hard, ref_id=iid, reason=reason[:500])
|
||||
if is_open and saved != 1:
|
||||
raise RuntimeError('自动建仓批准记录未保存,本次指令已回滚')
|
||||
return {'instruction_id': iid}
|
||||
|
||||
|
||||
def _make_instruction(c, price, now, *, account_locked=False) -> str:
|
||||
ymd = td.ymd(now)
|
||||
# 序号用完整时分秒 (2026-08-28 修): 原来 %1000 只留「分钟个位+秒」, 同日同票同动作
|
||||
# 第二条指令约 1/600 概率撞唯一键丢单一跳。撞了再退一步逐秒加一重试。
|
||||
|
|
@ -1272,13 +1339,13 @@ def _make_instruction(c, price, now) -> str:
|
|||
iid = None
|
||||
last_err = None
|
||||
from app.services import buy_budget, execution_orders
|
||||
with execution_orders.account_guard():
|
||||
with nullcontext() if account_locked else execution_orders.account_guard():
|
||||
budget = None
|
||||
if c['side'] == 'buy':
|
||||
buy_budget.before_parent(c['ts_code'], action=c['action'])
|
||||
if c['action'] == 'OPEN':
|
||||
budget = buy_budget.candidate(c.get('hard_numbers') or {}, c['ts_code'], c['qty'], price)
|
||||
for salt in range(3):
|
||||
for salt in range(1 if account_locked else 3):
|
||||
try_iid = cs.make_instruction_id(ymd, c["ts_code"], c["action"], seq + salt)
|
||||
try:
|
||||
affected = pms_repo.insert_instruction(
|
||||
|
|
@ -1469,8 +1536,9 @@ def _self_decide(c, verdict, params, today_auto_count) -> dict:
|
|||
"why": "系统采纳:合议放行" + (f",研判通过(把握度 {int(conf)})" if conf is not None else "")}
|
||||
# 五, 每日自动新建仓上限: 超过转交人 (0 = 不限)
|
||||
dmax = int((params or {}).get("self_decide_daily_max") or 0)
|
||||
if res["decision"] == "accept" and is_open and dmax and today_auto_count >= dmax:
|
||||
return _mode({"decision": "confirm", "why": f"今日自动新建仓已达上限 {dmax} 只,本只转交人裁决"})
|
||||
quota_reason = _quota_confirm_reason(today_auto_count, dmax)
|
||||
if quota_reason:
|
||||
return _mode({"decision": "confirm", "why": quota_reason})
|
||||
return _mode(res)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -119,10 +119,8 @@ def _sync(state, code, pos, lots, now, *, seed=True):
|
|||
{r['instruction_id'] for r in pos.get('_risk_lots', []) if r.get('instruction_id')})
|
||||
|
||||
|
||||
def _stop_commands():
|
||||
rows = pms_repo.list_effective_stock_params(('SET_STOP_PRICE',))
|
||||
if len(rows) >= 500:
|
||||
raise ValueError('人工止损命令超过完整核验上限')
|
||||
def _stop_commands(code=None):
|
||||
rows = pms_repo.list_effective_stock_params(('SET_STOP_PRICE',), ts_code=code)
|
||||
out = {}
|
||||
for row in sorted(rows, key=lambda r: r['id']):
|
||||
if _code_ok(row.get('ts_code')):
|
||||
|
|
@ -136,7 +134,7 @@ def observe_user_stop(code, quote, *, now=None):
|
|||
return None
|
||||
now = now or datetime.now()
|
||||
with rotation_repo.account_guard(), rotation_repo.locked(_key(code), KIND, {}) as state:
|
||||
event, why = rules.user_stop_event(_stop_commands().get(code), quote, now=now,
|
||||
event, why = rules.user_stop_event(_stop_commands(code).get(code), quote, now=now,
|
||||
max_age=param_store.get_int('PMS_EXEC_MINUTE_MAX_AGE_SEC', 90))
|
||||
if not event:
|
||||
return {'triggered': False, 'reason': why}
|
||||
|
|
@ -358,6 +356,8 @@ def drive(code, *, now=None):
|
|||
with rotation_repo.account_guard(), rotation_repo.locked(_key(code), KIND, {}) as state:
|
||||
if not state or not (enabled() or state.get('user_stop')) or state.get('phase') not in PAUSED:
|
||||
return
|
||||
if not settings.PMS_TABLE_PREFIX and param_store.execution_halted(locked=True):
|
||||
return
|
||||
if state.get('user_stop'):
|
||||
# 人工命令独立于评级扫描;清零后旧买单迟到仍须重开本轮收尾。
|
||||
pos, lots = _holding(code)
|
||||
|
|
@ -478,7 +478,7 @@ def resume_buy(code):
|
|||
'reset_at': state['reset_at']}, state['round_id'])
|
||||
|
||||
|
||||
def scan(*, now=None):
|
||||
def scan(*, now=None, user_stops_only=False):
|
||||
out = {'enabled': enabled(), 'reviews': {}, 'user_stops': {}, 'errors': []}
|
||||
if not supported():
|
||||
return out
|
||||
|
|
@ -487,16 +487,19 @@ def scan(*, now=None):
|
|||
commands = _stop_commands()
|
||||
# 人工止损不依赖模型资料可用;评析读取异常不阻断已记录的退出收尾。
|
||||
try:
|
||||
states = logic_state_service.state_map() if out['enabled'] else {}
|
||||
states = logic_state_service.state_map() if out['enabled'] and not user_stops_only else {}
|
||||
except Exception as exc:
|
||||
states = {}
|
||||
out['errors'].append(f'评析读取失败: {type(exc).__name__}: {exc}')
|
||||
positions = pms_repo.list_positions(only_open=True)
|
||||
codes = {p['ts_code'] for p in positions if _code_ok(p['ts_code'])}
|
||||
pending = rotation_repo.recent(KIND, limit=1000)
|
||||
if len(pending) >= 1000:
|
||||
raise RuntimeError('退出纪律记录超过核验上限')
|
||||
codes.update(r['value']['code'] for r in pending if r['value'].get('phase') in PAUSED)
|
||||
if user_stops_only:
|
||||
codes.intersection_update(commands)
|
||||
else:
|
||||
pending = rotation_repo.recent(KIND, limit=1000)
|
||||
if len(pending) >= 1000:
|
||||
raise RuntimeError('退出纪律记录超过核验上限')
|
||||
codes.update(r['value']['code'] for r in pending if r['value'].get('phase') in PAUSED)
|
||||
for code in sorted(codes):
|
||||
try:
|
||||
command = commands.get(code)
|
||||
|
|
@ -506,6 +509,8 @@ def scan(*, now=None):
|
|||
out['user_stops'][code] = observe_user_stop(code, quote, now=now)
|
||||
except Exception as exc:
|
||||
out['errors'].append(f'{code}人工止损: {type(exc).__name__}: {exc}')
|
||||
if user_stops_only:
|
||||
continue
|
||||
try:
|
||||
out['reviews'][code] = observe_review(code, (states.get(code) or {}).get('company_review'), now=now)
|
||||
from app.services import timing_state
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ def enqueue_with_reservation(payload, parent_id):
|
|||
if any(old[k] != v for k, v in [('parent_id', parent_id), ('ts_code', code), ('side', side), ('qty', qty)]) or abs(float(old['limit_price'])-price) > 1e-8:
|
||||
raise ValueError('委托幂等键与内容冲突')
|
||||
return
|
||||
if param_store.get_bool('PMS_GLOBAL_EXEC_HALT', False):
|
||||
if settings.PMS_TABLE_PREFIX and param_store.get_bool('PMS_GLOBAL_EXEC_HALT', False):
|
||||
raise ValueError('用户暂停全部执行')
|
||||
with books.scope('all'):
|
||||
parent = pms_repo.get_instruction(parent_id)
|
||||
|
|
@ -79,7 +79,11 @@ def enqueue_with_reservation(payload, parent_id):
|
|||
from app.services.buy_budget import check_order
|
||||
with books.scope(book):
|
||||
check_order(parent, payload)
|
||||
if side == 'buy':
|
||||
if not settings.PMS_TABLE_PREFIX:
|
||||
blocked = service.order_control_blocker(parent, side)
|
||||
if blocked:
|
||||
raise ValueError(blocked)
|
||||
if settings.PMS_TABLE_PREFIX and side == 'buy':
|
||||
if param_store.get_bool('PMS_GLOBAL_BUY_HALT', False):
|
||||
raise ValueError('用户暂停买入')
|
||||
reason = service.strategy_buy_blocker(book, parent)
|
||||
|
|
@ -171,9 +175,15 @@ def send_intent(intent, price, now, snapshot_version):
|
|||
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)
|
||||
if intent.get('manual_exit_cutoff'):
|
||||
blocker = execution_orders.code_settlement_blocker(intent['ts_code'], manual_exit_day=now.date().isoformat(),
|
||||
manual_exit_requested_at=intent.get('manual_exit_requested_at'))
|
||||
if blocker:
|
||||
return {'instruction_id': parent, 'state': blocker, 'pending': True}
|
||||
else:
|
||||
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')
|
||||
|
|
@ -190,7 +200,8 @@ def send_intent(intent, price, now, snapshot_version):
|
|||
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': []})
|
||||
'deadline': now.date().isoformat(), 'is_command': True, 'children': [],
|
||||
**{k:intent[k] for k in ('manual_exit_cutoff','manual_exit_requested_at') if k in intent}})
|
||||
elif existing['status'] not in ('PROPOSED', 'DISPATCHED'):
|
||||
return {'instruction_id': parent, 'state': '已有终态记录,次日重新规划'}
|
||||
elif (existing.get('progress') or {}).get('cancel_requested'):
|
||||
|
|
@ -204,24 +215,31 @@ def send_intent(intent, price, now, snapshot_version):
|
|||
retry_reason = execution_orders.retry_reason(parent, now=now)
|
||||
if retry_reason:
|
||||
return {'instruction_id': parent, 'state': retry_reason, 'pending': True}
|
||||
progress = (existing or {}).get('progress') or {}
|
||||
manual_cutoff = progress.get('manual_exit_cutoff') or intent.get('manual_exit_cutoff')
|
||||
cutoff = _session_deadline(now, manual_cutoff)
|
||||
if now >= cutoff:
|
||||
return {'instruction_id': parent, 'state': '轮换本次执行窗口已结束'}
|
||||
valid_until = min(now + dt.timedelta(minutes=10), cutoff) if manual_cutoff else cutoff
|
||||
child = execution_orders.next_child_id(parent)
|
||||
result = dispatcher.dispatch(instruction_id=child, parent_id=parent, intent='OPEN' if intent['side'] == 'buy' else 'EXIT',
|
||||
ts_code=intent['ts_code'], side=intent['side'], qty=quantity, limit_price=price,
|
||||
valid_until=(now.replace(hour=10, minute=0, second=0, microsecond=0) if now.hour < 10 else min(now+dt.timedelta(minutes=10), now.replace(hour=14, minute=57, second=0, microsecond=0))), note=intent['reason'])
|
||||
valid_until=valid_until, note=intent['reason'])
|
||||
if not result.get('ok'):
|
||||
raise ValueError('轮换委托未下发:'+str(result.get('error')))
|
||||
progress = (pms_repo.get_instruction(parent) or {}).get('progress', {})
|
||||
progress.setdefault('children', []).append({'ref': child, 'qty': quantity, 'limit': price,
|
||||
'mode': 'ws', 'ymd': int(now.strftime('%Y%m%d')), 'at': now.strftime('%H:%M:%S'), 'valid_until': '10:00'})
|
||||
'mode': 'ws', 'ymd': int(now.strftime('%Y%m%d')), 'at': now.strftime('%H:%M:%S'), 'valid_until': valid_until.strftime('%H:%M')})
|
||||
progress['quote'] = intent.get('quote_basis')
|
||||
pms_repo.update_instruction(parent, status='DISPATCHED', dispatch_ref=child, progress=progress)
|
||||
if not pms_repo.update_instruction(parent, status='DISPATCHED', dispatch_ref=child, progress=progress):
|
||||
raise RuntimeError('子委托与轮换父指令未一并保存')
|
||||
return {'instruction_id': parent, 'book_id': book, 'result': result}
|
||||
|
||||
|
||||
def pending_instructions():
|
||||
"""父记录与真实出口合并,父单本地结束不能隐去仍活动的子委托。"""
|
||||
with books.scope('all'):
|
||||
parents = pms_repo.list_instructions(statuses=('PROPOSED', 'RULE_PASSED', 'JUDGE_PASSED', 'DISPATCHED', 'WAIT_REVIEW'), limit=10000)
|
||||
parents = pms_repo.list_instructions(statuses=('PROPOSED', 'RULE_PASSED', 'JUDGE_PASSED', 'DISPATCHED', 'WAIT_REVIEW'), limit=None)
|
||||
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:
|
||||
|
|
@ -266,14 +284,17 @@ def refresh_orders(parents, quotes, now, snapshot_version, target, unavailable):
|
|||
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']
|
||||
manual = progress.get('manual_exit_cutoff') and side == 'sell' and parent['book_id'] == books.MICRO
|
||||
if progress.get('execution_profile') != 'rotation' or progress.get('rule_version') not in core.SUPPORTED_RULES:
|
||||
continue
|
||||
if progress['rule_version'] != core.EXECUTION_RULE and not manual:
|
||||
continue
|
||||
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')
|
||||
result = executor.cancel_instruction(parent['instruction_id'], reason=unavailable.get(code) or '微盘成员已调出', arbiter='system')
|
||||
waits.append(code+'撤单确认后处理剩余席位')
|
||||
if not result.get('ok'):
|
||||
waits.append(code+'撤单请求待重试')
|
||||
|
|
@ -312,7 +333,7 @@ def refresh_orders(parents, quotes, now, snapshot_version, target, unavailable):
|
|||
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}}
|
||||
'rule_version': progress['rule_version'], 'quote_basis': {**quote, **priced}}
|
||||
try:
|
||||
with db.savepoint():
|
||||
results.append(send_intent(intent, priced['limit_price'], now, snapshot_version))
|
||||
|
|
@ -321,20 +342,40 @@ def refresh_orders(parents, quotes, now, snapshot_version, target, unavailable):
|
|||
return results, waits
|
||||
|
||||
|
||||
def cancel_expired(now):
|
||||
from app.services import executor
|
||||
def _session_deadline(now, manual_exit_cutoff):
|
||||
# 旧父单没有人工退出时段标记,仍按批准的普通轮换窗口收尾。
|
||||
end = {None: (10, 0), '11:30': (11, 30), '14:57': (14, 57)}[manual_exit_cutoff]
|
||||
return now.replace(hour=end[0], minute=end[1], second=0, microsecond=0)
|
||||
|
||||
|
||||
def cancel_expired(now, *, continue_exits=False):
|
||||
from app.services import executor, rotation_service as service
|
||||
with books.scope('all'):
|
||||
rows = pms_repo.list_instructions(statuses=('PROPOSED', 'DISPATCHED'), limit=10000)
|
||||
rows = pms_repo.list_instructions(statuses=('PROPOSED', 'DISPATCHED'), limit=None)
|
||||
restrictions = None
|
||||
waits = []
|
||||
for row in rows:
|
||||
p = row.get('progress') or {}
|
||||
if p.get('execution_profile') != 'rotation':
|
||||
continue
|
||||
deadline = str(p.get('deadline') or '')
|
||||
if deadline < now.date().isoformat() or (deadline == now.date().isoformat() and now.hour >= 10):
|
||||
cutoff = _session_deadline(now, p.get('manual_exit_cutoff'))
|
||||
reason = None
|
||||
expired = deadline < now.date().isoformat() or (deadline == now.date().isoformat() and now >= cutoff)
|
||||
if expired:
|
||||
reason = '轮换当日执行窗口结束'
|
||||
elif row['book_id'] == books.MICRO and row.get('side') == 'buy':
|
||||
if restrictions is None:
|
||||
restrictions = service.micro_buy_restrictions()
|
||||
reason = restrictions.get(row['ts_code'])
|
||||
if reason:
|
||||
with books.scope(row['book_id']):
|
||||
result = executor.cancel_instruction(row['instruction_id'], reason='轮换当日执行窗口结束', arbiter='system')
|
||||
result = executor.cancel_instruction(row['instruction_id'], reason=reason, arbiter='system')
|
||||
if not result.get('ok'):
|
||||
raise RuntimeError('轮换委托撤销未确认')
|
||||
if expired and not continue_exits:
|
||||
raise RuntimeError('轮换委托撤销未确认')
|
||||
waits.append(row['ts_code']+'旧委托撤销待重试')
|
||||
return waits
|
||||
|
||||
|
||||
def book_trade(act, fees, seq):
|
||||
|
|
|
|||
|
|
@ -21,11 +21,17 @@ def documents_ready():
|
|||
return settings.INFRA_ROTATION_DOCUMENT_SCHEMA or settings.INFRA_STRATEGY_BOOK_SCHEMA
|
||||
|
||||
|
||||
def configuration():
|
||||
return {'mode': str(param_store.get('PMS_ROTATION_MODE', 'observe')),
|
||||
'rule_version': str(param_store.get('PMS_ROTATION_RULE_VERSION', core.RULE)),
|
||||
def _rotation_controls(*, locked=False):
|
||||
return {'mode': param_store.current_rotation_param('PMS_ROTATION_MODE', locked=locked),
|
||||
'control': param_store.current_rotation_param('PMS_ROTATION_CONTROL', locked=locked)}
|
||||
|
||||
|
||||
def configuration(*, current=False):
|
||||
controls = _rotation_controls() if current and not settings.PMS_TABLE_PREFIX else {
|
||||
'mode': str(param_store.get('PMS_ROTATION_MODE', 'observe')),
|
||||
'control': str(param_store.get('PMS_ROTATION_CONTROL', 'run'))}
|
||||
return {**controls, '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(','))-{''},
|
||||
'costs': {'rate': param_store.get_float('PMS_MICRO30_COMMISSION_RATE', .0002),
|
||||
|
|
@ -190,33 +196,96 @@ def control(*, mode=None, action=None, actor='user'):
|
|||
if not result.get('ok'):
|
||||
raise RuntimeError('档位保存失败')
|
||||
if action:
|
||||
result = param_store.set_param('PMS_ROTATION_CONTROL', action, actor)
|
||||
if not result.get('ok'):
|
||||
raise RuntimeError('控制保存失败')
|
||||
_set_control(action, actor)
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA and configuration()['mode'] == 'auto':
|
||||
cancel_control_orders(action)
|
||||
return status()
|
||||
|
||||
|
||||
def cancel_control_orders(action):
|
||||
def _set_control(action, actor, *, exit_command=None):
|
||||
"""先保存退出意图;显式恢复要等持仓、在途、清仓命令都已结清。"""
|
||||
if action not in ('exit_disable', 'run') or not documents_ready():
|
||||
if not param_store.set_param('PMS_ROTATION_CONTROL', action, actor).get('ok'):
|
||||
raise RuntimeError('轮换控制保存失败')
|
||||
return
|
||||
from app.services.rotation_execution import pending_instructions
|
||||
with store.account_guard():
|
||||
config = configuration(current=True)
|
||||
paper = config['mode'] == 'paper'
|
||||
key = 'state:paper' if paper else 'state:live'
|
||||
with store.locked(key, 'state', {'rotation': core.initial_state()}) as state:
|
||||
rotation = state['rotation']
|
||||
if action == 'exit_disable':
|
||||
rotation['disabled'] = True
|
||||
if not exit_command or exit_command != rotation.get('exit_command'):
|
||||
rotation['manual_exit_requested_at'] = dt.datetime.now(TZ).isoformat()
|
||||
if rotation['phase'] != 'original':
|
||||
rotation.update(phase='returning_original', reason='用户退出并停用')
|
||||
if exit_command:
|
||||
rotation['exit_command'] = exit_command
|
||||
elif rotation.get('disabled') or config['control'] == 'exit_disable':
|
||||
if rotation['phase'] != 'original':
|
||||
raise ValueError('微盘退出仍在办理;若已暂停,请选择退出并停用,待清仓确认后恢复')
|
||||
if not paper and settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
from app.core.command_spec import ACTIVE_TASK_STATES
|
||||
with books.scope(books.ORIGINAL):
|
||||
active = pms_repo.list_commands(statuses=ACTIVE_TASK_STATES, cmd_class='task',
|
||||
cmd_type='LIQUIDATE_ALL', limit=1)
|
||||
if active:
|
||||
raise ValueError('全账户清仓命令尚未确认结束,暂不能恢复微盘')
|
||||
if rotation.get('exit_command') and not paper:
|
||||
from app.core.command_spec import ST_DONE, ST_CANCELLED
|
||||
with books.scope(books.ORIGINAL):
|
||||
command = pms_repo.get_command(rotation['exit_command'])
|
||||
if not command or command.get('status') not in (ST_DONE, ST_CANCELLED):
|
||||
raise ValueError('全账户清仓命令尚未确认结束,暂不能恢复微盘')
|
||||
|
||||
if paper:
|
||||
held = (state.get('account') or {}).get('books', {}).get('micro30', {})
|
||||
if any(p.get('qty') for p in held.values()):
|
||||
raise ValueError('纸面微盘仍有持仓,请先完成退出')
|
||||
elif settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
if positions(books.MICRO) or _micro_unsettled(pending_instructions()) or not reconcile_books()['ok']:
|
||||
raise ValueError('微盘持仓、在途委托或对账尚未结清,请核对后恢复运行')
|
||||
rotation['disabled'] = False
|
||||
rotation.pop('exit_command', None)
|
||||
if not param_store.set_param('PMS_ROTATION_CONTROL', action, actor).get('ok'):
|
||||
raise RuntimeError('轮换控制保存失败')
|
||||
|
||||
|
||||
def micro_buy_restrictions(ts_code=None):
|
||||
"""用户单股命令经原账户命令台写入;轮换买单在两个账本作用域下都以此为准。"""
|
||||
from app.services import command_service
|
||||
with books.scope(books.ORIGINAL):
|
||||
params = command_service.effective_stock_params(
|
||||
cmd_types=('FREEZE_STOCK', 'UNFREEZE_STOCK', 'BLACKLIST_ADD', 'BLACKLIST_REMOVE'), ts_code=ts_code)
|
||||
return {code: ('用户冻结该股,暂停增持' if flags.get('frozen') else '用户黑名单禁止买入')
|
||||
for code, flags in params.items() if flags.get('frozen') or flags.get('black')}
|
||||
|
||||
|
||||
def cancel_control_orders(action, *, continue_exits=False):
|
||||
"""先保存暂停,再撤已发出的轮换委托;失败保留暂停并由调度重试。"""
|
||||
if action not in ('pause_all', 'pause_new', 'exit_disable'):
|
||||
return
|
||||
from app.services import executor
|
||||
with books.scope('all'):
|
||||
rows = pms_repo.list_instructions(statuses=LIVE, limit=10000)
|
||||
rows = pms_repo.list_instructions(statuses=LIVE, limit=None)
|
||||
failed = []
|
||||
for row in rows:
|
||||
if (row.get('progress') or {}).get('execution_profile') != 'rotation':
|
||||
continue
|
||||
if action != 'pause_all' and row['side'] != 'buy':
|
||||
funding_sale = action == 'exit_disable' and row['book_id'] == books.ORIGINAL and row['side'] == 'sell'
|
||||
if action != 'pause_all' and row['side'] != 'buy' and not funding_sale:
|
||||
continue
|
||||
with books.scope(row['book_id']):
|
||||
result = executor.cancel_instruction(row['instruction_id'], reason='用户轮换控制:'+action, arbiter='user')
|
||||
if not result.get('ok'):
|
||||
failed.append(row['instruction_id'])
|
||||
if failed:
|
||||
if continue_exits:
|
||||
return ['旧委托撤销待重试:'+'、'.join(failed)]
|
||||
raise RuntimeError('已停止新增指令,部分既有委托尚未完成撤销')
|
||||
return []
|
||||
|
||||
|
||||
def positions(book):
|
||||
|
|
@ -308,7 +377,7 @@ def _micro_unsettled(pending):
|
|||
|
||||
def tick(now=None):
|
||||
"""先独立提交轮换决定,再执行;两段均先锁账户,再锁轮换状态。"""
|
||||
config = configuration()
|
||||
config = configuration(current=settings.INFRA_STRATEGY_BOOK_SCHEMA)
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA and config['mode'] == 'auto':
|
||||
# 撤单、限制价查询或下发失败不能回滚已经确认的入场/退出任务。
|
||||
# 两段之间释放账户锁;执行段重新读取用户控制、资金和实际持仓。
|
||||
|
|
@ -318,6 +387,9 @@ def tick(now=None):
|
|||
return prepared
|
||||
with store.account_guard():
|
||||
return _tick(now)
|
||||
if config['mode'] == 'paper' and settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
with store.account_guard():
|
||||
return _tick(now)
|
||||
return _tick(now)
|
||||
|
||||
|
||||
|
|
@ -325,13 +397,14 @@ def _tick(now=None, *, prepare_only=False):
|
|||
now = now or dt.datetime.now(TZ)
|
||||
if now.tzinfo is None:
|
||||
now = now.replace(tzinfo=TZ)
|
||||
config = configuration()
|
||||
config = configuration(current=settings.INFRA_STRATEGY_BOOK_SCHEMA)
|
||||
day = now.date().isoformat()
|
||||
cancel_waits = []
|
||||
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:
|
||||
from app.services.rotation_execution import cancel_expired
|
||||
cancel_expired(now)
|
||||
continuing_exit = config['control'] == 'exit_disable'
|
||||
cancel_waits.extend(cancel_control_orders(config['control'], continue_exits=continuing_exit) or [])
|
||||
cancel_waits.extend(cancel_expired(now, continue_exits=continuing_exit) or [])
|
||||
if td.calendar_degraded(day) or not td.is_trade_day(day):
|
||||
return {'skipped': '交易日历不可用或非交易日'}
|
||||
key = 'state:'+('paper' if config['mode'] == 'paper' else 'live')
|
||||
|
|
@ -366,13 +439,13 @@ def _tick(now=None, *, prepare_only=False):
|
|||
# 仅作为已确认退出的执行上下文,不写成新快照、不生成任何入场。
|
||||
value = {'members': [], 'signal_ready': False, 'basket_ready': False,
|
||||
'effective_date': day, 'snapshot_version': saved.get('rotation', {}).get('last_snapshot'), 'signal': {}}
|
||||
if param_store.get_bool('PMS_GLOBAL_EXEC_HALT', False):
|
||||
if param_store.execution_halted(locked=settings.INFRA_STRATEGY_BOOK_SCHEMA):
|
||||
config['control'] = 'pause_all'
|
||||
elif param_store.get_bool('PMS_GLOBAL_BUY_HALT', False) and config['control'] == 'run':
|
||||
config['control'] = 'pause_new'
|
||||
manual_exit = config['control'] == 'exit_disable' and (570 <= now.hour*60+now.minute < 690 or 780 <= now.hour*60+now.minute < 895)
|
||||
if not 570 <= now.hour*60+now.minute < 600 and not manual_exit:
|
||||
if config['mode'] == 'auto':
|
||||
if config['mode'] == 'auto' and not settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
from app.services.rotation_execution import cancel_expired
|
||||
cancel_expired(now)
|
||||
return {'skipped': '执行窗口之外'}
|
||||
|
|
@ -421,14 +494,15 @@ def _tick(now=None, *, prepare_only=False):
|
|||
buy_prices = {}
|
||||
quote_errors = []
|
||||
limits = None
|
||||
if config['rule_version'] == core.EXECUTION_RULE and not prepare_only:
|
||||
if (config['rule_version'] == core.EXECUTION_RULE or manual_exit) 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:
|
||||
quote_codes = {p['ts_code'] for p in micro} if manual_exit else codes
|
||||
for c in quote_codes:
|
||||
try:
|
||||
execution_quotes[c] = market.execution_quote(c, now=now, limits=limits.get(c, {}))
|
||||
except Exception as exc:
|
||||
|
|
@ -444,11 +518,17 @@ def _tick(now=None, *, prepare_only=False):
|
|||
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'))
|
||||
active_basket = state['rotation']['phase'] in ('entering_micro', 'micro')
|
||||
buy_restrictions = micro_buy_restrictions() if config['control'] == 'run' and (fresh_entry or active_basket) and not exit_pending else {}
|
||||
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 []
|
||||
first_targets = [m['code'] for m in value.get('candidates', value['members'])
|
||||
if m['code'] not in unavailable][:core.SIZE]
|
||||
if first_targets and all(code in buy_restrictions for code in first_targets):
|
||||
static_issues.append('首选成员均受用户禁买命令约束,保留原策略仓位')
|
||||
if static_issues:
|
||||
state['blocked'] = static_issues
|
||||
return {'blocked': static_issues}
|
||||
|
|
@ -463,32 +543,26 @@ def _tick(now=None, *, prepare_only=False):
|
|||
if prepare_only:
|
||||
return {'prepared': True, 'mode': config['mode'], 'phase': s['phase']}
|
||||
if config['mode'] == 'auto' and s['phase'] in ('entering_micro', 'returning_original'):
|
||||
from app.services import executor
|
||||
for old in pending:
|
||||
if old['side'] == 'buy' and not (old['book_id'] == 'original' and closing_t0(old)) and (s['phase'] == 'returning_original' or old['book_id'] == 'original'):
|
||||
with books.scope(old['book_id']):
|
||||
result = executor.cancel_instruction(old['instruction_id'], reason='策略轮换停止新增投入', arbiter='system')
|
||||
if not result.get('ok'):
|
||||
raise RuntimeError('原买入撤单尚未确认,暂停轮换')
|
||||
with books.scope('original'):
|
||||
t0_open = any((r.get('state') or {}).get('open_leg') for r in pms_repo.active_strategies() if r.get('type') == 'T0')
|
||||
if t0_open or any(p.get('t0_qty', 0) for p in original):
|
||||
if _stop_leaving_orders(s['phase'], pending, original, cancel_waits):
|
||||
state['blocked'] = ['原策略尚有做T批次,等待完成后继续轮换']
|
||||
return {'blocked': state['blocked']}
|
||||
from app.services import command_service
|
||||
black = command_service.blacklist()
|
||||
refreshed, refresh_waits = [], []
|
||||
if config['mode'] == 'auto' and s.get('rule_version') == core.EXECUTION_RULE and config['control'] == 'run':
|
||||
if config['mode'] == 'auto' and (manual_exit or (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']):
|
||||
refreshable = pending
|
||||
if manual_exit:
|
||||
refreshable = [parent for parent in pending if parent['book_id'] == books.MICRO
|
||||
and parent['side'] == 'sell' and (parent.get('progress') or {}).get('manual_exit_cutoff')]
|
||||
refreshed, refresh_waits = refresh_orders(refreshable, execution_quotes, now, value['snapshot_version'],
|
||||
set(s['target']) if s['phase'] in ('entering_micro', 'micro') else set(), {**unavailable, **buy_restrictions})
|
||||
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 and c not in buy_restrictions for c in s['target']):
|
||||
# 先完成已有单的撤销确认,避免全部停牌时卡住递补;不新卖原仓筹资。
|
||||
state['blocked'] = quote_errors+target_waits+refresh_waits+['目标成员当前没有可核实执行报价,暂不卖出原策略筹资']
|
||||
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'])
|
||||
pending=pending, control=config['control'], buy_restrictions=buy_restrictions, costs=config['costs'])
|
||||
# 纸面档明确采用全沪深权限假设,不把它当实际账户权限判收。
|
||||
issues = core.preflight(value['members'], budget, boards, config['costs']) if value['basket_ready'] else ['名单未就绪']
|
||||
if issues and s.get('rule_version') != core.EXECUTION_RULE:
|
||||
|
|
@ -496,16 +570,19 @@ def _tick(now=None, *, prepare_only=False):
|
|||
waits.extend(issues)
|
||||
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)
|
||||
waits.extend(quote_errors+target_waits+refresh_waits+cancel_waits)
|
||||
if limits is None:
|
||||
limits = _limits(day)
|
||||
done = list(refreshed)
|
||||
for i, intent in enumerate(intents):
|
||||
if config['mode'] == 'paper' and intents and param_store.execution_halted(locked=True):
|
||||
state['blocked'] = ['用户暂停全部执行']
|
||||
return {'blocked': state['blocked']}
|
||||
for intent in 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:
|
||||
if s.get('rule_version') == core.EXECUTION_RULE or manual_exit:
|
||||
from app.services import execution_orders
|
||||
priced = execution_orders.quote_order(side, execution_quotes.get(c, {}), now)
|
||||
if priced.get('action') != 'FIRE':
|
||||
|
|
@ -513,7 +590,7 @@ def _tick(now=None, *, prepare_only=False):
|
|||
continue
|
||||
price = priced['limit_price']
|
||||
intent['quote_basis'] = {**execution_quotes.get(c, {}), **priced}
|
||||
intent['rule_version'] = core.EXECUTION_RULE
|
||||
intent['rule_version'] = s.get('rule_version', core.RULE)
|
||||
if not price or not lim or not lim.get('up_limit') or not lim.get('down_limit'):
|
||||
waits.append(c+'行情或限制价不完整')
|
||||
continue
|
||||
|
|
@ -526,6 +603,10 @@ def _tick(now=None, *, prepare_only=False):
|
|||
intent['qty'] = min(intent['qty'], int(pos.get('avail_qty') or 0))
|
||||
if not intent['qty']:
|
||||
continue
|
||||
if manual_exit and intent['book_id'] == books.MICRO and side == 'sell':
|
||||
intent['manual_exit_cutoff'] = '11:30' if now.hour < 12 else '14:57'
|
||||
if s.get('manual_exit_requested_at'):
|
||||
intent['manual_exit_requested_at'] = s['manual_exit_requested_at']
|
||||
# 报价是可变执行信息,不进入父任务幂等键。
|
||||
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]
|
||||
|
|
@ -558,6 +639,30 @@ def _tick(now=None, *, prepare_only=False):
|
|||
return {'mode': config['mode'], 'phase': s['phase'], 'results': done, 'blocked': waits+errors}
|
||||
|
||||
|
||||
def _stop_leaving_orders(phase, pending, original, waits):
|
||||
"""仅停离开策略的新增投入;卖回资金时,原策略可以继续使用实际到账的钱。"""
|
||||
from app.services import executor
|
||||
leaving = books.ORIGINAL if phase == 'entering_micro' else books.MICRO
|
||||
for parent in pending:
|
||||
if parent['book_id'] == leaving and parent['side'] == 'buy':
|
||||
cancel = leaving != books.ORIGINAL or not closing_t0(parent)
|
||||
else:
|
||||
cancel = (leaving == books.MICRO and parent['book_id'] == books.ORIGINAL and parent['side'] == 'sell'
|
||||
and (parent.get('progress') or {}).get('execution_profile') == 'rotation')
|
||||
if cancel:
|
||||
with books.scope(parent['book_id']):
|
||||
result = executor.cancel_instruction(parent['instruction_id'], reason='停止转入资金或新增投入', arbiter='system')
|
||||
if not result.get('ok'):
|
||||
if leaving == books.ORIGINAL:
|
||||
raise RuntimeError('旧轮换委托撤销尚未确认')
|
||||
waits.append(parent['instruction_id']+'撤销待重试,同股等待收尾')
|
||||
if leaving == books.MICRO:
|
||||
return False
|
||||
with books.scope(books.ORIGINAL):
|
||||
open_leg = any((r.get('state') or {}).get('open_leg') for r in pms_repo.active_strategies() if r.get('type') == 'T0')
|
||||
return open_leg or any(p.get('t0_qty', 0) for p in original)
|
||||
|
||||
|
||||
def original_buy_blocker():
|
||||
if not settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
return None
|
||||
|
|
@ -572,17 +677,57 @@ def closing_t0(parent):
|
|||
return p.get('origin_type') == 'strategy' and p.get('action') == 'T0_ROUND' and (p.get('progress') or {}).get('leg') == 'close'
|
||||
|
||||
|
||||
def strategy_buy_blocker(book, parent=None):
|
||||
config = configuration()
|
||||
def strategy_buy_blocker(book, parent=None, *, config=None):
|
||||
if book == books.ORIGINAL:
|
||||
if closing_t0(parent):
|
||||
return None
|
||||
return original_buy_blocker()
|
||||
config = config or configuration(current=True)
|
||||
if config['mode'] != 'auto' or config['control'] != 'run':
|
||||
return '微盘未在自动运行档或用户已停止新增'
|
||||
value, _ = store.get('state:live')
|
||||
if (value or {}).get('rotation', {}).get('phase') not in ('entering_micro', 'micro'):
|
||||
return '微盘阶段不允许新增买入'
|
||||
if parent and parent.get('ts_code'):
|
||||
return micro_buy_restrictions(parent['ts_code']).get(parent['ts_code'])
|
||||
return None
|
||||
|
||||
|
||||
def order_control_blocker(parent, side):
|
||||
"""模拟账户入队和最终发送标记持同一账户锁,两个时刻均核验最新命令。"""
|
||||
if param_store.execution_halted(locked=True):
|
||||
return '用户暂停全部执行'
|
||||
if not parent or parent.get('book_id') not in (books.ORIGINAL, books.MICRO):
|
||||
return '委托的策略来源无法核实'
|
||||
book = parent['book_id']
|
||||
progress = parent.get('progress') or {}
|
||||
if progress.get('cancel_requested') or parent.get('status') not in LIVE:
|
||||
return '来源指令已停止执行'
|
||||
rotation = progress.get('execution_profile') == 'rotation'
|
||||
config = None
|
||||
if rotation or book == books.MICRO:
|
||||
config = _rotation_controls(locked=True)
|
||||
if config['mode'] != 'auto' or config['control'] == 'pause_all':
|
||||
return '微盘未在自动执行档或用户暂停轮换'
|
||||
if rotation and side == 'sell' and book == books.ORIGINAL:
|
||||
live, _ = store.get('state:live')
|
||||
state = (live or {}).get('rotation', {})
|
||||
if state.get('phase') != 'entering_micro' or config['control'] == 'exit_disable':
|
||||
return '账户已停止转入微盘,原策略筹资卖单停止'
|
||||
restrictions = micro_buy_restrictions()
|
||||
if state.get('target') and all(code in restrictions for code in state['target']):
|
||||
return '当前目标成员均受用户禁买约束,停止筹资卖单'
|
||||
if side == 'buy':
|
||||
if param_store.locked_open_param('PMS_GLOBAL_BUY_HALT'):
|
||||
return '用户暂停买入'
|
||||
if book == books.ORIGINAL:
|
||||
code = parent.get('ts_code')
|
||||
if not code:
|
||||
return '买入证券无法核实'
|
||||
restriction = micro_buy_restrictions(code).get(code)
|
||||
if restriction:
|
||||
return restriction
|
||||
return strategy_buy_blocker(book, parent, config=config)
|
||||
return None
|
||||
|
||||
|
||||
|
|
@ -607,14 +752,30 @@ def reconcile_books():
|
|||
def on_user_command(cmd_type, command_id, issued_by):
|
||||
if issued_by in ('rotation', 'macro') or str(issued_by).startswith('macro'):
|
||||
return
|
||||
if cmd_type in ('LIQUIDATE_ALL', 'HALT_ALL', 'HALT_BUY'):
|
||||
if cmd_type in ('FREEZE_STOCK', 'BLACKLIST_ADD') and not settings.PMS_TABLE_PREFIX:
|
||||
from app.services import executor
|
||||
failed = False
|
||||
with store.account_guard():
|
||||
with books.scope(books.ORIGINAL):
|
||||
cmd = pms_repo.get_command(command_id)
|
||||
if not cmd or not cmd.get('ts_code'):
|
||||
raise ValueError('单股禁买命令已生效,证券待核对')
|
||||
if cmd['status'] != 'EFFECTIVE':
|
||||
return
|
||||
with books.scope(books.MICRO):
|
||||
rows = pms_repo.list_instructions(statuses=LIVE, ts_code=cmd['ts_code'], side='buy', limit=None)
|
||||
for row in rows:
|
||||
result = executor.cancel_instruction(row['instruction_id'], reason='用户单股禁买命令 '+command_id, arbiter='user')
|
||||
if not result.get('ok'):
|
||||
failed = True
|
||||
if failed:
|
||||
raise RuntimeError('单股禁买已生效,原微盘买单的撤销尚未确认')
|
||||
elif cmd_type in ('LIQUIDATE_ALL', 'HALT_ALL', 'HALT_BUY'):
|
||||
action = {'LIQUIDATE_ALL': 'exit_disable', 'HALT_ALL': 'pause_all', 'HALT_BUY': 'pause_new'}[cmd_type]
|
||||
result = param_store.set_param('PMS_ROTATION_CONTROL', action, issued_by)
|
||||
if not result.get('ok'):
|
||||
raise RuntimeError('全账户命令未同步到轮换控制')
|
||||
_set_control(action, issued_by, exit_command=command_id if cmd_type == 'LIQUIDATE_ALL' else None)
|
||||
from app.services import executor
|
||||
with books.scope('micro30'):
|
||||
for ins in pms_repo.list_instructions(statuses=LIVE, limit=10000):
|
||||
for ins in pms_repo.list_instructions(statuses=LIVE, limit=None):
|
||||
if cmd_type == 'HALT_ALL' or ins['side'] == 'buy':
|
||||
result = executor.cancel_instruction(ins['instruction_id'], reason='用户全账户命令'+command_id, arbiter='user')
|
||||
if not result.get('ok'):
|
||||
|
|
|
|||
|
|
@ -108,8 +108,14 @@ def consume(*, batch: int = 50, dry_run: bool = False) -> dict:
|
|||
from app.services import timing_fusion
|
||||
if timing_fusion.enabled():
|
||||
try:
|
||||
out['timing_fusion'] = timing_fusion.refresh(dry_run=dry_run)
|
||||
fusion = timing_fusion.refresh(dry_run=dry_run)
|
||||
out['timing_fusion'] = fusion
|
||||
if fusion.get('status') in ('DEGRADED', 'FAILED'):
|
||||
summary = '部分可用' if fusion['status'] == 'DEGRADED' else '不可用'
|
||||
out['errors'].append(f"合成择时{summary},"
|
||||
f"无效记录{fusion['invalid']}条,来源错误{len(fusion['source_errors'])}项")
|
||||
except Exception as exc:
|
||||
out['timing_fusion'] = {'enabled': True, 'status': 'FAILED'}
|
||||
out['errors'].append('合成择时未更新: '+type(exc).__name__)
|
||||
|
||||
prm = {"sell_conf_min": param_store.get_float("PMS_SIGNAL_SELL_CONF_MIN", 0.75),
|
||||
|
|
|
|||
|
|
@ -516,7 +516,7 @@ def scan(*, dry_run: bool = False, now=None) -> dict:
|
|||
"strategy": (st or {}).get("type")})
|
||||
if st:
|
||||
_tend_existing(st, p, a, fresh, hv, prm, buypause, handoff_cool,
|
||||
today, dry_run, out, strategy_service, accum_ok=accum_ok)
|
||||
today, dry_run, out, strategy_service, accum_ok=accum_ok, now=now)
|
||||
continue
|
||||
# 弱基本面试探仓的紧止盈自动挂载 (2026-09-11 工作包三 part 4): 无策略票里, 基本面看空加
|
||||
# 技术面看多的试探仓次日自动挂紧止盈, 不占每日名额。挂了 (或占位让路) 就不再走常规边。
|
||||
|
|
@ -656,7 +656,7 @@ def _attach_tight_trail(p, prm, live_codes, optout, dry_run, out, strategy_servi
|
|||
|
||||
|
||||
def _tend_existing(st, p, a, fresh, hv, prm, buypause, handoff_cool, today,
|
||||
dry_run, out, strategy_service, accum_ok=True):
|
||||
dry_run, out, strategy_service, accum_ok=True, now=None):
|
||||
"""已挂策略的票: 边三 (派发停买/回明确恢复) 与 边四 (接力切换)。"""
|
||||
code = p["ts_code"]
|
||||
note = str(st.get("note") or "")
|
||||
|
|
@ -680,14 +680,23 @@ def _tend_existing(st, p, a, fresh, hv, prm, buypause, handoff_cool, today,
|
|||
heat=hv, cushion=p.get("cushion_pct"), prm=prm)
|
||||
if ok_h and code in handoff_cool:
|
||||
ok_h, why_h = False, "接力条件到了但在接力冷却期内, 不动"
|
||||
if ok_h and _has_pending(st):
|
||||
ok_h, why_h = False, "接力条件到了但策略有在途委托, 等它走完 (下一跳再看)"
|
||||
if ok_h and _has_pending(st, review_history=True):
|
||||
ok_h, why_h = False, "上笔策略安排仍在途或尚未核实,暂缓接力"
|
||||
if ok_h:
|
||||
from app.services.strategy_runner import fresh_trail_price
|
||||
price = fresh_trail_price(code, now)
|
||||
cost = _f(p.get("avg_cost"))
|
||||
cushion = price / cost - 1 if cost and price else None
|
||||
ok_h, why_h = handoff_ready(price=price, price_ok=bool(price),
|
||||
upper=(st.get("params") or {}).get("upper"), heat=hv, cushion=cushion, prm=prm)
|
||||
if not ok_h and not why_h:
|
||||
why_h = "暂无有效突破行情,暂缓接力"
|
||||
if ok_h:
|
||||
if dry_run:
|
||||
out["handoffs"].append({"ts_code": code, "from": st["strategy_id"],
|
||||
"dry_run": True, "why": why_h})
|
||||
return
|
||||
_do_handoff(st, p, hv, prm, why_h, out, strategy_service)
|
||||
_do_handoff(st, p, hv, prm, why_h, out, strategy_service, high_water=price)
|
||||
return
|
||||
if why_h:
|
||||
out["skipped"].append({"ts_code": code, "why": why_h})
|
||||
|
|
@ -824,56 +833,73 @@ def _attach_one(edge, why, p, a, hv, prm, view, black, live_codes, optout,
|
|||
logger.warning("[自动挂载] %s 挂 %s: %s", code, stype, why)
|
||||
|
||||
|
||||
def _do_handoff(st, p, hv, prm, why, out, strategy_service):
|
||||
def _do_handoff(st, p, hv, prm, why, out, strategy_service, *, high_water):
|
||||
"""边四落地: 撤网格 (打接力标记) → 挂止盈 → 高水位从当日高点起算 → 留痕。"""
|
||||
code = p["ts_code"]
|
||||
old_note = str(st.get("note") or "")
|
||||
r1 = strategy_service.set_status(st["strategy_id"], "CANCELLED", by="auto") or {}
|
||||
if not r1.get("ok"):
|
||||
out["errors"].append(f"{code} 接力第一步撤网格失败: {r1.get('error')}")
|
||||
sid = st["strategy_id"]
|
||||
try:
|
||||
with pms_repo.strategy_guard(code):
|
||||
current = pms_repo.get_strategy(sid)
|
||||
if (not current or current.get("status") != "ACTIVE" or current.get("archived_at")
|
||||
or current.get("state") != st.get("state") or current.get("params") != st.get("params")
|
||||
or _has_pending(current)):
|
||||
out["skipped"].append({"ts_code": code, "why": "网格状态已变化或委托尚未核实,暂缓接力"})
|
||||
return
|
||||
# 撤旧、挂新和初始化高水位共用事务,任何一步失败均保留原网格。
|
||||
r1 = strategy_service.set_status(sid, "CANCELLED", by="auto") or {}
|
||||
if not r1.get("ok"):
|
||||
raise ValueError(r1.get("error") or "原网格撤下失败")
|
||||
note = (str(current.get("note") or "") + " " + MARK_HANDOFF_OUT)[:280]
|
||||
if pms_repo.update_strategy(sid, note=note) != 1:
|
||||
raise ValueError("原网格接力记录未保存")
|
||||
params = {"giveback": prm["trail_giveback"], "sell_ratio": prm["trail_sell_ratio"]}
|
||||
trail_note = f"{NOTE_HANDOFF}{why} 回撤 {prm['trail_giveback']:.1%}"
|
||||
r2 = strategy_service.attach({"ts_code": code, "type": "TRAIL", "autonomy": "auto",
|
||||
"params": params, "note": trail_note[:280]}, by="auto",
|
||||
view={"positions": [p]}) or {}
|
||||
if not r2.get("ok"):
|
||||
raise ValueError("、".join(r2.get("errors") or ["跟踪止盈未挂载"]))
|
||||
if pms_repo.update_strategy(r2["strategy_id"], state={"high_water": round(high_water, 3)}) != 1:
|
||||
raise ValueError("跟踪止盈高水位未保存")
|
||||
if pms_repo.insert_ledger(ts_code=code, action="HANDOFF", arbiter="rule", verdict="PASS",
|
||||
price_at=high_water, hard_numbers={"heat": hv, "from": sid, "to": r2["strategy_id"]},
|
||||
ref_id=r2["strategy_id"], reason=f"接力切换: {why}"[:500]) != 1:
|
||||
raise ValueError("接力记录未保存")
|
||||
except Exception as exc:
|
||||
logger.warning("网格接力未完成 %s: %s", code, exc)
|
||||
out["errors"].append(f"{code} 接力未完成,请核对策略状态:{exc}")
|
||||
_ledger(code, "REJECT", f"网格接力未完成:{exc}", {"heat": hv}, sid, out, action="HANDOFF")
|
||||
return
|
||||
n = pms_repo.update_strategy(st["strategy_id"],
|
||||
note=(old_note + " " + MARK_HANDOFF_OUT)[:280])
|
||||
if not n:
|
||||
# 标记没打上: 冷却推导会把这次接力误判成人工撤下 —— 只是偏保守 (多冷却), 但要留痕
|
||||
out["errors"].append(f"{code} 接力标记没写上 (影响 0 行), 冷却推导会偏保守")
|
||||
params = {"giveback": prm["trail_giveback"], "sell_ratio": prm["trail_sell_ratio"]}
|
||||
note = (f"{NOTE_HANDOFF}{why}"[:200] + f" 回撤 {prm['trail_giveback']:.1%}")
|
||||
r2 = strategy_service.attach({"ts_code": code, "type": "TRAIL", "autonomy": "auto",
|
||||
"params": params, "note": note[:280]}, by="auto") or {}
|
||||
if not r2.get("ok"):
|
||||
errs = "; ".join(str(x) for x in (r2.get("errors") or ["挂载校验未过"]))
|
||||
out["errors"].append(f"{code} 接力第二步挂止盈失败 (网格已撤!): {errs} —— "
|
||||
f"该票此刻无策略保护, 请人工处理")
|
||||
_ledger(code, "REJECT", f"接力半途而废: 网格已撤但止盈没挂上 ({errs})",
|
||||
{"heat": hv}, st["strategy_id"], out, action="HANDOFF")
|
||||
return
|
||||
hw0 = _f((p.get("price") if p.get("price_ok") else None), 0.0)
|
||||
if hw0:
|
||||
nn = pms_repo.update_strategy(r2["strategy_id"], state={"high_water": round(hw0, 3)})
|
||||
if not nn:
|
||||
out["errors"].append(f"{code} 止盈高水位初始化没写上 (影响 0 行), "
|
||||
f"将从下一跳现价起算, 只偏保守")
|
||||
out["handoffs"].append({"ts_code": code, "from": st["strategy_id"],
|
||||
"to": r2.get("strategy_id"), "why": why})
|
||||
_ledger(code, "PASS", f"接力切换: {why}",
|
||||
{"heat": hv, "from": st["strategy_id"], "to": r2.get("strategy_id")},
|
||||
r2.get("strategy_id"), out, action="HANDOFF")
|
||||
logger.warning("[自动挂载] %s 接力: 网格 %s → 止盈 %s", code, st["strategy_id"],
|
||||
r2.get("strategy_id"))
|
||||
|
||||
|
||||
def _has_pending(st) -> bool:
|
||||
"""策略上一笔委托还在途? 查不到按无在途 (接力多等一天没有代价, 误停一天有)。"""
|
||||
pend = (st.get("state") or {}).get("pending") or {}
|
||||
iid = pend.get("iid")
|
||||
if not iid:
|
||||
return False
|
||||
def _has_pending(st, *, review_history=False) -> bool:
|
||||
"""只有确认上一笔提议或委托已收尾才允许接管。"""
|
||||
from app.services import strategy_runner, execution_orders
|
||||
from config.settings import settings
|
||||
pending = (st.get("state") or {}).get("pending")
|
||||
try:
|
||||
ins = pms_repo.get_instruction(iid)
|
||||
except Exception: # noqa: BLE001
|
||||
done, parent = strategy_runner._pending_terminal(pending)
|
||||
if not done:
|
||||
return True
|
||||
code = st["ts_code"]
|
||||
if pms_repo.list_instructions(ts_code=code, statuses=strategy_runner.LIVE_INS, limit=1):
|
||||
return True
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA and param_store.get('PMS_DISPATCH_MODE', '') == 'ws':
|
||||
from app.repo import qmt_repo
|
||||
if qmt_repo.list_orders(ts_code=code, statuses=qmt_repo.LIVE, limit=1):
|
||||
return True
|
||||
if parent and not execution_orders.replacement_state(pending['iid'])['ready']:
|
||||
return True
|
||||
# 全票历史回报检查仅做锁外预检;锁内定向重读父单、活跃子单及本策略回报。
|
||||
return review_history and bool(execution_orders.code_settlement_blocker(code))
|
||||
return False
|
||||
return bool(ins and ins.get("status") in ("PROPOSED", "RULE_PASSED", "DISPATCHED", "WAIT_REVIEW"))
|
||||
except Exception as exc:
|
||||
logger.warning("网格在途状态无法核实,保留原策略 %s: %s", st.get("strategy_id"), exc)
|
||||
return True
|
||||
|
||||
|
||||
def _snap(a, hv, p) -> dict:
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@ signal_digest / intraday_exec 互相覆盖 (与 portfolio.neg_streak_map 用独
|
|||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from collections import Counter
|
||||
from contextlib import contextmanager, nullcontext
|
||||
from copy import deepcopy
|
||||
from datetime import datetime
|
||||
|
|
@ -58,7 +59,8 @@ from app.services import market, param_store, portfolio
|
|||
logger = logging.getLogger("pms.strategy")
|
||||
|
||||
# 指令在途 (未终态) 的状态集 —— 与 executor.LIVE 一致; 本层据此判断「上一笔还没走完, 先别再发」。
|
||||
LIVE_INS = ("PROPOSED", "RULE_PASSED", "DISPATCHED", "WAIT_REVIEW")
|
||||
LIVE_INS = ("PROPOSED", "RULE_PASSED", "JUDGE_PASSED", "DISPATCHED", "WAIT_REVIEW")
|
||||
FINAL_INS = {"CONFIRMED", "REJECTED", "EXPIRED", "CANCELLED"}
|
||||
|
||||
# 批次动作 (与 recon.ACTION_TO_LOT 对齐)
|
||||
A_T0 = "T0_ROUND" # 做T 买入与卖出 → T0 批次
|
||||
|
|
@ -138,11 +140,13 @@ def _correction_id(st: dict):
|
|||
@contextmanager
|
||||
def _current_strategy(st: dict):
|
||||
"""保护策略快照与落单;网格批次读取和状态更新也在同一账户锁内。"""
|
||||
from app.services.execution_orders import account_guard
|
||||
with account_guard():
|
||||
with pms_repo.strategy_guard(st['ts_code']):
|
||||
fresh = pms_repo.get_strategy(st["strategy_id"])
|
||||
if not fresh or fresh.get("status") != "ACTIVE" or fresh.get("archived_at"):
|
||||
raise RuntimeError("策略已停用,停止生成新指令或提议")
|
||||
owners = pms_repo.list_strategies(ts_code=st["ts_code"], statuses=["ACTIVE"], limit=2)
|
||||
if len(owners) != 1 or owners[0]["strategy_id"] != st["strategy_id"]:
|
||||
raise RuntimeError("同股生效交易方案不唯一,请先核对后再执行")
|
||||
if (fresh.get("state") or {}).get("correction_hold"):
|
||||
raise RuntimeError("策略成交数量已更正,等待核对后续安排")
|
||||
if _correction_id(fresh) != _correction_id(st):
|
||||
|
|
@ -209,10 +213,13 @@ def _emit_proposal(st: dict, dec: dict) -> str:
|
|||
def emit_from_spec(spec: dict) -> dict:
|
||||
"""confirm 提议被采纳后的回调 (main._decide 调): 按 hard_numbers 里存的动作参数发指令。
|
||||
返回 {ok, instruction_id} 或 {ok:false, error}。"""
|
||||
from app.services.execution_orders import account_guard
|
||||
try:
|
||||
with account_guard():
|
||||
st = pms_repo.get_strategy(spec.get("strategy_id"))
|
||||
strategy_id = spec.get("strategy_id")
|
||||
st = pms_repo.get_strategy(strategy_id)
|
||||
if not st:
|
||||
return {"ok": False, "error": "策略不存在或已撤下"}
|
||||
with pms_repo.strategy_guard(st["ts_code"]):
|
||||
st = pms_repo.get_strategy(strategy_id)
|
||||
if not st:
|
||||
return {"ok": False, "error": "策略不存在或已撤下"}
|
||||
if st.get("status") != "ACTIVE":
|
||||
|
|
@ -238,21 +245,22 @@ def emit_from_spec(spec: dict) -> dict:
|
|||
# ================================================================ 在途委托的收敛
|
||||
def _pending_terminal(pending: dict):
|
||||
"""上一笔委托是否已终态。返回 (已终态?, 指令行 or None)。无 pending 视为已终态。"""
|
||||
if not pending:
|
||||
if pending is None:
|
||||
return True, None
|
||||
if not isinstance(pending, dict):
|
||||
raise ValueError("在途记录格式无法核实,保留策略等待核对")
|
||||
iid = pending.get("iid")
|
||||
if not iid: # confirm 提议还没被采纳 —— 看提议是否还在等
|
||||
if not iid:
|
||||
pid = pending.get("pid")
|
||||
if not pid:
|
||||
return True, None
|
||||
pr = pms_repo.get_proposal(pid)
|
||||
if pr and pr.get("status") == "WAIT_USER":
|
||||
return False, None # 还在等人拍板, 先别再发
|
||||
return True, None # 已采纳(转指令,另有pending.iid)/驳回/过期
|
||||
proposal = pms_repo.get_proposal(pid) if pid else None
|
||||
if not proposal or proposal.get("status") not in ("WAIT_USER", "ACCEPTED", "DECLINED", "REJECTED", "EXPIRED"):
|
||||
raise ValueError("策略提议状态无法核实,保留等待")
|
||||
# 采纳会在同一事务把 pending 换成 iid;仅有旧 pid 不能证明委托已收尾。
|
||||
return proposal["status"] in ("DECLINED", "REJECTED", "EXPIRED"), None
|
||||
ins = pms_repo.get_instruction(iid)
|
||||
if not ins:
|
||||
if not ins or (ins.get("status") not in LIVE_INS and ins.get("status") not in FINAL_INS):
|
||||
raise ValueError("在途父指令无法核实,保留策略等待核对")
|
||||
return (ins.get("status") not in LIVE_INS), ins
|
||||
return ins["status"] in FINAL_INS, ins
|
||||
|
||||
|
||||
def _reconcile(st: dict, state: dict, pos: dict):
|
||||
|
|
@ -274,7 +282,7 @@ def _reconcile(st: dict, state: dict, pos: dict):
|
|||
return True
|
||||
filled = int(ins.get("exec_qty") or 0) > 0
|
||||
leg = pending.get("leg")
|
||||
if st.get('type') == 'GRID':
|
||||
if st.get('type') in ('GRID', 'TRAIL'):
|
||||
# 委托终态不等于已入账;新库存由真实批次投影,不恢复发单时的假设库存。
|
||||
from config.settings import settings
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA and param_store.get('PMS_DISPATCH_MODE', '') == 'ws':
|
||||
|
|
@ -586,6 +594,18 @@ def _tick_grid(st, pos, now, dry_run, out, buy_paused):
|
|||
|
||||
|
||||
# ================================================================ 评估器: 跟踪止盈 (设计 §七B)
|
||||
def fresh_trail_price(code, now=None):
|
||||
"""同一根当日有效分钟线的收盘价供止盈触发及接力初始高点使用。"""
|
||||
from app.core.execution_policy import local_now, positive, timestamp
|
||||
quote = market.execution_quote(code, now=now, limits={})
|
||||
at = timestamp(quote.get('as_of'))
|
||||
current = local_now(now)
|
||||
max_age = param_store.get_int('PMS_EXEC_MINUTE_MAX_AGE_SEC', 90)
|
||||
if at and at.date() == current.date() and 0 <= (current - at).total_seconds() <= max_age:
|
||||
return positive(quote.get('last'))
|
||||
return 0.
|
||||
|
||||
|
||||
def _eval_trail(st, pos, day, now, ctx):
|
||||
"""止盈意图在实际成交后扣减;撤单、部分成交、T+1不能消耗整次减持。"""
|
||||
state, params = ctx['state'], st.get('params') or {}
|
||||
|
|
@ -710,12 +730,16 @@ def tick(*, now=None, dry_run: bool = False) -> dict:
|
|||
if global_halt:
|
||||
out["notes"].append(f"全局当日T亏 {global_t_pnl:.0f} 元 达熔断线 {global_loss_cap:.0f} 元, 今日不再开新T")
|
||||
|
||||
owners = Counter(st.get("ts_code") for st in strategies)
|
||||
for st in strategies:
|
||||
out["checked"] += 1
|
||||
code = st.get("ts_code")
|
||||
from app.core.execution_policy import supported_code
|
||||
if not supported_code(code):
|
||||
continue
|
||||
if owners[code] > 1:
|
||||
out["errors"].append(f"{code} 有多条生效交易方案,暂停自动策略动作,请保留一条")
|
||||
continue
|
||||
fn = EVALUATORS.get(st.get("type"))
|
||||
if not fn:
|
||||
out["skipped"].append({"strategy_id": st.get("strategy_id"),
|
||||
|
|
@ -744,17 +768,22 @@ def tick(*, now=None, dry_run: bool = False) -> dict:
|
|||
|
||||
# 上一笔还没走完就别再发; 走完了先把结果并进 state
|
||||
if not _reconcile(st, state, pos):
|
||||
if not dry_run:
|
||||
pms_repo.update_strategy(st["strategy_id"], state=state)
|
||||
if not dry_run and state != (st.get("state") or {}):
|
||||
with _current_strategy(st):
|
||||
pms_repo.update_strategy(st["strategy_id"], state=state)
|
||||
out["skipped"].append({"strategy_id": st["strategy_id"], "why": "上一笔还在途, 等它走完"})
|
||||
continue
|
||||
|
||||
day = market.day_snapshot(code)
|
||||
if st.get("type") == "TRAIL":
|
||||
day = {"price": fresh_trail_price(code, now)}
|
||||
else:
|
||||
day = market.day_snapshot(code)
|
||||
if not day or not day.get("price"):
|
||||
out["skipped"].append({"strategy_id": st["strategy_id"],
|
||||
"why": f"{code} 无实时行情 (停牌/盘前), 顺延"})
|
||||
if not dry_run:
|
||||
pms_repo.update_strategy(st["strategy_id"], state=state)
|
||||
"why": f"{code} 暂无有效实时行情,等待更新后再评估"})
|
||||
if not dry_run and state != (st.get("state") or {}):
|
||||
with _current_strategy(st):
|
||||
pms_repo.update_strategy(st["strategy_id"], state=state)
|
||||
continue
|
||||
|
||||
# 单票当日T亏熔断 (做T)
|
||||
|
|
@ -778,8 +807,8 @@ def tick(*, now=None, dry_run: bool = False) -> dict:
|
|||
"qty": dec["qty"], "entry": dec.get("entry")}
|
||||
if pms_repo.update_strategy(st["strategy_id"], state=state) != 1:
|
||||
raise RuntimeError("策略状态未成功保存")
|
||||
out["queued"].append({"strategy_id": st["strategy_id"], "proposal_id": pid,
|
||||
"reason": dec["reason"]})
|
||||
group, record = "queued", {"strategy_id": st["strategy_id"],
|
||||
"proposal_id": pid, "reason": dec["reason"]}
|
||||
else:
|
||||
iid = _emit_instruction(st, dec, forced=False)
|
||||
state["pending"] = {"iid": iid, "leg": dec["leg"], "dir": dec.get("dir"),
|
||||
|
|
@ -788,14 +817,16 @@ def tick(*, now=None, dry_run: bool = False) -> dict:
|
|||
"grid_restore": dec.get("_grid_restore")}
|
||||
if pms_repo.update_strategy(st["strategy_id"], state=state) != 1:
|
||||
raise RuntimeError("策略状态未成功保存")
|
||||
out["fired"].append({"strategy_id": st["strategy_id"], "instruction_id": iid,
|
||||
"side": dec["side"], "qty": dec["qty"], "reason": dec["reason"]})
|
||||
group, record = "fired", {"strategy_id": st["strategy_id"],
|
||||
"instruction_id": iid, "side": dec["side"], "qty": dec["qty"], "reason": dec["reason"]}
|
||||
out[group].append(record)
|
||||
else:
|
||||
out["fired"].append({"strategy_id": st["strategy_id"], "dry_run": True,
|
||||
"side": dec["side"], "qty": dec["qty"], "reason": dec["reason"]})
|
||||
|
||||
if not dry_run and not dec:
|
||||
pms_repo.update_strategy(st["strategy_id"], state=state)
|
||||
if not dry_run and not dec and state != (st.get("state") or {}):
|
||||
with _current_strategy(st):
|
||||
pms_repo.update_strategy(st["strategy_id"], state=state)
|
||||
except Exception as e: # noqa: BLE001 —— 单策略异常不拖垮整轮
|
||||
logger.exception("[strategy] 评估失败 %s", st.get("strategy_id"))
|
||||
out["errors"].append(f"{st.get('strategy_id')}: {type(e).__name__}: {e}")
|
||||
|
|
@ -834,7 +865,9 @@ def force_t0_close(*, now=None) -> dict:
|
|||
open_leg = state.get("open_leg")
|
||||
if not open_leg:
|
||||
out["skipped"].append({"strategy_id": st["strategy_id"], "why": "无未平仓"})
|
||||
pms_repo.update_strategy(st["strategy_id"], state=state)
|
||||
if state != (st.get("state") or {}):
|
||||
with _current_strategy(st):
|
||||
pms_repo.update_strategy(st["strategy_id"], state=state)
|
||||
continue
|
||||
if state.get("pending"):
|
||||
out["skipped"].append({"strategy_id": st["strategy_id"], "why": "平回委托已在途"})
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ from __future__ import annotations
|
|||
import json
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from uuid import uuid4
|
||||
|
||||
from app.core import tradedays as td
|
||||
from app.repo import pms_repo
|
||||
|
|
@ -73,12 +74,7 @@ def validate(cfg: dict, view=None) -> dict:
|
|||
if dup:
|
||||
reasons.append(f"该股已挂着一条 {dup[0].get('type')} 策略(生效中), 先撤下再挂新的")
|
||||
|
||||
prm = view["params"]
|
||||
scale = _f(prm.get("scale"))
|
||||
stock_cap = _f(prm.get("stock_cap"))
|
||||
mv = _f(pos.get("market_value"))
|
||||
avail = int(pos.get("avail_qty") or 0)
|
||||
cap_room = max(0.0, stock_cap * scale - mv) # 这只票离单股上限还差多少钱
|
||||
|
||||
if typ == "T0":
|
||||
tr = _f(params.get("t_ratio"))
|
||||
|
|
@ -87,6 +83,10 @@ def validate(cfg: dict, view=None) -> dict:
|
|||
if avail <= 0:
|
||||
reasons.append("做T 需要 T+1 可卖的存量股(当前可卖为 0), 无法在存量上做差价")
|
||||
elif typ == "GRID":
|
||||
scale = _f(view["params"].get("scale"))
|
||||
stock_cap = _f(view["params"].get("stock_cap"))
|
||||
mv = _f(pos.get("market_value"))
|
||||
cap_room = max(0.0, stock_cap * scale - mv)
|
||||
lo, hi = _f(params.get("lower")), _f(params.get("upper"))
|
||||
mid = _f(params.get("center")) or _f(pos.get("price"))
|
||||
step = _f(params.get("step"))
|
||||
|
|
@ -111,21 +111,40 @@ def validate(cfg: dict, view=None) -> dict:
|
|||
return {"ok": not reasons, "reasons": reasons}
|
||||
|
||||
|
||||
def attach(cfg: dict, by: str = "user") -> dict:
|
||||
def attach(cfg: dict, by: str = "user", *, view=None) -> dict:
|
||||
"""挂载一条策略。先校验, 违反返回 {ok:false, errors}; 通过则写 pms_strategy(ACTIVE)。"""
|
||||
v = validate(cfg)
|
||||
if not v["ok"]:
|
||||
return {"ok": False, "errors": v["reasons"]}
|
||||
code = cfg["ts_code"]
|
||||
ymd = td.ymd()
|
||||
sid = f"STR_{ymd}_{code.replace('.', '')}_{int(datetime.now().timestamp()) % 1000000}"
|
||||
saved = pms_repo.insert_strategy(strategy_id=sid, ts_code=code, stype=cfg["type"],
|
||||
autonomy=cfg.get("autonomy") or "auto",
|
||||
params=cfg.get("params") or {}, state={},
|
||||
status="ACTIVE", note=cfg.get("note"))
|
||||
if saved != 1:
|
||||
return {"ok": False, "errors": ["交易方案未成功保存"]}
|
||||
return {"ok": True, "strategy_id": sid, "ts_code": code}
|
||||
code = (cfg or {}).get("ts_code")
|
||||
from app.core.execution_policy import supported_code
|
||||
if not supported_code(code):
|
||||
return {"ok": False, "errors": ["交易方案仅支持规范沪深A股代码"]}
|
||||
# 组合、行业及分钟行情在锁外读取。接力可传入本次扫描已有的持仓快照。
|
||||
try:
|
||||
snapshot = portfolio.positions_view() if view is None else view
|
||||
quoted = _pos(snapshot, code)
|
||||
except Exception as exc:
|
||||
return {"ok": False, "errors": [f"读持仓失败,暂不能挂载:{type(exc).__name__}"]}
|
||||
if not quoted:
|
||||
return {"ok": False, "errors": ["该股没有可核实的持仓快照"]}
|
||||
with pms_repo.strategy_guard(code):
|
||||
current = pms_repo.get_position(code) or {}
|
||||
qty = int(current.get("total_qty") or 0)
|
||||
price = _f(quoted.get("price")) if quoted.get("price_ok") else _f(current.get("avg_cost"))
|
||||
pos = dict(quoted, total_qty=qty, avail_qty=int(current.get("avail_qty") or 0),
|
||||
market_value=qty * price, price=price)
|
||||
if cfg.get("type") == "GRID" and qty > 0 and price <= 0:
|
||||
return {"ok": False, "errors": ["持仓市值无法核实,暂不能确定网格投入上限"]}
|
||||
v = validate(cfg, view={**snapshot, "positions": [pos]})
|
||||
if not v["ok"]:
|
||||
return {"ok": False, "errors": v["reasons"]}
|
||||
ymd = td.ymd()
|
||||
sid = f"STR_{ymd}_{code.replace('.', '')}_{uuid4().hex[:12]}"
|
||||
saved = pms_repo.insert_strategy(strategy_id=sid, ts_code=code, stype=cfg["type"],
|
||||
autonomy=cfg.get("autonomy") or "auto",
|
||||
params=cfg.get("params") or {}, state={},
|
||||
status="ACTIVE", note=cfg.get("note"))
|
||||
if saved != 1:
|
||||
return {"ok": False, "errors": ["交易方案未成功保存"]}
|
||||
return {"ok": True, "strategy_id": sid, "ts_code": code}
|
||||
|
||||
|
||||
def set_status(strategy_id: str, status: str, by: str = "user") -> dict:
|
||||
|
|
@ -136,9 +155,28 @@ def set_status(strategy_id: str, status: str, by: str = "user") -> dict:
|
|||
st = pms_repo.get_strategy(strategy_id)
|
||||
if not st:
|
||||
return {"ok": False, "error": "策略不存在"}
|
||||
if pms_repo.update_strategy(strategy_id, status=status) != 1:
|
||||
return {"ok": False, "error": "交易方案状态未成功保存"}
|
||||
return {"ok": True, "strategy_id": strategy_id, "ts_code": st.get("ts_code"), "status": status}
|
||||
code = st["ts_code"]
|
||||
with pms_repo.strategy_guard(code):
|
||||
st = pms_repo.get_strategy(strategy_id)
|
||||
if not st or st.get("archived_at"):
|
||||
return {"ok": False, "error": "交易方案已归档或不存在"}
|
||||
old = st["status"]
|
||||
if status == "ACTIVE":
|
||||
if old not in ("PAUSED", "ACTIVE"):
|
||||
return {"ok": False, "error": "已结束的交易方案不能恢复,请按当前持仓重新挂载"}
|
||||
if (st.get("state") or {}).get("correction_hold"):
|
||||
return {"ok": False, "error": "成交数量更正仍待核对,无法恢复交易方案"}
|
||||
active = pms_repo.list_strategies(ts_code=code, statuses=["ACTIVE"], limit=2)
|
||||
if any(row["strategy_id"] != strategy_id for row in active):
|
||||
return {"ok": False, "error": "同股已有其他生效方案,请先处理该方案"}
|
||||
if old == "PAUSED" and int((pms_repo.get_position(code) or {}).get("total_qty") or 0) <= 0:
|
||||
return {"ok": False, "error": "该股已无持仓,无法恢复交易方案"}
|
||||
if old != status:
|
||||
if old in ("DONE", "CANCELLED"):
|
||||
return {"ok": False, "error": "已结束的交易方案不能再改变状态"}
|
||||
if pms_repo.update_strategy(strategy_id, status=status) != 1:
|
||||
return {"ok": False, "error": "交易方案状态未成功保存"}
|
||||
return {"ok": True, "strategy_id": strategy_id, "ts_code": code, "status": status}
|
||||
|
||||
|
||||
def cancel_by_code(ts_code: str, reason: str = "") -> dict:
|
||||
|
|
|
|||
|
|
@ -21,30 +21,39 @@ def refresh(*, now=None, dry_run=False):
|
|||
return {'enabled': False}
|
||||
import requests
|
||||
from app.services import exec_advisor, timing_state, risk_exit
|
||||
now = now or datetime.now()
|
||||
response = requests.get(exec_advisor.base_url()+'/api/intraday/timing_fusion', timeout=(2,5))
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
received_at = now or datetime.now()
|
||||
if data.get('version') != contract.VERSION or data.get('enabled') is not True:
|
||||
raise ValueError('合成择时来源未就绪')
|
||||
if not isinstance(data.get('errors', []), list):
|
||||
raise ValueError('合成择时来源错误清单格式无效')
|
||||
states = data.get('states')
|
||||
if not isinstance(states, list) or len(states)>6000:
|
||||
raise ValueError('合成择时响应超过完整读取边界')
|
||||
# 广域v3f覆盖上千只票。缺少第二组的无方向记录不进入PMS账本;
|
||||
# 已有方向必须接收WAIT,及时撤销过期或冲突的旧结论。
|
||||
known_rows = rotation_repo.recent(timing_state.KIND, 6001)
|
||||
if len(known_rows) >= 6001:
|
||||
known = rotation_repo.document_ids(timing_state.KIND, limit=6001)
|
||||
if len(known) >= 6001:
|
||||
raise ValueError('本地方向状态超过完整读取上限')
|
||||
known = {r['id'] for r in known_rows}
|
||||
out = {'enabled': True, 'accepted': 0, 'invalid': 0, 'source_errors': data.get('errors', []), 'actions': []}
|
||||
out = {'enabled': True, 'accepted': 0, 'invalid': 0, 'source_errors': data.get('errors', []),
|
||||
'directions': 0, 'waiting': 0, 'no_evidence': 0, 'actions': []}
|
||||
since = param_store.get('PMS_TIMING_FUSION_START_AT', '')
|
||||
for state in states:
|
||||
if state.get('action') == 'WAIT' and timing_state.key(state.get('ts_code', '')) not in known:
|
||||
continue
|
||||
event = contract.valid(state, now, since=param_store.get('PMS_TIMING_FUSION_START_AT', ''))
|
||||
event = contract.valid(state, received_at, since=since)
|
||||
if not event:
|
||||
out['invalid'] += 1
|
||||
continue
|
||||
code = event['ts_code']
|
||||
if event['action'] == 'WAIT':
|
||||
out['waiting'] += 1
|
||||
if not any(group['evidence'] for group in event['groups'].values()):
|
||||
out['no_evidence'] += 1
|
||||
if timing_state.key(code) not in known:
|
||||
continue
|
||||
else:
|
||||
out['directions'] += 1
|
||||
if dry_run:
|
||||
out['accepted'] += 1
|
||||
continue
|
||||
|
|
@ -62,10 +71,18 @@ def refresh(*, now=None, dry_run=False):
|
|||
raise RuntimeError('合成择时依据未成功入账')
|
||||
out['accepted'] += 1
|
||||
if event['action'] == 'BUY':
|
||||
result = risk_exit.recover_on_buy(code, event, now=now)
|
||||
result = risk_exit.recover_on_buy(code, event, now=now or datetime.now())
|
||||
elif event['action'] == 'SELL':
|
||||
result = risk_exit.on_signal(event, now=now)
|
||||
result = risk_exit.on_signal(event, now=now or datetime.now())
|
||||
else:
|
||||
continue
|
||||
out['actions'].append({'ts_code':code, 'action':event['action'], 'result':result})
|
||||
if out['source_errors'] or out['invalid']:
|
||||
out['status'] = 'DEGRADED' if out['directions'] or out['waiting'] > out['no_evidence'] else 'FAILED'
|
||||
elif out['directions']:
|
||||
out['status'] = 'DIRECTION'
|
||||
elif out['waiting'] > out['no_evidence']:
|
||||
out['status'] = 'NO_DIRECTION'
|
||||
else:
|
||||
out['status'] = 'NO_EVIDENCE'
|
||||
return out
|
||||
|
|
|
|||
|
|
@ -114,14 +114,9 @@ def _alert_scan() -> int:
|
|||
return max(_limit(), param_store.get_int("PMS_UPSTREAM_ALERT_SCAN", 300))
|
||||
|
||||
|
||||
def _alert_per_cat() -> int:
|
||||
# 每个类型最多留最新 N 条, 高频源只占它自己那一档, 不挤别的类。
|
||||
return max(1, param_store.get_int("PMS_UPSTREAM_ALERT_PER_CAT", 20))
|
||||
|
||||
|
||||
def _alert_max_age_sec() -> int:
|
||||
# 告警时间窗(秒): 0=不限龄。只用于给每条打"陈旧"标记(超龄置灰), **不删条目** ——
|
||||
# 用户拍板"置灰标注"而非隐藏, 风控面板不丢信息。与每类上限正交: 先按龄标 stale, 再每类限量。
|
||||
# 风控面板保留陈旧记录,便于交易员核对当天的完整过程。
|
||||
return max(0, param_store.get_int("PMS_UPSTREAM_ALERT_MAX_AGE_MIN", 60)) * 60
|
||||
|
||||
|
||||
|
|
@ -375,9 +370,7 @@ def snapshot(force: bool = False) -> dict:
|
|||
# 信号栏一行 = 上游流里的一条原始消息, 同一只票一天出现很多次 (实测活跃票一天十来条)。
|
||||
# 这里按代码把五个源汇到一起, 给页面的「这只票今天的信号」抽屉用。
|
||||
#
|
||||
# 与 snapshot() 的分工: 那个是**全场最近 N 条**的快照 (告警每类只留最新 20 条), 到下午
|
||||
# 一只活跃票早盘的信号早被挤掉了; 这个是**单票回扫到当天开盘**, 所以才叫"全部"。
|
||||
# 两份缓存不合并 —— 口径不同, 合并会让单票结果被全场快照污染。
|
||||
# 全场快照按来源分页,单票明细按股票汇集当天事件。各自的扫描边界和缓存独立。
|
||||
#
|
||||
# 只读纪律与 snapshot() 逐字相同: 一律 XREVRANGE / ZREVRANGE, 不建消费组、不 ACK、不写。
|
||||
# **这个接口只能点击触发, 绝不能加进页面轮询** —— 加进去就是每 30 秒对上游做一次全流扫描,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,87 @@
|
|||
# 交易规则修复与历史复盘
|
||||
|
||||
## 核查范围与证据
|
||||
|
||||
本次维护面向 155 模拟 PMS。交易源码基线是 `4b34f42`,修复提交为 `06cfb0a` 和 `165d252`,最终测试代码指纹为 `383510093af5`。已有 ETF 研究工作树另有用户改动,修复在独立交易分支完成。账户收益没有参与本次规则的选择。
|
||||
|
||||
从可追溯的首次设计提交 `dc96db7`(7 月 27 日)到 11 月 18 日 13:11 的维护基线,开发机原仓所有本地引用能到达 602 个不同提交,交易基线的祖先含其自身共 532 个。9 月 18 日 00:00 至 11 月 18 日 13:11:13(北京)全部引用为 353 个提交,交易基线为 283 个。全仓数量包含研究分支、26 次交接副本同步及 5 次方案副本同步。用稳定 patch-id 逐对核验,`934d46f/5b75ade`、`64af252/c725569`、`47f5621/39d209a` 各自是同一代码补丁。以下按业务事件和故障机制合并,提交数量不能用作事故数或人天。
|
||||
|
||||
可用同一份本地引用复算范围及合并记录。截止时间固定在修复前。
|
||||
|
||||
```sh
|
||||
git rev-list --all --count --since='2026-09-18T00:00:00+08:00' --until='2026-11-18T13:11:13+08:00'
|
||||
git rev-list 4b34f42 --count --since='2026-09-18T00:00:00+08:00' --until='2026-11-18T13:11:13+08:00'
|
||||
git log --all --merges --until='2026-11-18T13:11:13+08:00' --format='%h %p %s'
|
||||
```
|
||||
|
||||
可核实的关键合并是 11 月 3 日的 `7fe3c5d`(父 `bb044b7/8dc744d`)和 `2b1c447`(父 `9bc94eb/ebef980`)。前者将已发布的委托与跨日复核修复合入一条分支;后者把实际运行基线与异步研判、撤换限价和微盘递补整合到后来维护的交易线上。比较各自父提交可以看到当时不同角色确有代码差异。未取得内网 Gitea 的 PR 正文、审阅及驳回记录,故意设计的认定依据是当时方案、实际差异及原始验收。两笔合并不折算成两次相同事故。
|
||||
|
||||
## 修改后从交易入口看到的结果
|
||||
|
||||
| 业务入口 | 断网验证与行为差异 | 对原业务的保护 |
|
||||
|---|---|---|
|
||||
| 人工与自动建仓 | 同份研究仅从第 300 移到第 301 名仍可继续;资格确实消失会被拦;等待期间报告实质改变会重新研判。两个当日新仓或新仓与跨日续买同时争最后一个自动名额,只能批准一笔。 | 初选仍执行榜单范围。自动额度已用完或启用限额但计数未知时交人。批准写零行或报错时父指令与额度一起回滚,余下候选仍有机会。 |
|
||||
| 盘中择时与人工止损 | 使用解码完成时的时钟验证 BUY、SELL,区分来源降级、有证据无方向和没有证据。执行暂停时同一触价多次扫描只产生一次退出意图。 | 保留未来和过期数据拒绝、首次停买、二次独立确认、T+1 和已有撤单收尾。来源无法核验时不会伪装成正常的中性判断。 |
|
||||
| 策略挂载、恢复和网格接管 | 并发挂载和恢复最多一个 ACTIVE。旧单在途状态不明延后切换;切换途中记账失败恢复原网格。次日盘中昨日价格不能触发跟踪止盈。 | 已确立的止盈意图在部分成交后只执行实际剩余;历史重复 ACTIVE 不会被脚本擅自挑一条继续操作;重选归属有清晰依据。 |
|
||||
| 跨日续买与拆单 | 等待异步研判的一笔可排到队尾,其他旧指令能获得复核。三日减持 3,000 股首日可投 1,000;成交 700 并撤掉余单后可再投 300。报价到发单之间又发生成交时,按最新余量再算。 | 保留一轮一次的研判预算、父指令总额、末日和紧急单原有执行方式。已排队和未入账的成交不能释放第二份额度。 |
|
||||
| 微盘禁买、退出与恢复 | 同一条个股禁买覆盖微盘建单、重挂与两个账本发给 QMT 前的买入复核。有明确个股冻结或黑名单时,反向做 T 的买回也受约束;仅轮换暂停原策略新增时,已开的做 T 平回继续按原规则办理。14:00 手动退出的卖单不会在 14:01 被普通轮换的 10 点规则撤掉。退出完成且无相关持仓、在途或待核实事项时,才能恢复新轮次。 | 冻结不妨碍卖出或原委托撤销。已经进入发送或被券商接收的委托仍可能在撤销确认前成交,继续按真实回报入账。禁买席位不把资金自动摊给余票。全局暂停时仍允许必要撤单;原策略正常的持仓与做 T 收尾不因微盘退出而被随意中断。 |
|
||||
|
||||
逐票与授权的新专项是 `scripts/test_plan_rank_boundary.py` 和 `scripts/test_auto_open_approval.py`。择时、止损、策略交接、跨日复核、日配额、微盘轮换的反例已补进既有专项和全量入口。并发用例在隔离 SQLite 上验证可观察的事务结果,目标机 MariaDB 代理的锁行为仍需候选环境核验。
|
||||
|
||||
## 有意设计与后来的偏离
|
||||
|
||||
| 演变节点 | 当时解决的业务问题 | 本次处理 |
|
||||
|---|---|---|
|
||||
| 7 月 `dc96db7/c4833ba/e32b6df`,QMT 协议、持仓快照与逐笔回报 | 下单结果异步到达,查询、WS 和账本有各自职责;外部人工交易也要认领 | 保留唯一 WS、真实成交归属和已成交/在途/待入账约束。阶段性缺少记录不能被解释成已撤完。 |
|
||||
| 9 月 `44ec6d4/980235c/f5cc1b8`,自主决策与上游质地排序 | 上游研究筛候选、bionic 判断时机、PMS 控额度。特定条件才允许自动批准,超过日额交人 | 保留正常入池顺序和人工权限。修正截断榜被当作逐票资格、额度读错按零和批准留账不完整等偏离。 |
|
||||
| 10 月 `65d2940/0acf7b8/984e3d7`,冻结计划版本、后台准备与人工快速采纳 | 点击确认时重新做全量研究会变慢,报价细微变化也曾导致重复确认。人工可用完整版本的逐票资料 | 快慢入口共用同版逐票资格。留痕可以不同,资格判断必须一致;重新发现候选时仍使用事先确定的榜单。 |
|
||||
| 10 月 `e9afc7a/bafecb7/fad1bb1`,成交归属、网格库存与退出 | 同股不同轮次、底仓与网格增量不能混算;已成交与尚在途有不同后果 | 保留额度、库存、成交和 T+1 约束;只纠正当日拆单重复扣量、策略重复接管和未知在途时的交接。 |
|
||||
| 10 月下旬 `e13740e/316d31d`,独立微盘账本及轮换 | 微盘不受原选股规则限制,仍使用同一账户的现金和用户禁令;决定先记账,外部撤单可后续收尾 | 账户冻结跨账本一致,用户主动退出与普通早盘轮换使用各自有效窗口。资金回原策略与重新进入微盘分别核验。 |
|
||||
| 11 月 `35838b8/7c60cdc/1cad2b3`,人工止损、退出与来源核验 | 原方案已要求全局暂停时记录触价,并区分来源是否就绪、是否发出方向 | 保留首次 SELL 停买和二次独立确认。补计时、调度、落账、异常汇报,不用行情缺失替代中性或直接清仓。 |
|
||||
|
||||
## 两个月内哪些返工有共同原因
|
||||
|
||||
在全引用、去合并提交的文件历史里,`proposal_service.py` 修改 20 次,`executor.py` 17 次,`plan_recheck.py` 和 `plan_feed.py` 各 14 次。这些数字用于定位反复改动的入口。已有的[月度故障与返工根因复盘](月度故障与返工根因复盘_2026-11-18.md)保留跨 PMS、ITD、bionic、选股桥的提交索引、具体事故和自然回执。
|
||||
|
||||
| 已有证据 | 共因及这次新发现 | 防止再发生的办法 |
|
||||
|---|---|---|
|
||||
| 9 月测试统计误写,10 月 `55e2ed6` 补独立进程和断网守卫;11 月 `60440ad → e4a77bf → 28abddf` 中 Decimal 导致 8 个新提议和 1 个复核保存失败,外层任务仍成功 | 测试替换掉了真正失败的入口或返回值。此次人工止损单元测试通过,定时任务却在调用前返回;采集合成方向也可能失败而任务报成功 | 沿实际调度、服务和最终账本配对断言。模拟红线包括写入抛错或零影响行;正常的零方向要有单独对照。 |
|
||||
| 10 月 `e9afc7a/0871885` 修逐笔与汇总重复认领,`bafecb7` 修网格侵入底仓;11 月网格又补未成交与部分成交库存 | 同一经济事实在多个阶段用不同口径计算。普通三日单今日部分成交同时改变分母和已用量;批准与父指令也曾分别成功 | 核验每一步的委托、成交、在途、入账和剩余。授权与额度在同一事务内判定;重试后要验证总量和身份。 |
|
||||
| 10 月 29 日主题裁剪影响汇川技术,11 月 10 日人工关注华策影视被截断后修回;11 月 12 日又识别关注与正式告警池覆盖差异 | 混用候选名单、逐票资格、研判和实际订阅。合格股票跌出有限榜单又被解释成失格 | 用同一输入同时走普通初选和已有提议复核,覆盖边界内外与真实失格。保留冻结时的身份与版本,不重新为历史判断补理由。 |
|
||||
| 10 月 `316d31d` 隔离演练揭示撤单失败能回滚退出;11 月人工止损、微盘恢复、策略接管再遇观察与执行耦合 | “决定已形成”“可以执行”“已成交”是三个时点。共用暂停、共用截止时间或把未知在途当作空,就会丢掉用户意图或重开风险 | 同一事件序列检查暂停、持久退出意图、恢复、迟到回报及可卖余量。优先复用现有账户锁、所有者与订单状态。 |
|
||||
| 11 月 `34087b5 → 67b0277 → e672781 → e4a7424` 恢复交易员认可的单行信息流;红利研究 `7fcae67/a7d1d55` 撤回旧标签推断;11 月 16 日试探预算从整轮禁增收回为约束单次 OPEN | 功能迭代、规则主动改变和实现越界需要分别处理。页面反馈和代码返回成功都不能代替用户任务完成 | 只把可复现、可判定的错误做成永久回归。策略参数与底仓重估须有业务依据,不由维护者从单笔亏损推导。 |
|
||||
|
||||
本窗口未找到标准 Git Revert 提交,也未找到生产镜像整批回滚的充分证据。已确认的规则撤回、页面返修和候选阶段故障分别列在上表,授权重置模拟账本亦有独立用途。
|
||||
|
||||
## 可安全删除什么
|
||||
|
||||
- `bee6fc8` 已将告警改成按当天来源分页。基线全仓按符号检索,旧私有函数 `_alert_per_cat` 只剩定义。此次删除函数及“每类只留 20 条”的误导注释。原参数名、存量参数值和接口保留,参数说明标明其兼容用途,避免交易员误以为可以调节现行信号覆盖。
|
||||
- 保留逐笔之外的外部人工成交兜底、冻结版本 A→B→A 的真实顺序,以及仍供旧增持、SAR 和停买恢复使用的技术状态映射。删除它们分别会导致漏账、丢版本、丢风险依据。迁移前需要对调用方和实际 155 角色镜像逐项核对。
|
||||
- 未重新定义网格下破后底仓的退出时限、首次 SELL 的含义或第二次确认的证据要求。它们会改变投资决策。修复误拒、误放和误记可以直接按批准设计验收。
|
||||
|
||||
## 二阶影响与维护成本
|
||||
|
||||
实际生效的个股命令按业务范围完整读取,发送单票时按股票过滤。已验证较早的有效冻结和止损在其后出现 500 条命令时仍能找到。参数写入只在事务提交后使进程缓存失效,控制状态晚一步保存失败时外部仍看到上次已提交的结果。
|
||||
|
||||
个股冻结恢复一致后,微盘可能出现空位、现金闲置或候选替补,原有资金与持仓限制继续生效。自动日额读不到或真实委托状态未知时,部分处理将等待或交给交易员。使用同一份已冻结输入核对可以减少无意义确认;等待和移交的原因须能从实际业务结果读到。择时修正后,首次停买和恢复事件数可能变化;保留有效期与独立证据校验,避免把两个重复读数升级为清仓。自动批准与状态切换会增加短事务竞争;模型研判、外部取数不宜扩大锁持有时间。优先用既有锁、现有调度和断网回归,不增加长期巡检、外部服务或第二条 QMT 通道。
|
||||
|
||||
部署后按自然发生的事件对账。关注批准与父指令、暂停期人工触价、跨日复核轮次、微盘冻结阻断及 T+1 余量。候选环境足以证明这些分支可达;线上发生次数和对交易收益的影响另需真实流水。
|
||||
|
||||
## 155 发布与自然验收的位置
|
||||
|
||||
受影响的运行角色为 web、worker 和唯一的 pms-ws。web 处理人工确认和运行控制,worker 计算、批准、执行和复核,WS 的发送进程会调用此次修改的最终发送检查。beat 的任务名和调度表沿用原配置,行情及 ETF 观测容器没有本次业务变更。11 月 18 日 13:08 的 web/worker 发布记录及先前 WS 的镜像记录只能确定取证起点。正式发布前须分别核验三个现用镜像、宿主机和私有挂载,保留角色差异,构建对应候选,在禁止联网、无业务配置的容器内跑全量回归,然后排空 worker、先退出旧 WS 再启动唯一新 WS。
|
||||
|
||||
| 观察场景 | 验收依据 |
|
||||
|---|---|
|
||||
| 研究材料未变,仅越过普通榜单边界;两路争同一日额度 | web 提议详情、worker 的 plan_pull/proposal_scan、当时的计划/消费版本、父指令和批准账。真失格不能批准,成功批准有且仅有与其关联的记录。 |
|
||||
| 方向跨越请求往返或触价时暂停 | signal_digest 的来源错误、无效数及方向状态;intraday_exec 的风险退出结果;方向/止损状态文档和下游父指令。记录触价、停买、可卖数量和实际成交各自的时点。 |
|
||||
| 跨日等待、部分成交或策略交接 | 父指令的 daily_review、实际 QMT 子单、成交日和累计量、已入账量及同股生效策略。合计不超父单与日配额,已被管理的存量策略有明确归属。 |
|
||||
| 微盘冻结、下午退出、解除停用 | 核对 rotation_tick、微盘持仓与阶段、单股命令、发送前拦截及撤销回报。发出后看连续 ACK、最新资金、缺口、待入账和两个策略账本对账;冻结中的合法卖出可继续执行。 |
|
||||
|
||||
这些点用于核对下一次自然发生的业务事件。未产生相应事件时保留待验,不通过运行账户制造反例。
|
||||
|
||||
## 本次交付回执
|
||||
|
||||
2026-11-18 18:16 北京时间。在开发机以独立、干净的 Git 工作树检出交易代码 `165d252`,运行 `python scripts/run_tests.py`。92 个登记套件全部通过,进程退出码 0,末行为 `ALL SUITES PASS`,代码指纹 `383510093af5`。修复前同机从 `4b34f42` 断网跑完原有 90 组。终版包括逐票 5 例、自动批准 8 例、合成择时 22 例、人工止损与事务 26 例、策略与成交修正 30 例、微盘轮换 57 例;均是各套件的总例数。三个页面护栏包含在全量中,本次没有改布局或交互。
|
||||
|
||||
本机未发现可用的容器引擎。通过登记的 SSH 入口连接 155 和内网 Gitea 均在握手阶段关闭。当前成果为本地代码与回归通过,155 三角色候选镜像的构建、断网回归、定向重建及自然回执待完成。未操作 188 或运行账本。外部资金与收益归因不在本轮。
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
# 开发资料入口
|
||||
|
||||
11月18日的选股、择时和账户执行修复见[交易规则修复与历史复盘](交易规则修复与历史复盘_2026-11-18.md),含设计依据、断网反例、历史清理与155发布边界。
|
||||
|
||||
11月18日13:09最新:[资金提示的预期延迟与故障区分](资金提示预期延迟与故障区分_2026-11-18.md)。本次为午休新增603087尚待接入被误报,原池实时资金正常;午后首轮13:00不在基线有效窗口,13:03六路自然计算完成。13:08:30仅发布155 web与worker的cac3ba3,开发机90组、两候选89/87组断网通过;13:09确认原始登记时间自然传递,15只优先股正常、提示为空、账本正常。其他来源仍按各自发布周期核验。
|
||||
|
||||
11月18日10:35最新:[关注持仓信号过滤核验](关注持仓信号过滤核验_2026-11-18.md)。PMS未按名单身份过滤资金或股价类别,125组实际页面计算函数回放一致;14只均有ITD及MTF资金计算证据。果麦文化盘中跌11.82%但独立股价生产者未发布,历史同版本有301股票跌约8%的提醒,生产者代码、名单与去重仍待定位,尚未修复。不能以资金恢复代替股价提醒验收。
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
运行: docker compose run --rm pms-web python scripts/run_tests.py
|
||||
|
||||
历史准备逐路覆盖、旧证据拒绝及嵌套业务失败核验专项21例。
|
||||
选股逐票复核专项5例;自动建仓批准与跨日竞争同一额度专项8例。
|
||||
|
||||
test_batch54_units.py 用户候选来源、到期核验、权限、显式取用、技术面补读及评级排序透传 (10例)
|
||||
|
||||
|
|
@ -23,12 +24,12 @@
|
|||
|
||||
包含 (例数按 2026-10-26 隔离回归实跑校正):
|
||||
test_batch57_units.py 异步请求持久化、截止时间、旧结果隔离、结果优先收取、真实调度入口及行情来源 (32例)
|
||||
test_batch56_units.py 微盘v3冻结候选、按序递补与部分成交保护 (40例)
|
||||
test_batch56_units.py 微盘冻结篮子、账户禁买、部分成交、人工退出和恢复 (57例)
|
||||
test_batch55_units.py 共同撤换状态、报价、依据监控与取消确认 (34例)
|
||||
test_sim_reset_units.py 双账本重置、空仓证据、配置与去重保留、幂等及SQL回滚 (9例)
|
||||
test_batch53_units.py QMT下单策略类型、原字段与签名、双策略买卖归属及读取故障保留队列 (8例)
|
||||
test_batch52_units.py 当日限价、撤单拒绝、迟到成交、发送恢复与真实SQL状态保护 (22例)
|
||||
test_batch51_units.py 跨日决策复核、撤单回报、数量约束、人工确认与真实SQL事务及完整选股接线及异步等待 (37例)
|
||||
test_batch52_units.py 当日限价、撤单拒绝、迟到成交、日配额与发送前复核 (29例)
|
||||
test_batch51_units.py 跨日决策复核、撤单、共享日额、异步轮转和并发领取 (42例)
|
||||
test_batch50_units.py QMT限价等待、价格来源故障、用户暂停、配额防重、试算隔离及收盘边界 (17例)
|
||||
test_batch49_units.py 持仓与指令分组、同名股票及批次归属、撤单账本、未迁移及缺数展示 (14例)
|
||||
test_batch48_units.py 策略账本隔离、轮换优先级、交易单位、模拟直接启用、读数及微盘50规则换版 (55例)
|
||||
|
|
@ -207,11 +208,12 @@ HERE = os.path.dirname(os.path.abspath(__file__))
|
|||
ROOT = os.path.dirname(HERE)
|
||||
# 退出纪律专项:两次独立清仓信号、评级下降、事务、双策略隔离及撤单收尾。
|
||||
SUITES = [
|
||||
"test_plan_rank_boundary.py", "test_auto_open_approval.py",
|
||||
"test_business_evidence.py", # 历史准备覆盖与嵌套业务失败(21例)
|
||||
"test_signal_health.py", # 资金监测可用性与交易员提示,7例
|
||||
"test_proposal_trader_view.py", # 当前分析、数据库读数保存及旧确认保护(21例)
|
||||
"test_watch_missing_fund.py",
|
||||
"test_timing_fusion.py", # 三组证据契约、二次清仓可达性与恢复(19例)
|
||||
"test_timing_fusion.py", # 三组择时证据、收件时钟、来源退化与退出收尾
|
||||
"test_signal_recovery.py", # 最新方向、停买恢复、网格合议、关注入口与负净成本(16例)
|
||||
"test_grid_execution.py",
|
||||
"test_buy_budget.py",
|
||||
|
|
|
|||
|
|
@ -0,0 +1,192 @@
|
|||
"""自动建仓名额、待执行指令和批准账本的事务边界。"""
|
||||
import unit_test_guard # noqa: F401
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from contextlib import ExitStack
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from threading import Barrier
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlalchemy import create_engine, event, text
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from app.db import session
|
||||
from app.repo import pms_repo
|
||||
from app.services import judge, param_store, proposal_service as ps
|
||||
from config.settings import settings
|
||||
|
||||
NOW = datetime.now().replace(hour=10, minute=0, second=0, microsecond=0)
|
||||
CODES = ('600000.SH', '600001.SH')
|
||||
DDL = [
|
||||
'CREATE TABLE pms_runtime_param(param_key TEXT PRIMARY KEY, param_value TEXT)',
|
||||
"INSERT INTO pms_runtime_param VALUES('PMS_SELF_DECIDE','full'), ('PMS_SELF_DECIDE_DAILY_MAX','1')",
|
||||
'CREATE TABLE pms_ws_state(id INTEGER PRIMARY KEY)',
|
||||
'INSERT INTO pms_ws_state VALUES(1)',
|
||||
'''CREATE TABLE pms_instruction(id INTEGER PRIMARY KEY,instruction_id TEXT UNIQUE,origin_type TEXT,
|
||||
origin_id TEXT,ts_code TEXT,action TEXT,side TEXT,qty INTEGER,limit_price REAL,window_tdays INTEGER,
|
||||
status TEXT,exec_qty INTEGER,progress_json TEXT,created_at TEXT,updated_at TEXT,archived_at TEXT)''',
|
||||
'''CREATE TABLE pms_action_ledger(id INTEGER PRIMARY KEY,ts_code TEXT,decided_at TEXT,action TEXT,
|
||||
arbiter TEXT,verdict TEXT,price_at REAL,hard_numbers_json TEXT,failed_checks_json TEXT,reason TEXT,
|
||||
ref_id TEXT,outcome_scored INTEGER)''',
|
||||
'''CREATE TABLE pms_proposal(id INTEGER PRIMARY KEY,proposal_id TEXT UNIQUE,ts_code TEXT,action TEXT,
|
||||
qty INTEGER,hard_numbers_json TEXT,status TEXT,expire_at TEXT,decided_at TEXT,judge_verdict TEXT,
|
||||
judge_reason TEXT,archived_at TEXT,created_at TEXT)''',
|
||||
]
|
||||
|
||||
|
||||
def candidate(code):
|
||||
return {'ts_code': code, 'action': 'OPEN', 'side': 'buy', 'qty': 100, 'price': 10.,
|
||||
'reason': '当前计划合格', 'judge_required': True, 'hard_numbers': {'consensus': {'route': '放行'}}}
|
||||
|
||||
|
||||
class AutoOpenApproval(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.stack = ExitStack()
|
||||
self.addCleanup(self.stack.close)
|
||||
root = self.stack.enter_context(tempfile.TemporaryDirectory())
|
||||
engine = create_engine('sqlite:///' + root + '/ledger.db')
|
||||
self.stack.callback(engine.dispose)
|
||||
# SQLite 用短写事务实现与生产账户行锁相同的互斥,事务与所有业务 SQL 仍走真实路径。
|
||||
@event.listens_for(engine, 'begin')
|
||||
def begin(conn):
|
||||
conn.exec_driver_sql('BEGIN IMMEDIATE')
|
||||
|
||||
@event.listens_for(engine, 'before_cursor_execute', retval=True)
|
||||
def locking_sql(conn, cursor, statement, parameters, context, executemany):
|
||||
return statement.removesuffix(' FOR UPDATE'), parameters
|
||||
|
||||
with engine.begin() as conn:
|
||||
for statement in DDL:
|
||||
conn.execute(text(statement))
|
||||
self.use(session, 'get_engine', return_value=engine)
|
||||
self.use(settings, 'INFRA_STRATEGY_BOOK_SCHEMA', False)
|
||||
self.use(param_store, 'get_int', side_effect=lambda key, default=0: default)
|
||||
self.use(param_store, 'get_bool', side_effect=lambda key, default=False: default)
|
||||
self.use(ps, 'check_candidate_rules', return_value={'passed': True})
|
||||
self.params = {'self_decide': 'full', 'self_decide_daily_max': 1}
|
||||
|
||||
def use(self, owner, name, *args, **kwargs):
|
||||
return self.stack.enter_context(patch.object(owner, name, *args, **kwargs))
|
||||
|
||||
def route(self, code, params=None, out=None):
|
||||
if out is None:
|
||||
out = dict(autonomy='full', open_autonomy='full', skipped=[], rejected=[], executed=[], queued=[])
|
||||
verdict = {'verdict': judge.PASS, 'confidence': 80, 'raw': {'ok': True}, 'reason': '通过'}
|
||||
ps._route_one(candidate(code), {'positions': []}, params or self.params, {}, False, NOW, False,
|
||||
out, prepared_verdict=verdict)
|
||||
return out
|
||||
|
||||
def ledger(self):
|
||||
return session.fetch_all('SELECT * FROM pms_action_ledger')
|
||||
|
||||
def test_carried_order_and_new_open_share_last_place(self):
|
||||
from app.repo import qmt_repo
|
||||
from app.services import daily_buy_review as daily
|
||||
self.use(qmt_repo, 'orders_for_parent', return_value=[])
|
||||
pms_repo.insert_instruction(instruction_id='earlier', origin_type='proposal', origin_id=None,
|
||||
ts_code=CODES[0], action='OPEN', side='buy', qty=100, status='WAIT_REVIEW', progress={})
|
||||
parent = pms_repo.get_instruction('earlier')
|
||||
fresh = {'auto_exec': True, 'candidate': candidate(CODES[0]), 'verdict': {'verdict': 'PASS'},
|
||||
'self_decide': {'decision': 'accept'}}
|
||||
gate = Barrier(2)
|
||||
def existing():
|
||||
gate.wait()
|
||||
return daily._finish_review(parent, fresh, NOW)
|
||||
def new_open():
|
||||
gate.wait()
|
||||
return self.route(CODES[1])
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
existing_result = pool.submit(existing)
|
||||
new_result = pool.submit(new_open)
|
||||
actual = [existing_result.result(), 'approved' if new_result.result()['executed'] else 'queued']
|
||||
self.assertCountEqual(actual, ['approved', 'queued'])
|
||||
self.assertEqual(pms_repo.system_pass_count_today(NOW.replace(hour=0)), 1)
|
||||
self.assertEqual(len(pms_repo.list_proposals(statuses=['WAIT_USER'])), 1)
|
||||
|
||||
def test_competing_stocks_share_one_place(self):
|
||||
gate = Barrier(2)
|
||||
def route(code):
|
||||
gate.wait()
|
||||
return self.route(code)
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
results = list(pool.map(route, CODES))
|
||||
self.assertEqual(sum(len(r['executed']) for r in results), 1)
|
||||
queued = [p for r in results for p in r['queued']]
|
||||
self.assertEqual(len(queued), 1)
|
||||
self.assertIn('上限', queued[0]['why'])
|
||||
self.assertEqual(len(pms_repo.list_instructions()), 1)
|
||||
self.assertEqual(pms_repo.system_pass_count_today(NOW.replace(hour=0)), 1)
|
||||
|
||||
def test_audit_failure_rolls_back_parent_and_preserves_place(self):
|
||||
for failure in [RuntimeError('审计写入中断'), 0]:
|
||||
with self.subTest(failure=failure):
|
||||
with patch.object(pms_repo, 'insert_ledger', **(
|
||||
{'side_effect': failure} if isinstance(failure, Exception) else {'return_value': failure})):
|
||||
with self.assertRaises(RuntimeError):
|
||||
self.route(CODES[0])
|
||||
self.assertEqual(pms_repo.list_instructions(), [])
|
||||
self.assertEqual(self.ledger(), [])
|
||||
success = self.route(CODES[1])
|
||||
self.assertEqual(success['_sd_accepts'], 1)
|
||||
self.assertEqual(self.ledger()[0]['ref_id'], success['executed'][0]['instruction_id'])
|
||||
|
||||
def test_parent_write_failure_does_not_consume_a_place_in_the_same_round(self):
|
||||
out = dict(autonomy='full', open_autonomy='full', skipped=[], rejected=[], executed=[], queued=[])
|
||||
with patch.object(pms_repo, 'insert_instruction', return_value=0):
|
||||
with self.assertRaises(RuntimeError):
|
||||
self.route(CODES[0], out=out)
|
||||
self.assertEqual(out.get('_sd_accepts', 0), 0)
|
||||
self.assertEqual(self.ledger(), [])
|
||||
self.route(CODES[1], out=out)
|
||||
self.assertEqual(out['_sd_accepts'], 1)
|
||||
|
||||
def test_unreadable_count_falls_back_to_manual(self):
|
||||
with patch.object(pms_repo, 'system_pass_count_today', side_effect=RuntimeError('计数读取失败')):
|
||||
initial = self.route(CODES[0], dict(self.params, _sd_base_count=ps._auto_open_count(NOW)))
|
||||
raced = self.route(CODES[1])
|
||||
for result in (initial, raced):
|
||||
self.assertIn('无法核实', result['queued'][0]['why'])
|
||||
self.assertEqual(pms_repo.list_instructions(), [])
|
||||
self.assertEqual(self.ledger(), [])
|
||||
|
||||
def test_disabled_limit_allows_and_duplicate_input_is_idempotent(self):
|
||||
session.execute("UPDATE pms_runtime_param SET param_value='0' WHERE param_key='PMS_SELF_DECIDE_DAILY_MAX'")
|
||||
params = {'self_decide': 'full', 'self_decide_daily_max': 0}
|
||||
with patch.object(pms_repo, 'system_pass_count_today', side_effect=RuntimeError('计数不可读')):
|
||||
first = self.route(CODES[0], params)
|
||||
second = self.route(CODES[0], params)
|
||||
self.assertTrue(first['executed'])
|
||||
self.assertTrue(second['skipped'])
|
||||
self.assertEqual(len(pms_repo.list_instructions()), 1)
|
||||
self.assertEqual(len(self.ledger()), 1)
|
||||
|
||||
def test_final_approval_observes_new_limit_and_purchase_halts(self):
|
||||
self.route(CODES[0])
|
||||
stale = dict(self.params, self_decide_daily_max=3)
|
||||
capped = self.route(CODES[1], stale)
|
||||
self.assertIn('上限 1', capped['queued'][0]['why'])
|
||||
for flag in ('PMS_GLOBAL_EXEC_HALT', 'PMS_GLOBAL_BUY_HALT'):
|
||||
with self.subTest(flag=flag):
|
||||
session.execute('INSERT INTO pms_runtime_param VALUES(:key, :value)', {'key': flag, 'value': 'true'})
|
||||
stopped = self.route(CODES[1], stale)
|
||||
self.assertIn('暂停', stopped['skipped'][0]['why'])
|
||||
session.execute('DELETE FROM pms_runtime_param WHERE param_key=:key', {'key': flag})
|
||||
self.assertEqual(len(pms_repo.list_instructions()), 1)
|
||||
|
||||
def test_non_open_and_manual_decisions_preserve_independent_routes(self):
|
||||
p = dict(self.params, _sd_base_count=None)
|
||||
dca = {'action': ps.ae.A_DCA, 'decide_kind': ps.ae.DK_DCA_DEEP}
|
||||
target = {'action': ps.ae.A_EXIT, 'decide_kind': ps.ae.DK_TARGET_PRICE}
|
||||
verdict = {'verdict': judge.PASS, 'raw': {}, 'confidence': 90}
|
||||
self.assertEqual(ps._self_decide(dca, verdict, p, None)['decision'], 'accept')
|
||||
self.assertEqual(ps._self_decide(target, verdict, p, None)['decision'], 'accept')
|
||||
already_full = self.route(CODES[0], dict(self.params, _sd_base_count=1))
|
||||
self.assertTrue(already_full['queued'])
|
||||
self.assertEqual(pms_repo.list_instructions(), [])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
@ -16,9 +16,12 @@ import os
|
|||
import sys
|
||||
import traceback
|
||||
from types import SimpleNamespace
|
||||
from contextlib import nullcontext
|
||||
from unittest.mock import patch
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from app.services import strategy_runner
|
||||
from app.services import param_store # noqa: E402
|
||||
from app.services import strategy_advisor as adv # noqa: E402
|
||||
from app.services import strategy_service as svc # noqa: E402
|
||||
|
|
@ -54,7 +57,7 @@ def _out():
|
|||
|
||||
def _pos(**kw):
|
||||
d = {"ts_code": "600000.SH", "total_qty": 1000, "avail_qty": 1000,
|
||||
"price": 10.0, "price_ok": True, "market_value": 10000.0,
|
||||
"price": 10.0, "avg_cost": 9.5, "price_ok": True, "market_value": 10000.0,
|
||||
"cushion_pct": 0.05, "support_ref": 9.0, "pressure_ref": 11.0,
|
||||
"frozen_reason": "NONE"}
|
||||
d.update(kw)
|
||||
|
|
@ -573,7 +576,7 @@ def _():
|
|||
fake_svc = SimpleNamespace(
|
||||
set_status=lambda sid, s, by="user": rec.status_calls.append((sid, s, by)) or
|
||||
{"ok": True},
|
||||
attach=lambda cfg, by="user": rec.attach_calls.append((cfg, by)) or
|
||||
attach=lambda cfg, by="user", **kw: rec.attach_calls.append((cfg, by)) or
|
||||
{"ok": True, "strategy_id": "S_T"},
|
||||
pause_buy=lambda *a, **k: [], clear_buypause=lambda *a, **k: {"ok": True})
|
||||
orig_led, orig_upd = pms_repo.insert_ledger, pms_repo.update_strategy
|
||||
|
|
@ -581,7 +584,11 @@ def _():
|
|||
pms_repo.update_strategy = lambda sid, **kw: rec.upd_calls.append((sid, kw)) or 1
|
||||
try:
|
||||
out = _out()
|
||||
adv._tend_existing(st, _pos(price=10.6), {"cls": adv.CLS_CLEAR}, True, 0.9,
|
||||
with (patch.object(strategy_runner, "fresh_trail_price", return_value=10.6),
|
||||
patch.object(pms_repo, "list_instructions", return_value=[]),
|
||||
patch.object(pms_repo, "strategy_guard", return_value=nullcontext()),
|
||||
patch.object(pms_repo, "get_strategy", return_value=dict(st, status="ACTIVE"))):
|
||||
adv._tend_existing(st, _pos(price=10.6), {"cls": adv.CLS_CLEAR}, True, 0.9,
|
||||
_prm(), {}, set(), TODAY, False, out, fake_svc)
|
||||
assert rec.status_calls == [("S_G", "CANCELLED", "auto")], rec.status_calls
|
||||
marks = [kw for sid, kw in rec.upd_calls if sid == "S_G"]
|
||||
|
|
@ -625,8 +632,10 @@ def _():
|
|||
pms_repo.get_instruction = orig_get
|
||||
# dry_run 只报不动
|
||||
out3 = _out()
|
||||
adv._tend_existing(st, _pos(price=10.6), {"cls": adv.CLS_CLEAR}, True, 0.9,
|
||||
_prm(), {}, set(), TODAY, True, out3, fake_svc)
|
||||
with (patch.object(strategy_runner, "fresh_trail_price", return_value=10.6),
|
||||
patch.object(pms_repo, "list_instructions", return_value=[])):
|
||||
adv._tend_existing(st, _pos(price=10.6), {"cls": adv.CLS_CLEAR}, True, 0.9,
|
||||
_prm(), {}, set(), TODAY, True, out3, fake_svc)
|
||||
assert out3["handoffs"] and out3["handoffs"][0]["dry_run"] and not rec.status_calls
|
||||
|
||||
|
||||
|
|
@ -635,7 +644,7 @@ def _():
|
|||
rec = _Rec()
|
||||
fake_svc = SimpleNamespace(
|
||||
set_status=lambda sid, s, by="user": {"ok": True},
|
||||
attach=lambda cfg, by="user": {"ok": False, "errors": ["跟踪止盈触发时要卖出, 但当前 T+1 可卖为 0"]},
|
||||
attach=lambda cfg, by="user", **kw: {"ok": False, "errors": ["跟踪止盈触发时要卖出, 但当前 T+1 可卖为 0"]},
|
||||
pause_buy=lambda *a, **k: [], clear_buypause=lambda *a, **k: {"ok": True})
|
||||
manual = {"strategy_id": "S_M", "ts_code": "600000.SH", "type": "GRID",
|
||||
"note": "手工网格", "params": {"upper": 10.5}, "state": {}}
|
||||
|
|
@ -648,16 +657,20 @@ def _():
|
|||
adv._tend_existing(auto_trail, _pos(price=10.6), {"cls": adv.CLS_CLEAR}, True, 0.9,
|
||||
_prm(), {}, set(), TODAY, False, out2, fake_svc)
|
||||
assert any("本轮无需变更" in s["why"] for s in out2["skipped"]), out2["skipped"]
|
||||
# 半途而废: 撤成挂败 → errors 里有「人工处理」, 留痕 HANDOFF/REJECT
|
||||
# 新方案挂载失败,留下接力未完成的原因与 REJECT 回执。
|
||||
orig_led, orig_upd = pms_repo.insert_ledger, pms_repo.update_strategy
|
||||
pms_repo.insert_ledger = lambda **kw: rec.ledger.append(kw) or 1
|
||||
pms_repo.update_strategy = lambda sid, **kw: 1
|
||||
try:
|
||||
grid = dict(manual, note="自动挂载: 网格")
|
||||
out3 = _out()
|
||||
adv._tend_existing(grid, _pos(price=10.6), {"cls": adv.CLS_CLEAR}, True, 0.9,
|
||||
with (patch.object(strategy_runner, "fresh_trail_price", return_value=10.6),
|
||||
patch.object(pms_repo, "list_instructions", return_value=[]),
|
||||
patch.object(pms_repo, "strategy_guard", return_value=nullcontext()),
|
||||
patch.object(pms_repo, "get_strategy", return_value=dict(grid, status="ACTIVE"))):
|
||||
adv._tend_existing(grid, _pos(price=10.6), {"cls": adv.CLS_CLEAR}, True, 0.9,
|
||||
_prm(), {}, set(), TODAY, False, out3, fake_svc)
|
||||
assert any("人工处理" in e for e in out3["errors"]), out3["errors"]
|
||||
assert any("接力未完成" in e for e in out3["errors"]), out3["errors"]
|
||||
assert any(x.get("action") == "HANDOFF" and x.get("verdict") == "REJECT"
|
||||
for x in rec.ledger), rec.ledger
|
||||
finally:
|
||||
|
|
|
|||
|
|
@ -291,6 +291,8 @@ def _route(c, *, values, judge_resp, open_autonomy="propose_only"):
|
|||
p(pms_repo, "insert_instruction", lambda **kw: got["instructions"].append(kw) or 1)
|
||||
p(pms_repo, "insert_proposal", lambda **kw: got["proposals"].append(kw) or 1)
|
||||
p(pms_repo, "list_ledger", lambda **kw: [])
|
||||
p(pms_repo, "get_param", lambda key, *, for_update=False:
|
||||
{"PMS_OPEN_AUTONOMY": open_autonomy, **values}.get(key))
|
||||
_patch_params(p, param_store, {"PMS_EXEC_WINDOW_TDAYS": 3, "PMS_PROPOSAL_TTL_HOURS": 24,
|
||||
"PMS_JUDGE_TICK_BUDGET_SEC": 150, **values})
|
||||
out = {"autonomy": "propose_only", "open_autonomy": open_autonomy, "executed": [],
|
||||
|
|
|
|||
|
|
@ -116,7 +116,8 @@ class DailyPlanTest(unittest.TestCase):
|
|||
p = dict(top_n=1, tiers=['强传导'], include_observe=False, min_score=0,
|
||||
min_sources=0, min_upside=0, theme_cap_local=1, exclude_st=False,
|
||||
rank_by_quality=True, rank_by_upstream=True, observe_if_fund_bull=False)
|
||||
with patch.object(pf, '_params', return_value=p), patch.object(pf, 'get_plan', return_value=pf.parse_plan(raw)):
|
||||
with patch.object(pf, '_params', return_value=p), patch.object(pf, 'get_plan', return_value=pf.parse_plan(raw)), \
|
||||
patch.object(pf, 'read_current_plan', return_value=pf.parse_plan(dict(raw, main=raw['main'][1:]))):
|
||||
self.assertEqual([x['ts_code'] for x in pf.candidates(record=False)['items']], ['600001.SH'])
|
||||
fresh = pf.candidates(record=False, recheck_code='600002.SH')
|
||||
self.assertEqual([x['ts_code'] for x in fresh['items']], ['600002.SH'])
|
||||
|
|
|
|||
|
|
@ -47,6 +47,9 @@ class DailyReviewTests(unittest.TestCase):
|
|||
return int(isinstance(value, dict) and path[2:] in value)
|
||||
connection.create_function('JSON_CONTAINS_PATH', 3, contains_path)
|
||||
event.listen(self.engine, 'connect', register_json_functions)
|
||||
@event.listens_for(self.engine, 'before_cursor_execute', retval=True)
|
||||
def sqlite_lock_syntax(conn, cursor, statement, parameters, context, executemany):
|
||||
return statement.removesuffix(' FOR UPDATE'), parameters
|
||||
self.use(session, 'get_engine', return_value=self.engine)
|
||||
self.use(session, '_check_cooldown')
|
||||
self.use(execution_repo, 'pending_corrections', return_value=0)
|
||||
|
|
@ -61,6 +64,10 @@ class DailyReviewTests(unittest.TestCase):
|
|||
'verdict': {'verdict': 'PASS', 'reason': 'today', 'degraded': False}, 'auto_exec': True}
|
||||
self.scan = self.use(ps, 'scan_and_route', side_effect=lambda **kw: {'ok': True, 'review': copy.deepcopy(self.fresh)})
|
||||
ddl = [
|
||||
'CREATE TABLE pms_ws_state(id INTEGER PRIMARY KEY)',
|
||||
'INSERT INTO pms_ws_state VALUES(1)',
|
||||
'CREATE TABLE pms_runtime_param(param_key TEXT PRIMARY KEY,param_value TEXT)',
|
||||
"INSERT INTO pms_runtime_param VALUES ('PMS_SELF_DECIDE','full')",
|
||||
'CREATE TABLE pms_proposal(id INTEGER PRIMARY KEY,proposal_id TEXT UNIQUE,ts_code TEXT,action TEXT,qty INTEGER,hard_numbers_json TEXT,status TEXT,expire_at TEXT,decided_at TEXT,judge_verdict TEXT,judge_reason TEXT,archived_at TEXT,created_at TEXT)',
|
||||
'CREATE TABLE pms_action_ledger(id INTEGER PRIMARY KEY,ts_code TEXT,decided_at TEXT,action TEXT,arbiter TEXT,verdict TEXT,price_at REAL,hard_numbers_json TEXT,failed_checks_json TEXT,reason TEXT,ref_id TEXT,outcome_scored INTEGER)',
|
||||
'CREATE TABLE pms_instruction(id INTEGER PRIMARY KEY,instruction_id TEXT UNIQUE,origin_type TEXT,origin_id TEXT,ts_code TEXT,action TEXT,side TEXT,qty INTEGER,limit_price REAL,window_tdays INTEGER,status TEXT,exec_qty INTEGER,progress_json TEXT,created_at TEXT,updated_at TEXT,archived_at TEXT)']
|
||||
|
|
@ -84,6 +91,59 @@ class DailyReviewTests(unittest.TestCase):
|
|||
p = p or self.prop()
|
||||
return dr.decide_confirmation(p, 'ACCEPTED', '交易员确认', p['hard_numbers']['proposal_revision'], now=NOW)
|
||||
|
||||
def test_each_waiting_parent_gets_a_turn_after_pending_or_failure(self):
|
||||
for iid in ('Q', 'R'):
|
||||
pms_repo.insert_instruction(instruction_id=iid, origin_type='plan', origin_id=iid,
|
||||
ts_code=CODE, action='OPEN', side='buy', qty=600, limit_price=None,
|
||||
window_tdays=3, status='PROPOSED', progress={'decision_ymd': 20261029})
|
||||
seen = []
|
||||
def decide(**kwargs):
|
||||
iid = kwargs['review_instruction']['instruction_id']
|
||||
seen.append(iid)
|
||||
if iid == 'P':
|
||||
return {'ok': False, 'errors': ['资料暂不可用']}
|
||||
if iid == 'Q':
|
||||
return {'ok': True, 'skipped': [{'by': 'judge_pending'}]}
|
||||
return {'ok': True, 'review': None}
|
||||
self.scan.side_effect = decide
|
||||
for minute in range(4):
|
||||
dr.process(now=NOW + timedelta(minutes=minute))
|
||||
self.assertEqual(seen, ['P', 'Q', 'R', 'P'])
|
||||
self.assertEqual(pms_repo.get_instruction('R')['status'], 'EXPIRED')
|
||||
self.assertEqual(self.parent()['progress']['daily_review']['attempts'], 2)
|
||||
|
||||
def test_concurrent_review_skips_an_active_request_and_recovers_after_worker_loss(self):
|
||||
calls = []
|
||||
def decide(**kwargs):
|
||||
calls.append(kwargs['review_instruction']['instruction_id'])
|
||||
if len(calls) == 1:
|
||||
dr.process(now=NOW + timedelta(seconds=1))
|
||||
return {'ok': True, 'skipped': [{'by': 'judge_pending'}]}
|
||||
self.scan.side_effect = decide
|
||||
self.run_review()
|
||||
self.assertEqual(calls, ['P'])
|
||||
old = self.parent()
|
||||
self.assertIsNotNone(dr._claim_review(old, NOW))
|
||||
self.assertIsNone(dr._claim_review(old, NOW))
|
||||
dr.process(now=NOW + timedelta(seconds=5))
|
||||
self.assertEqual(len(calls), 1)
|
||||
dr.process(now=NOW + timedelta(minutes=5))
|
||||
self.assertEqual(len(calls), 2)
|
||||
|
||||
def test_waiting_parent_beyond_newest_500_is_serviced(self):
|
||||
with session.transaction():
|
||||
for index in range(500):
|
||||
pms_repo.insert_instruction(instruction_id=f'held-{index}', origin_type='plan',
|
||||
origin_id='plan', ts_code=CODE, action='OPEN', side='buy', qty=100,
|
||||
limit_price=None, window_tdays=3, status='WAIT_REVIEW', progress={
|
||||
'daily_review': {'ymd': 20261030, 'status': 'pending'},
|
||||
'trade_correction_hold': True})
|
||||
self.scan.side_effect = lambda **kwargs: {'ok': True, 'review': None}
|
||||
result = self.run_review()
|
||||
self.assertEqual(result['ended'], ['P'])
|
||||
self.assertEqual(len(result['waiting']), 500)
|
||||
self.assertEqual(result['errors'], [])
|
||||
|
||||
def test_async_pending_keeps_parent_for_later_result(self):
|
||||
self.scan.side_effect=None
|
||||
self.scan.return_value={'ok':True,'review':None,
|
||||
|
|
@ -189,6 +249,19 @@ class DailyReviewTests(unittest.TestCase):
|
|||
self.params['PMS_GLOBAL_BUY_HALT'] = True
|
||||
self.run_review(); self.assertEqual(self.parent()['status'], 'WAIT_REVIEW'); self.scan.assert_not_called()
|
||||
|
||||
def test_no_remaining_auto_places_queues_existing_parent(self):
|
||||
session.execute("INSERT INTO pms_runtime_param VALUES ('PMS_SELF_DECIDE_DAILY_MAX','1')")
|
||||
pms_repo.insert_ledger(ts_code='600000.SH', action='OPEN', arbiter='judge', verdict='PASS',
|
||||
price_at=10, ref_id='recent_open', reason='系统采纳:原新建仓')
|
||||
self.fresh['self_decide'] = {'decision': 'accept'}
|
||||
result = self.run_review()
|
||||
self.assertEqual(result['queued'], ['P'])
|
||||
self.assertEqual(self.parent()['status'], 'WAIT_REVIEW')
|
||||
self.assertIn('上限', self.prop()['hard_numbers']['reason'])
|
||||
self.assertEqual(pms_repo.system_pass_count_today('2000-01-01'), 1)
|
||||
self.assertTrue(self.accept()['ok'])
|
||||
self.assertEqual(pms_repo.system_pass_count_today('2000-01-01'), 1)
|
||||
|
||||
def test_auto_accept_is_counted_in_existing_daily_limit(self):
|
||||
self.fresh['self_decide'] = {'decision': 'accept'}
|
||||
self.run_review()
|
||||
|
|
|
|||
|
|
@ -223,4 +223,160 @@ class ZBookTest(unittest.TestCase):
|
|||
cash.assert_called_once_with('original', 'buy', 2330., 0, '1')
|
||||
|
||||
|
||||
class DailyQuantityTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
from app.services import command_service, market, param_store, portfolio
|
||||
self.now = datetime(2026, 11, 18, 10, 10)
|
||||
self.code = '600000.SH'
|
||||
self.parent = {'instruction_id': 'reduction', 'ts_code': self.code, 'action': 'TRIM',
|
||||
'side': 'sell', 'qty': 3000, 'exec_qty': 0, 'status': 'DISPATCHED', 'origin_type': 'plan',
|
||||
'progress': {'deadline': '2026-11-20', 'is_command': True, 'children': []}}
|
||||
self.orders = []
|
||||
self.position = {'ts_code': self.code, 'total_qty': 5000, 'avail_qty': 5000}
|
||||
self.use(pms_repo, 'list_instructions', return_value=[self.parent])
|
||||
self.use(pms_repo, 'get_instruction', return_value=self.parent)
|
||||
self.use(qmt_repo, 'orders_for_parent', side_effect=lambda _: copy.deepcopy(self.orders))
|
||||
self.use(qmt_repo, 'get_order', side_effect=lambda iid: next(
|
||||
(r for r in self.orders if r['instruction_id'] == iid), None))
|
||||
self.use(execution_repo, 'pending_corrections', return_value=0)
|
||||
self.use(execution_repo, 'pending_trades', return_value=[])
|
||||
self.use(dispatcher, 'mode', return_value='ws')
|
||||
self.use(param_store, 'get_bool', side_effect=lambda k, d=False: k == 'PMS_EXEC_REPRICE_ENABLED' or d)
|
||||
self.use(param_store, 'get_int', side_effect=lambda k, d=0: d)
|
||||
self.use(param_store, 'get_float', side_effect=lambda k, d=0.: d)
|
||||
self.use(param_store, 'get', side_effect=lambda k, d=None: d)
|
||||
self.use(command_service, 'effective_stock_params', return_value={})
|
||||
self.use(portfolio, 'positions_view', return_value={'positions': [self.position],
|
||||
'params': {'exec_halt': False, 'buy_halt': False}, 'sector_ready': True})
|
||||
self.use(market, 'day_snapshot', return_value={'price': 10, 'low': 9, 'high': 11})
|
||||
self.use(market, 'execution_quote', return_value={'source': '测试', 'kind': 'level1',
|
||||
'last': 10, 'bid': 9.99, 'ask': 10, 'as_of': self.now.isoformat()+'+08:00',
|
||||
'limit_down': 9, 'limit_up': 11})
|
||||
|
||||
def use(self, obj, name, **kwargs):
|
||||
hook = patch.object(obj, name, **kwargs)
|
||||
self.addCleanup(hook.stop)
|
||||
return hook.start()
|
||||
|
||||
def child(self, qty, filled, *, day=None, status='CANCELLED', progress=True):
|
||||
ref = f"reduction_D{len(self.orders)+1:02d}"
|
||||
day = day or self.now
|
||||
self.orders.append({'instruction_id': ref, 'parent_id': 'reduction', 'qty': qty,
|
||||
'cum_qty': filled, 'status': status, 'created_at': day})
|
||||
self.parent['exec_qty'] += filled
|
||||
if progress:
|
||||
self.parent['progress']['children'].append({'ref': ref, 'qty': qty,
|
||||
'ymd': int(day.strftime('%Y%m%d'))})
|
||||
|
||||
def quantity(self):
|
||||
result = executor.run_tick(now=self.now, dry_run=True)
|
||||
self.assertEqual(result['errors'], [])
|
||||
self.assertEqual(result['rejected'], [])
|
||||
return sum(item['qty'] for item in result['fired'])
|
||||
|
||||
def test_outdated_execution_read_cannot_overwrite_a_new_review_claim(self):
|
||||
from app.services import daily_buy_review
|
||||
self.parent.update(action='OPEN', side='buy', status='PROPOSED')
|
||||
self.use(pms_repo, 'get_instruction', side_effect=lambda _: copy.deepcopy(self.parent))
|
||||
self.use(pms_repo, 'instruction_buy_stop', return_value=None)
|
||||
update = self.use(pms_repo, 'update_instruction')
|
||||
|
||||
def gate(_, *, now):
|
||||
self.parent.update(status='WAIT_REVIEW')
|
||||
self.parent['progress']['daily_review'] = {'ymd': 20261118, 'status': 'pending', 'attempts': 1}
|
||||
return '旧买入安排需完成当日复核'
|
||||
self.use(daily_buy_review, 'blocker', side_effect=gate)
|
||||
result = executor.run_tick(now=self.now)
|
||||
self.assertEqual(result['errors'], [])
|
||||
self.assertTrue(result['waited'])
|
||||
update.assert_not_called()
|
||||
|
||||
def test_partial_fill_does_not_shrink_opening_allotment(self):
|
||||
self.assertEqual(self.quantity(), 1000)
|
||||
self.child(1000, 700)
|
||||
self.assertEqual(self.quantity(), 300)
|
||||
self.assertEqual(self.quantity(), 300)
|
||||
self.child(300, 300, status='FILLED', progress=False)
|
||||
self.assertEqual(self.quantity(), 0)
|
||||
|
||||
def test_yesterday_and_missing_progress_are_counted_on_their_own_day(self):
|
||||
self.child(600, 600, day=datetime(2026, 11, 17), status='FILLED')
|
||||
self.child(800, 300, progress=False)
|
||||
self.assertEqual(self.quantity(), 500)
|
||||
self.orders[-1]['status'] = 'PARTIAL'
|
||||
self.assertEqual(self.quantity(), 0)
|
||||
self.assertEqual(executor.dispatch_allowance(self.parent, self.now, 3), 0)
|
||||
self.orders[-1]['status'] = 'CANCELLED'
|
||||
self.parent['exec_qty'] = 600
|
||||
self.assertEqual(self.quantity(), 0)
|
||||
self.parent['exec_qty'] = 900
|
||||
self.assertEqual(self.quantity(), 500)
|
||||
|
||||
def test_partial_lot_and_full_odd_lot_exit(self):
|
||||
self.child(1000, 725)
|
||||
self.assertEqual(self.quantity(), 200)
|
||||
self.parent.update(action='EXIT', qty=1000)
|
||||
self.position.update(total_qty=275, avail_qty=275)
|
||||
self.parent['progress']['urgent'] = True
|
||||
self.assertEqual(self.quantity(), 100)
|
||||
self.parent['progress']['deadline'] = '2026-11-18'
|
||||
self.assertEqual(self.quantity(), 275)
|
||||
self.parent['progress']['urgent'] = False
|
||||
self.assertEqual(self.quantity(), 275)
|
||||
|
||||
def test_fill_between_quote_and_dispatch_waits_then_the_next_tick_uses_balance(self):
|
||||
from app.services import execution_orders
|
||||
clock = self.use(dispatcher, 'datetime', wraps=datetime)
|
||||
clock.now.return_value = self.now
|
||||
self.use(pms_repo, 'account_decision_guard', side_effect=nullcontext)
|
||||
outlet = self.use(dispatcher, '_ws', return_value={'ok': True})
|
||||
|
||||
def other_tick_settles(_):
|
||||
self.child(1000, 700, progress=False)
|
||||
return 'reduction_D02'
|
||||
self.use(execution_orders, 'next_child_id', side_effect=other_tick_settles)
|
||||
result = executor.run_tick(now=self.now)
|
||||
self.assertEqual(result['errors'], [])
|
||||
self.assertEqual(result['fired'], [])
|
||||
self.assertIn('300股', result['waited'][0]['reason'])
|
||||
outlet.assert_not_called()
|
||||
self.assertEqual(self.quantity(), 300)
|
||||
|
||||
def test_rotation_and_urgent_parents_keep_their_execution_rules(self):
|
||||
from app.services import execution_orders
|
||||
self.use(execution_orders, 'enabled', return_value=False)
|
||||
self.use(execution_orders, 'account_guard', side_effect=nullcontext)
|
||||
ordinary_guard = self.use(pms_repo, 'account_decision_guard')
|
||||
self.use(dispatcher, '_ws', return_value={'ok': True})
|
||||
clock = self.use(dispatcher, 'datetime', wraps=datetime)
|
||||
clock.now.return_value = self.now
|
||||
for field, value in [('execution_profile', 'rotation'), ('urgent', True)]:
|
||||
self.parent['progress'][field] = value
|
||||
result = dispatcher.dispatch(instruction_id='reduction_D01', parent_id='reduction',
|
||||
ts_code=self.code, side='sell', qty=3000, limit_price=10, valid_until=self.now)
|
||||
self.assertTrue(result['ok'], result)
|
||||
del self.parent['progress'][field]
|
||||
ordinary_guard.assert_not_called()
|
||||
|
||||
def test_dispatch_rechecks_day_usage_after_quote_and_retains_idempotent_retry(self):
|
||||
self.child(1000, 700, progress=False)
|
||||
clock = self.use(dispatcher, 'datetime', wraps=datetime)
|
||||
clock.now.return_value = self.now
|
||||
guard = self.use(pms_repo, 'account_decision_guard', side_effect=nullcontext)
|
||||
outlet = self.use(dispatcher, '_ws', return_value={'ok': True})
|
||||
from app.services import execution_orders
|
||||
self.use(execution_orders, 'enabled', return_value=False)
|
||||
def send(qty, iid='reduction_D02'):
|
||||
return dispatcher.dispatch(instruction_id=iid, parent_id='reduction', ts_code=self.code,
|
||||
side='sell', qty=qty, limit_price=10, valid_until=self.now.replace(minute=20))
|
||||
stale = send(1000)
|
||||
self.assertFalse(stale['ok'])
|
||||
self.assertTrue(stale['deferred'])
|
||||
self.assertIn('300股', stale['error'])
|
||||
outlet.assert_not_called()
|
||||
self.assertTrue(send(300)['ok'])
|
||||
self.assertTrue(send(1000, 'reduction_D01')['ok'])
|
||||
self.assertEqual(guard.call_count, 3)
|
||||
|
||||
|
||||
if __name__ == '__main__': unittest.main()
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ class StrategyOutboxTest(unittest.TestCase):
|
|||
try:
|
||||
with ExitStack() as stack:
|
||||
from contextlib import nullcontext
|
||||
from app.services import execution_orders
|
||||
from app.services import execution_orders, rotation_service
|
||||
from app.repo import execution_repo
|
||||
stack.enter_context(patch.object(execution_orders, 'account_guard', side_effect=nullcontext))
|
||||
stack.enter_context(patch.object(execution_repo, 'pending_corrections', return_value=0))
|
||||
|
|
@ -79,6 +79,7 @@ class StrategyOutboxTest(unittest.TestCase):
|
|||
(qmt_repo, 'mark_sent', {}),
|
||||
(pms_repo, 'get_instruction', {'side_effect': read}),
|
||||
(execution_conflicts, 'queued_buy_blocker', {'return_value': None}),
|
||||
(rotation_service, 'order_control_blocker', {'return_value': None}),
|
||||
]:
|
||||
stack.enter_context(patch.object(obj, name, **kw))
|
||||
send = stack.enter_context(patch.object(runner, '_send', new_callable=AsyncMock))
|
||||
|
|
|
|||
|
|
@ -34,6 +34,303 @@ def state(snap=None):
|
|||
return m.transition(m.initial_state(), snap, micro_qty=0, original_qty=0, budget=100000)
|
||||
|
||||
|
||||
class MicroBuyRestrictionTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
from app.core import strategy_book as books
|
||||
from app.repo import pms_repo
|
||||
from app.services import rotation_service
|
||||
self.enterContext(patch.object(rotation_service.store, 'account_guard', side_effect=nullcontext))
|
||||
self.commands = []
|
||||
|
||||
def effective_commands(*args, **kwargs):
|
||||
self.assertEqual(books.CURRENT.get(), books.ORIGINAL)
|
||||
return list(self.commands)
|
||||
|
||||
self.enterContext(patch.object(pms_repo, 'list_effective_stock_params', side_effect=effective_commands))
|
||||
|
||||
def command(self, code, kind):
|
||||
self.commands.append({'id': len(self.commands)+1, 'ts_code': code, 'cmd_type': kind})
|
||||
|
||||
def parent(self, code, side='buy'):
|
||||
return {'instruction_id': code+'-'+side, 'ts_code': code, 'book_id': 'micro30',
|
||||
'side': side, 'action': 'OPEN' if side == 'buy' else 'EXIT', 'qty': 300,
|
||||
'exec_qty': 100, 'status': 'DISPATCHED', 'origin_type': 'rotation',
|
||||
'progress': {'execution_profile': 'rotation', 'rule_version': m.EXECUTION_RULE,
|
||||
'deadline': '2026-11-03'}}
|
||||
|
||||
def test_entry_preserves_other_members_and_authorized_sales(self):
|
||||
from app.services import rotation_service
|
||||
snap = snapshot()
|
||||
basket = state(snap)
|
||||
held, frozen, black = '000001.SZ', '600000.SH', '600001.SH'
|
||||
self.command(held, 'FREEZE_STOCK')
|
||||
self.command(frozen, 'FREEZE_STOCK')
|
||||
self.command(black, 'BLACKLIST_ADD')
|
||||
quotes = {r['code']: {'price': 10.} for r in snap['members']}
|
||||
quotes[held] = {'price': 10.}
|
||||
def intents():
|
||||
orders, _ = m.orders(basket, snap, original=[{'ts_code': held, 'total_qty': 100}],
|
||||
micro=[], quotes=quotes, cash=100000, account_mv=1000, cap_amount=110000,
|
||||
buy_restrictions=rotation_service.micro_buy_restrictions())
|
||||
return {(r['ts_code'], r['side']) for r in orders}
|
||||
|
||||
allowed = intents()
|
||||
expected = {(r['code'], 'buy') for r in snap['members'] if r['code'] not in (frozen, black)}
|
||||
self.assertEqual(allowed, expected | {(held, 'sell')})
|
||||
self.command(frozen, 'UNFREEZE_STOCK')
|
||||
self.assertEqual(intents(), allowed | {(frozen, 'buy')})
|
||||
self.command(black, 'BLACKLIST_REMOVE')
|
||||
self.assertEqual(intents(), allowed | {(frozen, 'buy'), (black, 'buy')})
|
||||
|
||||
def test_live_partial_buy_is_cancelled_without_replacing_its_remainder(self):
|
||||
from app.services import rotation_service, rotation_execution as execution, executor, execution_orders
|
||||
parent = self.parent('600000.SH')
|
||||
self.command(parent['ts_code'], 'FREEZE_STOCK')
|
||||
now = dt.datetime(2026, 11, 3, 9, 40)
|
||||
with patch.object(executor, 'cancel_instruction', return_value={'ok': True}) as cancel, \
|
||||
patch.object(execution_orders, 'request_replace') as reprice, \
|
||||
patch.object(execution, 'send_intent') as send:
|
||||
results, waits = execution.refresh_orders([parent], {parent['ts_code']: {'price': 10.}}, now,
|
||||
'version', {parent['ts_code']}, rotation_service.micro_buy_restrictions())
|
||||
self.assertTrue(waits)
|
||||
self.assertEqual(results, [])
|
||||
self.assertEqual(cancel.call_args.args, (parent['instruction_id'],))
|
||||
self.assertIn('冻结', cancel.call_args.kwargs['reason'])
|
||||
reprice.assert_not_called()
|
||||
send.assert_not_called()
|
||||
|
||||
def test_user_command_cancels_only_live_micro_buys_in_that_stock(self):
|
||||
from app.services import rotation_service, executor
|
||||
from app.repo import pms_repo
|
||||
from app.core import strategy_book as books
|
||||
parent = self.parent('600000.SH')
|
||||
def buys(**filters):
|
||||
self.assertEqual(books.CURRENT.get(), books.MICRO)
|
||||
self.assertEqual((filters['side'], filters['ts_code']), ('buy', parent['ts_code']))
|
||||
self.assertIn('DISPATCHED', filters['statuses'])
|
||||
return [parent]
|
||||
def get_command(command_id):
|
||||
self.assertEqual(books.CURRENT.get(), books.ORIGINAL)
|
||||
return {'ts_code': parent['ts_code'], 'status': 'EFFECTIVE'}
|
||||
with patch.object(pms_repo, 'get_command', side_effect=get_command), \
|
||||
patch.object(pms_repo, 'list_instructions', side_effect=buys), \
|
||||
patch.object(executor, 'cancel_instruction', return_value={'ok': True}) as cancel:
|
||||
rotation_service.on_user_command('BLACKLIST_ADD', 'cmd', 'user')
|
||||
cancel.assert_called_once_with(parent['instruction_id'], reason='用户单股禁买命令 cmd', arbiter='user')
|
||||
|
||||
def test_next_poll_retries_failed_cancel_of_legacy_parent_only_on_buy_side(self):
|
||||
from app.services import rotation_service, rotation_execution as execution, executor
|
||||
from app.repo import pms_repo
|
||||
code = '600000.SH'
|
||||
old = self.parent(code)
|
||||
old['progress']['rule_version'] = m.RULE
|
||||
same_stock_sell = self.parent(code, side='sell')
|
||||
other_buy = self.parent('600001.SH')
|
||||
self.command(code, 'FREEZE_STOCK')
|
||||
|
||||
def active(**filters):
|
||||
return [old] if filters.get('side') == 'buy' else [old, same_stock_sell, other_buy]
|
||||
|
||||
with patch.object(pms_repo, 'get_command', return_value={'ts_code': code, 'status': 'EFFECTIVE'}), \
|
||||
patch.object(pms_repo, 'list_instructions', side_effect=active), \
|
||||
patch.object(executor, 'cancel_instruction', side_effect=[{'ok': False}, {'ok': True}]) as cancel:
|
||||
with self.assertRaises(RuntimeError):
|
||||
rotation_service.on_user_command('FREEZE_STOCK', 'cmd', 'user')
|
||||
execution.cancel_expired(dt.datetime(2026, 11, 3, 9, 35))
|
||||
self.assertEqual([call.args[0] for call in cancel.call_args_list],
|
||||
[old['instruction_id'], old['instruction_id']])
|
||||
self.assertEqual([call.kwargs['arbiter'] for call in cancel.call_args_list], ['user', 'system'])
|
||||
|
||||
def test_original_final_send_distinguishes_rotation_stop_from_account_buy_ban(self):
|
||||
from app.services import rotation_service as r, execution_orders as e
|
||||
from app.repo import pms_repo, qmt_repo
|
||||
code = '600000.SH'
|
||||
parent = dict(self.parent(code),book_id='original',origin_type='strategy',action='T0_ROUND',progress={'leg':'open'})
|
||||
child = {'parent_id':parent['instruction_id'],'ts_code':code,'side':'buy'}
|
||||
phase = {'rotation':{'phase':'original'}}
|
||||
self.enterContext(patch.object(r.settings,'INFRA_STRATEGY_BOOK_SCHEMA',True))
|
||||
self.enterContext(patch.object(r.settings,'PMS_TABLE_PREFIX',''))
|
||||
self.enterContext(patch.object(e,'account_guard',side_effect=nullcontext))
|
||||
self.enterContext(patch.object(e,'correction_blocker',return_value=None))
|
||||
self.enterContext(patch.object(pms_repo,'get_param',return_value=None))
|
||||
self.enterContext(patch.object(pms_repo,'get_instruction',return_value=parent))
|
||||
self.enterContext(patch.object(qmt_repo,'get_order',return_value=child))
|
||||
self.enterContext(patch.object(qmt_repo,'mark_send_attempt',return_value=True))
|
||||
self.enterContext(patch.object(r.store,'get',return_value=(phase,1)))
|
||||
def marked():
|
||||
return e.prepare_send('child')['marked']
|
||||
self.assertTrue(marked())
|
||||
phase['rotation']['phase'] = 'entering_micro'
|
||||
self.assertFalse(marked())
|
||||
parent['progress']['leg'] = 'close'
|
||||
self.assertTrue(marked())
|
||||
for stop, release in [('FREEZE_STOCK','UNFREEZE_STOCK'), ('BLACKLIST_ADD','BLACKLIST_REMOVE')]:
|
||||
self.command(code, stop)
|
||||
for leg in ('open','close'):
|
||||
parent['progress']['leg'] = leg
|
||||
self.assertFalse(marked())
|
||||
child['side'] = parent['side'] = 'sell'
|
||||
self.assertTrue(marked())
|
||||
child['side'] = parent['side'] = 'buy'
|
||||
self.command(code, release)
|
||||
self.assertTrue(marked())
|
||||
|
||||
def test_queued_then_frozen_then_released_at_final_send(self):
|
||||
from app.services import execution_orders as eo, execution_conflicts as conflicts, rotation_service
|
||||
from app.services import param_store, risk_exit
|
||||
from app.repo import pms_repo, qmt_repo
|
||||
from config.settings import settings
|
||||
parent = self.parent('600000.SH')
|
||||
order = {'instruction_id': 'child', 'parent_id': parent['instruction_id'],
|
||||
'ts_code': parent['ts_code'], 'side': 'buy', 'status': 'QUEUED'}
|
||||
self.enterContext(patch.object(settings, 'PMS_TABLE_PREFIX', ''))
|
||||
self.enterContext(patch.object(pms_repo, 'get_param', side_effect=lambda key, **kwargs:
|
||||
{'PMS_ROTATION_MODE': 'auto', 'PMS_ROTATION_CONTROL': 'run'}.get(key)))
|
||||
self.enterContext(patch.object(settings, 'INFRA_STRATEGY_BOOK_SCHEMA', True))
|
||||
self.enterContext(patch.object(eo, 'account_guard', side_effect=nullcontext))
|
||||
self.enterContext(patch.object(eo, 'correction_blocker', return_value=None))
|
||||
self.enterContext(patch.object(risk_exit, 'buy_blocker', return_value=None))
|
||||
self.enterContext(patch.object(param_store, 'get_bool', return_value=False))
|
||||
self.enterContext(patch.object(param_store, 'get', side_effect=lambda key, default=None: default))
|
||||
self.enterContext(patch.object(rotation_service, 'configuration', return_value={'mode': 'auto', 'control': 'run'}))
|
||||
self.enterContext(patch.object(rotation_service.store, 'get', return_value=({'rotation': {'phase': 'micro'}}, 1)))
|
||||
self.enterContext(patch.object(pms_repo, 'get_instruction', return_value=parent))
|
||||
self.enterContext(patch.object(pms_repo, 'instruction_buy_stop', return_value=False))
|
||||
self.enterContext(patch.object(pms_repo, 'list_instructions', return_value=[]))
|
||||
self.enterContext(patch.object(qmt_repo, 'get_order', return_value=order))
|
||||
mark = self.enterContext(patch.object(qmt_repo, 'mark_send_attempt', return_value=True))
|
||||
self.assertFalse(conflicts.queued_buy_blocker(order))
|
||||
for stop, resume in (('FREEZE_STOCK', 'UNFREEZE_STOCK'), ('BLACKLIST_ADD', 'BLACKLIST_REMOVE')):
|
||||
self.command(order['ts_code'], stop)
|
||||
self.assertTrue(conflicts.queued_buy_blocker(order))
|
||||
self.assertFalse(eo.prepare_send('child')['marked'])
|
||||
mark.assert_not_called()
|
||||
self.command(order['ts_code'], resume)
|
||||
self.assertFalse(conflicts.queued_buy_blocker(order))
|
||||
self.assertTrue(eo.prepare_send('child')['marked'])
|
||||
mark.assert_called_once_with('child')
|
||||
mark.reset_mock()
|
||||
|
||||
|
||||
def test_new_global_halt_blocks_even_exit_sell_but_preserves_order_cancellation(self):
|
||||
from app.repo import pms_repo, qmt_repo
|
||||
from app.services import dispatcher, execution_orders as eo, param_store, rotation_service
|
||||
from config.settings import settings
|
||||
parent = self.parent('600000.SH', side='sell')
|
||||
order = {'instruction_id': 'child', 'parent_id': parent['instruction_id'],
|
||||
'ts_code': parent['ts_code'], 'side': 'sell', 'status': 'QUEUED'}
|
||||
authoritative = {'PMS_ROTATION_MODE': 'auto', 'PMS_ROTATION_CONTROL': 'exit_disable'}
|
||||
def current_param(key, *, for_update=False):
|
||||
self.assertTrue(for_update)
|
||||
return authoritative.get(key)
|
||||
self.enterContext(patch.object(settings, 'INFRA_STRATEGY_BOOK_SCHEMA', True))
|
||||
self.enterContext(patch.object(settings, 'PMS_TABLE_PREFIX', ''))
|
||||
self.enterContext(patch.object(param_store, 'get_bool', return_value=False))
|
||||
self.enterContext(patch.object(pms_repo, 'get_param', side_effect=current_param))
|
||||
self.enterContext(patch.object(eo, 'account_guard', side_effect=nullcontext))
|
||||
self.enterContext(patch.object(eo, 'correction_blocker', return_value=None))
|
||||
self.enterContext(patch.object(pms_repo, 'get_instruction', return_value=parent))
|
||||
self.enterContext(patch.object(qmt_repo, 'get_order', return_value=order))
|
||||
self.enterContext(patch.object(rotation_service, 'configuration',
|
||||
return_value={'mode': 'auto', 'control': 'exit_disable'}))
|
||||
mark = self.enterContext(patch.object(qmt_repo, 'mark_send_attempt', return_value=True))
|
||||
self.assertTrue(eo.prepare_send('child')['marked'])
|
||||
mark.reset_mock()
|
||||
authoritative['PMS_GLOBAL_EXEC_HALT'] = 'true'
|
||||
result = eo.prepare_send('child')
|
||||
self.assertFalse(result['marked'])
|
||||
self.assertIn('暂停', result['blocked'])
|
||||
mark.assert_not_called()
|
||||
with patch.object(dispatcher, 'mode', return_value='ws'), \
|
||||
patch.object(dispatcher, 'channel_status', return_value={'online': True}), \
|
||||
patch.object(qmt_repo, 'request_cancel', return_value=1) as request:
|
||||
cancelled = dispatcher.cancel(instruction_id=parent['instruction_id'])
|
||||
self.assertTrue(cancelled['ok'])
|
||||
self.assertTrue(cancelled['cancel_pending'])
|
||||
self.assertEqual(request.call_args.kwargs['parent_id'], parent['instruction_id'])
|
||||
|
||||
|
||||
class StockRestrictionIssueTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
from sqlalchemy import create_engine, event, text
|
||||
from app.db import session
|
||||
from app.repo import pms_repo
|
||||
from app.services import param_store
|
||||
from config.settings import settings
|
||||
self.engine = create_engine('sqlite:///:memory:')
|
||||
self.addCleanup(self.engine.dispose)
|
||||
self.enterContext(patch.object(session, 'get_engine', return_value=self.engine))
|
||||
self.enterContext(patch.object(session, '_check_cooldown'))
|
||||
self.enterContext(patch.object(settings, 'INFRA_STRATEGY_BOOK_SCHEMA', True))
|
||||
self.enterContext(patch.object(settings, 'PMS_TABLE_PREFIX', ''))
|
||||
self.enterContext(patch.object(param_store, 'get_bool', return_value=False))
|
||||
self.enterContext(patch.object(pms_repo, 'list_instructions', return_value=[]))
|
||||
|
||||
@event.listens_for(self.engine, 'begin')
|
||||
def transaction_started(conn):
|
||||
conn.info['account_locked'] = False
|
||||
|
||||
@event.listens_for(self.engine, 'before_cursor_execute', retval=True)
|
||||
def sqlite_dialect(conn, cursor, statement, params, context, many):
|
||||
# SQLite内存事务用写操作取得锁。保留生产的加锁次序和全部业务SQL。
|
||||
if "doc_id='account-execution' FOR UPDATE" in statement:
|
||||
conn.info['account_locked'] = True
|
||||
statement = ("UPDATE pms_rotation_document SET revision=revision "
|
||||
"WHERE doc_id='account-execution' RETURNING doc_id")
|
||||
else:
|
||||
statement = statement.replace(' FOR UPDATE', '')
|
||||
statement = statement.replace('ON DUPLICATE KEY UPDATE doc_id=VALUES(doc_id)',
|
||||
'ON CONFLICT(doc_id) DO NOTHING')
|
||||
statement = statement.replace('ON DUPLICATE KEY UPDATE updated_at =',
|
||||
'ON CONFLICT(book_id,ts_code) DO UPDATE SET updated_at =')
|
||||
if statement.startswith('UPDATE pms_command SET status') and 'EFFECTIVE' in params:
|
||||
self.assertTrue(conn.info['account_locked'])
|
||||
return statement, params
|
||||
|
||||
with self.engine.begin() as conn:
|
||||
conn.execute(text('CREATE TABLE pms_command (id INTEGER PRIMARY KEY, book_id TEXT, '
|
||||
'command_id TEXT UNIQUE, cmd_class TEXT, cmd_type TEXT, ts_code TEXT, params_json TEXT, '
|
||||
'status TEXT, progress_json TEXT, issued_by TEXT, issued_at TEXT, done_at TEXT, note TEXT, archived_at TEXT)'))
|
||||
conn.execute(text('CREATE TABLE pms_position (book_id TEXT, ts_code TEXT, status TEXT, '
|
||||
'updated_at TEXT, frozen_reason TEXT, PRIMARY KEY(book_id,ts_code))'))
|
||||
conn.execute(text('CREATE TABLE pms_rotation_document (doc_id TEXT PRIMARY KEY, kind TEXT, '
|
||||
'payload_json TEXT, revision INTEGER, updated_at TEXT)'))
|
||||
|
||||
def issue(self, kind):
|
||||
from app.services import command_service
|
||||
return command_service.issue(kind, {'ts_code': '600000.SH'})
|
||||
|
||||
def test_failed_new_effective_status_restores_prior_freeze_and_position(self):
|
||||
from sqlalchemy import text
|
||||
from app.repo import pms_repo
|
||||
from app.services import command_service
|
||||
first = self.issue('FREEZE_STOCK')
|
||||
self.assertTrue(first['ok'], first)
|
||||
with self.engine.begin() as conn:
|
||||
conn.execute(text("CREATE TRIGGER refuse_effective BEFORE UPDATE OF status ON pms_command "
|
||||
"WHEN NEW.status='EFFECTIVE' BEGIN SELECT RAISE(IGNORE); END"))
|
||||
for kind in ('FREEZE_STOCK', 'UNFREEZE_STOCK'):
|
||||
with self.subTest(kind=kind):
|
||||
result = self.issue(kind)
|
||||
self.assertFalse(result['ok'], result)
|
||||
self.assertEqual(pms_repo.get_command(result['command_id'])['status'], 'CANCELLED')
|
||||
self.assertEqual(pms_repo.get_command(first['command_id'])['status'], 'EFFECTIVE')
|
||||
self.assertTrue(command_service.effective_stock_params()['600000.SH']['frozen'])
|
||||
self.assertEqual(pms_repo.get_position('600000.SH')['frozen_reason'], 'COMMAND_HALT')
|
||||
|
||||
def test_releasing_one_restriction_keeps_the_other_effective(self):
|
||||
from app.services import command_service, rotation_service
|
||||
for kind, frozen, black in [('FREEZE_STOCK', True, False), ('BLACKLIST_ADD', True, True),
|
||||
('BLACKLIST_REMOVE', True, False), ('BLACKLIST_ADD', True, True),
|
||||
('UNFREEZE_STOCK', False, True), ('BLACKLIST_REMOVE', False, False)]:
|
||||
with self.subTest(kind=kind):
|
||||
result = self.issue(kind)
|
||||
self.assertTrue(result['ok'], result)
|
||||
flags = command_service.effective_stock_params()['600000.SH']
|
||||
self.assertEqual((flags.get('frozen', False), flags.get('black', False)), (frozen, black))
|
||||
self.assertEqual('600000.SH' in rotation_service.micro_buy_restrictions(), frozen or black)
|
||||
|
||||
|
||||
class SnapshotTests(unittest.TestCase):
|
||||
def test_v2_remains_compatible_without_candidates(self):
|
||||
snap = snapshot(rule=m.RULE)
|
||||
|
|
@ -156,7 +453,7 @@ class TargetTests(unittest.TestCase):
|
|||
|
||||
class ServiceBoundaries(unittest.TestCase):
|
||||
def prepare(self, snap, funds, scale, *, prepare_only=True, fail_limits=False,
|
||||
missing_position=False, refresh=None):
|
||||
missing_position=False, refresh=None, configure=None, now=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'],
|
||||
|
|
@ -167,6 +464,7 @@ class ServiceBoundaries(unittest.TestCase):
|
|||
with ExitStack() as stack:
|
||||
for obj, name, value in (
|
||||
(r, 'configuration', config), (r, 'documents_ready', True), (r, 'activation_errors', []),
|
||||
(r.param_store, 'execution_halted', False),
|
||||
(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}),
|
||||
|
|
@ -177,7 +475,7 @@ class ServiceBoundaries(unittest.TestCase):
|
|||
(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', []),
|
||||
(r.pms_repo, 'active_strategies', []), (command_service, 'effective_stock_params', {}),
|
||||
(execution, 'pending_instructions', [])):
|
||||
stack.enter_context(patch.object(obj, name, return_value=value))
|
||||
stack.enter_context(patch.object(execution, 'refresh_orders',
|
||||
|
|
@ -190,9 +488,53 @@ class ServiceBoundaries(unittest.TestCase):
|
|||
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)
|
||||
if configure:
|
||||
configure(stack, stored, config)
|
||||
result = r._tick(now or dt.datetime(2026, 11, 3, 9, 31, tzinfo=r.TZ), prepare_only=prepare_only)
|
||||
return stored, result, stage.call_count
|
||||
|
||||
def test_all_first_choices_frozen_keeps_original_exposure(self):
|
||||
from app.services import command_service
|
||||
snap = snapshot()
|
||||
def configure(stack, stored, config):
|
||||
flags = {row['code']:{'frozen':True} for row in snap['members']}
|
||||
stack.enter_context(patch.object(command_service,'effective_stock_params',return_value=flags))
|
||||
saved, result, writes = self.prepare(snap,100000,100000,configure=configure)
|
||||
self.assertEqual(saved['rotation']['phase'],'original')
|
||||
self.assertIn('禁买',result['blocked'][0])
|
||||
self.assertEqual(writes,0)
|
||||
|
||||
def test_afternoon_tick_sells_micro_without_waiting_for_original_t0(self):
|
||||
from app.db import session as db
|
||||
from app.services import rotation_service as r, rotation_execution as execution, execution_orders, executor
|
||||
snap = snapshot()
|
||||
code = snap['members'][0]['code']
|
||||
sold = []
|
||||
def configure(stack, stored, config):
|
||||
stored['rotation'] = state(snap)
|
||||
config['control'] = 'exit_disable'
|
||||
holding = {'ts_code':code,'total_qty':300,'avail_qty':200}
|
||||
stack.enter_context(patch.object(r,'positions',side_effect=lambda b: [holding] if b=='micro30' else []))
|
||||
old = {'instruction_id':'old','book_id':'micro30','ts_code':'600001.SH','side':'buy'}
|
||||
stack.enter_context(patch.object(execution,'pending_instructions',return_value=[old]))
|
||||
stack.enter_context(patch.object(executor,'cancel_instruction',return_value={'ok':False}))
|
||||
stack.enter_context(patch.object(r,'_micro_unsettled',return_value=True))
|
||||
stack.enter_context(patch.object(r.pms_repo,'active_strategies',return_value=[{'type':'T0','state':{'open_leg':'sell'}}]))
|
||||
stack.enter_context(patch.object(r,'_limits',return_value={code:{'up_limit':11,'down_limit':9}}))
|
||||
stack.enter_context(patch.object(execution_orders,'quote_order',return_value={'action':'FIRE','limit_price':10}))
|
||||
stack.enter_context(patch.object(db,'savepoint',side_effect=nullcontext))
|
||||
def send(intent, price, now, version):
|
||||
sold.append(intent)
|
||||
return {'ok':True}
|
||||
stack.enter_context(patch.object(execution,'send_intent',side_effect=send))
|
||||
now = dt.datetime(2026,11,3,14,12,tzinfo=r.TZ)
|
||||
saved, result, _ = self.prepare(snap,100000,100000,prepare_only=False,configure=configure,now=now)
|
||||
self.assertEqual(saved['rotation']['phase'],'returning_original')
|
||||
self.assertTrue(any('撤销待重试' in message for message in saved['blocked']))
|
||||
self.assertEqual(len(sold),1)
|
||||
self.assertEqual((sold[0]['side'],sold[0]['book_id'],sold[0]['qty'],sold[0]['manual_exit_cutoff']),
|
||||
('sell','micro30',200,'14:57'))
|
||||
|
||||
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')
|
||||
|
|
@ -273,11 +615,14 @@ class ServiceBoundaries(unittest.TestCase):
|
|||
with self.assertRaises(ValueError):
|
||||
r.pending_instructions()
|
||||
|
||||
def send_existing(self, settled, cancel=False):
|
||||
def send_existing(self, settled, cancel=False, *, now=None, cutoff=None):
|
||||
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',
|
||||
side = 'sell' if cutoff else 'buy'
|
||||
parent = {'instruction_id':'p', 'book_id':'micro30', 'ts_code':'600000.SH', 'side':side,
|
||||
'qty':300, 'exec_qty':100, 'status':'DISPATCHED',
|
||||
'progress':{'children':[{'ref':'p_D01'}], **({'cancel_requested':True} if cancel else {})}}
|
||||
if cutoff:
|
||||
parent['progress']['manual_exit_cutoff'] = cutoff
|
||||
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), \
|
||||
|
|
@ -287,10 +632,79 @@ class ServiceBoundaries(unittest.TestCase):
|
|||
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')
|
||||
result = r.send_intent({'id':'p','book_id':'micro30','ts_code':'600000.SH','side':side,
|
||||
'qty':300,'reason':'补齐'},10,now or dt.datetime(2026,11,3,9,40),'snapshot')
|
||||
return result, send.call_args
|
||||
|
||||
def test_legacy_exit_can_reopen_after_system_expiry_and_honors_user_cancellation(self):
|
||||
from app.services import execution_orders as e
|
||||
day = '2026-11-03'
|
||||
record = {'instruction_id':'legacy','book_id':'micro30','ts_code':'600000.SH','side':'sell',
|
||||
'status':'CANCELLED','progress':{'execution_profile':'rotation','deadline':day}}
|
||||
settled = {'active':[], 'pending_trades':0, 'booked':100, 'confirmed_filled':100, 'missing_orders':[]}
|
||||
with patch.object(e.pms_repo,'list_instructions',return_value=[record]), \
|
||||
patch.object(e,'replacement_state',return_value=settled):
|
||||
def check(authorized_at=None):
|
||||
return e.code_settlement_blocker(record['ts_code'],manual_exit_day=day,
|
||||
manual_exit_requested_at=authorized_at)
|
||||
record['progress']['cancel_requested'] = {'arbiter':'system','at':day+'T10:00:00'}
|
||||
self.assertFalse(check())
|
||||
settled['active'] = [{'status':'ACCEPTED'}]
|
||||
self.assertIn('仍在途',check())
|
||||
settled['active'] = []
|
||||
settled['booked'] = 0
|
||||
self.assertIn('入账',check())
|
||||
settled['booked'] = 100
|
||||
record['progress']['cancel_requested'] = {'arbiter':'user','at':day+'T13:55:00'}
|
||||
self.assertIn('已被撤销',check())
|
||||
self.assertIn('已被撤销',check(day+'T13:54:59+08:00'))
|
||||
self.assertFalse(check(day+'T13:55:01+08:00'))
|
||||
|
||||
def test_manual_exit_retries_only_remaining_and_honors_its_own_session(self):
|
||||
for hour, minute, cutoff, deadline in ((11,27,'11:30',(11,30)), (14,0,'14:57',(14,10)),
|
||||
(14,53,'14:57',(14,57))):
|
||||
with self.subTest(hour=hour, minute=minute):
|
||||
now = dt.datetime(2026,11,3,hour,minute)
|
||||
result, sent = self.send_existing({'ready':True,'remaining':200}, now=now, cutoff=cutoff)
|
||||
self.assertEqual(sent.kwargs['side'], 'sell')
|
||||
self.assertEqual(sent.kwargs['qty'], 200)
|
||||
self.assertEqual(sent.kwargs['valid_until'], now.replace(hour=deadline[0],minute=deadline[1]))
|
||||
ended, sent = self.send_existing({'ready':True,'remaining':200},
|
||||
now=dt.datetime(2026,11,3,14,57), cutoff='14:57')
|
||||
self.assertIsNone(sent)
|
||||
self.assertIn('结束', ended['state'])
|
||||
|
||||
def test_expiry_preserves_manual_afternoon_order_across_ticks(self):
|
||||
from app.services import rotation_execution as r, executor
|
||||
base = {'execution_profile':'rotation', 'deadline':'2026-11-03'}
|
||||
parents = [{'book_id':'micro30', 'instruction_id':key, 'progress':dict(base, **extra)}
|
||||
for key, extra in [('regular',{}), ('am',{'manual_exit_cutoff':'11:30'}),
|
||||
('pm',{'manual_exit_cutoff':'14:57'})]]
|
||||
with patch.object(r.pms_repo, 'list_instructions', return_value=parents), \
|
||||
patch.object(executor, 'cancel_instruction', return_value={'ok':True}) as cancel:
|
||||
for hour, minute, expected in [(10,0,['regular']), (14,0,['regular','am']),
|
||||
(14,57,['regular','am','pm'])]:
|
||||
cancel.reset_mock()
|
||||
r.cancel_expired(dt.datetime(2026,11,3,hour,minute))
|
||||
self.assertEqual([c.args[0] for c in cancel.call_args_list], expected)
|
||||
|
||||
def test_v2_manual_exit_retries_but_v2_regular_still_uses_frozen_price(self):
|
||||
from app.services import rotation_execution as r, execution_orders as e
|
||||
common = {'book_id':'micro30','ts_code':'600000.SH','side':'sell','qty':300}
|
||||
progress = {'execution_profile':'rotation','deadline':'2026-11-03','rule_version':m.RULE}
|
||||
parents = [dict(common,instruction_id='manual',progress=dict(progress,manual_exit_cutoff='14:57')),
|
||||
dict(common,instruction_id='regular',progress=progress)]
|
||||
with patch.object(r.qmt_repo,'orders_for_parent',return_value=[]), \
|
||||
patch.object(e,'quote_order',return_value={'action':'FIRE','limit_price':10}), \
|
||||
patch.object(e,'replacement_state',return_value={'ready':True,'remaining':100}), \
|
||||
patch.object(r.db,'savepoint',side_effect=nullcontext), \
|
||||
patch.object(r,'send_intent',return_value={'ok':True}) as send:
|
||||
results, waits = r.refresh_orders(parents,{},dt.datetime(2026,11,3,14,0),'snapshot',set(),{})
|
||||
self.assertEqual(results,[{'ok':True}])
|
||||
self.assertEqual(waits,[])
|
||||
self.assertEqual(send.call_args.args[0]['id'],'manual')
|
||||
self.assertEqual(send.call_args.args[0]['rule_version'],m.RULE)
|
||||
|
||||
def test_replacement_waits_until_cancellation_and_booking_finish(self):
|
||||
result, sent = self.send_existing({'ready':False,'remaining':200})
|
||||
self.assertTrue(result['pending'])
|
||||
|
|
@ -338,6 +752,78 @@ class ServiceBoundaries(unittest.TestCase):
|
|||
replace.assert_not_called()
|
||||
|
||||
|
||||
class ResumeTests(unittest.TestCase):
|
||||
def test_return_to_original_preserves_released_cash_and_original_t0(self):
|
||||
from app.services import rotation_service as r, executor
|
||||
rows = [
|
||||
('old-open','original','buy',{}), ('fund','original','sell',{'execution_profile':'rotation'}),
|
||||
('open','micro30','buy',{}), ('exit','micro30','sell',{}),
|
||||
('close-t0','original','buy',{'leg':'close'}), ('trader-sell','original','sell',{})]
|
||||
parents = [dict(instruction_id=i,book_id=b,side=s,progress=p) for i,b,s,p in rows]
|
||||
parents[-2].update(origin_type='strategy', action='T0_ROUND')
|
||||
with patch.object(executor,'cancel_instruction',return_value={'ok':True}) as cancel, \
|
||||
patch.object(r.pms_repo,'active_strategies',return_value=[{'type':'T0','state':{'open_leg':'buy'}}]) as t0:
|
||||
self.assertFalse(r._stop_leaving_orders('returning_original',parents,[{'t0_qty':100}],[]))
|
||||
self.assertEqual([c.args[0] for c in cancel.call_args_list],['fund','open'])
|
||||
t0.assert_not_called()
|
||||
cancel.reset_mock()
|
||||
self.assertTrue(r._stop_leaving_orders('entering_micro',parents,[{'t0_qty':100}],[]))
|
||||
self.assertEqual([c.args[0] for c in cancel.call_args_list],['old-open'])
|
||||
|
||||
def test_explicit_resume_requires_settlement_and_does_not_reuse_old_signal(self):
|
||||
from app.services import rotation_service as r, rotation_execution as execution
|
||||
snap = snapshot()
|
||||
saved = {'rotation':state(snap)}
|
||||
config = {'mode':'auto','control':'run'}
|
||||
@contextmanager
|
||||
def locked(*args):
|
||||
yield saved
|
||||
def save(_key, value, _actor):
|
||||
config['control'] = value
|
||||
return {'ok':True}
|
||||
with ExitStack() as stub:
|
||||
for obj, name, kwargs in [
|
||||
(r,'documents_ready',{'return_value':True}), (r.store,'account_guard',{'side_effect':nullcontext}),
|
||||
(r.store,'locked',{'side_effect':locked}), (r,'configuration',{'return_value':config}),
|
||||
(r.param_store,'set_param',{'side_effect':save}), (execution,'pending_instructions',{'return_value':[]})]:
|
||||
stub.enter_context(patch.object(obj,name,**kwargs))
|
||||
stub.enter_context(patch.object(r.settings,'INFRA_STRATEGY_BOOK_SCHEMA',True))
|
||||
held = stub.enter_context(patch.object(r,'positions',return_value=[]))
|
||||
unsettled = stub.enter_context(patch.object(r,'_micro_unsettled',return_value=False))
|
||||
recon = stub.enter_context(patch.object(r,'reconcile_books',return_value={'ok':True}))
|
||||
active = stub.enter_context(patch.object(r.pms_repo,'list_commands',return_value=[]))
|
||||
command = stub.enter_context(patch.object(r.pms_repo,'get_command',return_value={'status':'EXECUTING'}))
|
||||
r._set_control('exit_disable','user',exit_command='clear-all')
|
||||
self.assertEqual(saved['rotation']['phase'],'returning_original')
|
||||
with self.assertRaisesRegex(ValueError,'退出仍在办理'):
|
||||
r._set_control('run','user')
|
||||
saved['rotation'] = m.transition(saved['rotation'],snap,micro_qty=0,original_qty=0,budget=100000,control='exit_disable')
|
||||
for status in ('EXECUTING','UNKNOWN'):
|
||||
command.return_value = {'status':status}
|
||||
with self.assertRaisesRegex(ValueError,'清仓命令'):
|
||||
r._set_control('run','user')
|
||||
command.return_value = {'status':'DONE'}
|
||||
active.return_value = [{'command_id':'another-clear-all'}]
|
||||
with self.assertRaisesRegex(ValueError,'清仓命令'):
|
||||
r._set_control('run','user')
|
||||
active.return_value = []
|
||||
for query, active in [(held,[{'total_qty':100}]), (unsettled,True), (recon,{'ok':False})]:
|
||||
old = query.return_value
|
||||
query.return_value = active
|
||||
with self.assertRaisesRegex(ValueError,'尚未结清'):
|
||||
r._set_control('run','user')
|
||||
self.assertTrue(saved['rotation']['disabled'])
|
||||
self.assertEqual(config['control'],'exit_disable')
|
||||
query.return_value = old
|
||||
r._set_control('run','user')
|
||||
self.assertFalse(saved['rotation']['disabled'])
|
||||
self.assertEqual(config['control'],'run')
|
||||
same_day = m.transition(saved['rotation'],snap,micro_qty=0,original_qty=0,budget=100000)
|
||||
self.assertEqual(same_day['phase'],'original')
|
||||
next_day = m.transition(same_day,snapshot(day='2026-11-04'),micro_qty=0,original_qty=0,budget=100000)
|
||||
self.assertEqual(next_day['phase'],'entering_micro')
|
||||
|
||||
|
||||
class ReservationTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
from app.repo import execution_repo
|
||||
|
|
|
|||
|
|
@ -0,0 +1,153 @@
|
|||
"""逐票核验须穿过普通发现窗口,同时保留原有资格条件。"""
|
||||
import unit_test_guard # noqa: F401
|
||||
import json
|
||||
import sys
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import requests
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from app.core.plan_revision import basis, fingerprint, portfolio_context
|
||||
from app.repo import pms_repo
|
||||
from app.services import plan_feed, plan_recheck, portfolio, proposal_service
|
||||
from test_wiring import install_fakes
|
||||
|
||||
|
||||
CODE = '603999.SH'
|
||||
UPSTREAM_CODE = 'SH603999'
|
||||
|
||||
|
||||
class PlanRankBoundaryTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
fake = install_fakes(prices={CODE: 10}, positions=[], params={
|
||||
'PMS_PLAN_API_BASE': 'http://bridge.invalid', 'PMS_PLAN_TOP': '300',
|
||||
'PMS_CONSENSUS_ROUTE': 'false', 'PMS_PLAN_BY_ADVICE': 'false',
|
||||
'PMS_PLAN_TOP_N': '50', 'PMS_PLAN_SNAPSHOT': 'false',
|
||||
'PMS_PLAN_THEME_CAP_LOCAL': '0', 'PMS_PLAN_VERSION_RECHECK': 'true',
|
||||
})
|
||||
fake.qmt.put_snapshot(kind='funds', data={'total_asset': 2000000, 'available_cash': 2000000})
|
||||
self.date = datetime.now().date().isoformat()
|
||||
self.row = {'code': UPSTREAM_CODE, 'name': '样本', 'research_rank': 301, 'rank': 301,
|
||||
'tier': '强传导', 'score': 231, 'verdict': '候选'}
|
||||
self.plan = {'date': self.date, 'main': [self.row], 'observe': [],
|
||||
'rank_axis': 'quality', 'decision': {'id': 'published-v2'}}
|
||||
self.ordinary = [dict(self.row, code=f'SH{600000+i}', rank=i+1, research_rank=i+1)
|
||||
for i in range(300)]
|
||||
self.plan['main'] = self.ordinary + [self.row]
|
||||
plan_feed.invalidate()
|
||||
self.addCleanup(plan_feed.invalidate)
|
||||
self.http = self.enterContext(patch('requests.get', side_effect=self.response))
|
||||
self.record = self.enterContext(patch.object(pms_repo, 'insert_plan_snapshot', return_value=1))
|
||||
old = dict(self.plan, decision={'id': 'published-v1'}, main=[dict(self.row, rank=1, research_rank=1)])
|
||||
selected = plan_feed.select_candidates(plan_feed.parse_plan(old))['items'][0]
|
||||
self.view = portfolio.positions_view()
|
||||
self.params = proposal_service._scan_params(self.view)
|
||||
self.hard = {'decision': old['decision'], 'consumption_id': 'original-consumption',
|
||||
'plan_basis': basis(selected), 'scan_params_hash': fingerprint(self.params),
|
||||
'portfolio_context_hash': portfolio_context(self.view,
|
||||
proposal_service._inflight_open_commitments(self.view))}
|
||||
|
||||
def response(self, url, params, timeout):
|
||||
if url.endswith('/check'):
|
||||
self.assertEqual(params, {'code': CODE, 'include_user_candidates': 1})
|
||||
rows = [r for r in self.plan['main'] if r['code'] == UPSTREAM_CODE]
|
||||
result = dict(self.plan, main=rows, check_code=UPSTREAM_CODE, found=bool(rows))
|
||||
else:
|
||||
self.assertEqual(url, 'http://bridge.invalid/plan')
|
||||
count = int(params['top'])
|
||||
self.assertEqual(count, 300)
|
||||
result = dict(self.plan, main=self.plan['main'][:count])
|
||||
reply = requests.Response()
|
||||
reply.status_code = 200
|
||||
reply.encoding = 'utf-8'
|
||||
reply._content = json.dumps(result, ensure_ascii=False).encode('utf-8')
|
||||
return reply
|
||||
|
||||
def test_current_rank_301_preserves_qualification_across_routes(self):
|
||||
discovery = plan_feed.candidates(record=False)
|
||||
self.assertNotIn(CODE, [r['ts_code'] for r in discovery['items']])
|
||||
fast = plan_recheck.current_basis(self.hard, CODE, record=False)
|
||||
self.assertTrue(fast['ok'], fast)
|
||||
self.record.assert_not_called()
|
||||
|
||||
automatic = proposal_service.verify_open_basis(self.hard, CODE)
|
||||
self.assertTrue(automatic['ok'], automatic)
|
||||
self.assertEqual(automatic['provenance']['consumption_id'], fast['provenance']['consumption_id'])
|
||||
prepared = plan_recheck.prepare_new(self.hard, CODE, {'verdict': 'PASS'})
|
||||
self.assertEqual(prepared['preparation']['status'], 'ready')
|
||||
self.assertEqual(self.record.call_count, 2)
|
||||
for receipt in self.record.call_args_list:
|
||||
self.assertEqual(receipt.kwargs['meta']['kind'], 'rechecked')
|
||||
self.assertEqual(receipt.kwargs['meta']['order'], [CODE])
|
||||
self.assertEqual(receipt.kwargs['meta']['decision'], self.plan['decision'])
|
||||
recorded = receipt.kwargs['roster'][0]
|
||||
self.assertEqual(recorded['research_rank'], 301)
|
||||
self.assertNotIn('selection_rank', recorded)
|
||||
|
||||
def test_background_refresh_reprices_one_stock_outside_the_discovery_window(self):
|
||||
hard = dict(self.hard, preparation={'status': 'pending'}, proposal_revision='v1-shown',
|
||||
prepared_judgement={'verdict': 'PASS', 'confidence': 80, 'degraded': False})
|
||||
proposal = {'proposal_id': 'pending', 'ts_code': CODE, 'action': 'OPEN', 'qty': 100,
|
||||
'hard_numbers': hard}
|
||||
with patch.object(pms_repo, 'record_proposal_check', return_value=1), \
|
||||
patch.object(pms_repo, 'publish_prepared_proposal', return_value=1) as publish:
|
||||
result = plan_recheck.refresh_one(proposal)
|
||||
self.assertTrue(result['ok'], result)
|
||||
saved = publish.call_args.args[1]['hard_numbers']
|
||||
self.assertEqual(saved['preparation']['status'], 'ready')
|
||||
self.assertEqual(saved['research_rank'], 301)
|
||||
for key in ('selection_rank', 'execution_rank', 'priority_reason'):
|
||||
self.assertNotIn(key, saved)
|
||||
self.assertEqual(self.http.call_count, 3)
|
||||
|
||||
def test_report_published_between_two_checks_requires_a_new_judgement(self):
|
||||
hard = dict(self.hard, preparation={'status': 'pending'}, proposal_revision='v1-shown',
|
||||
prepared_judgement={'verdict': 'PASS', 'reason': '基于旧评析', 'confidence': 80})
|
||||
proposal = {'proposal_id': 'pending', 'ts_code': CODE, 'action': 'OPEN', 'qty': 100,
|
||||
'hard_numbers': hard}
|
||||
|
||||
def publish_new_report(url, params, timeout):
|
||||
if self.http.call_count == 2:
|
||||
self.plan['decision'] = {'id': 'published-v3'}
|
||||
self.row['company_review_text'] = '新的买方评析'
|
||||
return self.response(url, params, timeout)
|
||||
|
||||
self.http.side_effect = publish_new_report
|
||||
new_judgement = {'verdict': 'PASS', 'reason': '已核验新评析', 'confidence': 80, 'raw': {}}
|
||||
with patch.object(pms_repo, 'record_proposal_check', return_value=1), \
|
||||
patch.object(pms_repo, 'publish_prepared_proposal', return_value=1) as publish, \
|
||||
patch.object(proposal_service.judge, 'request', return_value=new_judgement) as judge:
|
||||
result = plan_recheck.refresh_one(proposal)
|
||||
self.assertTrue(result['ok'], result)
|
||||
judge.assert_called_once()
|
||||
self.assertTrue(judge.call_args.kwargs['force'])
|
||||
self.assertEqual(publish.call_args.args[2]['reason'], '已核验新评析')
|
||||
self.assertEqual(publish.call_args.args[1]['hard_numbers']['decision']['id'], 'published-v3')
|
||||
|
||||
def test_absent_in_complete_version_is_a_real_qualification_loss(self):
|
||||
self.plan['main'] = self.ordinary
|
||||
for record in (False, True):
|
||||
self.assertTrue(plan_recheck.current_basis(self.hard, CODE, record=record)['withdraw'])
|
||||
self.plan['refresh_status'] = {'pending_codes': [UPSTREAM_CODE]}
|
||||
retry = plan_recheck.current_basis(self.hard, CODE, record=True)
|
||||
self.assertFalse(retry['ok'])
|
||||
self.assertEqual(retry['stage'], 'research_pending')
|
||||
self.assertFalse(retry.get('withdraw'))
|
||||
|
||||
def test_material_change_or_record_failure_blocks_automatic_approval(self):
|
||||
self.row['risk'] = ['新增风险']
|
||||
changed = proposal_service.verify_open_basis(self.hard, CODE)
|
||||
self.assertFalse(changed['ok'])
|
||||
self.assertTrue(changed['research_changed'])
|
||||
self.record.side_effect = RuntimeError('核验账本写入失败')
|
||||
blocked = proposal_service.verify_open_basis(self.hard, CODE)
|
||||
self.assertFalse(blocked['ok'])
|
||||
self.assertEqual(blocked['stage'], 'record')
|
||||
self.assertFalse(blocked.get('withdraw'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
@ -143,6 +143,7 @@ class ServiceTests(unittest.TestCase):
|
|||
self.use(executor,'cancel_instruction',cancel)
|
||||
schema={
|
||||
'pms_command':'id INTEGER PRIMARY KEY,book_id TEXT,command_id TEXT,cmd_class TEXT,cmd_type TEXT,ts_code TEXT,params_json TEXT,status TEXT,progress_json TEXT,issued_by TEXT,issued_at TEXT,archived_at TEXT',
|
||||
'pms_runtime_param':'param_key TEXT PRIMARY KEY,param_value TEXT',
|
||||
'pms_position':'id INTEGER PRIMARY KEY,book_id TEXT,ts_code TEXT,total_qty INTEGER,avail_qty INTEGER,opened_date TEXT,status TEXT',
|
||||
'pms_lot':'id INTEGER PRIMARY KEY,book_id TEXT,ts_code TEXT,instruction_id TEXT,qty INTEGER,status TEXT,open_date TEXT',
|
||||
'pms_instruction':'id INTEGER PRIMARY KEY,book_id TEXT,instruction_id TEXT UNIQUE,origin_type TEXT,origin_id TEXT,ts_code TEXT,action TEXT,side TEXT,qty INTEGER,limit_price REAL,window_tdays INTEGER,status TEXT,exec_qty INTEGER DEFAULT 0,exec_avg_price REAL,progress_json TEXT,created_at TEXT,updated_at TEXT,archived_at TEXT',
|
||||
|
|
|
|||
|
|
@ -157,6 +157,84 @@ class ServiceTests(recovery.DirectionTests):
|
|||
self.assertEqual(timing_fusion.refresh(now=now)['accepted'],1)
|
||||
self.assertEqual(timing_state.latest(base.CODE,now)['action'],'WAIT')
|
||||
|
||||
def test_refresh_verifies_at_receipt_and_preserves_lease(self):
|
||||
import json
|
||||
import requests
|
||||
from app.services import exec_advisor
|
||||
sent = self.setup_fusion()
|
||||
received = sent + dt.timedelta(milliseconds=20)
|
||||
current = [sent]
|
||||
event = fused(at=sent)
|
||||
event['verified_at'] = (sent + dt.timedelta(milliseconds=5)).isoformat()
|
||||
|
||||
def get(url, timeout):
|
||||
self.assertTrue(url.endswith('/api/intraday/timing_fusion'))
|
||||
current[0] = received
|
||||
response = requests.Response()
|
||||
response.status_code = 200
|
||||
response._content = json.dumps({'version': contract.VERSION, 'enabled': True,
|
||||
'states': [event]}).encode()
|
||||
return response
|
||||
|
||||
with patch.object(timing_fusion, 'datetime') as clock, patch.object(exec_advisor, 'base_url', return_value='http://offline.invalid'), patch.object(requests, 'get', side_effect=get):
|
||||
clock.now.side_effect = lambda: current[0]
|
||||
first = timing_fusion.refresh()
|
||||
self.assertEqual((first['accepted'], first['invalid'], self.state()['phase']), (1, 0, 'STOP_BUY'))
|
||||
event['verified_at'] = (received + dt.timedelta(milliseconds=5)).isoformat()
|
||||
self.assertEqual(timing_fusion.refresh()['status'], 'FAILED')
|
||||
event['verified_at'] = sent.isoformat()
|
||||
event['expires_at'] = received.isoformat()
|
||||
self.assertEqual(timing_fusion.refresh()['status'], 'FAILED')
|
||||
self.assertEqual(self.state()['count'], 1)
|
||||
|
||||
def test_actual_digest_reports_partial_batch_wait_and_failure(self):
|
||||
import requests
|
||||
from app import scheduler
|
||||
from app.services import exec_advisor
|
||||
from types import SimpleNamespace
|
||||
now = self.setup_fusion()
|
||||
wait = fused('WAIT', at=now)
|
||||
response = {'version': contract.VERSION, 'enabled': True, 'states': [wait]}
|
||||
with patch.object(signal_service.portfolio, 'positions_view', return_value={}), \
|
||||
patch.object(signal_service, 'streams', return_value=[]), patch.object(signal_service, '_load_seen', return_value={}), \
|
||||
patch.object(exec_advisor, 'base_url', return_value='http://offline.invalid'), \
|
||||
patch.object(timing_fusion, 'datetime') as clock, patch.object(requests, 'get', return_value=SimpleNamespace(raise_for_status=lambda:None, json=lambda:response)):
|
||||
clock.now.return_value = now
|
||||
def digest():
|
||||
return signal_service.consume(dry_run=True)
|
||||
clean = digest()
|
||||
self.assertTrue(clean['ok'])
|
||||
self.assertEqual(clean['timing_fusion']['status'], 'NO_DIRECTION')
|
||||
self.assertIn('NO_DIRECTION', scheduler._brief(clean))
|
||||
response['states'] = []
|
||||
self.assertEqual(digest()['timing_fusion']['status'], 'NO_EVIDENCE')
|
||||
response.update(states=[fused(at=now), None], errors=['主力资金来源异常'])
|
||||
partial = digest()
|
||||
self.assertFalse(partial['ok'])
|
||||
self.assertEqual(partial['timing_fusion']['accepted'], 1)
|
||||
self.assertEqual(partial['timing_fusion']['status'], 'DEGRADED')
|
||||
response['states'] = [None]
|
||||
failure = digest()
|
||||
self.assertFalse(failure['ok'])
|
||||
self.assertIn('FAILED', scheduler._brief(failure))
|
||||
|
||||
def test_previously_known_wait_after_one_thousand_other_rows_is_saved(self):
|
||||
from types import SimpleNamespace
|
||||
now = self.setup_fusion()
|
||||
self.save(fused(at=now))
|
||||
ids = [{'doc_id':'timing-direction:' + str(600100 + i) + '.SH', 'kind':timing_state.KIND,
|
||||
'payload_json':'{}', 'revision':1, 'updated_at':'2099-01-01'} for i in range(1001)]
|
||||
from sqlalchemy import text
|
||||
with self.engine.begin() as conn:
|
||||
conn.execute(text('INSERT INTO pms_rotation_document VALUES (:doc_id,:kind,:payload_json,:revision,:updated_at)'), ids)
|
||||
payload = {'version': contract.VERSION, 'enabled': True, 'states':[fused('WAIT',at=now,n=2)]}
|
||||
with patch('requests.get', return_value=SimpleNamespace(raise_for_status=lambda:None, json=lambda:payload)):
|
||||
self.assertEqual(timing_fusion.refresh(now=now)['accepted'], 1)
|
||||
self.assertEqual(timing_state.latest(base.CODE,now)['action'], 'WAIT')
|
||||
with patch.object(rotation_repo, 'document_ids', return_value=set(range(6001))):
|
||||
with self.assertRaisesRegex(ValueError, '超过完整读取上限'):
|
||||
timing_fusion.refresh(now=now)
|
||||
|
||||
def test_fusion_manual_halt_not_recovered(self):
|
||||
now=self.setup_fusion();a=fused(at=now);self.save(a);risk_exit.on_signal(a,now=now)
|
||||
self.params['PMS_GLOBAL_BUY_HALT']=True
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ import datetime as dt
|
|||
import json
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
|
@ -13,8 +15,9 @@ sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|||
from sqlalchemy import create_engine, event, text
|
||||
from app.core import strategy_book as books
|
||||
from app.db import session as db
|
||||
from app.repo import pms_repo
|
||||
from app.repo import pms_repo, qmt_repo
|
||||
from app.services import strategy_runner as runner, rotation_service, grid_inventory
|
||||
from app.services import execution_orders, strategy_advisor, strategy_service
|
||||
from config.settings import settings
|
||||
|
||||
CODE = '600001.SH'
|
||||
|
|
@ -56,7 +59,7 @@ class StrategyCorrectionTests(unittest.TestCase):
|
|||
(runner.param_store, 'get_float', lambda key, default=0: default),
|
||||
(runner.param_store, 'get_int', lambda key, default=0: default),
|
||||
(runner.param_store, 'get', lambda key, default=None: default),
|
||||
(runner.portfolio, 'positions_view', lambda: {'positions': [{'ts_code': CODE, 'total_qty': 100, 'avail_qty': 100}], 'totals': {'scale': 10000}}),
|
||||
(runner.portfolio, 'positions_view', lambda: {'positions': [{'ts_code': CODE, 'total_qty': 100, 'avail_qty': 100}], 'totals': {'scale': 10000}, 'params': {'scale': 10000, 'stock_cap': .2}}),
|
||||
(runner.market, 'day_snapshot', lambda code: {'price': 10, 'bar_time': self.now.isoformat()}),
|
||||
(pms_repo, 'get_position', lambda code: {'total_qty':100, 'add_qty':100, 'avail_qty':100}),
|
||||
(grid_inventory, 'snapshot', lambda st, pos: {'invested':1000, 'filled_levels':{'0':{'price':10,'qty':100,'sell_target':10.4}}}),
|
||||
|
|
@ -256,6 +259,208 @@ class StrategyCorrectionTests(unittest.TestCase):
|
|||
self.assertFalse(any('account-execution' in sql for sql in self.statements))
|
||||
self.assertEqual(self.count('pms_instruction'), 0)
|
||||
|
||||
def attach_trail(self):
|
||||
return strategy_service.attach({'ts_code': CODE, 'type': 'TRAIL', 'params': {'giveback': .05}})
|
||||
|
||||
def test_resume_cannot_take_over_another_active_strategy(self):
|
||||
self.assertTrue(strategy_service.set_status('GRID', 'PAUSED')['ok'])
|
||||
trail = self.attach_trail()
|
||||
self.assertTrue(trail['ok'], trail)
|
||||
self.assertFalse(strategy_service.set_status('GRID', 'ACTIVE')['ok'])
|
||||
self.assertEqual(pms_repo.get_strategy('GRID')['status'], 'PAUSED')
|
||||
self.assertTrue(strategy_service.set_status(trail['strategy_id'], 'CANCELLED')['ok'])
|
||||
self.assertFalse(strategy_service.set_status(trail['strategy_id'], 'ACTIVE')['ok'])
|
||||
with patch.object(pms_repo, 'get_position', return_value=None):
|
||||
self.assertFalse(strategy_service.set_status('GRID', 'ACTIVE')['ok'])
|
||||
self.assertTrue(strategy_service.set_status('GRID', 'ACTIVE')['ok'])
|
||||
self.assertEqual([s['strategy_id'] for s in pms_repo.active_strategies()], ['GRID'])
|
||||
|
||||
def test_snapshot_is_read_outside_lock_and_actual_availability_is_rechecked_inside(self):
|
||||
self.assertTrue(strategy_service.set_status('GRID', 'PAUSED')['ok'])
|
||||
def quote_snapshot():
|
||||
self.assertIsNone(db._TRANSACTION.get())
|
||||
return {'positions': [{'ts_code': CODE, 'total_qty': 100, 'avail_qty': 100,
|
||||
'price_ok': True, 'price': 10}],
|
||||
'params': {'scale': 10000, 'stock_cap': .2}}
|
||||
def changed_position(code):
|
||||
self.assertIsNotNone(db._TRANSACTION.get())
|
||||
return {'total_qty': 180, 'avail_qty': 0}
|
||||
with (patch.object(runner.portfolio, 'positions_view', side_effect=quote_snapshot),
|
||||
patch.object(pms_repo, 'get_position', side_effect=changed_position)):
|
||||
self.assertFalse(self.attach_trail()['ok'])
|
||||
grid = {'ts_code': CODE, 'type': 'GRID', 'params': {'lower': 9, 'center': 10,
|
||||
'upper': 11, 'step': .5, 'max_capital': 500}}
|
||||
result = strategy_service.attach(grid)
|
||||
self.assertIn('超过', result['errors'][0], result)
|
||||
with patch.object(runner.portfolio, 'positions_view', side_effect=RuntimeError('行情不可用')):
|
||||
self.assertFalse(self.attach_trail()['ok'])
|
||||
self.assertFalse(pms_repo.active_strategies())
|
||||
|
||||
def test_legacy_account_without_position_cannot_attach_or_resume_but_can_cancel(self):
|
||||
self.use(settings, 'INFRA_STRATEGY_BOOK_SCHEMA', False)
|
||||
with self.engine.begin() as connection:
|
||||
connection.execute(text('CREATE TABLE pms_position (ts_code TEXT PRIMARY KEY,total_qty INTEGER)'))
|
||||
connection.execute(text("INSERT INTO pms_strategy(strategy_id,ts_code,type,status) "
|
||||
"VALUES ('legacy',:code,'GRID','PAUSED')"), {'code': CODE})
|
||||
with (patch.object(runner.portfolio, 'positions_view', return_value={'positions': []}),
|
||||
patch.object(pms_repo, 'get_position', return_value=None)):
|
||||
self.assertFalse(self.attach_trail()['ok'])
|
||||
self.assertFalse(strategy_service.set_status('legacy', 'ACTIVE')['ok'])
|
||||
self.assertTrue(strategy_service.set_status('legacy', 'CANCELLED')['ok'])
|
||||
self.assertTrue(any('FROM pms_position WHERE ts_code' in sql and 'FOR UPDATE' in sql
|
||||
for sql in self.statements))
|
||||
|
||||
def test_concurrent_attach_and_resume_leave_one_owner(self):
|
||||
# SQLite 没有行锁,用写事务模拟同一账户锁的排它性;读写走真实 repo。
|
||||
@event.listens_for(self.engine, 'begin')
|
||||
def begin_immediate(connection):
|
||||
connection.exec_driver_sql('BEGIN IMMEDIATE')
|
||||
self.assertTrue(strategy_service.set_status('GRID', 'PAUSED')['ok'])
|
||||
barrier = threading.Barrier(2)
|
||||
def compete(action):
|
||||
barrier.wait(timeout=5)
|
||||
return action()
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
results = list(pool.map(compete, [self.attach_trail, lambda: strategy_service.set_status('GRID', 'ACTIVE')]))
|
||||
self.assertEqual(sum(result['ok'] for result in results), 1, results)
|
||||
self.assertEqual(len(pms_repo.active_strategies()), 1)
|
||||
self.assertTrue(any("doc_id='account-execution' FOR UPDATE" in sql for sql in self.statements))
|
||||
|
||||
def test_existing_duplicate_owners_stop_tick_and_manual_acceptance(self):
|
||||
pms_repo.insert_strategy(strategy_id='TRAIL', ts_code=CODE, stype='TRAIL')
|
||||
out = runner.tick(now=self.now)
|
||||
self.assertFalse(out['ok'])
|
||||
self.assertEqual(out['fired'] + out['queued'], [])
|
||||
with self.assertLogs('pms.strategy', level='ERROR'):
|
||||
result = runner.emit_from_spec(dict(DECISION, strategy_id='GRID'))
|
||||
self.assertFalse(result['ok'])
|
||||
self.assertEqual(self.count('pms_instruction'), 0)
|
||||
self.assertTrue(strategy_service.set_status('TRAIL', 'PAUSED')['ok'])
|
||||
|
||||
def test_stale_missing_or_future_price_cannot_move_trailing_high_or_trigger(self):
|
||||
strategy_service.set_status('GRID', 'PAUSED')
|
||||
strategy_id = self.attach_trail()['strategy_id']
|
||||
pms_repo.update_strategy(strategy_id, state={'high_water': 12, 'armed': True})
|
||||
for bars in ([], [{'close': 20, 'time': self.now - dt.timedelta(days=1)}],
|
||||
[{'close': 8, 'time': self.now - dt.timedelta(seconds=91)}],
|
||||
[{'close': 20, 'time': self.now + dt.timedelta(seconds=1)}],
|
||||
[{'close': 'nan', 'time': self.now}]):
|
||||
with self.subTest(bars=bars), patch.object(runner.market, '_bars', return_value=bars):
|
||||
out = runner.tick(now=self.now)
|
||||
self.assertTrue(out['ok'], out)
|
||||
self.assertFalse(out['fired'])
|
||||
self.assertEqual(pms_repo.get_strategy(strategy_id)['state']['high_water'], 12)
|
||||
self.assertNotIn('trail_due', pms_repo.get_strategy(strategy_id)['state'])
|
||||
with patch.object(runner.market, '_bars', return_value=[{'close': 10, 'time': self.now}]):
|
||||
out = runner.tick(now=self.now)
|
||||
self.assertEqual([row['qty'] for row in out['fired']], [100], out)
|
||||
|
||||
def test_late_receipt_is_settled_once_then_only_remainder_fires_on_fresh_quote(self):
|
||||
strategy_service.set_status('GRID', 'PAUSED')
|
||||
strategy_id = self.attach_trail()['strategy_id']
|
||||
due = {'qty': 200, 'leg': 'trail_sell', 'partial': False, 'high_water': 12, 'reason': '回撤'}
|
||||
pms_repo.update_strategy(strategy_id, state={'pending': {'iid':'prior', 'leg':'trail_sell'},
|
||||
'trail_due': due, 'high_water': 12, 'armed': True})
|
||||
pms_repo.insert_instruction(instruction_id='prior', origin_type='strategy', origin_id=strategy_id,
|
||||
ts_code=CODE, action='EXIT', side='sell', qty=200, status='CANCELLED')
|
||||
pms_repo.update_instruction('prior', exec_qty=100)
|
||||
self.use(runner.param_store, 'get', lambda key, default=None: 'ws' if key == 'PMS_DISPATCH_MODE' else default)
|
||||
with (patch.object(runner.market, '_bars', return_value=[]),
|
||||
patch.object(execution_orders, 'replacement_state', return_value={'ready': False})):
|
||||
self.assertFalse(runner.tick(now=self.now)['fired'])
|
||||
self.assertEqual(pms_repo.get_strategy(strategy_id)['state']['trail_due']['qty'], 200)
|
||||
self.assertEqual(pms_repo.get_strategy(strategy_id)['state']['pending']['iid'], 'prior')
|
||||
with (patch.object(runner.market, '_bars', return_value=[]),
|
||||
patch.object(execution_orders, 'replacement_state', return_value={'ready': True})):
|
||||
for _ in range(2):
|
||||
self.assertFalse(runner.tick(now=self.now)['fired'])
|
||||
after = pms_repo.get_strategy(strategy_id)['state']
|
||||
self.assertEqual(after['trail_due']['qty'], 100)
|
||||
self.assertIsNone(after['pending'])
|
||||
with patch.object(runner.market, '_bars', return_value=[{'time': self.now, 'close': 10}]):
|
||||
out = runner.tick(now=self.now)
|
||||
self.assertEqual([row['qty'] for row in out['fired']], [100], out)
|
||||
|
||||
def test_grid_waits_on_missing_unrecognized_or_unsettled_parent_and_proposal(self):
|
||||
def waiting(pending):
|
||||
return strategy_advisor._has_pending({'strategy_id': 'GRID', 'ts_code': CODE, 'state': {'pending': pending}})
|
||||
self.assertFalse(waiting(None))
|
||||
self.assertTrue(waiting({}))
|
||||
with patch.object(pms_repo, 'list_instructions', return_value=[{'status': 'DISPATCHED'}]):
|
||||
self.assertTrue(waiting(None))
|
||||
with patch.object(pms_repo, 'list_instructions', side_effect=RuntimeError('不能核实同股在途指令')):
|
||||
self.assertTrue(waiting(None))
|
||||
self.assertTrue(waiting({'iid': 'absent'}))
|
||||
self.assertTrue(waiting({'pid': 'absent'}))
|
||||
for status in ('JUDGE_PASSED', 'UNKNOWN'):
|
||||
with patch.object(pms_repo, 'get_instruction', return_value={'status': status}):
|
||||
self.assertTrue(waiting({'iid': 'prior'}))
|
||||
with patch.object(pms_repo, 'get_instruction', side_effect=RuntimeError('读失败')):
|
||||
self.assertTrue(waiting({'iid': 'prior'}))
|
||||
for status in ('WAIT_USER', 'ACCEPTED', 'unknown'):
|
||||
with patch.object(pms_repo, 'get_proposal', return_value={'status': status}):
|
||||
self.assertTrue(waiting({'pid': 'prior'}))
|
||||
with patch.object(pms_repo, 'get_proposal', return_value={'status':'DECLINED'}):
|
||||
self.assertFalse(waiting({'pid': 'prior'}))
|
||||
self.use(runner.param_store, 'get', lambda key, default=None: 'ws' if key == 'PMS_DISPATCH_MODE' else default)
|
||||
with patch.object(qmt_repo, 'list_orders', return_value=[{'status':'PARTIAL'}]):
|
||||
self.assertTrue(waiting(None))
|
||||
with (patch.object(qmt_repo, 'list_orders', return_value=[]),
|
||||
patch.object(pms_repo, 'get_instruction', return_value={'status': 'CONFIRMED'})):
|
||||
for ready in (False, True):
|
||||
with patch.object(execution_orders, 'replacement_state', return_value={'ready': ready}):
|
||||
self.assertEqual(waiting({'iid':'prior'}), not ready)
|
||||
|
||||
def test_handoff_requires_current_breakout_and_seeds_the_new_trail_from_its_quote(self):
|
||||
prm = {'handoff_enabled': True, 'rules': [strategy_advisor.R_HANDOFF],
|
||||
'heat_th': .7, 'trail_giveback': .05, 'trail_sell_ratio': .5}
|
||||
pms_repo.update_strategy('GRID', note='自动挂载: 网格', params_json='{"upper":11}')
|
||||
st = pms_repo.get_strategy('GRID')
|
||||
pos = {'ts_code': CODE, 'price': 13, 'price_ok': True, 'avg_cost': 9.8, 'cushion_pct': .3}
|
||||
self.use(runner.param_store, 'get', lambda key, default=None: 'ws' if key == 'PMS_DISPATCH_MODE' else default)
|
||||
def read_history(code):
|
||||
self.assertIsNone(db._TRANSACTION.get())
|
||||
return ''
|
||||
def tend(bars):
|
||||
out = {'skipped': [], 'errors': [], 'handoffs': []}
|
||||
with (patch.object(runner.market, '_bars', return_value=bars),
|
||||
patch.object(qmt_repo, 'list_orders', return_value=[]),
|
||||
patch.object(execution_orders, 'code_settlement_blocker', side_effect=read_history),
|
||||
patch.object(runner.portfolio, 'positions_view', side_effect=AssertionError('接力须复用持仓快照')),
|
||||
patch.object(pms_repo, 'insert_ledger', return_value=1)):
|
||||
strategy_advisor._tend_existing(st, pos, {}, False, .9, prm, {}, set(), 20261105,
|
||||
False, out, strategy_service, now=self.now)
|
||||
return out
|
||||
for bars in ([], [{'time': self.now - dt.timedelta(days=1), 'close': 13}],
|
||||
[{'time': self.now, 'close': 10.5}]):
|
||||
self.assertFalse(tend(bars)['handoffs'])
|
||||
self.assertEqual(pms_repo.get_strategy('GRID')['status'], 'ACTIVE')
|
||||
out = tend([{'time': self.now, 'close': 11.5}])
|
||||
self.assertEqual(len(out['handoffs']), 1, out)
|
||||
self.assertEqual(pms_repo.get_strategy('GRID')['status'], 'CANCELLED')
|
||||
active = pms_repo.active_strategies()
|
||||
self.assertEqual(len(active), 1)
|
||||
self.assertEqual(active[0]['state']['high_water'], 11.5)
|
||||
|
||||
def test_failed_handoff_ledger_rolls_back_both_strategies_and_high_water(self):
|
||||
out = {'errors': [], 'skipped': [], 'handoffs': []}
|
||||
history = []
|
||||
def fail_pass(**row):
|
||||
history.append(row['verdict'])
|
||||
return int(row['verdict'] == 'REJECT')
|
||||
with patch.object(pms_repo, 'insert_ledger', side_effect=fail_pass):
|
||||
strategy_advisor._do_handoff(self.original, {'ts_code': CODE}, .9,
|
||||
{'trail_giveback': .05, 'trail_sell_ratio': .5}, '站上上界', out,
|
||||
strategy_service, high_water=12)
|
||||
self.assertTrue(out['errors'], out)
|
||||
self.assertFalse(out['handoffs'])
|
||||
self.assertEqual(history, ['PASS', 'REJECT'])
|
||||
self.assertEqual(self.count('pms_strategy'), 1)
|
||||
active = pms_repo.get_strategy('GRID')
|
||||
self.assertEqual(active['status'], 'ACTIVE')
|
||||
self.assertEqual(active['state'], self.original['state'])
|
||||
self.assertEqual(active['note'], self.original['note'])
|
||||
|
||||
def test_soft_archived_parent_does_not_archive_current_strategy(self):
|
||||
iid = runner._emit_instruction(self.original, DECISION)
|
||||
with self.engine.begin() as conn:
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import copy
|
|||
import datetime as dt
|
||||
import json
|
||||
import unittest
|
||||
import tempfile
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from unittest.mock import patch
|
||||
import test_risk_exit as base
|
||||
from app.core import risk_exit as core, strategy_book as books
|
||||
|
|
@ -23,6 +25,14 @@ def quote(price=10, at=NOW):
|
|||
|
||||
|
||||
class CoreTests(unittest.TestCase):
|
||||
def test_simulation_cached_halt_read_also_rejects_invalid_old_value(self):
|
||||
from app.services import param_store
|
||||
from config.settings import settings
|
||||
key = 'PMS_GLOBAL_EXEC_HALT'
|
||||
with patch.object(settings, 'PMS_TABLE_PREFIX', ''), patch.object(settings, 'INFRA_STRATEGY_BOOK_SCHEMA', True), \
|
||||
patch.object(param_store, 'refresh', return_value={key:{'param_value':'unknown'}}):
|
||||
self.assertTrue(param_store.get_bool(key))
|
||||
|
||||
def test_equal_and_below_trigger_above_does_not(self):
|
||||
for price, trigger in [(9.99,True),(10,True),(10.01,False)]:
|
||||
self.assertEqual(bool(core.user_stop_event(command(),quote(price),now=NOW)[0]),trigger)
|
||||
|
|
@ -49,6 +59,89 @@ class CoreTests(unittest.TestCase):
|
|||
self.assertIsNone(core.user_stop_event(command(issued=NOW),quote(at=NOW-dt.timedelta(seconds=1)),now=NOW)[0])
|
||||
|
||||
|
||||
class ParamTransactionTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
from sqlalchemy import create_engine, event
|
||||
from app.db import session as db
|
||||
from app.services import param_store
|
||||
from config.settings import settings
|
||||
|
||||
tmp = self.enterContext(tempfile.TemporaryDirectory())
|
||||
engine = create_engine('sqlite:///' + tmp + '/params.sqlite')
|
||||
self.addCleanup(engine.dispose)
|
||||
self.enterContext(patch.object(db, 'get_engine', return_value=engine))
|
||||
self.enterContext(patch.object(settings, 'PMS_TABLE_PREFIX', ''))
|
||||
self.enterContext(patch.object(settings, 'INFRA_ROTATION_DOCUMENT_SCHEMA', True))
|
||||
self.enterContext(patch.object(param_store, '_cache', {'loaded':False, 'at':0, 'data':{}, 'error':None}))
|
||||
self.fail_state = False
|
||||
self.at_state_update = None
|
||||
|
||||
@event.listens_for(engine, 'before_cursor_execute', retval=True)
|
||||
def dialect(conn, cursor, sql, params, context, many):
|
||||
sql = sql.replace(' FOR UPDATE', '')
|
||||
if 'ON DUPLICATE KEY UPDATE' in sql:
|
||||
if 'pms_runtime_param' in sql:
|
||||
sql = sql.replace('ON DUPLICATE KEY UPDATE', 'ON CONFLICT(param_key) DO UPDATE SET')
|
||||
else:
|
||||
sql = sql.partition('ON DUPLICATE KEY UPDATE')[0] + 'ON CONFLICT DO NOTHING'
|
||||
if sql.startswith('UPDATE pms_rotation_document SET payload_json=') and self.at_state_update:
|
||||
self.at_state_update()
|
||||
if self.fail_state:
|
||||
raise RuntimeError('模拟状态文档末笔写入失败')
|
||||
return sql, params
|
||||
|
||||
with engine.begin() as conn:
|
||||
conn.exec_driver_sql('CREATE TABLE pms_runtime_param (param_key TEXT PRIMARY KEY, param_value TEXT, '
|
||||
'updated_by TEXT, updated_at TEXT)')
|
||||
conn.exec_driver_sql('CREATE TABLE pms_rotation_document (doc_id TEXT PRIMARY KEY, kind TEXT, '
|
||||
'payload_json TEXT, revision INTEGER, updated_at TEXT)')
|
||||
self.key = 'PMS_ROTATION_CONTROL'
|
||||
self.assertTrue(param_store.set_param('PMS_ROTATION_MODE', 'paper', 'isolated')['ok'])
|
||||
self.assertTrue(param_store.set_param(self.key, 'pause_all', 'isolated')['ok'])
|
||||
self.pool = self.enterContext(ThreadPoolExecutor(max_workers=1))
|
||||
|
||||
def test_real_control_and_document_publish_together_across_rollback_and_commit(self):
|
||||
from app.services import rotation_service, param_store
|
||||
|
||||
def check_readers():
|
||||
self.assertEqual(param_store.get(self.key), 'exit_disable')
|
||||
self.assertEqual(self.pool.submit(param_store.get, self.key).result(timeout=3), 'pause_all')
|
||||
def fresh_reader():
|
||||
return param_store.refresh(force=True)[self.key]['param_value']
|
||||
self.assertEqual(self.pool.submit(fresh_reader).result(timeout=3), 'pause_all')
|
||||
|
||||
self.at_state_update = check_readers
|
||||
self.fail_state = True
|
||||
with self.assertRaisesRegex(RuntimeError, '末笔写入失败'):
|
||||
rotation_service._set_control('exit_disable', 'isolated')
|
||||
self.assertEqual(pms_repo.get_param(self.key), 'pause_all')
|
||||
self.assertEqual(param_store.get(self.key), 'pause_all')
|
||||
self.assertIsNone(rotation_service.store.get('state:paper')[0])
|
||||
self.fail_state = False
|
||||
rotation_service._set_control('exit_disable', 'isolated')
|
||||
self.assertTrue(rotation_service.store.get('state:paper')[0]['rotation']['disabled'])
|
||||
self.assertEqual(self.pool.submit(param_store.get, self.key).result(timeout=3), 'exit_disable')
|
||||
|
||||
def test_savepoint_rollback_and_release_preserve_outer_value_until_commit(self):
|
||||
from app.db import session as db
|
||||
from app.services import param_store
|
||||
|
||||
with db.transaction():
|
||||
self.assertTrue(param_store.set_param(self.key, 'pause_new')['ok'])
|
||||
with self.assertRaisesRegex(ValueError, '回滚'):
|
||||
with db.savepoint():
|
||||
self.assertTrue(param_store.set_param(self.key, 'run')['ok'])
|
||||
self.assertEqual(param_store.get(self.key), 'run')
|
||||
raise ValueError('回滚')
|
||||
self.assertEqual(param_store.get(self.key), 'pause_new')
|
||||
with db.savepoint():
|
||||
self.assertTrue(param_store.set_param(self.key, 'exit_disable')['ok'])
|
||||
self.assertEqual(param_store.get(self.key), 'exit_disable')
|
||||
self.assertEqual(param_store.get(self.key), 'exit_disable')
|
||||
self.assertEqual(self.pool.submit(param_store.get, self.key).result(timeout=3), 'pause_all')
|
||||
self.assertEqual(self.pool.submit(param_store.get, self.key).result(timeout=3), 'exit_disable')
|
||||
|
||||
|
||||
class ServiceTests(base.ServiceTests):
|
||||
def put_stop(self, price=10, cid='STOP1'):
|
||||
c=command(price,cid)
|
||||
|
|
@ -60,6 +153,28 @@ class ServiceTests(base.ServiceTests):
|
|||
service.drive(CODE,now=NOW)
|
||||
return result
|
||||
|
||||
def test_old_freeze_and_stop_survive_500_newer_effective_commands(self):
|
||||
from sqlalchemy import text
|
||||
from app.services import rotation_service
|
||||
|
||||
self.insert('pms_command', dict(book_id='original', command_id='old-freeze', cmd_class='param',
|
||||
cmd_type='FREEZE_STOCK', ts_code=CODE, status='EFFECTIVE'))
|
||||
self.put_stop(cid='old-stop')
|
||||
other = '600002.SH'
|
||||
with self.engine.begin() as conn:
|
||||
sql = text("INSERT INTO pms_command (book_id,command_id,cmd_class,cmd_type,ts_code,status) "
|
||||
"VALUES ('original',:cid,'param',:kind,:code,'EFFECTIVE')")
|
||||
conn.execute(sql, [{'cid':kind+str(i), 'kind':kind, 'code':other}
|
||||
for kind in ('FREEZE_STOCK', 'SET_STOP_PRICE') for i in range(500)])
|
||||
freeze = ('FREEZE_STOCK',)
|
||||
self.assertEqual(len(pms_repo.list_effective_stock_params(freeze)), 501)
|
||||
filtered = pms_repo.list_effective_stock_params(freeze, ts_code=CODE)
|
||||
self.assertEqual([row['command_id'] for row in filtered], ['old-freeze'])
|
||||
self.assertIn(CODE, rotation_service.micro_buy_restrictions())
|
||||
self.assertEqual(set(rotation_service.micro_buy_restrictions(CODE)), {CODE})
|
||||
self.assertEqual(service._stop_commands()[CODE]['command_id'], 'old-stop')
|
||||
self.assertTrue(service.observe_user_stop(CODE, quote(), now=NOW)['triggered'])
|
||||
|
||||
def test_scan_generates_one_exit_without_model_or_second_signal(self):
|
||||
self.put_stop()
|
||||
with patch.object(market,'execution_quote',return_value=quote()) as fetch, \
|
||||
|
|
@ -187,6 +302,77 @@ class ServiceTests(base.ServiceTests):
|
|||
self.assertEqual(self.state()['phase'],'EXIT_PENDING');self.assertEqual(self.instructions(),[])
|
||||
service.drive(CODE,now=NOW);self.assertEqual(len(self.instructions()),1)
|
||||
|
||||
def test_missing_pause_defaults_and_bad_input_cannot_release_pause(self):
|
||||
from app.services import param_store
|
||||
self.assertFalse(param_store.execution_halted())
|
||||
self.change("INSERT INTO pms_runtime_param VALUES ('PMS_GLOBAL_EXEC_HALT','True')")
|
||||
for value in (None, 'ture', 2):
|
||||
with self.subTest(value=value):
|
||||
self.assertFalse(param_store.set_param('PMS_GLOBAL_EXEC_HALT', value)['ok'])
|
||||
self.assertTrue(param_store.execution_halted())
|
||||
self.change("UPDATE pms_runtime_param SET param_value='unknown'")
|
||||
self.assertTrue(param_store.execution_halted())
|
||||
with patch.object(pms_repo,'get_param',side_effect=RuntimeError('isolated')):
|
||||
self.assertTrue(param_store.execution_halted())
|
||||
|
||||
def test_pause_writer_uses_same_account_transaction_as_exit(self):
|
||||
from app.db import session as db
|
||||
from app.services import param_store
|
||||
|
||||
def save_switch(key, value, by):
|
||||
self.assertIsNotNone(db._TRANSACTION.get())
|
||||
self.assertEqual(key, 'PMS_GLOBAL_EXEC_HALT')
|
||||
db.execute('INSERT INTO pms_runtime_param VALUES (:key,:value)', {'key':key, 'value':str(value)})
|
||||
return 1
|
||||
|
||||
with patch.object(pms_repo, 'set_param', side_effect=save_switch), patch.object(param_store, 'refresh', return_value={}):
|
||||
self.assertTrue(param_store.set_param('PMS_GLOBAL_EXEC_HALT', True)['ok'])
|
||||
self.assertTrue(param_store.execution_halted())
|
||||
self.put_stop()
|
||||
service.observe_user_stop(CODE, quote(), now=NOW)
|
||||
service.drive(CODE, now=NOW)
|
||||
self.assertEqual(self.instructions(), [])
|
||||
|
||||
def test_intraday_scheduler_observes_without_any_execution_while_halted(self):
|
||||
from app import scheduler
|
||||
from app.services import strategy_runner
|
||||
self.put_stop()
|
||||
self.change("INSERT INTO pms_runtime_param VALUES ('PMS_GLOBAL_EXEC_HALT','True')")
|
||||
self.change('UPDATE pms_position SET avail_qty=0')
|
||||
lease = {}
|
||||
original_read = pms_repo.get_param
|
||||
|
||||
def read(key, **opts):
|
||||
return lease.get(key) if key.startswith('INFRA_LOCK_') else original_read(key, **opts)
|
||||
|
||||
with patch.object(scheduler.td, 'is_trade_day', return_value=True), \
|
||||
patch.object(scheduler, 'in_session', return_value=True), \
|
||||
patch.object(pms_repo, 'get_param', side_effect=read), \
|
||||
patch.object(pms_repo, 'set_param', side_effect=lambda k,v,*a: lease.update({k:v})), \
|
||||
patch.object(service, 'datetime') as clock, patch.object(market, 'execution_quote', return_value=quote()), \
|
||||
patch.object(service, 'drive') as drive, patch.object(executor, 'materialize_plans') as plans, \
|
||||
patch.object(executor, 'run_tick') as orders, patch.object(strategy_runner, 'tick') as strategies:
|
||||
clock.now.return_value = NOW
|
||||
result = scheduler.intraday_exec.run()
|
||||
self.assertTrue(result['risk_exit']['user_stops'][CODE]['triggered'], result)
|
||||
self.assertFalse(scheduler.intraday_exec.run()['risk_exit']['user_stops'][CODE]['triggered'])
|
||||
for call in (drive, plans, orders, strategies):
|
||||
call.assert_not_called()
|
||||
self.assertEqual(self.instructions(), [])
|
||||
self.assertEqual(self.state()['phase'], 'EXIT_PENDING')
|
||||
# worker缓存仍读成未暂停时,退出事务重新锁定并读取真实开关。
|
||||
with patch.object(service, '_stop_buys') as stop_buys:
|
||||
service.drive(CODE, now=NOW)
|
||||
stop_buys.assert_not_called()
|
||||
self.assertEqual(self.instructions(), [])
|
||||
self.change("UPDATE pms_runtime_param SET param_value='False'")
|
||||
with patch.object(market, 'execution_quote', return_value=quote(11)):
|
||||
service.scan(now=NOW)
|
||||
service.scan(now=NOW)
|
||||
self.assertEqual(len(self.instructions()), 1)
|
||||
self.assertEqual(self.instructions()[0]['qty'], 1600)
|
||||
self.assertEqual(self.state()['user_stop']['command_id'], 'STOP1')
|
||||
|
||||
def test_other_book_and_no_holding_do_not_create_orders(self):
|
||||
self.put_stop()
|
||||
with books.scope('micro30'):
|
||||
|
|
|
|||
|
|
@ -10,6 +10,9 @@
|
|||
"""
|
||||
import unit_test_guard # noqa: F401,单独运行也禁止联网
|
||||
import os
|
||||
from contextlib import contextmanager
|
||||
from copy import deepcopy
|
||||
from threading import RLock
|
||||
import sys
|
||||
import traceback
|
||||
from datetime import date, datetime
|
||||
|
|
@ -39,6 +42,7 @@ class FakeRepo:
|
|||
self.strategies = {}
|
||||
self._lot_id = 0
|
||||
self._strategy_id = 0
|
||||
self._auto_open_lock = RLock()
|
||||
|
||||
# --- runtime param ---
|
||||
def all_params(self):
|
||||
|
|
@ -49,7 +53,7 @@ class FakeRepo:
|
|||
self.params[key] = str(value)
|
||||
return 1
|
||||
|
||||
def get_param(self, key):
|
||||
def get_param(self, key, *, for_update=False):
|
||||
return self.params.get(key)
|
||||
|
||||
# --- command ---
|
||||
|
|
@ -111,9 +115,10 @@ class FakeRepo:
|
|||
and c["cmd_type"] == cmd_type and (not ts_code or c["ts_code"] == ts_code)]
|
||||
return sorted(hits, key=lambda c: c["id"])[-1] if hits else None
|
||||
|
||||
def list_effective_stock_params(self, cmd_types=None):
|
||||
return [c for c in self.commands.values()
|
||||
def list_effective_stock_params(self, cmd_types=None, *, ts_code=None):
|
||||
return [c for c in reversed(self.commands.values())
|
||||
if c["cmd_class"] == "param" and c["status"] == "EFFECTIVE" and c["ts_code"]
|
||||
and (ts_code is None or c["ts_code"] == ts_code)
|
||||
and (not cmd_types or c["cmd_type"] in cmd_types)]
|
||||
|
||||
# --- plan ---
|
||||
|
|
@ -460,6 +465,11 @@ class FakeRepo:
|
|||
s = self.strategies.get(strategy_id)
|
||||
return dict(s) if s else None
|
||||
|
||||
@contextmanager
|
||||
def strategy_guard(self, ts_code):
|
||||
with self._auto_open_lock:
|
||||
yield
|
||||
|
||||
def active_strategies(self):
|
||||
return self.list_strategies(statuses=["ACTIVE"], limit=1000)
|
||||
|
||||
|
|
@ -472,6 +482,16 @@ class FakeRepo:
|
|||
return {(r["ts_code"], r["action"]) for r in self.ledger
|
||||
if r.get("verdict") == "REJECT" and r.get("arbiter") == "system"}
|
||||
|
||||
@contextmanager
|
||||
def account_decision_guard(self):
|
||||
with self._auto_open_lock:
|
||||
instructions, ledger = deepcopy(self.instructions), deepcopy(self.ledger)
|
||||
try:
|
||||
yield
|
||||
except BaseException:
|
||||
self.instructions, self.ledger = instructions, ledger
|
||||
raise
|
||||
|
||||
def system_pass_count_today(self, since):
|
||||
# 今天系统自决自动采纳的新建仓只数: 账本里 OPEN 的 PASS 且 reason 以「系统采纳」开头
|
||||
return sum(1 for r in self.ledger
|
||||
|
|
|
|||
Loading…
Reference in New Issue