接入微盘30股观察页面与分策略轮换基础能力
This commit is contained in:
parent
27cd570f95
commit
e13740e85e
|
|
@ -9,3 +9,6 @@ logs
|
|||
.claude
|
||||
.codex
|
||||
.agents
|
||||
|
||||
# 历史实验数据不打进交易运行镜像。
|
||||
research/
|
||||
|
|
|
|||
|
|
@ -0,0 +1,242 @@
|
|||
"""微盘轮换状态与订单计算。纯函数,无行情、数据库或下单依赖。"""
|
||||
import copy
|
||||
import datetime as dt
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
|
||||
RULE = 'micro30-v1-20261027'
|
||||
SIZE = 30
|
||||
PHASES = ('original', 'entering_micro', 'micro', 'returning_original')
|
||||
|
||||
|
||||
def initial_state():
|
||||
return {'phase': 'original', 'cycle': 0, 'last_snapshot': None, 'entry_date': None,
|
||||
'exit_date': None, 'reason': None, 'target': [], 'entry_budget': 0., 'disabled': False,
|
||||
'entry_quantities': {}, 'consumed_entry': None, 'closed_date': None}
|
||||
|
||||
|
||||
def snapshot_check(value, execution_date, *, require_frozen=True):
|
||||
errors = []
|
||||
if not isinstance(value, dict):
|
||||
return ['没有监控快照']
|
||||
if value.get('rule_version') != RULE or value.get('basket_size') != SIZE:
|
||||
errors.append('策略规则版本不一致')
|
||||
if value.get('effective_date') != execution_date or str(value.get('data_date', '')) >= execution_date:
|
||||
errors.append('快照日期不适用于本次执行')
|
||||
if require_frozen and value.get('frozen') is not True:
|
||||
errors.append('执行快照尚未冻结')
|
||||
keys = ('rule_version', 'basket_size', 'data_date', 'effective_date', 'signal_ready',
|
||||
'basket_ready', 'signal', 'members', 'source_hashes', 'errors')
|
||||
try:
|
||||
body = {k: value[k] for k in keys}
|
||||
digest = hashlib.sha256(json.dumps(body, sort_keys=True, ensure_ascii=False,
|
||||
separators=(',', ':'), allow_nan=False).encode()).hexdigest()
|
||||
if digest != value.get('snapshot_version'):
|
||||
errors.append('快照摘要不一致')
|
||||
signal = value['signal']
|
||||
if signal.get('date') != value['data_date'] or signal.get('ready') is not value['signal_ready']:
|
||||
errors.append('信号就绪或日期字段冲突')
|
||||
if any(type(signal.get(k)) is not bool for k in ('growth_break', 'low', 'high')):
|
||||
errors.append('信号必须是明确的布尔值')
|
||||
if value['basket_ready']:
|
||||
members = value['members']
|
||||
if len(members) != SIZE or len({m['code'] for m in members}) != SIZE:
|
||||
errors.append('目标名单不是30只唯一股票')
|
||||
if any(m.get('eligible') is not True or m.get('rank') != i+1 for i, m in enumerate(members)):
|
||||
errors.append('名单资格或排名不完整')
|
||||
except (KeyError, ValueError, TypeError):
|
||||
errors.append('快照字段不完整或数字非法')
|
||||
return errors
|
||||
|
||||
|
||||
def transition(state, snapshot, *, micro_qty, original_qty, budget, control='run'):
|
||||
s = copy.deepcopy(state)
|
||||
if s['phase'] not in PHASES:
|
||||
raise ValueError('未知轮换阶段')
|
||||
ready = bool(snapshot and snapshot.get('signal_ready'))
|
||||
signal = snapshot['signal'] if ready else {}
|
||||
if control == 'pause_all':
|
||||
return s
|
||||
if control == 'exit_disable':
|
||||
s['disabled'] = True
|
||||
if s['phase'] != 'original' or micro_qty:
|
||||
s['phase'], s['reason'] = 'returning_original', '用户退出并停用'
|
||||
if ready and signal.get('high') and s['phase'] in ('entering_micro', 'micro'):
|
||||
s.update(phase='returning_original', exit_date=snapshot['effective_date'], reason='微盘活跃度高位')
|
||||
if s['phase'] == 'returning_original' and micro_qty == 0:
|
||||
s.update(phase='original', target=[], entry_budget=0., entry_quantities={},
|
||||
closed_date=(snapshot or {}).get('effective_date') or s.get('exit_date'))
|
||||
return s # 清完当天不立即重开下一轮。
|
||||
fresh_entry = snapshot and snapshot.get('snapshot_version') != s.get('consumed_entry') and snapshot.get('effective_date') != s.get('closed_date')
|
||||
if s['phase'] == 'original' and fresh_entry and not s['disabled'] and control == 'run' and ready and snapshot.get('basket_ready') and not signal.get('high') and (signal.get('growth_break') or signal.get('low')):
|
||||
if micro_qty:
|
||||
raise ValueError('旧微盘持仓未清完,不能再次入场')
|
||||
s.update(phase='entering_micro', cycle=s['cycle']+1, entry_date=snapshot['effective_date'],
|
||||
entry_budget=max(0., budget), entry_quantities={}, consumed_entry=snapshot['snapshot_version'],
|
||||
reason='成长主线瓦解' if signal['growth_break'] else '微盘活跃度极低')
|
||||
if snapshot and snapshot.get('basket_ready') and s['phase'] in ('entering_micro', 'micro'):
|
||||
s['target'] = [r['code'] for r in snapshot['members']]
|
||||
if snapshot:
|
||||
s['last_snapshot'] = snapshot['snapshot_version']
|
||||
return s
|
||||
|
||||
|
||||
def board(code):
|
||||
if code.startswith('68'):
|
||||
return 'STAR'
|
||||
if code.startswith('30'):
|
||||
return 'CHINEXT'
|
||||
return 'MAIN'
|
||||
|
||||
|
||||
def min_qty(code):
|
||||
return 200 if board(code) == 'STAR' else 100
|
||||
|
||||
|
||||
def fee(value, side, *, rate=.0003, minimum=5., sell_tax=.0005, transfer_rate=.00001):
|
||||
return round(max(minimum, value*rate)+value*transfer_rate+(value*sell_tax if side == 'sell' else 0), 2)
|
||||
|
||||
|
||||
def buy_qty(code, price, budget, costs):
|
||||
if not math.isfinite(price) or price <= 0 or not math.isfinite(budget) or budget <= 0:
|
||||
return 0
|
||||
qty = int(budget/price)//100*100
|
||||
while qty >= min_qty(code) and qty*price+fee(qty*price, 'buy', **costs) > budget:
|
||||
qty -= 100
|
||||
return qty if qty >= min_qty(code) else 0
|
||||
|
||||
|
||||
def preflight(members, budget, allowed_boards, costs):
|
||||
issues = []
|
||||
if len(members) != SIZE:
|
||||
return ['目标名单数量不是30只']
|
||||
for m in members:
|
||||
if board(m['code']) not in allowed_boards:
|
||||
issues.append(m['code']+'板块权限未确认')
|
||||
if not buy_qty(m['code'], float(m['close']), budget/SIZE, costs):
|
||||
issues.append(m['code']+'等资金份额不足最小交易单位及费用')
|
||||
return issues
|
||||
|
||||
|
||||
def orders(state, snapshot, *, original, micro, quotes, cash, account_mv, cap_amount,
|
||||
reserve_cash=0., pending=(), control='run', blacklisted=(), costs=None):
|
||||
"""只形成意图,不把计划卖出计入现金。调用者落表后再执行。"""
|
||||
from app.core.planner import plan_reduce_exposure
|
||||
costs = costs or {}
|
||||
result, waits = [], []
|
||||
if control == 'pause_all':
|
||||
return result, ['用户暂停全部执行']
|
||||
active_codes = {r['ts_code'] for r in pending}
|
||||
original_by = {r['ts_code']: r for r in original if r.get('total_qty', 0) > 0}
|
||||
micro_by = {r['ts_code']: r for r in micro if r.get('total_qty', 0) > 0}
|
||||
phase = state['phase']
|
||||
if phase == 'entering_micro':
|
||||
positions = [{**p, 'price': quotes.get(c, {}).get('price')} for c, p in original_by.items()]
|
||||
release = sum(p['total_qty']*p['price'] for p in positions if p.get('price'))
|
||||
plan = plan_reduce_exposure(release_amount=release, positions=positions)
|
||||
seen = set()
|
||||
for item in plan['items']:
|
||||
if item['side'] != 'sell':
|
||||
continue
|
||||
c = item['ts_code']
|
||||
if c in seen:
|
||||
continue
|
||||
seen.add(c)
|
||||
# 同股先完成加仓部分,再由下一跳重算剩余批次;不能提前整股清仓。
|
||||
result.append({'book_id': 'original', 'ts_code': c, 'side': 'sell',
|
||||
'qty': min(int(item['qty']), original_by[c]['total_qty']), 'reason': item['reason']})
|
||||
for c, p in original_by.items():
|
||||
if c not in seen and p.get('total_qty'):
|
||||
result.append({'book_id': 'original', 'ts_code': c, 'side': 'sell', 'qty': p['total_qty'], 'reason': '轮换剩余持仓退出'})
|
||||
target = set(state['target']) if phase in ('entering_micro', 'micro') else set()
|
||||
for c, p in micro_by.items():
|
||||
if phase == 'returning_original' or (snapshot and snapshot.get('basket_ready') and c not in target):
|
||||
result.append({'book_id': 'micro30', 'ts_code': c, 'side': 'sell', 'qty': p['total_qty'], 'reason': '高位清仓或成员调出'})
|
||||
result = [r for r in result if r['ts_code'] not in active_codes]
|
||||
if phase not in ('entering_micro', 'micro') or control != 'run' or not snapshot or not snapshot.get('basket_ready'):
|
||||
return result, waits
|
||||
available = max(0., min(cash-reserve_cash, cap_amount-account_mv))
|
||||
# 入场期间保留既定每股资金目标,以便原策略分批回款后继续完成建仓。
|
||||
missing = sorted(c for c in target if c not in micro_by)
|
||||
candidates = sorted(target) if phase == 'entering_micro' else missing
|
||||
per_new = available/len(missing) if missing else 0.
|
||||
for c in candidates:
|
||||
if c in original_by or c in active_codes or c in blacklisted:
|
||||
waits.append(c+'旧仓、在途委托或黑名单阻止买入')
|
||||
continue
|
||||
q = quotes.get(c, {})
|
||||
price = q.get('price')
|
||||
if not price or price <= 0:
|
||||
waits.append(c+'没有有效行情')
|
||||
continue
|
||||
owned_qty = micro_by.get(c, {}).get('total_qty', 0)
|
||||
if phase == 'entering_micro':
|
||||
quantities = state.setdefault('entry_quantities', {})
|
||||
# 首次有行情时固定数量,部分成交只补剩余,不随价格下跌重新等权。
|
||||
if c not in quantities:
|
||||
quantities[c] = buy_qty(c, price, state['entry_budget']/SIZE, costs)
|
||||
remaining_qty = max(0, quantities[c]-owned_qty)
|
||||
desired = remaining_qty*price+fee(remaining_qty*price, 'buy', **costs) if remaining_qty else 0.
|
||||
else:
|
||||
desired = per_new
|
||||
budget = min(available, desired)
|
||||
qty = buy_qty(c, price, budget, costs)
|
||||
if phase == 'entering_micro':
|
||||
qty = min(qty, remaining_qty)
|
||||
if qty:
|
||||
result.append({'book_id': 'micro30', 'ts_code': c, 'side': 'buy', 'qty': qty, 'reason': '初始建仓补齐' if phase == 'entering_micro' else '名单调入'})
|
||||
available -= qty*price+fee(qty*price, 'buy', **costs)
|
||||
return result, waits
|
||||
|
||||
|
||||
def apply_paper_fill(account, order, price, trade_date, costs, prices=None):
|
||||
"""独立纸面现金账,不接真实成交。相同成交键只记一次,T+1按买入日约束。"""
|
||||
key = order['id']
|
||||
if key in account['fills']:
|
||||
return False
|
||||
book, code, side, qty = order['book_id'], order['ts_code'], order['side'], order['qty']
|
||||
positions = account['books'][book]
|
||||
position = positions.setdefault(code, {'qty': 0, 'cost': 0., 'lots': []})
|
||||
amount = qty*price
|
||||
fees = fee(amount, side, **costs)
|
||||
from app.core import rotation_account as capital
|
||||
values = {}
|
||||
prices = prices or {code: price}
|
||||
for b in ('original', 'micro30'):
|
||||
if any(c not in prices for c, p in account['books'][b].items() if p['qty']):
|
||||
raise ValueError('纸面划拨前需要完整持仓估值')
|
||||
values[b] = sum(p['qty']*prices.get(c, 0) for c, p in account['books'][b].items())
|
||||
if 'capital' not in account:
|
||||
account['capital'] = capital.seed(account['cash'], values)
|
||||
cashbook = account['capital']
|
||||
if side == 'buy':
|
||||
if account['cash']+1e-8 < amount+fees:
|
||||
raise ValueError('纸面账户现金不足')
|
||||
need = max(0., amount+fees-cashbook['cash'][book])
|
||||
if need:
|
||||
capital.transfer(cashbook, 'original' if book == 'micro30' else 'micro30', book, need, values, 'fund:'+key)
|
||||
capital.fill(cashbook, book, side, amount, fees, key)
|
||||
account['cash'] -= amount+fees
|
||||
position['qty'] += qty
|
||||
position['cost'] += amount+fees
|
||||
position['lots'].append({'date': trade_date, 'qty': qty})
|
||||
else:
|
||||
available = sum(r['qty'] for r in position['lots'] if r['date'] < trade_date)
|
||||
if qty > available or qty > position['qty']:
|
||||
raise ValueError('纸面账本可卖数量不足')
|
||||
capital.fill(cashbook, book, side, amount, fees, key)
|
||||
old_qty = position['qty']
|
||||
position['qty'] -= qty
|
||||
released_cost = position['cost']*qty/old_qty
|
||||
position['cost'] -= released_cost
|
||||
account['cash'] += amount-fees
|
||||
account['realized'][book] += amount-fees-released_cost
|
||||
remaining = qty
|
||||
for lot in position['lots']:
|
||||
taken = min(remaining, lot['qty']) if lot['date'] < trade_date else 0
|
||||
lot['qty'] -= taken
|
||||
remaining -= taken
|
||||
account['fees'][book] += fees
|
||||
account['fills'][key] = {**order, 'price': price, 'date': trade_date, 'fee': fees}
|
||||
return True
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
"""分策略现金、内部划拨和单位净值。划拨只改变本金和份额,不产生利润。"""
|
||||
import math
|
||||
|
||||
BOOKS = ('original', 'micro30')
|
||||
|
||||
|
||||
def seed(cash, values):
|
||||
if cash < 0 or any(v < 0 or not math.isfinite(v) for v in [cash, *values.values()]):
|
||||
raise ValueError('初始资产非法')
|
||||
return {'cash': {'original': cash, 'micro30': 0.},
|
||||
'units': {b: values.get(b, 0)+(cash if b == 'original' else 0) for b in BOOKS},
|
||||
'net_capital': {b: values.get(b, 0)+(cash if b == 'original' else 0) for b in BOOKS},
|
||||
'fees': {b: 0. for b in BOOKS}, 'transfers': {}, 'fills': {}}
|
||||
|
||||
|
||||
def metrics(account, values):
|
||||
result = {}
|
||||
for b in BOOKS:
|
||||
equity = account['cash'][b]+values.get(b, 0.)
|
||||
units = account['units'][b]
|
||||
result[b] = {'cash': account['cash'][b], 'market_value': values.get(b, 0.), 'equity': equity,
|
||||
'net_capital': account['net_capital'][b], 'profit': equity-account['net_capital'][b],
|
||||
'nav': equity/units if units > 1e-8 else None, 'fees': account['fees'][b]}
|
||||
result['account'] = {'cash': sum(account['cash'].values()), 'market_value': sum(values.values()),
|
||||
'equity': sum(r['equity'] for r in result.values()),
|
||||
'profit': sum(r['profit'] for r in result.values()), 'fees': sum(account['fees'].values())}
|
||||
return result
|
||||
|
||||
|
||||
def transfer(account, source, target, amount, values, key):
|
||||
if key in account['transfers']:
|
||||
old = account['transfers'][key]
|
||||
if (old['source'], old['target'], old['amount']) != (source, target, amount):
|
||||
raise ValueError('划拨幂等键内容不一致')
|
||||
return False
|
||||
if source == target or source not in BOOKS or target not in BOOKS or amount <= 0 or not math.isfinite(amount):
|
||||
raise ValueError('划拨参数非法')
|
||||
if account['cash'][source]+1e-6 < amount:
|
||||
raise ValueError('策略可划拨现金不足')
|
||||
before = metrics(account, values)
|
||||
for b, change in ((source, -amount), (target, amount)):
|
||||
nav = before[b]['nav']
|
||||
if nav is None:
|
||||
if abs(before[b]['equity']) > 1e-6:
|
||||
raise ValueError('策略有资产但没有可核实份额')
|
||||
nav = 1.
|
||||
if nav <= 0:
|
||||
raise ValueError('非正净值不能自动划拨')
|
||||
account['cash'][b] += change
|
||||
account['net_capital'][b] += change
|
||||
account['units'][b] += change/nav
|
||||
account['transfers'][key] = {'source': source, 'target': target, 'amount': amount,
|
||||
'source_nav': before[source]['nav'], 'target_nav': before[target]['nav']}
|
||||
return True
|
||||
|
||||
|
||||
def fill(account, book, side, amount, fee_amount, key):
|
||||
if key in account['fills']:
|
||||
if account['fills'][key] != {'book': book, 'side': side, 'amount': amount, 'fee': fee_amount}:
|
||||
raise ValueError('成交资金幂等键冲突')
|
||||
return False
|
||||
if book not in BOOKS or side not in ('buy', 'sell') or amount <= 0 or fee_amount < 0:
|
||||
raise ValueError('成交资金参数非法')
|
||||
change = (amount if side == 'sell' else -amount)-fee_amount
|
||||
if account['cash'][book]+change < -1e-6:
|
||||
raise ValueError('策略现金不足,不能跨账本透支')
|
||||
account['cash'][book] += change
|
||||
account['fees'][book] += fee_amount
|
||||
account['fills'][key] = {'book': book, 'side': side, 'amount': amount, 'fee': fee_amount}
|
||||
return True
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
"""策略账本范围。默认原策略,全账户范围只允许读取。"""
|
||||
import contextlib
|
||||
import contextvars
|
||||
import re
|
||||
|
||||
ORIGINAL = 'original'
|
||||
MICRO = 'micro30'
|
||||
CURRENT = contextvars.ContextVar('strategy_book', default=ORIGINAL)
|
||||
TABLES = frozenset({'pms_command', 'pms_plan', 'pms_position', 'pms_lot',
|
||||
'pms_instruction', 'pms_proposal', 'pms_action_ledger', 'pms_cash_flow', 'pms_strategy'})
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def scope(book):
|
||||
if book not in (ORIGINAL, MICRO, 'all'):
|
||||
raise ValueError('未知策略账本')
|
||||
token = CURRENT.set(book)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
CURRENT.reset(token)
|
||||
|
||||
|
||||
def scoped_sql(sql, book=None):
|
||||
"""只改既有单表语句的顶层条件,字符串与括号内部不参与定位。"""
|
||||
book = book or CURRENT.get()
|
||||
if book not in (ORIGINAL, MICRO, 'all'):
|
||||
raise ValueError('未知策略账本')
|
||||
match = re.search(r'\b(?:FROM|UPDATE|INTO)\s+(pms_\w+)\b', sql, re.I)
|
||||
if not match or match.group(1) not in TABLES:
|
||||
return sql
|
||||
verb = sql.lstrip().split()[0].upper()
|
||||
if verb not in ('SELECT', 'INSERT', 'UPDATE', 'DELETE'):
|
||||
return sql
|
||||
if book == 'all':
|
||||
if verb != 'SELECT':
|
||||
raise ValueError('全账户范围禁止直接写入策略账本')
|
||||
return sql
|
||||
if verb == 'INSERT':
|
||||
m = re.match(r'(\s*INSERT\s+INTO\s+pms_\w+\s*\()([^)]*)(\)\s*VALUES\s*\()', sql, re.I)
|
||||
if not m or re.search(r'\bbook_id\b', m.group(2), re.I):
|
||||
raise ValueError('策略账本INSERT形状不受支持或重复指定归属')
|
||||
return m.group(1)+'book_id, '+m.group(2)+m.group(3)+"'"+book+"', "+sql[m.end():]
|
||||
# 屏蔽引号,不改变字符下标。顶层WHERE整体包裹,避免OR越过账本条件。
|
||||
masked = re.sub(r"'(?:''|\\.|[^'])*'|\"(?:\\.|[^\"])*\"", lambda m: ' '*len(m.group()), sql)
|
||||
depth, tokens = 0, []
|
||||
for m in re.finditer(r'\(|\)|\b(?:WHERE|GROUP\s+BY|ORDER\s+BY|LIMIT|FOR\s+UPDATE)\b', masked, re.I):
|
||||
if m.group() == '(':
|
||||
depth += 1
|
||||
elif m.group() == ')':
|
||||
depth -= 1
|
||||
elif depth == 0:
|
||||
tokens.append((m.group().upper(), m.start(), m.end()))
|
||||
where = next((t for t in tokens if t[0] == 'WHERE'), None)
|
||||
tail = next((t for t in tokens if t[0] != 'WHERE'), None)
|
||||
end = tail[1] if tail else len(sql)
|
||||
condition = "book_id = '"+book+"'"
|
||||
if where:
|
||||
return sql[:where[1]]+'WHERE '+condition+' AND ('+sql[where[2]:end].strip()+') '+sql[end:]
|
||||
return sql[:end].rstrip()+' WHERE '+condition+' '+sql[end:]
|
||||
|
|
@ -19,6 +19,8 @@ import logging
|
|||
import re
|
||||
import time
|
||||
import threading
|
||||
import contextlib
|
||||
import contextvars
|
||||
|
||||
from sqlalchemy import create_engine, text
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
|
@ -29,6 +31,30 @@ logger = logging.getLogger("pms.db")
|
|||
|
||||
_engines = {}
|
||||
_lock = threading.Lock()
|
||||
_TRANSACTION = contextvars.ContextVar('pms_transaction', default=None)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def transaction():
|
||||
"""跨多条单表语句的原子事务。只在显式业务编排中启用。"""
|
||||
existing = _TRANSACTION.get()
|
||||
if existing is not None:
|
||||
yield existing
|
||||
return
|
||||
with get_engine('proxy').begin() as connection:
|
||||
token = _TRANSACTION.set(connection)
|
||||
try:
|
||||
yield connection
|
||||
finally:
|
||||
_TRANSACTION.reset(token)
|
||||
|
||||
@contextlib.contextmanager
|
||||
def savepoint():
|
||||
"""一个子委托失败只回滚自身,已规划的退出阶段仍可提交。"""
|
||||
with transaction() as connection:
|
||||
with connection.begin_nested():
|
||||
yield connection
|
||||
|
||||
|
||||
# 短路冷却: 库刚连挂过就直接快速失败, 不再逐次等 TCP 超时。
|
||||
# 目的 —— 库不可达时页面仍能秒开 (每个接口只吃一次超时), 调度任务也不会被拖死。
|
||||
|
|
@ -114,6 +140,9 @@ TABLE_PREFIX = _load_prefix()
|
|||
|
||||
def map_tables(sql: str) -> str:
|
||||
"""按实例表名前缀改写 SQL 里的 pms_* 表名; 前缀为空原样返回。"""
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
from app.core.strategy_book import scoped_sql
|
||||
sql = scoped_sql(str(sql))
|
||||
if not TABLE_PREFIX:
|
||||
return str(sql)
|
||||
return _PMS_TBL_RE.sub(TABLE_PREFIX + r"\1", str(sql))
|
||||
|
|
@ -170,6 +199,9 @@ def ping(name: str = "proxy") -> dict:
|
|||
def fetch_all(sql: str, params=None, source: str = "proxy") -> list:
|
||||
sql = map_tables(sql)
|
||||
assert_single_table(sql)
|
||||
connection = _TRANSACTION.get()
|
||||
if connection is not None and source == 'proxy':
|
||||
return [dict(r) for r in connection.execute(text(sql), params or {}).mappings().fetchall()]
|
||||
_check_cooldown(source)
|
||||
try:
|
||||
with get_engine(source).connect() as c:
|
||||
|
|
@ -203,6 +235,9 @@ def execute(sql: str, params=None, source: str = "proxy") -> int:
|
|||
"""写操作 (INSERT/UPDATE/DELETE)。返回受影响行数。"""
|
||||
sql = map_tables(sql)
|
||||
assert_single_table(sql)
|
||||
connection = _TRANSACTION.get()
|
||||
if connection is not None and source == 'proxy':
|
||||
return int(connection.execute(text(sql), params or {}).rowcount or 0)
|
||||
_check_cooldown(source)
|
||||
try:
|
||||
with get_engine(source).begin() as c:
|
||||
|
|
@ -224,6 +259,9 @@ def execute_many(sql: str, seq_params: list, source: str = "proxy") -> int:
|
|||
assert_single_table(sql)
|
||||
if not seq_params:
|
||||
return 0
|
||||
connection = _TRANSACTION.get()
|
||||
if connection is not None and source == 'proxy':
|
||||
return int(connection.execute(text(sql), seq_params).rowcount or 0)
|
||||
_check_cooldown(source)
|
||||
try:
|
||||
with get_engine(source).begin() as c:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,67 @@
|
|||
"""轮换文档仓储。快照、纸面账本、控制、验收证据分别保存,单条SQL单表。"""
|
||||
import contextlib
|
||||
import datetime as dt
|
||||
import json
|
||||
from sqlalchemy import text
|
||||
from app.db import session as db
|
||||
|
||||
|
||||
def encode(value):
|
||||
return json.dumps(value, ensure_ascii=False, sort_keys=True, default=str, allow_nan=False)
|
||||
|
||||
|
||||
def get(key):
|
||||
row = db.fetch_one('SELECT payload_json, revision FROM pms_rotation_document WHERE doc_id=:key', {'key': key})
|
||||
if row:
|
||||
return json.loads(row['payload_json']), int(row['revision'])
|
||||
return None, 0
|
||||
|
||||
|
||||
def put_once(key, kind, value):
|
||||
"""同键只能保存相同内容;重复快照可重试,历史版本不可覆盖。"""
|
||||
body = encode(value)
|
||||
db.execute('INSERT INTO pms_rotation_document (doc_id,kind,payload_json,revision,updated_at) '
|
||||
'VALUES (:key,:kind,:body,1,:now) ON DUPLICATE KEY UPDATE doc_id=VALUES(doc_id)',
|
||||
{'key': key, 'kind': kind, 'body': body, 'now': dt.datetime.now()})
|
||||
old, _ = get(key)
|
||||
if encode(old) != body:
|
||||
raise ValueError('同一幂等键出现不同内容')
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def locked(key, kind, initial):
|
||||
"""行锁内读、修改、保存同一文档;异常整体回滚。"""
|
||||
db.execute('INSERT INTO pms_rotation_document (doc_id,kind,payload_json,revision,updated_at) '
|
||||
'VALUES (:key,:kind,:body,0,:now) ON DUPLICATE KEY UPDATE doc_id=VALUES(doc_id)',
|
||||
{'key': key, 'kind': kind, 'body': encode(initial), 'now': dt.datetime.now()})
|
||||
with db.transaction():
|
||||
row = db.fetch_one('SELECT payload_json,revision FROM pms_rotation_document WHERE doc_id=:key FOR UPDATE', {'key': key})
|
||||
value = json.loads(row['payload_json'])
|
||||
yield value
|
||||
db.execute('UPDATE pms_rotation_document SET payload_json=:body,revision=:revision,updated_at=:now WHERE doc_id=:key',
|
||||
{'key': key, 'body': encode(value), 'revision': int(row['revision'])+1, 'now': dt.datetime.now()})
|
||||
|
||||
|
||||
def recent(kind, limit=100):
|
||||
rows = db.fetch_all('SELECT doc_id,payload_json,updated_at FROM pms_rotation_document '
|
||||
'WHERE kind=:kind ORDER BY updated_at DESC LIMIT :n', {'kind': kind, 'n': min(1000, int(limit))})
|
||||
return [{'id': r['doc_id'], 'value': json.loads(r['payload_json']), 'updated_at': str(r['updated_at'])} for r in rows]
|
||||
|
||||
|
||||
def stage_locked(key, value):
|
||||
"""在既有行锁事务内先发布阶段,供同事务的下发闸读取;外部仍不可见。"""
|
||||
if db._TRANSACTION.get() is None:
|
||||
raise RuntimeError('阶段写入必须处于持锁事务')
|
||||
db.execute('UPDATE pms_rotation_document SET payload_json=:body WHERE doc_id=:key',
|
||||
{'key': key, 'body': encode(value)})
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def account_guard():
|
||||
"""只获取账户锁,不在退出时覆盖内层更新的资金保留文档。"""
|
||||
db.execute('INSERT INTO pms_rotation_document (doc_id,kind,payload_json,revision,updated_at) '
|
||||
"VALUES ('account-execution','funds_reservation',:body,0,:now) ON DUPLICATE KEY UPDATE doc_id=VALUES(doc_id)",
|
||||
{'body': encode({'epoch_seq': 0, 'available': 0., 'orders': {}}), 'now': dt.datetime.now()})
|
||||
with db.transaction():
|
||||
db.fetch_one("SELECT doc_id FROM pms_rotation_document WHERE doc_id='account-execution' FOR UPDATE")
|
||||
yield
|
||||
|
|
@ -197,7 +197,11 @@ def plan_pull():
|
|||
@guard(trade_day=True)
|
||||
def premarket():
|
||||
from app.services import ledger_service
|
||||
return ledger_service.premarket()
|
||||
result = ledger_service.premarket()
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
from app.services.rotation_service import reset_micro_available
|
||||
result['micro_available'] = reset_micro_available()
|
||||
return result
|
||||
|
||||
|
||||
@celery_app.task(name="pms.command_poll")
|
||||
|
|
@ -344,7 +348,11 @@ def nav_snapshot():
|
|||
在运营日报 (15:30) 之前。同日重跑覆盖, 以最后一次为准。历史不回填
|
||||
(2026-08-28 拍板: 启用日之前的净值继续查人工表格)。"""
|
||||
from app.services import publish_export
|
||||
return publish_export.record_nav_snapshot()
|
||||
result = publish_export.record_nav_snapshot()
|
||||
if settings.INFRA_ROTATION_DOCUMENT_SCHEMA or settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
from app.services.rotation_service import settle_day
|
||||
result['rotation'] = settle_day()
|
||||
return result
|
||||
|
||||
|
||||
@celery_app.task(name="pms.daily_report")
|
||||
|
|
@ -356,7 +364,15 @@ def daily_report():
|
|||
|
||||
|
||||
# ================================================================ beat 调度表
|
||||
@celery_app.task(name='pms.rotation_tick')
|
||||
@guard(trade_day=True, respect_exec_halt=False)
|
||||
def rotation_tick():
|
||||
from app.services.rotation_service import tick
|
||||
return tick()
|
||||
|
||||
|
||||
celery_app.conf.beat_schedule = {
|
||||
'rotation_tick': {'task': 'pms.rotation_tick', 'schedule': crontab(minute='*'), 'options': {'expires': 50}},
|
||||
# 技术面取数 (2026-09-11): 决策系统夜扫实测 00:38 完, 06:30 留足余量; 在计划拉取 (08:40) 之前。
|
||||
"tech_pull": {"task": "pms.tech_pull", "schedule": crontab(hour=6, minute=30)},
|
||||
"plan_pull": {"task": "pms.plan_pull", "schedule": crontab(hour=8, minute=40)},
|
||||
|
|
|
|||
|
|
@ -173,6 +173,10 @@ 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)
|
||||
return res
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -48,6 +48,7 @@ from __future__ import annotations
|
|||
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from config.settings import settings
|
||||
|
||||
from app.core import ws_codec as wsc
|
||||
from app.repo import qmt_repo
|
||||
|
|
@ -140,6 +141,15 @@ def dispatch(*, instruction_id: str, ts_code: str, side: str, qty: int, limit_pr
|
|||
"""递一手给下游。返回 {ok, ref, mode, note, error}; ok=False 时 executor 不改指令状态。"""
|
||||
m = mode()
|
||||
try:
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA and str(side).lower() == 'buy':
|
||||
from app.core import strategy_book
|
||||
from app.services.rotation_service import strategy_buy_blocker
|
||||
if strategy_book.CURRENT.get() == strategy_book.ORIGINAL:
|
||||
from app.repo import pms_repo
|
||||
parent = pms_repo.get_instruction(_parent_of(instruction_id, parent_id))
|
||||
blocked = strategy_buy_blocker(strategy_book.ORIGINAL, parent)
|
||||
if blocked:
|
||||
return {'ok': False, 'ref': None, 'mode': m, 'note': '', 'error': blocked}
|
||||
if str(side).lower() == 'buy':
|
||||
from app.services.execution_conflicts import buy_blocker
|
||||
from app.repo import pms_repo
|
||||
|
|
@ -222,7 +232,11 @@ def _ws(*, instruction_id, ts_code, side, qty, limit_price, valid_until, intent=
|
|||
return {"ok": False, "ref": None, "mode": MODE_WS, "note": "",
|
||||
"error": f"指令不合协议要求 ({e.code}): {e.message}"}
|
||||
|
||||
qmt_repo.enqueue_order(
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
from app.services.rotation_execution import enqueue_with_reservation
|
||||
enqueue_with_reservation(payload, _parent_of(instruction_id, parent_id))
|
||||
else:
|
||||
qmt_repo.enqueue_order(
|
||||
instruction_id=payload["instruction_id"],
|
||||
parent_id=_parent_of(instruction_id, parent_id),
|
||||
ts_code=payload["ts_code"], side=payload["side"], qty=payload["qty"],
|
||||
|
|
|
|||
|
|
@ -17,9 +17,34 @@ def buy_blocker(ts_code):
|
|||
|
||||
def queued_buy_blocker(row):
|
||||
"""发出前再次检查。异常由通道保留队列等待,不按无冲突继续。"""
|
||||
from config.settings import settings
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
from app.core import strategy_book as books
|
||||
from app.services import param_store, rotation_service
|
||||
if param_store.get_bool('PMS_GLOBAL_EXEC_HALT', False):
|
||||
return '用户暂停全部执行'
|
||||
with books.scope('all'):
|
||||
parent = pms_repo.get_instruction(row.get('parent_id'))
|
||||
if not parent or parent.get('book_id') not in (books.ORIGINAL, books.MICRO):
|
||||
raise RuntimeError('委托的策略来源无法核实')
|
||||
if (parent.get('progress') or {}).get('execution_profile') == 'rotation' and param_store.get('PMS_ROTATION_CONTROL', 'run') == 'pause_all':
|
||||
return '用户暂停轮换全部执行'
|
||||
if row.get('side') != 'buy':
|
||||
return None
|
||||
if param_store.get_bool('PMS_GLOBAL_BUY_HALT', False):
|
||||
return '用户暂停买入'
|
||||
reason = rotation_service.strategy_buy_blocker(parent['book_id'], parent)
|
||||
if reason:
|
||||
return reason
|
||||
with books.scope(parent['book_id']):
|
||||
return _queued_parent_check(row, parent)
|
||||
if row.get('side') != 'buy':
|
||||
return None
|
||||
parent = pms_repo.get_instruction(row.get('parent_id'))
|
||||
return _queued_parent_check(row, parent)
|
||||
|
||||
|
||||
def _queued_parent_check(row, parent):
|
||||
if not parent:
|
||||
raise RuntimeError('买入来源指令无法核实,暂不发送')
|
||||
if pms_repo.instruction_buy_stop(row.get('parent_id')) or (parent.get('progress') or {}).get('stopped_by_exit'):
|
||||
|
|
|
|||
|
|
@ -154,7 +154,8 @@ def run_tick(*, now=None, dry_run: bool = False) -> dict:
|
|||
"errors": [], "mode": dispatcher.mode(), "dry_run": dry_run}
|
||||
|
||||
instrs = [i for i in pms_repo.list_instructions(statuses=list(LIVE), limit=200)
|
||||
if int(i.get("qty") or 0) > int(i.get("exec_qty") or 0)]
|
||||
if int(i.get("qty") or 0) > int(i.get("exec_qty") or 0)
|
||||
and (i.get('progress') or {}).get('execution_profile') != 'rotation']
|
||||
if not instrs:
|
||||
return out
|
||||
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ from app.core import tradedays as td
|
|||
from app.core import ws_codec as wsc
|
||||
from app.repo import downstream_repo, pms_repo, qmt_repo
|
||||
from app.services import market, param_store, portfolio
|
||||
from config.settings import settings
|
||||
|
||||
logger = logging.getLogger("pms.ledger")
|
||||
|
||||
|
|
@ -214,7 +215,12 @@ def consume_ws_trades(*, limit: int = 500) -> dict:
|
|||
if not pid:
|
||||
continue
|
||||
try:
|
||||
ins = pms_repo.get_instruction(pid)
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
from app.core.strategy_book import scope
|
||||
with scope('all'):
|
||||
ins = pms_repo.get_instruction(pid)
|
||||
else:
|
||||
ins = pms_repo.get_instruction(pid)
|
||||
if ins:
|
||||
parent_actions[pid] = ins.get("action")
|
||||
except Exception as e:
|
||||
|
|
@ -237,6 +243,15 @@ def consume_ws_trades(*, limit: int = 500) -> dict:
|
|||
booked_codes, booked_seqs = set(), []
|
||||
for act in mapped["actions"]:
|
||||
seq = act.get("seq")
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
try:
|
||||
from app.services.rotation_execution import book_trade
|
||||
if book_trade(act, fees_by_no.get(act.get('order_id')) or [], seq):
|
||||
out['actions'] += 1
|
||||
booked_seqs.append(seq)
|
||||
except Exception as e:
|
||||
out['errors'].append(f"策略成交入账未完成,保留待重试: {type(e).__name__}: {e}")
|
||||
continue
|
||||
try:
|
||||
_apply_action(act)
|
||||
out["actions"] += 1
|
||||
|
|
@ -417,6 +432,18 @@ def replay_fills(*, limit: int = 500) -> dict:
|
|||
if ws_owned:
|
||||
logger.info("[回放] %s 笔本实例 ws 委托的汇总镜像跳过,逐笔通道负责入账", len(ws_owned))
|
||||
fresh = external
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA and fresh:
|
||||
from app.repo import rotation_repo
|
||||
import hashlib
|
||||
for fill in fresh:
|
||||
key = _fill_key(fill)
|
||||
rotation_repo.put_once('external:'+hashlib.sha256(key.encode()).hexdigest()[:40],
|
||||
'unattributed_fill', fill)
|
||||
ft = str(fill.get('done_time') or '')[:19] or t_max
|
||||
seen[key] = ft
|
||||
t_max = max(t_max, ft)
|
||||
out['unattributed'] = len(fresh)
|
||||
fresh = []
|
||||
|
||||
try:
|
||||
instrs = _open_instructions()
|
||||
|
|
@ -805,6 +832,9 @@ def reconcile(*, apply_fix: bool = True, force: bool = False) -> dict:
|
|||
force=True 绕过爆炸半径限制 (见 _recon_blast_guard) —— 只在人工确认下游读数确实
|
||||
正确之后使用。
|
||||
"""
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
from app.services.rotation_service import reconcile_books
|
||||
return reconcile_books()
|
||||
out = {"ok": True, "diffs": [], "fixes": [], "errors": [], "columns": {}, "severity": rc.SEV_OK}
|
||||
src = positions_source()
|
||||
ds = {"rows": src["rows"], "columns": src["columns"], "raw_count": src["raw_count"]}
|
||||
|
|
|
|||
|
|
@ -40,6 +40,14 @@ SECRET_KEYS = ("PMS_QMT_SIGN_SEED_HEX", "PMS_QMT_PEER_PUBKEY_B64", "PMS_SESSION_
|
|||
|
||||
# 运行态开关: key -> (默认值, 类型, 说明)
|
||||
RUNTIME_EXTRA = {
|
||||
'PMS_ROTATION_MODE': ('observe', str, '微盘轮换模式:observe观察、paper独立纸面、auto自动。自动须通过验收门槛'),
|
||||
'PMS_ROTATION_MONITOR_URL': ('', str, '微盘监控只读接口根地址,留空停用拉取'),
|
||||
'PMS_ROTATION_CONTROL': ('run', str, '轮换控制:run运行、pause_new暂停新增、pause_all暂停全部、exit_disable退出并停用'),
|
||||
'PMS_MICRO30_ALLOWED_BOARDS': ('', str, '实际已核实板块权限,MAIN、CHINEXT、STAR逗号分隔;留空不得自动买入'),
|
||||
'PMS_MICRO30_COMMISSION_RATE': (0.0002, float, '微盘佣金估算率;自动执行前核实实际费率'),
|
||||
'PMS_MICRO30_MIN_COMMISSION': (5.0, float, '微盘每笔最低佣金估算,元;自动执行前核实'),
|
||||
'PMS_MICRO30_SELL_TAX': (0.0005, float, '微盘卖出印花税估算率;自动执行前核实'),
|
||||
'PMS_MICRO30_TRANSFER_RATE': (0.00001, float, '微盘过户费用估算率;自动执行前核实'),
|
||||
'PMS_PLAN_VERSION_RECHECK': (False, bool, '自主建仓采纳前核验计划版本;新版发布与留痕验收后开启,当前不可核验时暂停采纳'),
|
||||
"PMS_GLOBAL_BUY_HALT": (False, bool, "全局暂停买入 (HALT_BUY 命令置位)"),
|
||||
"PMS_GLOBAL_EXEC_HALT": (False, bool, "全局暂停执行"),
|
||||
|
|
|
|||
|
|
@ -0,0 +1,108 @@
|
|||
"""实有策略现金与净值。账本迁移演练后,在无在途委托的维护窗口初始化。"""
|
||||
import datetime as dt
|
||||
from app.core import rotation_account as core, strategy_book as books
|
||||
from app.repo import rotation_repo as store, pms_repo, qmt_repo
|
||||
from app.services import market
|
||||
from app.core import ws_codec
|
||||
|
||||
|
||||
def marked_values():
|
||||
values = {b: 0. for b in core.BOOKS}
|
||||
missing = []
|
||||
positions = []
|
||||
with books.scope('all'):
|
||||
rows = pms_repo.list_positions(only_open=True)
|
||||
for r in rows:
|
||||
quote = market.day_snapshot(r['ts_code'])
|
||||
price = quote.get('price')
|
||||
if not price or price <= 0:
|
||||
missing.append(r['ts_code'])
|
||||
continue
|
||||
value = int(r['total_qty'])*price
|
||||
values[r['book_id']] += value
|
||||
positions.append({'book_id': r['book_id'], 'code': r['ts_code'], 'qty': r['total_qty'],
|
||||
'market_value': value, 'price': price, 'cost': float(r.get('avg_cost') or 0)*int(r['total_qty'])})
|
||||
if missing:
|
||||
raise ValueError('策略估值行情缺失:'+','.join(missing))
|
||||
return values, positions
|
||||
|
||||
|
||||
def initialize():
|
||||
"""只在显式迁移操作中调用;不可在交易路径猜测重建现金归属。"""
|
||||
from app.db import session as db
|
||||
from app.services.rotation_service import reconcile_books
|
||||
with store.account_guard():
|
||||
if store.get('capital:live')[0]:
|
||||
return {'exists': True}
|
||||
pending = db.fetch_all("SELECT instruction_id FROM pms_qmt_order WHERE status IN ('QUEUED','SENDING','SENT','ACCEPTED','SUBMITTED','PARTIAL') LIMIT 1")
|
||||
if pending or not reconcile_books()['ok']:
|
||||
raise ValueError('现金初始化需要无在途委托且数量对账一致')
|
||||
snap = qmt_repo.latest_snapshot('funds')
|
||||
if not snap or snap.get('age_sec', 999) > 60:
|
||||
raise ValueError('现金初始化需要一分钟内资金快照')
|
||||
funds = ws_codec.parse_funds_snapshot(snap['payload'])
|
||||
if funds.get('available_cash') is None or funds.get('frozen_cash', 0):
|
||||
raise ValueError('资金冻结未解除或现金未知')
|
||||
values, _ = marked_values()
|
||||
if values['micro30']:
|
||||
raise ValueError('迁移初始微盘账本必须为空')
|
||||
value = core.seed(float(funds['available_cash']), values)
|
||||
value.update(started_at=dt.datetime.now().isoformat(), source_funds_seq=snap['seq'])
|
||||
store.put_once('capital:live', 'capital', value)
|
||||
return {'initialized': True, 'source_funds_seq': snap['seq']}
|
||||
|
||||
|
||||
def reserve_book(book, demand, pending, phase):
|
||||
"""账户锁内调用。只划拨已存在现金,跨策略在途保留不可再次使用。"""
|
||||
reserved = {b: 0. for b in core.BOOKS}
|
||||
with books.scope('all'):
|
||||
for o in pending:
|
||||
if o['side'] != 'buy':
|
||||
continue
|
||||
parent = pms_repo.get_instruction(o['parent_id'])
|
||||
if not parent or parent.get('book_id') not in core.BOOKS:
|
||||
raise ValueError('在途委托策略归属未知')
|
||||
remaining = max(0, int(o['qty'])-int(o.get('cum_qty') or 0))
|
||||
if remaining:
|
||||
from app.services.rotation_service import configuration
|
||||
from app.core.micro_rotation import fee
|
||||
amount = remaining*float(o['limit_price'])
|
||||
reserved[parent['book_id']] += amount+fee(amount,'buy',**configuration()['costs'])
|
||||
with store.locked('capital:live', 'capital', {}) as value:
|
||||
if not value.get('cash'):
|
||||
raise ValueError('分策略现金尚未初始化')
|
||||
need = max(0., demand-(value['cash'][book]-reserved[book]))
|
||||
if not need:
|
||||
return
|
||||
source = 'original' if book == 'micro30' else 'micro30'
|
||||
if (book == 'micro30' and phase not in ('entering_micro','micro')) or (book == 'original' and phase not in ('returning_original','original')):
|
||||
raise ValueError('当前阶段不允许现金划拨')
|
||||
if value['cash'][source]-reserved[source] < need-1e-6:
|
||||
raise ValueError('源策略现金不足或仍被在途委托占用')
|
||||
values, _ = marked_values()
|
||||
key = 'allocation:'+str(len(value['transfers'])+1)
|
||||
core.transfer(value, source, book, need, values, key)
|
||||
store.put_once('transfer:'+key, 'cash_transfer', {'key': key, **value['transfers'][key]})
|
||||
|
||||
|
||||
def record_fill(book, side, amount, fees, key):
|
||||
with store.locked('capital:live', 'capital', {}) as value:
|
||||
if not value.get('cash'):
|
||||
raise ValueError('分策略现金尚未初始化')
|
||||
core.fill(value, book, side, amount, fees, key)
|
||||
|
||||
|
||||
def view():
|
||||
value, _ = store.get('capital:live')
|
||||
if not value:
|
||||
return {'ready': False, 'reason': '分策略现金尚未初始化'}
|
||||
values, positions = marked_values()
|
||||
result = core.metrics(value, values)
|
||||
snap = qmt_repo.latest_snapshot('funds')
|
||||
cash_difference = None
|
||||
if snap and snap.get('age_sec', 999) <= 60:
|
||||
funds = ws_codec.parse_funds_snapshot(snap['payload'])
|
||||
if funds.get('available_cash') is not None and funds.get('frozen_cash') is not None:
|
||||
cash_difference = result['account']['cash']-funds['available_cash']-funds['frozen_cash']
|
||||
return {'ready': True, 'metrics': result, 'positions': positions, 'cash_difference': cash_difference,
|
||||
'started_at': value.get('started_at'), 'transfer_count': len(value['transfers'])}
|
||||
|
|
@ -0,0 +1,203 @@
|
|||
"""策略轮换执行适配,共用PMS出口和账户资金串行保护。"""
|
||||
import datetime as dt
|
||||
from app.core import strategy_book as books, micro_rotation as core, ws_codec
|
||||
from app.db import session as db
|
||||
from app.repo import pms_repo, qmt_repo, rotation_repo as store
|
||||
from app.services import param_store
|
||||
from config.settings import settings
|
||||
|
||||
TERMINAL = qmt_repo.FINAL
|
||||
|
||||
|
||||
def enqueue_with_reservation(payload, parent_id):
|
||||
"""所有策略共用一个账户锁;出口行与预算保留同一事务提交。"""
|
||||
from app.services import rotation_service as service
|
||||
with store.locked('account-execution', 'funds_reservation', {'epoch_seq': 0, 'available': 0., 'orders': {}}) as account:
|
||||
code, side, qty, price = payload['ts_code'], payload['side'], int(payload['qty']), float(payload['limit_price'])
|
||||
old = qmt_repo.get_order(payload['instruction_id'])
|
||||
if old:
|
||||
if any(old[k] != v for k, v in [('parent_id', parent_id), ('ts_code', code), ('side', side), ('qty', qty)]) or abs(float(old['limit_price'])-price) > 1e-8:
|
||||
raise ValueError('委托幂等键与内容冲突')
|
||||
return
|
||||
if param_store.get_bool('PMS_GLOBAL_EXEC_HALT', False):
|
||||
raise ValueError('用户暂停全部执行')
|
||||
with books.scope('all'):
|
||||
parent = pms_repo.get_instruction(parent_id)
|
||||
if not parent:
|
||||
raise ValueError('委托没有可核实的策略父指令')
|
||||
book = parent['book_id']
|
||||
if side == 'buy':
|
||||
if param_store.get_bool('PMS_GLOBAL_BUY_HALT', False):
|
||||
raise ValueError('用户暂停买入')
|
||||
reason = service.strategy_buy_blocker(book, parent)
|
||||
if reason:
|
||||
raise ValueError(reason)
|
||||
# 同名股票全账户互斥。最多读取10001行,达到上限拒绝继续,避免截断漏检查。
|
||||
active = db.fetch_all("SELECT * FROM pms_qmt_order WHERE status NOT IN ('FILLED','CANCELLED','REJECTED','EXPIRED','SEND_FAILED','ABORTED') ORDER BY id LIMIT 10001")
|
||||
if len(active) > 10000:
|
||||
raise ValueError('在途委托超出核验上限')
|
||||
if any(o['ts_code'] == code and o['side'] != side for o in active):
|
||||
raise ValueError('同名股票有相反方向在途委托')
|
||||
if side == 'sell':
|
||||
with books.scope(book):
|
||||
position = pms_repo.get_position(code) or {}
|
||||
booked = sum(int(o['qty'])-int(o.get('cum_qty') or 0) for o in active if o['ts_code'] == code and o['side'] == 'sell')
|
||||
from app.services.ledger_service import positions_source
|
||||
source = positions_source()
|
||||
physical = next((r for r in source['rows'] if r['ts_code'] == code), {})
|
||||
if source['source'] != 'ws':
|
||||
raise ValueError('实际可卖量需要新鲜QMT快照')
|
||||
physical_available = int(physical.get('available_qty', physical.get('avail_qty', 0)) or 0)
|
||||
# 已受理卖单通常已从QMT可卖量扣除。只额外预留尚未发出的数量。
|
||||
unsent = sum(int(o['qty'])-int(o.get('cum_qty') or 0) for o in active if o['ts_code'] == code and o['side'] == 'sell' and o['status'] in ('QUEUED','SENDING','SENT'))
|
||||
available = min(int(position.get('avail_qty') or 0)-booked, physical_available-unsent)
|
||||
if qty > max(0, available):
|
||||
raise ValueError('账户或策略实际可卖数量不足')
|
||||
if qty != int(position.get('total_qty') or 0) and (qty < core.min_qty(code) or qty % 100):
|
||||
raise ValueError('部分卖出数量不符合交易单位')
|
||||
else:
|
||||
with books.scope(books.MICRO if book == books.ORIGINAL else books.ORIGINAL):
|
||||
other = pms_repo.get_position(code) or {}
|
||||
if int(other.get('total_qty') or 0) > 0:
|
||||
raise ValueError('同名股票旧策略持仓尚未卖清')
|
||||
if qty < core.min_qty(code) or qty % 100:
|
||||
raise ValueError('买入数量不符合交易单位')
|
||||
funds = qmt_repo.latest_snapshot('funds')
|
||||
if not funds or funds.get('age_sec') is None or not 0 <= funds['age_sec'] <= 60:
|
||||
raise ValueError('账户资金快照超过一分钟或不可用')
|
||||
parsed = ws_codec.parse_funds_snapshot(funds['payload'])
|
||||
available = parsed.get('available_cash')
|
||||
if available is None:
|
||||
raise ValueError('账户可用现金不可核实')
|
||||
reserved = account['orders']
|
||||
terminal = []
|
||||
for iid in reserved:
|
||||
order = qmt_repo.get_order(iid)
|
||||
if order and order['status'] in TERMINAL and order.get('final_at'):
|
||||
terminal.append(order)
|
||||
# 全部委托终态,且新的资金快照晚于全部终态,才开启新预算周期。
|
||||
settled = not reserved or (len(terminal) == len(reserved) and all(funds['received_at'] > o['final_at'] for o in terminal))
|
||||
if settled and funds['seq'] > account['epoch_seq']:
|
||||
account.update(epoch_seq=funds['seq'], available=float(available), orders={}, market_value=float(parsed.get('market_value') or 0))
|
||||
costs = service.configuration()['costs']
|
||||
demand = qty*price+core.fee(qty*price, 'buy', **costs)
|
||||
remaining = account['available']-sum(account['orders'].values())
|
||||
reserve_cash = param_store.get_float('PMS_TOTAL_SCALE', 0)*param_store.get_float('PMS_CASH_RESERVE', 0)
|
||||
cash_limit = min(float(available), remaining)-reserve_cash
|
||||
# 不另扣QMT冻结现金,不额外增加卖出回笼。
|
||||
scale = param_store.get_float('PMS_TOTAL_SCALE', 0)
|
||||
cap = scale*param_store.get_float('PMS_PORTFOLIO_CAP', 0)
|
||||
mv = parsed.get('market_value')
|
||||
if mv is None:
|
||||
mv = parsed.get('total_market_value')
|
||||
if mv is None:
|
||||
raise ValueError('账户总市值不可核实')
|
||||
# 委托未终态前保守保留本预算周期新增市值;不借旧行情绕过全账户上限。
|
||||
cap_remaining = min(cap-float(mv), cap-float(account.get('market_value', mv))-sum(account['orders'].values()))
|
||||
if demand > cash_limit+1e-8 or qty*price > cap_remaining+1e-8:
|
||||
raise ValueError('账户可用现金或全账户仓位预算不足')
|
||||
from app.services import rotation_accounting
|
||||
live, _ = store.get('state:live')
|
||||
phase = (live or {}).get('rotation', {}).get('phase', 'original')
|
||||
rotation_accounting.reserve_book(book, demand, active, phase)
|
||||
account['orders'][payload['instruction_id']] = demand
|
||||
qmt_repo.enqueue_order(instruction_id=payload['instruction_id'], parent_id=parent_id,
|
||||
ts_code=code, side=side, qty=qty, limit_price=price, valid_until=payload['valid_until'],
|
||||
intent=payload['intent'], note=payload.get('note'))
|
||||
|
||||
|
||||
def send_intent(intent, price, now, snapshot_version):
|
||||
from app.services import dispatcher
|
||||
if dispatcher.mode() != 'ws':
|
||||
raise ValueError('轮换自动执行只支持唯一WS通道,不使用shadow成交认领')
|
||||
parent = intent['id']
|
||||
book = intent['book_id']
|
||||
with books.scope(book), db.transaction():
|
||||
existing = pms_repo.get_instruction(parent)
|
||||
if not existing:
|
||||
command = 'ROT_CMD_'+book+'_'+now.strftime('%Y%m%d')
|
||||
if not pms_repo.get_command(command):
|
||||
pms_repo.insert_command(command_id=command, cmd_class='task', cmd_type='ROTATE_MICRO',
|
||||
ts_code=None, params={'rule_version': core.RULE, 'snapshot_version': snapshot_version},
|
||||
status='EXECUTING', issued_by='rotation', note='微盘与原策略账户轮换')
|
||||
plan = 'P_'+parent
|
||||
action = 'OPEN' if intent['side'] == 'buy' else 'EXIT'
|
||||
pms_repo.insert_plans([{'plan_id': plan, 'command_id': command, 'ts_code': intent['ts_code'],
|
||||
'action': action, 'qty': intent['qty'], 'amount': intent['qty']*price,
|
||||
'deadline': now.date(), 'status': 'EXECUTING', 'reason': intent['reason']}])
|
||||
pms_repo.insert_instruction(instruction_id=parent, origin_type='plan', origin_id=plan,
|
||||
ts_code=intent['ts_code'], action=action, side=intent['side'], qty=intent['qty'],
|
||||
window_tdays=1, progress={'execution_profile': 'rotation', 'snapshot_version': snapshot_version,
|
||||
'deadline': now.date().isoformat(), 'is_command': True, 'children': []})
|
||||
elif existing['status'] not in ('PROPOSED', 'DISPATCHED'):
|
||||
return {'instruction_id': parent, 'state': '已有终态记录,次日重新规划'}
|
||||
child = parent+'_D01'
|
||||
result = dispatcher.dispatch(instruction_id=child, parent_id=parent, intent='OPEN' if intent['side'] == 'buy' else 'EXIT',
|
||||
ts_code=intent['ts_code'], side=intent['side'], qty=intent['qty'], limit_price=price,
|
||||
valid_until=(now.replace(hour=10, minute=0, second=0, microsecond=0) if now.hour < 10 else min(now+dt.timedelta(minutes=10), now.replace(hour=14, minute=57, second=0, microsecond=0))), note=intent['reason'])
|
||||
if not result.get('ok'):
|
||||
raise ValueError('轮换委托未下发:'+str(result.get('error')))
|
||||
progress = (pms_repo.get_instruction(parent) or {}).get('progress', {})
|
||||
progress['children'] = [{'ref': child, 'qty': intent['qty'], 'limit': price,
|
||||
'mode': 'ws', 'ymd': int(now.strftime('%Y%m%d')), 'at': now.strftime('%H:%M:%S'), 'valid_until': '10:00'}]
|
||||
pms_repo.update_instruction(parent, status='DISPATCHED', dispatch_ref=child, progress=progress)
|
||||
return {'instruction_id': parent, 'book_id': book, 'result': result}
|
||||
|
||||
|
||||
def cancel_expired(now):
|
||||
from app.services import executor
|
||||
with books.scope('all'):
|
||||
rows = pms_repo.list_instructions(statuses=('PROPOSED', 'DISPATCHED'), limit=10000)
|
||||
for row in rows:
|
||||
p = row.get('progress') or {}
|
||||
if p.get('execution_profile') != 'rotation':
|
||||
continue
|
||||
deadline = str(p.get('deadline') or '')
|
||||
if deadline < now.date().isoformat() or (deadline == now.date().isoformat() and now.hour >= 10):
|
||||
with books.scope(row['book_id']):
|
||||
result = executor.cancel_instruction(row['instruction_id'], reason='轮换当日执行窗口结束', arbiter='system')
|
||||
if not result.get('ok'):
|
||||
raise RuntimeError('轮换委托撤销未确认')
|
||||
|
||||
|
||||
def book_trade(act, fees, seq):
|
||||
"""策略归属、批次、费用、指令进度和inbox标记同成同败。"""
|
||||
from app.services import ledger_service as ledger
|
||||
parent = act.get('instruction_id')
|
||||
if not parent:
|
||||
raise ValueError('逐笔成交缺少父指令归属')
|
||||
with store.account_guard(), db.transaction():
|
||||
inbox = db.fetch_one('SELECT processed FROM pms_qmt_inbox WHERE seq=:seq FOR UPDATE', {'seq': seq})
|
||||
if not inbox or inbox['processed'] != 0:
|
||||
return False
|
||||
with books.scope('all'):
|
||||
instruction = pms_repo.get_instruction(parent)
|
||||
if not instruction or instruction.get('book_id') not in (books.ORIGINAL, books.MICRO):
|
||||
raise ValueError('父指令策略归属不可核实')
|
||||
if instruction['ts_code'] != act['ts_code'] or instruction['side'] != ('buy' if act['kind'] == 'BUY' else 'sell'):
|
||||
raise ValueError('成交证券或方向与父指令冲突')
|
||||
if int(instruction['exec_qty'])+int(act['qty']) > int(instruction['qty']):
|
||||
raise ValueError('成交超过父指令剩余数量,等待核对')
|
||||
with books.scope(instruction['book_id']):
|
||||
before = pms_repo.get_position(act['ts_code']) or {}
|
||||
qty = int(act['qty'])
|
||||
if act['kind'] != 'BUY' and qty > int(before.get('total_qty') or 0):
|
||||
raise ValueError('卖出成交超出所属策略持仓,等待核对')
|
||||
ledger._apply_action(act)
|
||||
after = pms_repo.get_position(act['ts_code']) or {}
|
||||
delta = qty if act['kind'] == 'BUY' else -qty
|
||||
if int(after.get('total_qty') or 0) != int(before.get('total_qty') or 0)+delta:
|
||||
raise ValueError('成交数量未完整入账')
|
||||
fresh = pms_repo.get_instruction(parent)
|
||||
if int(fresh['exec_qty']) != int(instruction['exec_qty'])+qty:
|
||||
raise ValueError('指令成交进度未原子更新')
|
||||
for f in fees:
|
||||
pms_repo.insert_cash_flow(ymd=int(dt.datetime.now().strftime('%Y%m%d')), kind='FEE',
|
||||
amount=f['amount'], ts_code=f['ts_code'], estimated=f['estimated'],
|
||||
trade_no=f['trade_no'], instruction_id=f['instruction_id'], note=f['note'])
|
||||
from app.services.rotation_accounting import record_fill
|
||||
record_fill(instruction['book_id'], 'buy' if act['kind'] == 'BUY' else 'sell',
|
||||
qty*float(act['price']), -sum(float(f['amount']) for f in fees), str(seq))
|
||||
ledger.recompute_position(act['ts_code'])
|
||||
qmt_repo.inbox_mark([seq], processed=qmt_repo.PROC_BOOKED, note='按策略归属原子入账')
|
||||
return True
|
||||
|
|
@ -0,0 +1,486 @@
|
|||
"""宏观微盘策略编排。观察与纸面状态独立;只有验收完成才能接现有委托通道。"""
|
||||
import datetime as dt
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
from zoneinfo import ZoneInfo
|
||||
import requests
|
||||
from config.settings import settings
|
||||
from app.core import micro_rotation as core, strategy_book as books, tradedays as td
|
||||
from app.repo import pms_repo, qmt_repo, rotation_repo as store
|
||||
from app.services import param_store, portfolio, market
|
||||
|
||||
TZ = ZoneInfo('Asia/Shanghai')
|
||||
LIVE = ('PROPOSED', 'RULE_PASSED', 'JUDGE_PASSED', 'DISPATCHED')
|
||||
TERMINAL = qmt_repo.FINAL
|
||||
|
||||
|
||||
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')),
|
||||
'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),
|
||||
'minimum': param_store.get_float('PMS_MICRO30_MIN_COMMISSION', 5.),
|
||||
'sell_tax': param_store.get_float('PMS_MICRO30_SELL_TAX', .0005),
|
||||
'transfer_rate': param_store.get_float('PMS_MICRO30_TRANSFER_RATE', .00001)}}
|
||||
|
||||
|
||||
def pull(execution_date=None):
|
||||
config = configuration()
|
||||
day = execution_date or dt.datetime.now(TZ).date().isoformat()
|
||||
if not config['url']:
|
||||
return None
|
||||
response = requests.get(config['url']+'/v1/snapshot', params={'effective_date': day}, timeout=(3, 15))
|
||||
response.raise_for_status()
|
||||
if len(response.content) > 1024*1024:
|
||||
raise ValueError('监控快照超过大小限制')
|
||||
value = response.json()
|
||||
errors = core.snapshot_check(value, day, require_frozen=False)
|
||||
if errors:
|
||||
raise ValueError(';'.join(errors))
|
||||
return value
|
||||
|
||||
|
||||
def activation_errors():
|
||||
if settings.PMS_TABLE_PREFIX:
|
||||
return ['本次策略只开放155模拟实例']
|
||||
if not settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
return ['策略账本迁移尚未启用']
|
||||
evidence, _ = store.get('validation:'+core.RULE)
|
||||
evidence = evidence or {}
|
||||
required = ('backtest', 'capacity', 'fees', 'board_permissions', 'migration', 'regression', 'browser', 'fill_attribution', 'fund_reservation')
|
||||
errors = [k+'尚未验收' for k in required if not evidence.get(k)]
|
||||
observed = store.recent('observed_day', 1000)
|
||||
paper = store.recent('paper_day', 1000)
|
||||
dates = sorted({r['value']['date'] for r in observed if r['value'].get('rule_version') == core.RULE})
|
||||
paper_dates = sorted({r['value']['date'] for r in paper if r['value'].get('rule_version') == core.RULE and r['value'].get('healthy')})
|
||||
if len(dates) < 3:
|
||||
errors.append('有效观察不足3个交易日')
|
||||
if len(paper_dates) < 10 or (len(dates) >= 3 and any(d <= dates[2] for d in paper_dates[:10])):
|
||||
errors.append('观察完成后的健康纸面运行不足10个交易日')
|
||||
return errors
|
||||
|
||||
|
||||
def status():
|
||||
config = configuration()
|
||||
out = {'mode': config['mode'], 'control': config['control'], 'rule_version': core.RULE,
|
||||
'basket_size': core.SIZE, 'schema_ready': settings.INFRA_STRATEGY_BOOK_SCHEMA, 'documents_ready': documents_ready(),
|
||||
'phase': 'original', 'snapshot': None, 'errors': [], 'activation_errors': []}
|
||||
try:
|
||||
out['snapshot'] = pull()
|
||||
except Exception as exc:
|
||||
out['errors'].append('监控暂不可用:'+type(exc).__name__)
|
||||
if documents_ready():
|
||||
value, _ = store.get('state:'+('paper' if config['mode'] == 'paper' else 'live'))
|
||||
if value:
|
||||
out.update(state=value, phase=value.get('rotation', {}).get('phase', 'original'))
|
||||
out['activation_errors'] = activation_errors()
|
||||
out['recent'] = store.recent('execution', 30)
|
||||
out['observed_days'] = len({r['value']['date'] for r in store.recent('observed_day', 1000) if r['value'].get('rule_version') == core.RULE})
|
||||
out['paper_days'] = len({r['value']['date'] for r in store.recent('paper_day', 1000) if r['value'].get('rule_version') == core.RULE and r['value'].get('healthy')})
|
||||
out['daily'] = store.recent('paper_day' if config['mode'] == 'paper' else 'live_day', 500)
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA and config['mode'] != 'paper':
|
||||
try:
|
||||
from app.services.rotation_accounting import view
|
||||
out['accounting'] = view()
|
||||
out['reconciliation'] = reconcile_books()
|
||||
except Exception as exc:
|
||||
out['errors'].append('策略资金待核实:'+type(exc).__name__)
|
||||
else:
|
||||
out['activation_errors'] = ['策略账本迁移尚未启用']
|
||||
out['stock_budget'] = param_store.get_float('PMS_TOTAL_SCALE', 0)*param_store.get_float('PMS_PORTFOLIO_CAP', 0)
|
||||
out['costs'] = config['costs']
|
||||
out['boards'] = sorted(config['boards'])
|
||||
return out
|
||||
|
||||
|
||||
def control(*, mode=None, action=None, actor='user'):
|
||||
if mode not in (None, 'observe', 'paper', 'auto') or action not in (None, 'run', 'pause_new', 'pause_all', 'exit_disable'):
|
||||
raise ValueError('轮换档位或控制动作非法')
|
||||
if mode in ('paper', 'auto') and not documents_ready():
|
||||
raise ValueError('观察与纸面记录表尚未建立')
|
||||
if mode == 'paper':
|
||||
days = {r['value']['date'] for r in store.recent('observed_day', 1000) if r['value'].get('rule_version') == core.RULE}
|
||||
if len(days) < 3:
|
||||
raise ValueError('有效观察不足3个交易日,暂不进入纸面运行')
|
||||
if mode == 'auto':
|
||||
errors = activation_errors()
|
||||
if errors:
|
||||
raise ValueError(';'.join(errors))
|
||||
if mode:
|
||||
current, _ = store.get('state:live') if settings.INFRA_STRATEGY_BOOK_SCHEMA else (None, 0)
|
||||
if current and current.get('rotation', {}).get('phase') != 'original' and mode != 'auto':
|
||||
raise ValueError('实有微盘持仓或轮换在途,请先使用暂停或退出操作,不直接切换账本模式')
|
||||
result = param_store.set_param('PMS_ROTATION_MODE', mode, actor)
|
||||
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('控制保存失败')
|
||||
return status()
|
||||
|
||||
|
||||
def positions(book):
|
||||
with books.scope(book):
|
||||
return pms_repo.list_positions(only_open=True)
|
||||
|
||||
|
||||
def _paper_seed():
|
||||
funds = portfolio.cash_view()
|
||||
if funds['source'] != 'ws' or funds.get('cash_avail') is None:
|
||||
raise ValueError('纸面账户初始化需要有效QMT资金快照')
|
||||
account = {'cash': funds['cash_avail'], 'books': {'original': {}, 'micro30': {}},
|
||||
'realized': {'original': 0., 'micro30': 0.}, 'fees': {'original': 0., 'micro30': 0.},
|
||||
'fills': {}, 'seeded_at': dt.datetime.now(TZ).isoformat(), 'seed_asset': funds.get('total_asset')}
|
||||
with books.scope('original'):
|
||||
for p in pms_repo.list_positions(only_open=True):
|
||||
lots = [{'date': str(r['open_date'])[:10], 'qty': int(r['qty'])} for r in pms_repo.list_lots(p['ts_code'])]
|
||||
if sum(r['qty'] for r in lots) != p['total_qty']:
|
||||
raise ValueError('原策略批次数量与主档不一致')
|
||||
account['books']['original'][p['ts_code']] = {'qty': p['total_qty'],
|
||||
'cost': float(p.get('avg_cost') or 0)*p['total_qty'], 'lots': lots,
|
||||
'base_qty': p['base_qty'], 'cushion_pct': float(p.get('cushion_pct') or 0)}
|
||||
return account
|
||||
|
||||
|
||||
def _paper_positions(account, book, day):
|
||||
return [{'ts_code': code, 'total_qty': p['qty'], 'avail_qty': sum(r['qty'] for r in p['lots'] if r['date'] < day),
|
||||
'base_qty': p.get('base_qty', p['qty']), 'cushion_pct': p.get('cushion_pct'), 'neg_cushion_days': p.get('neg_cushion_days', 0)}
|
||||
for code, p in account['books'][book].items() if p['qty']]
|
||||
|
||||
|
||||
def _quote(code, now):
|
||||
quote = market.day_snapshot(code)
|
||||
raw = quote.get('bar_time')
|
||||
if not raw:
|
||||
return {}
|
||||
try:
|
||||
stamp = dt.datetime.fromisoformat(str(raw).replace('Z', '+00:00'))
|
||||
if stamp.tzinfo is None:
|
||||
stamp = stamp.replace(tzinfo=TZ)
|
||||
if not 0 <= (now-stamp).total_seconds() <= 180:
|
||||
return {}
|
||||
except ValueError:
|
||||
return {}
|
||||
return quote
|
||||
|
||||
|
||||
def _limits(day):
|
||||
from app.db import session as db
|
||||
rows = db.fetch_all('SELECT ts_code,up_limit,down_limit FROM gp_stk_limit WHERE trade_date=:day', {'day': day})
|
||||
return {r['ts_code']: r for r in rows}
|
||||
|
||||
|
||||
def tick(now=None):
|
||||
"""交易态与所有账户委托使用相同的锁顺序:账户锁,然后轮换状态锁。"""
|
||||
config = configuration()
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA and config['mode'] == 'auto':
|
||||
with store.account_guard():
|
||||
return _tick(now)
|
||||
return _tick(now)
|
||||
|
||||
|
||||
def _tick(now=None):
|
||||
now = now or dt.datetime.now(TZ)
|
||||
if now.tzinfo is None:
|
||||
now = now.replace(tzinfo=TZ)
|
||||
config = configuration()
|
||||
day = now.date().isoformat()
|
||||
if config['mode'] == 'auto' and settings.INFRA_STRATEGY_BOOK_SCHEMA and now.hour*60+now.minute >= 600:
|
||||
from app.services.rotation_execution import cancel_expired
|
||||
cancel_expired(now)
|
||||
if td.calendar_degraded(day) or not td.is_trade_day(day):
|
||||
return {'skipped': '交易日历不可用或非交易日'}
|
||||
key = 'state:'+('paper' if config['mode'] == 'paper' else 'live')
|
||||
saved, _ = store.get(key) if documents_ready() else (None, 0)
|
||||
saved = saved or {}
|
||||
exit_pending = saved.get('rotation', {}).get('phase') == 'returning_original' or config['control'] == 'exit_disable'
|
||||
value, errors = None, []
|
||||
try:
|
||||
value = pull(day)
|
||||
errors = core.snapshot_check(value, day)
|
||||
except Exception as exc:
|
||||
errors = ['监控暂不可用:'+type(exc).__name__]
|
||||
if errors:
|
||||
value = None
|
||||
if not documents_ready():
|
||||
return {'snapshot': value, 'mode': 'observe', 'blocked': errors+['观察记录表尚未启用']}
|
||||
if value:
|
||||
store.put_once('snapshot:'+day+':'+value['snapshot_version'], 'snapshot', value)
|
||||
if value['signal_ready'] and value['basket_ready']:
|
||||
store.put_once('observed:'+day, 'observed_day', {'date': day, 'rule_version': core.RULE, 'snapshot_version': value['snapshot_version']})
|
||||
if config['mode'] == 'observe':
|
||||
return {'mode': 'observe', 'snapshot': value, 'blocked': errors}
|
||||
if config['mode'] == 'auto':
|
||||
blocked = activation_errors()
|
||||
# 初次启用必须完成全部验收;已确认的退出不因监控/观察证据故障卡住。
|
||||
if blocked and not (exit_pending and settings.INFRA_STRATEGY_BOOK_SCHEMA):
|
||||
return {'blocked': blocked}
|
||||
if not value and not exit_pending:
|
||||
return {'blocked': errors}
|
||||
if value is None:
|
||||
# 仅作为已确认退出的执行上下文,不写成新快照、不生成任何入场。
|
||||
value = {'members': [], 'signal_ready': False, 'basket_ready': False,
|
||||
'effective_date': day, 'snapshot_version': saved.get('rotation', {}).get('last_snapshot'), 'signal': {}}
|
||||
exit_pending = exit_pending or (value.get('signal_ready') and value.get('signal', {}).get('high') and saved.get('rotation', {}).get('phase') in ('entering_micro', 'micro'))
|
||||
if param_store.get_bool('PMS_GLOBAL_EXEC_HALT', False):
|
||||
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':
|
||||
from app.services.rotation_execution import cancel_expired
|
||||
cancel_expired(now)
|
||||
return {'skipped': '执行窗口之外'}
|
||||
key = 'state:'+('paper' if config['mode'] == 'paper' else 'live')
|
||||
with store.locked(key, 'state', {'rotation': core.initial_state()}) as state:
|
||||
if config['mode'] == 'paper':
|
||||
if 'account' not in state:
|
||||
state['account'] = _paper_seed()
|
||||
account = state['account']
|
||||
original = _paper_positions(account, 'original', day)
|
||||
micro = _paper_positions(account, 'micro30', day)
|
||||
cash = account['cash']
|
||||
pending = []
|
||||
else:
|
||||
recon = reconcile_books()
|
||||
if recon['diffs'] or not recon['ok']:
|
||||
return {'blocked': ['策略账本与QMT尚未对齐']}
|
||||
original, micro = positions('original'), positions('micro30')
|
||||
funds = portfolio.cash_view()
|
||||
if funds['source'] != 'ws':
|
||||
raise ValueError('真实资金快照不可用')
|
||||
cash = funds['cash_avail']
|
||||
with books.scope('all'):
|
||||
pending = pms_repo.list_instructions(statuses=LIVE, limit=10000)
|
||||
codes = {r['ts_code'] for r in original+micro}|{r['code'] for r in value['members']}
|
||||
quotes = {c: _quote(c, now) for c in codes}
|
||||
missing = [r['ts_code'] for r in original+micro if not quotes.get(r['ts_code'])]
|
||||
if missing and not exit_pending:
|
||||
state['blocked'] = ['持仓行情不完整,无法核算全账户仓位']+missing
|
||||
return {'blocked': state['blocked']}
|
||||
account_mv = sum(r['total_qty']*quotes.get(r['ts_code'], {}).get('price', 0) for r in original+micro)
|
||||
if config['mode'] == 'paper' and 'capital' not in account and not missing:
|
||||
from app.core import rotation_account as capital
|
||||
account['capital'] = capital.seed(cash, {'original': account_mv, 'micro30': 0.})
|
||||
scale = param_store.get_float('PMS_TOTAL_SCALE', 0.)
|
||||
cap = scale*param_store.get_float('PMS_PORTFOLIO_CAP', 0.)
|
||||
reserve = scale*param_store.get_float('PMS_CASH_RESERVE', 0.)
|
||||
budget = max(0., min(cap, cash+account_mv-reserve))
|
||||
s = core.transition(state['rotation'], value, micro_qty=sum(r['total_qty'] for r in micro),
|
||||
original_qty=sum(r['total_qty'] for r in original), budget=budget, control=config['control'])
|
||||
state['rotation'] = s
|
||||
if s['phase'] == 'returning_original' and not s.get('exit_date'):
|
||||
s['exit_date'] = day
|
||||
store.stage_locked(key, state)
|
||||
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):
|
||||
state['blocked'] = ['原策略尚有做T批次,等待完成后继续轮换']
|
||||
return {'blocked': state['blocked']}
|
||||
from app.services import command_service
|
||||
black = command_service.blacklist()
|
||||
intents, waits = core.orders(s, value, original=original, micro=micro, quotes=quotes,
|
||||
cash=cash, account_mv=account_mv, cap_amount=cap, reserve_cash=reserve,
|
||||
pending=pending, control=config['control'], blacklisted=black, costs=config['costs'])
|
||||
# 纸面档明确采用全沪深权限假设,不把它当实际账户权限判收。
|
||||
boards = config['boards'] if config['mode'] == 'auto' else {'MAIN', 'STAR', 'CHINEXT'}
|
||||
issues = core.preflight(value['members'], budget, boards, config['costs']) if value['basket_ready'] else ['名单未就绪']
|
||||
if issues:
|
||||
intents = [r for r in intents if r['side'] == 'sell']
|
||||
waits.extend(issues)
|
||||
limits = _limits(day)
|
||||
done = []
|
||||
for i, intent in enumerate(intents):
|
||||
c, side = intent['ts_code'], intent['side']
|
||||
quote = quotes.get(c, {})
|
||||
price = quote.get('price')
|
||||
lim = limits.get(c)
|
||||
if not price or not lim or not lim.get('up_limit') or not lim.get('down_limit'):
|
||||
waits.append(c+'行情或限制价不完整')
|
||||
continue
|
||||
if (side == 'buy' and price >= float(lim['up_limit'])) or (side == 'sell' and price <= float(lim['down_limit'])):
|
||||
waits.append(c+'涨跌停阻止成交')
|
||||
continue
|
||||
rows = original if intent['book_id'] == 'original' else micro
|
||||
pos = next((r for r in rows if r['ts_code'] == c), {})
|
||||
if side == 'sell':
|
||||
intent['qty'] = min(intent['qty'], int(pos.get('avail_qty') or 0))
|
||||
if not intent['qty']:
|
||||
continue
|
||||
token = hashlib.sha256(json.dumps([day, s['cycle'], intent, len(state.get('account', {}).get('fills', {}))], sort_keys=True).encode()).hexdigest()[:24]
|
||||
intent['id'] = 'ROT_'+token
|
||||
if config['mode'] == 'paper':
|
||||
core.apply_paper_fill(account, intent, price, day, config['costs'],
|
||||
prices={c: q['price'] for c, q in quotes.items() if q.get('price')})
|
||||
done.append(intent)
|
||||
else:
|
||||
from app.services.rotation_execution import send_intent
|
||||
from app.db import session as db
|
||||
try:
|
||||
with db.savepoint():
|
||||
done.append(send_intent(intent, price, now, value['snapshot_version']))
|
||||
except Exception as exc:
|
||||
waits.append(c+'委托待重试:'+type(exc).__name__)
|
||||
if s['phase'] == 'entering_micro' and not original and all(any(p['ts_code'] == c and p['total_qty'] >= s.get('entry_quantities', {}).get(c, 0) for p in micro) for c in s['target']) and not pending:
|
||||
s['phase'] = 'micro'
|
||||
state.update(last_tick=now.isoformat(), blocked=waits+errors, last_intents=intents, last_results=done,
|
||||
signal_healthy=bool(value.get('signal_ready') and value.get('basket_ready')),
|
||||
progress={'original_market_value': sum(r['total_qty']*quotes.get(r['ts_code'], {}).get('price', 0) for r in original),
|
||||
'micro_market_value': sum(r['total_qty']*quotes.get(r['ts_code'], {}).get('price', 0) for r in micro),
|
||||
'available_cash': cash, 'allowed_stock_budget': budget, 'original_count': len(original), 'micro_count': len(micro)})
|
||||
if config['mode'] == 'paper':
|
||||
state['board_permission_assumption'] = '纸面使用沪深全部板块,不是实际权限核实'
|
||||
state['nav'] = account['cash']+sum(p['qty']*quotes.get(c, {}).get('price', 0.) for b in account['books'].values() for c, p in b.items())
|
||||
if done:
|
||||
event = {'at': now.isoformat(), 'mode': config['mode'], 'phase': s['phase'], 'results': done, 'snapshot_version': value['snapshot_version']}
|
||||
store.put_once('execution:'+hashlib.sha256(json.dumps(event, sort_keys=True, default=str).encode()).hexdigest(), 'execution', event)
|
||||
return {'mode': config['mode'], 'phase': s['phase'], 'results': done, 'blocked': waits+errors}
|
||||
|
||||
|
||||
def original_buy_blocker():
|
||||
if not settings.INFRA_STRATEGY_BOOK_SCHEMA:
|
||||
return None
|
||||
value, _ = store.get('state:live')
|
||||
phase = (value or {}).get('rotation', {}).get('phase', 'original')
|
||||
return '账户正在转入或运行微盘策略,原策略停止新增投入' if phase in ('entering_micro', 'micro') else None
|
||||
|
||||
|
||||
|
||||
def closing_t0(parent):
|
||||
p = parent or {}
|
||||
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()
|
||||
if book == books.ORIGINAL:
|
||||
if closing_t0(parent):
|
||||
return None
|
||||
return original_buy_blocker()
|
||||
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 '微盘阶段不允许新增买入'
|
||||
return None
|
||||
|
||||
|
||||
def reconcile_books():
|
||||
from app.services.ledger_service import positions_source
|
||||
from app.core.recon import diff_positions
|
||||
source = positions_source()
|
||||
totals = {}
|
||||
with books.scope('all'):
|
||||
rows = pms_repo.list_positions()
|
||||
for row in rows:
|
||||
code = row['ts_code']
|
||||
totals[code] = totals.get(code, 0)+int(row['total_qty'])
|
||||
if source['source'] != 'ws':
|
||||
return {'ok': False, 'diffs': [], 'fixes': [], 'errors': ['没有有效账户持仓事实源'], 'severity': 'ERROR'}
|
||||
differences = diff_positions([{'ts_code': c, 'total_qty': q} for c, q in totals.items()], source['rows'])
|
||||
return {'ok': not differences, 'diffs': differences, 'fixes': [], 'errors': [],
|
||||
'severity': 'ERROR' if differences else 'OK', 'source': source['source'],
|
||||
'note': '策略数量之和核对账户;不猜测差异归属,不自动把差异并入任何策略'}
|
||||
|
||||
|
||||
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'):
|
||||
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('全账户命令未同步到轮换控制')
|
||||
from app.services import executor
|
||||
with books.scope('micro30'):
|
||||
for ins in pms_repo.list_instructions(statuses=LIVE, limit=10000):
|
||||
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'):
|
||||
raise RuntimeError('用户命令已停新增,既有委托撤销尚未确认')
|
||||
elif cmd_type in ('OPEN_TARGET', 'INCREASE_EXPOSURE', 'REDUCE_EXPOSURE', 'EXIT_STOCK', 'REDUCE_STOCK'):
|
||||
state, _ = store.get('state:live')
|
||||
if state and state['rotation']['phase'] != 'original':
|
||||
result = param_store.set_param('PMS_ROTATION_CONTROL', 'pause_all', issued_by)
|
||||
if not result.get('ok'):
|
||||
raise RuntimeError('用户命令冲突暂停未保存')
|
||||
store.put_once('conflict:'+command_id, 'command_conflict', {'command_id': command_id, 'reason': '用户命令优先,轮换已暂停'})
|
||||
|
||||
|
||||
def settle_day(now=None):
|
||||
now = now or dt.datetime.now(TZ)
|
||||
day = now.date().isoformat()
|
||||
if not documents_ready() or now.hour*60+now.minute < 15*60+20 or td.calendar_degraded(day) or not td.is_trade_day(day):
|
||||
return {'skipped': '未到有效交易日结算时间或观察表未启用'}
|
||||
config = configuration()
|
||||
if config['mode'] == 'auto':
|
||||
from app.services.rotation_accounting import view
|
||||
result = view()
|
||||
healthy = result['ready'] and result['cash_difference'] is not None and abs(result['cash_difference']) <= .02 and reconcile_books()['ok']
|
||||
record = {'date': day, 'rule_version': core.RULE, 'healthy': healthy, **result}
|
||||
store.put_once('live_day:'+day, 'live_day', record)
|
||||
return record
|
||||
if config['mode'] != 'paper':
|
||||
return {'skipped': '非纸面模式'}
|
||||
with store.locked('state:paper', 'state', {'rotation': core.initial_state()}) as state:
|
||||
account = state.get('account')
|
||||
if not account:
|
||||
return {'skipped': '纸面账户尚未初始化'}
|
||||
from app.core import rotation_account as capital
|
||||
values = {b: 0. for b in ('original','micro30')}
|
||||
missing = []
|
||||
for book, rows in account['books'].items():
|
||||
for code, p in rows.items():
|
||||
if not p['qty']:
|
||||
continue
|
||||
q = market.day_snapshot(code)
|
||||
stamp = str(q.get('bar_time') or '')
|
||||
try:
|
||||
at = dt.datetime.fromisoformat(stamp)
|
||||
valid = at.date().isoformat() == day and at.hour*60+at.minute >= 890
|
||||
except ValueError:
|
||||
valid = False
|
||||
if not valid or not q.get('price'):
|
||||
missing.append(code)
|
||||
continue
|
||||
values[book] += p['qty']*q['price']
|
||||
healthy = bool(not missing and state.get('signal_healthy') and str(state.get('last_tick','')).startswith(day) and config['control'] != 'pause_all')
|
||||
metrics = capital.metrics(account['capital'], values) if account.get('capital') and not missing else None
|
||||
record = {'date': day, 'rule_version': core.RULE, 'healthy': healthy, 'missing_prices': missing,
|
||||
'metrics': metrics, 'fees': account['fees'], 'snapshot_version': state['rotation'].get('last_snapshot')}
|
||||
if metrics:
|
||||
state['metrics'] = metrics
|
||||
state['nav'] = metrics['account']['equity']
|
||||
store.put_once('paper_day:'+day, 'paper_day', record)
|
||||
return record
|
||||
|
||||
|
||||
def reset_micro_available(now=None):
|
||||
now = now or dt.datetime.now(TZ)
|
||||
if now.hour*60+now.minute >= 565:
|
||||
return {'skipped': '可卖数量只在盘前更新'}
|
||||
with books.scope('micro30'), store.locked('micro_available:'+now.date().isoformat(), 'daily_reset', {'done': False}) as marker:
|
||||
if marker['done']:
|
||||
return {'skipped': '当日已完成'}
|
||||
for p in pms_repo.list_positions(only_open=True):
|
||||
lots = pms_repo.list_lots(p['ts_code'])
|
||||
available = sum(int(r['qty']) for r in lots if str(r['open_date'])[:10] < now.date().isoformat())
|
||||
pms_repo.update_position(p['ts_code'], avail_qty=min(available, p['total_qty']))
|
||||
marker['done'] = True
|
||||
return {'done': True}
|
||||
|
|
@ -134,6 +134,11 @@ def _emit_instruction(st: dict, dec: dict, *, forced: bool = False) -> str:
|
|||
limit_price 不在这里定 —— 交由 run_tick 的择时按实时行情现算 (与 materialize_plans
|
||||
同口径: 那里也是 limit_price=None)。
|
||||
"""
|
||||
from config.settings import settings
|
||||
if settings.INFRA_STRATEGY_BOOK_SCHEMA and dec.get('leg') != 'close':
|
||||
from app.services.rotation_service import original_buy_blocker
|
||||
if original_buy_blocker():
|
||||
raise ValueError('策略轮换期间停止原策略开新腿')
|
||||
code = st["ts_code"]
|
||||
now = datetime.now()
|
||||
iid = f"STR{_today()}{now.strftime('%H%M%S')}_{code.replace('.', '')}_{dec['leg'][:1]}"[:40]
|
||||
|
|
|
|||
|
|
@ -39,6 +39,24 @@ STATIC_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "static")
|
|||
|
||||
app = FastAPI(title="PMS 持仓管理系统", version=VERSION)
|
||||
|
||||
|
||||
@app.get('/rotation')
|
||||
def rotation_page():
|
||||
return FileResponse(os.path.join(STATIC_DIR, 'rotation.html'))
|
||||
|
||||
|
||||
@app.get('/api/rotation')
|
||||
def rotation_status():
|
||||
from app.services.rotation_service import status
|
||||
return ok(status)
|
||||
|
||||
|
||||
@app.post('/api/ops/rotation/control')
|
||||
def rotation_control(request: Request, payload: dict = Body(...)):
|
||||
from app.services.rotation_service import control
|
||||
return ok_logged('rotation_control', control, mode=payload.get('mode'),
|
||||
action=payload.get('action'), actor=_actor(request, payload), by=_actor(request, payload), params=payload)
|
||||
|
||||
# 把 static 目录挂到 /static —— 内网隔离时的离线兜底:
|
||||
# 页面默认从 unpkg 取 Vue3 / ElementPlus / axios, 浏览器上不了外网就会白屏 (更准确地说是
|
||||
# 只剩一个光秃秃的头部, 因为 el-* 组件全渲染不出来)。把三个库放进 static/vendor/ 再把
|
||||
|
|
|
|||
|
|
@ -621,6 +621,7 @@ body.dock-r:not(.r-fold) .side-r .strip{display:none;}
|
|||
<el-dropdown-item v-if="isAdmin" command="view">切换到{{ mode==='ops' ? '交易员' : '运维' }}视图</el-dropdown-item>
|
||||
<el-dropdown-item command="plan" :divided="isAdmin">选股计划</el-dropdown-item>
|
||||
<el-dropdown-item command="report">日报</el-dropdown-item>
|
||||
<el-dropdown-item command="rotation">策略轮换</el-dropdown-item>
|
||||
<el-dropdown-item v-if="authed" command="export" title="按公司《量化数据》模板导出当日持仓、平仓与净值(系统接管后的数据)">导出持仓表</el-dropdown-item>
|
||||
<el-dropdown-item v-if="isAdmin && mode==='ops'" command="ops">运维操作</el-dropdown-item>
|
||||
<el-dropdown-item command="auto" divided>自动刷新 {{ autoRefresh ? '开 · 点此关闭' : '关 · 点此开启' }}</el-dropdown-item>
|
||||
|
|
@ -4654,6 +4655,7 @@ createApp({
|
|||
return (who ? who + ' · ' : '') + (mode.value === 'ops' ? '运维' : '交易员');
|
||||
});
|
||||
function hdMenu(c) {
|
||||
if (c === 'rotation') { window.location.href = '/rotation'; return; }
|
||||
if (c === 'view') mode.value = mode.value === 'ops' ? 'trader' : 'ops';
|
||||
else if (c === 'plan') openPlan();
|
||||
else if (c === 'report') openReport();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
<!doctype html>
|
||||
<html lang="zh-CN">
|
||||
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>策略轮换 · PMS</title>
|
||||
<style>
|
||||
*{box-sizing:border-box}body{margin:0;background:#f4f6f8;color:#182638;font:15px/1.6 system-ui,-apple-system,sans-serif}main{max-width:1240px;margin:28px auto;padding:0 24px}header{display:flex;align-items:center;justify-content:space-between}h1{font-size:25px;margin:0}h2{font-size:18px;margin:0 0 14px}.sub{color:#627185}.grid{display:grid;grid-template-columns:repeat(3,1fr);gap:16px}.card{background:white;border:1px solid #dde4eb;border-radius:10px;padding:20px;margin-top:18px}.metric{font-size:28px;font-weight:650}.label{font-size:13px;color:#627185}button,a{color:#194c79}button{border:1px solid #c5d3e0;border-radius:6px;background:#fff;padding:8px 12px;cursor:pointer;margin:4px}button:disabled{opacity:.45;cursor:not-allowed}.warning{color:#8c4e09;background:#fff8e8;padding:12px;border-radius:6px;white-space:pre-line}table{border-collapse:collapse;width:100%;font-size:13px}th,td{text-align:left;border-bottom:1px solid #e8edf2;padding:10px 8px}th{color:#627185}.scroll{overflow-x:auto}.positive{color:#b7252f}.negative{color:#208356}.empty{color:#627185;padding:18px}#errors:empty{display:none}.controls{display:flex;flex-wrap:wrap;gap:8px}.small{font-size:12px}@media(max-width:760px){.grid{grid-template-columns:1fr}main{padding:0 12px}.metric{font-size:24px}}
|
||||
</style></head>
|
||||
<body><main>
|
||||
<header><div><h1>策略轮换</h1><div class="sub">原主观量化策略与微盘30股策略共用账户,分别记录。</div></div><div><a href="/">返回工作台</a><button id="refresh">刷新</button></div></header>
|
||||
<div class="card warning" id="errors"></div>
|
||||
<div class="grid"><section class="card"><div class="label">运行档位</div><div class="metric" id="mode">读取中</div><div class="sub" id="control"></div></section><section class="card"><div class="label">账户资金使用阶段</div><div class="metric" id="phase">读取中</div><div class="sub" id="reason"></div></section><section class="card"><div class="label">名单与数据日期</div><div class="metric" id="count">—</div><div class="sub" id="date"></div></section></div>
|
||||
<section class="card"><h2>宏观信号</h2><div class="grid"><div><div class="label">成长主线瓦解</div><div class="metric" id="growth">—</div></div><div><div class="label">微盘活跃度历史位置</div><div class="metric" id="percentile">—</div></div><div><div class="label">本次判断</div><div class="metric" id="signal">—</div></div></div><p class="sub">成长主线瓦解或低于历史5%分位时入场,高于历史95%分位时退出。中证2000成交活跃度用于观察市场状态。股汇对冲指标只作用于原策略。</p><div class="small sub" id="version"></div></section>
|
||||
<section class="card"><h2>目标名单与实际进度</h2><p class="sub">目标固定30只。未完成建仓或尚未卖出的调出成员,会使实际数量暂时偏离30只。保留成员不重新等权。</p><p id="progress" class="sub"></p><div class="scroll"><table><thead><tr><th>排名</th><th>代码</th><th>名称</th><th>总市值(亿元)</th><th>收盘价</th><th>全日成交额(万元)</th><th>资格</th><th>实际股数</th><th>名单变动</th><th>初始金额/日成交额</th></tr></thead><tbody id="members"></tbody></table></div><div class="warning" id="waits" hidden></div></section>
|
||||
<section class="card"><h2>资金与收益记录</h2><div id="account" class="sub">尚无纸面或自动执行账本。</div><div class="scroll"><table><thead><tr><th>策略</th><th>资产</th><th>净投入</th><th>盈亏</th><th>单位净值</th><th>最大回撤</th><th>累计费用</th></tr></thead><tbody id="summary"></tbody></table><table><thead><tr><th>策略</th><th>股票</th><th>股数</th><th>账面成本</th><th>持仓权重</th><th>估值</th></tr></thead><tbody id="positions"></tbody></table></div><div id="reconcile" class="small sub"></div><p class="small sub">历史30只研究收益不等于账户轮换收益。纸面结果单独记录,资金划拨不计为盈利。开盘窗口容量与实际成交偏差在执行后记录。</p></section>
|
||||
<section class="card"><h2>验收与控制</h2><div class="warning" id="activation"></div><p class="sub">观察至少3个交易日,再纸面运行至少10个交易日。自动档需要回测、容量、账本、资金占用及页面验收完成。</p><div class="controls"><button data-mode="observe">观察</button><button data-mode="paper">纸面模拟</button><button data-mode="auto" id="automatic">自动执行</button><button data-action="pause_new">暂停新增</button><button data-action="pause_all">暂停全部</button><button data-action="run">恢复运行</button><button data-action="exit_disable">退出并停用</button></div><div class="small sub">控制操作需要系统管理员权限。退出并停用会产生卖出安排;观察和纸面档不会向QMT发单。</div></section>
|
||||
</main><script>
|
||||
const modes={observe:'观察',paper:'纸面模拟',auto:'自动执行'},phases={original:'原策略运行',entering_micro:'转入微盘',micro:'微盘运行',returning_original:'转回原策略'},controls={run:'运行',pause_new:'暂停新增',pause_all:'暂停全部',exit_disable:'退出并停用'};
|
||||
const el=id=>document.getElementById(id),fmt=(x,n=2)=>x==null?'—':Number(x).toLocaleString('zh-CN',{maximumFractionDigits:n});
|
||||
function cell(row,value){const td=document.createElement('td');td.textContent=value;row.append(td);return td}
|
||||
function line(table,values){const tr=document.createElement('tr');values.forEach(v=>cell(tr,v));table.append(tr)}
|
||||
async function request(url,options){const r=await fetch(url,options);if(r.status===401)throw Error('请先返回工作台登录');if(r.status===403)throw Error('此操作需要系统管理员权限');const v=await r.json();if(!r.ok||v.ok===false)throw Error(v.error||'请求失败');return v.data||v}
|
||||
async function load(){try{const v=await request('/api/rotation'),s=v.snapshot||{},g=s.signal||{},st=v.state||{},a=st.account;el('errors').textContent=[...(v.errors||[]),...(s.errors||[]),...(!s.basket_ready?['目标名单尚未就绪,停止新增和换仓。']:[])].join('\n');el('mode').textContent=modes[v.mode]||'未知档位';el('control').textContent=controls[v.control]||'未知控制';el('phase').textContent=phases[v.phase]||'等待状态';el('reason').textContent=(st.rotation||{}).reason||'等待有效信号';el('count').textContent=(s.members||[]).length+' / 30';el('date').textContent=s.data_date?('数据 '+s.data_date+' · 执行 '+s.effective_date+' · '+(s.frozen?'已冻结':'未冻结')):'尚无快照';el('growth').textContent=s.signal_ready?(g.growth_break?'已触发':'未触发'):'数据未就绪';el('percentile').textContent=g.activity_percentile==null?'—':fmt(g.activity_percentile*100)+'%';el('signal').textContent=!s.signal_ready?'等待数据':g.high?'高位退出':g.low?'极低位入场':g.growth_break?'成长瓦解入场':'保持当前阶段';el('version').textContent='规则 '+(s.rule_version||v.rule_version)+' · 快照 '+(s.snapshot_version||'暂无');el('members').replaceChildren();(s.members||[]).forEach(m=>line(el('members'),[m.rank,m.code,m.name,fmt(m.total_mv_wan/10000),fmt(m.close),fmt(m.daily_amount_yuan/10000),m.eligible?'通过':'未通过']));el('waits').hidden=!(st.blocked||[]).length;el('waits').textContent=(st.blocked||[]).join('\n');el('positions').replaceChildren();if(a){el('account').textContent='纸面可用现金 '+fmt(a.cash)+' 元 · 最近估值 '+fmt(st.nav)+' 元 · 初始化 '+a.seeded_at;Object.entries(a.books||{}).forEach(([book,rows])=>Object.entries(rows).filter(([,p])=>p.qty).forEach(([code,p])=>line(el('positions'),[book==='micro30'?'微盘30股':'原策略',code,p.qty,fmt(p.cost),fmt(a.realized[book]),fmt(a.fees[book])])))}else el('account').textContent='尚无纸面或自动执行账本。';el('activation').textContent=(v.activation_errors||[]).join('\n')||'自动执行的前置验收已完成。';el('automatic').disabled=!!(v.activation_errors||[]).length;renderDetails(v)}catch(e){el('errors').textContent=e.message}}
|
||||
function renderDetails(v){
|
||||
const st=v.state||{},a=st.account,s=v.snapshot||{},p=st.progress||{},metrics=st.metrics||(v.accounting||{}).metrics||{};
|
||||
const names={original:'原策略',micro30:'微盘30股',account:'全账户'};
|
||||
const positions=a?Object.entries(a.books||{}).flatMap(([book,rows])=>Object.entries(rows).filter(([,p])=>p.qty).map(([code,p])=>({book_id:book,code,qty:p.qty,cost:p.cost,market_value:null}))):((v.accounting||{}).positions||[]);
|
||||
el('progress').textContent='股票预算上限 '+fmt(v.stock_budget)+' 元 · 原策略剩余 '+fmt(p.original_market_value)+' 元 · 微盘已建 '+fmt(p.micro_market_value)+' 元 · 可用现金 '+fmt(p.available_cash)+' 元 · 观察 '+(v.observed_days||0)+' 日 · 健康纸面 '+(v.paper_days||0)+' 日';
|
||||
el('members').replaceChildren();const target=new Set((s.members||[]).map(x=>x.code));
|
||||
(s.members||[]).forEach(m=>{const held=positions.find(x=>x.book_id==='micro30'&&x.code===m.code);line(el('members'),[m.rank,m.code,m.name,fmt(m.total_mv_wan/10000),fmt(m.close),fmt(m.daily_amount_yuan/10000),s.basket_ready&&m.eligible?'通过':'待核实',held?held.qty:'—',!positions.length?'待建仓':held?'保留':'调入',m.daily_amount_yuan?fmt(v.stock_budget/30/m.daily_amount_yuan*100)+'%':'未知']);});
|
||||
positions.filter(x=>x.book_id==='micro30'&&!target.has(x.code)).forEach(x=>line(el('members'),['—',x.code,'残余持仓','—','—','—','等待卖出',x.qty,'调出','—']));
|
||||
el('positions').replaceChildren();positions.forEach(x=>line(el('positions'),[names[x.book_id],x.code,x.qty,fmt(x.cost),x.market_value&&metrics[x.book_id]?.equity?fmt(x.market_value/metrics[x.book_id].equity*100)+'%':'—',fmt(x.market_value)]));
|
||||
el('summary').replaceChildren();Object.entries(metrics).forEach(([book,r])=>{let peak=0,dd=0;[...(v.daily||[])].reverse().forEach(x=>{const n=(x.value.metrics||{})[book]?.nav;if(n>0){peak=Math.max(peak,n);dd=Math.min(dd,n/peak-1);}});line(el('summary'),[names[book],fmt(r.equity),fmt(r.net_capital),fmt(r.profit),fmt(r.nav,4),book==='account'?'—':fmt(dd*100)+'%',fmt(r.fees)]);});
|
||||
if(!a&&v.accounting?.ready)el('account').textContent='实际账本自 '+v.accounting.started_at+' 起记录 · 内部划拨 '+v.accounting.transfer_count+' 笔';
|
||||
const diff=v.accounting?.cash_difference;el('reconcile').textContent=v.reconciliation?('持仓对账 '+(v.reconciliation.ok?'一致':'待核对')+' · 现金差异 '+fmt(diff)+' 元'):'交易账本尚未启用,当前仅观察。';
|
||||
}
|
||||
el('refresh').onclick=load;document.querySelectorAll('[data-mode],[data-action]').forEach(b=>b.onclick=async()=>{try{if(b.dataset.mode==='auto'&&!confirm('将启用已验收的155模拟账户自动执行。确认启用?'))return;if(b.dataset.action==='exit_disable'&&!confirm('将停止新增并退出微盘持仓。确认执行?'))return;await request('/api/ops/rotation/control',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(b.dataset.mode?{mode:b.dataset.mode}:{action:b.dataset.action})});await load()}catch(e){el('errors').textContent=e.message}});load();setInterval(load,30000);
|
||||
</script></body></html>
|
||||
|
|
@ -17,6 +17,10 @@ from pydantic_settings import BaseSettings, SettingsConfigDict
|
|||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8", extra="ignore")
|
||||
|
||||
# 数据库基础设施能力位。迁移核验完成后才在部署环境打开,不是交易开关。
|
||||
INFRA_STRATEGY_BOOK_SCHEMA: bool = False
|
||||
INFRA_ROTATION_DOCUMENT_SCHEMA: bool = False
|
||||
|
||||
# =========================================================
|
||||
# 一、基础设施 (键名对齐 bionic_trader, 同一 .env 可共用)
|
||||
# =========================================================
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
-- 轮换状态、快照、验收证据及独立纸面账本。与交易表分开存储。
|
||||
CREATE TABLE IF NOT EXISTS pms_rotation_document (
|
||||
doc_id VARCHAR(128) NOT NULL PRIMARY KEY,
|
||||
kind VARCHAR(32) NOT NULL,
|
||||
payload_json LONGTEXT NOT NULL,
|
||||
revision BIGINT NOT NULL DEFAULT 0,
|
||||
updated_at DATETIME NOT NULL,
|
||||
KEY idx_kind_updated (kind, updated_at)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='轮换持久状态与独立纸面记录';
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
# 微盘30股策略接入PMS
|
||||
|
||||
## 交付范围
|
||||
|
||||
按用户补充要求,研究工作收口到既有验证与30股对照,实施重点为PMS接入。bionic独立容器计算确定性每日快照,PMS负责策略切换、资金和委托归属,沿用唯一pms-ws连接。
|
||||
|
||||
PMS新增“策略轮换”入口,地址为/rotation,默认观察档。页面展示宏观信号、30股名单、数据健康、调入调出、资金进度、分策略账本、验收门槛和控制。业务参数通过ParamStore读取。用户已确认佣金万二,创业板和科创板权限齐全。最低佣金暂按5元假设,仍须核实。
|
||||
|
||||
## 上线开关
|
||||
|
||||
观察表与交易账本独立启用。INFRA_ROTATION_DOCUMENT_SCHEMA=true表示仅观察及独立纸面记录表已建立。INFRA_STRATEGY_BOOK_SCHEMA=false时,既有持仓、成交认领和委托链路仍使用原口径。首次上线只开前者。
|
||||
|
||||
PMS通过http://bionic-micro30-monitor:38130/v1/snapshot拉取快照。监控连接PMS现有Docker网络,不持有交易密钥。08:50之前的版本才能冻结;当天晚启动不补造执行快照,也不计合格观察日。
|
||||
|
||||
## 交易账本与执行
|
||||
|
||||
migrate_strategy_books.py给命令、方案、指令、持仓、批次、提议、动作账本、现金流水和个股策略增加book_id。原记录默认original,微盘使用micro30。迁移逐表比对原字段哈希,包括数量、现金、成本及历史字段。持仓唯一键改为策略加股票代码。pms_rotation_document保存版本快照、轮换阶段、资金划拨、纸面账本、验收记录和净值。
|
||||
|
||||
成交按父委托归属,不跨策略按股票认领。成交、费用、批次及回报完成标记放入同一事务,异常回滚。无法归属的人工成交留待核对。两账本数量之和与QMT对账,差异不自动归入任意策略。
|
||||
|
||||
四阶段为原策略运行、转入微盘、微盘运行、转回原策略。转入暂停原策略新投入,允许未完成做T平回;原策略按原降仓顺序释放资金。买入只使用已确认可用现金。微盘独立日频执行,不经过原策略研判、网格或保垫。内部划拨改变本金和份额,不计利润。
|
||||
|
||||
默认09:30至10:00执行。窗口结束撤单,次日重算。高位退出优先于入场。残余未出清不启动新一轮,同名股票先卖旧仓。监控故障不自动退出;已经确认的退出保留,可对有有效报价的持仓继续执行。
|
||||
|
||||
## 启用顺序
|
||||
|
||||
先建观察表、发布独立监控与PMS页面,记录至少3个真实交易日。再启用独立纸面账户,至少运行10个真实交易日。纸面不使用shadow真实成交认领路径。
|
||||
|
||||
正式交易账本迁移在维护窗口、无在途订单且对账一致时进行。先备份,再迁移,初始化分策略现金,最后在所有PMS进程启用INFRA_STRATEGY_BOOK_SCHEMA。在此之前不得开启自动执行。
|
||||
|
||||
自动档还检查回测、容量、费率、板块权限、迁移、回归、浏览器、成交归属和资金占用证据。历史回放不抵扣观察日或纸面日。188不在本次上线范围,带真实仓前缀的实例拒绝自动启用。
|
||||
|
||||
回退时关闭新策略自动执行,继续接收在途回报。已经发生策略交易后保留策略账本,不退旧表结构。
|
||||
|
||||
## 验证记录
|
||||
|
||||
开发机经断网护栏运行scripts/run_tests.py,全部批次通过。新增微盘批次35项通过。页面三道静态护栏通过。桌面与390像素窄屏浏览器已检查30行名单、缺数据提示、控制请求和自动档禁用,无页面脚本错误。
|
||||
|
||||
155隔离迁移、匹配镜像回归及观察功能真机结果将在本次部署后追加。自动交易与纸面交易日验收尚未完成。
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
# 微盘30股轮动实施规格
|
||||
|
||||
用户已确认按2026年10月27日方案实施。目标为合格沪深A股总市值最小30只,宏观信号沿用100只实验。成长主线瓦解或中证2000活跃度低于此前756日的5%分位入场,高于95%分位退出。收盘决策,下一交易日执行。没有固定20日退出。
|
||||
|
||||
## 验收目标
|
||||
|
||||
先完成2024年1月2日至2026年10月23日的30只回测,与不可覆盖的100只修复版比较。报告绝对收益、回撤、逐轮结果、费用、换手、集中度、无法成交及容量。历史缺失与复权账户的限制必须保留。最低佣金、板块权限和当前资金不能用猜测值称为实测。
|
||||
|
||||
监控归bionic的独立CPU容器。PMS拉取版本化只读快照,默认观察。信号与名单分别判定就绪,每个执行日在北京时间08:50冻结。快照缺失不是清仓。监控无下单密钥,无业务表写权限。
|
||||
|
||||
PMS保留一条QMT连接。策略账本必须贯穿命令、方案、指令、批次、成交、资金和净值。原记录归原策略。按委托编号认领成交,无法归属的外部成交待核对。资金划拨不计收益,策略数量之和与QMT一致。
|
||||
|
||||
## 行为边界
|
||||
|
||||
原策略运行、转入微盘、微盘运行、转回原策略四个阶段持久化。入场接管允许的全部仓位。先暂停原策略买入并处理在途与做T,按原降仓排序卖出,确认释放资金后买入微盘。退出优先,残余未卖完不接受新的微盘轮次。确认释放的现金可先交回原策略。同名股票先卖旧仓,再买新仓。
|
||||
|
||||
微盘首次等资金,保留成员不重新等权。目标30只不等于任何时刻实际持仓恰好30只。失败买入留现金,失败卖出留持仓,不用名单外股票凑数。
|
||||
|
||||
微盘不经过原质地、研判、网格、均线和保垫规则。股汇对冲只影响原策略。用户命令、实际现金、全账户仓位、交易权限、行情和对账约束仍有效。默认09:30至10:00执行,窗口末撤单,次日按最新有效名单续办。共享资金占用,不重复计算QMT已冻结现金及卖出回笼。
|
||||
|
||||
## 上线与撤销
|
||||
|
||||
155先观察至少3个交易日,再独立纸面账本运行至少10个交易日。纸面账本不得调用认领真实成交的shadow路径。历史隔离回放补齐罕见切换,不往运行账户注入假信号。验收条件满足后才能自动执行,188不在本次上线范围。
|
||||
|
||||
回退关闭自动执行,保留策略归属及在途回报。已发生策略交易后不得盲目回退旧表结构。原全量断网回归、页面三道护栏、浏览器验收及迁移演练均须记录机器与结果。
|
||||
|
||||
## 实施记录
|
||||
|
||||
本文件为规格,不是已验收声明。各阶段实测结果另记DEVLOG与研究报告。观察与纸面交易日要求按真实经过的交易日计数,历史回放不能替代。
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
"""显式迁移策略归属。默认只检查;--apply只用于已确认的隔离演练或155维护窗口。"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from sqlalchemy import text
|
||||
from app.db import session as db
|
||||
from app.core.strategy_book import TABLES
|
||||
from config.settings import settings
|
||||
|
||||
|
||||
def inspect(connection, table):
|
||||
return list(connection.execute(text('SHOW COLUMNS FROM '+table)).mappings())
|
||||
|
||||
|
||||
def migrate(apply=False, documents_only=False):
|
||||
if settings.PMS_TABLE_PREFIX:
|
||||
raise RuntimeError('此迁移脚本只允许155模拟实例或无前缀隔离演练')
|
||||
results = []
|
||||
if documents_only:
|
||||
if apply:
|
||||
sql = (Path(__file__).resolve().parents[1]/'ddl_rotation_v1.sql').read_text()
|
||||
sql = '\n'.join(l for l in sql.splitlines() if not l.strip().startswith('--')).strip().rstrip(';')
|
||||
with db.get_engine('proxy').begin() as c:
|
||||
c.execute(text(sql))
|
||||
return [{'documents_only': True, 'applied': apply}]
|
||||
with db.get_engine('proxy').begin() as c:
|
||||
for table in sorted(TABLES):
|
||||
columns = inspect(c, table)
|
||||
before = c.execute(text('SELECT COUNT(*) FROM '+table)).scalar_one()
|
||||
original_cols = [r['Field'] for r in columns if r['Field'] != 'book_id']
|
||||
projection = ','.join('`'+name+'`' for name in original_cols)
|
||||
old_rows = c.execute(text('SELECT '+projection+' FROM '+table+' ORDER BY `id`')).mappings().all()
|
||||
checksum = hashlib.sha256(json.dumps([dict(r) for r in old_rows], sort_keys=True, default=str).encode()).hexdigest()
|
||||
exists = any(r['Field'] == 'book_id' for r in columns)
|
||||
if apply and not exists:
|
||||
c.execute(text('ALTER TABLE '+table+" ADD COLUMN book_id VARCHAR(16) NOT NULL DEFAULT 'original'"))
|
||||
if apply:
|
||||
other = c.execute(text('SELECT COUNT(*) FROM '+table+" WHERE book_id NOT IN ('original','micro30')")).scalar_one()
|
||||
if other:
|
||||
raise RuntimeError('存在未知策略归属')
|
||||
if table == 'pms_position':
|
||||
indexes = list(c.execute(text('SHOW INDEX FROM pms_position')).mappings())
|
||||
for name in {r['Key_name'] for r in indexes if r['Key_name'] != 'PRIMARY' and not r['Non_unique']}:
|
||||
cols = [r['Column_name'] for r in sorted(indexes, key=lambda r: r['Seq_in_index']) if r['Key_name'] == name]
|
||||
if cols == ['ts_code']:
|
||||
if not re.fullmatch(r'[A-Za-z0-9_]+', name):
|
||||
raise RuntimeError('索引名非法')
|
||||
c.execute(text('ALTER TABLE pms_position DROP INDEX `'+name+'`, ADD UNIQUE KEY uk_book_code (book_id,ts_code)'))
|
||||
after = c.execute(text('SELECT COUNT(*) FROM '+table)).scalar_one()
|
||||
after_rows = c.execute(text('SELECT '+projection+' FROM '+table+' ORDER BY `id`')).mappings().all()
|
||||
after_checksum = hashlib.sha256(json.dumps([dict(r) for r in after_rows], sort_keys=True, default=str).encode()).hexdigest()
|
||||
if before != after or checksum != after_checksum:
|
||||
raise RuntimeError('迁移时数量、现金、成本或历史字段变化,必须在维护窗口重核')
|
||||
results.append({'table': table, 'rows': before, 'book_id_existed': exists, 'applied': apply, 'original_columns_sha256': checksum})
|
||||
if apply:
|
||||
sql = (Path(__file__).resolve().parents[1]/'ddl_rotation_v1.sql').read_text()
|
||||
sql = '\n'.join(l for l in sql.splitlines() if not l.strip().startswith('--')).strip().rstrip(';')
|
||||
c.execute(text(sql))
|
||||
return results
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--apply', action='store_true')
|
||||
parser.add_argument('--documents-only', action='store_true', help='仅建观察和纸面记录表,不迁移交易账本')
|
||||
args = parser.parse_args()
|
||||
print(json.dumps(migrate(args.apply, args.documents_only), ensure_ascii=False, indent=2))
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
"""只允许专用隔离数据库的迁移与真实SQL事务演练。不得对业务库运行。"""
|
||||
import datetime as dt,json,os,sys
|
||||
from pathlib import Path
|
||||
from urllib.parse import urlsplit
|
||||
from unittest.mock import patch
|
||||
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
|
||||
from sqlalchemy import text
|
||||
from config.settings import settings
|
||||
from app.db import session as db
|
||||
from app.core import strategy_book as books,rotation_account as capital
|
||||
from app.repo import pms_repo,qmt_repo,rotation_repo as store
|
||||
from app.services import rotation_execution as execution
|
||||
from init_db import parse_statements
|
||||
from migrate_strategy_books import migrate
|
||||
|
||||
|
||||
def main():
|
||||
u=urlsplit(settings.PROXY_DB_URL)
|
||||
assert u.hostname=='micro30-rehearsal-db' and u.path=='/micro30_rehearsal' and not settings.PMS_TABLE_PREFIX,'仅允许专用隔离库'
|
||||
assert not settings.INFRA_STRATEGY_BOOK_SCHEMA,'演练应从旧表开始'
|
||||
with db.get_engine().begin() as c:
|
||||
assert not c.execute(text('SHOW TABLES')).all(),'隔离库必须为空'
|
||||
for kind,table,sql in parse_statements((Path(__file__).resolve().parents[1]/'ddl_pms_v1.sql').read_text()):
|
||||
assert kind!='?';c.execute(text(sql))
|
||||
pms_repo.ensure_position('600000.SH');pms_repo.bump_position_qty('600000.SH',total_delta=100,avail_delta=100)
|
||||
pms_repo.insert_lot(ts_code='600000.SH',lot_type='BASE',qty=100,open_price=20,open_date=dt.date(2024,1,2))
|
||||
pms_repo.insert_cash_flow(ymd=20240102,kind='FEE',amount=-5,trade_no='legacy',estimated=False)
|
||||
proof=migrate(True);assert all(r['applied'] for r in proof)
|
||||
assert all(r['original_columns_sha256'] for r in proof)
|
||||
migrate(True) # 重跑不得改历史字段或重复索引。
|
||||
settings.INFRA_STRATEGY_BOOK_SCHEMA=True
|
||||
with books.scope('micro30'):
|
||||
pms_repo.ensure_position('600000.SH');pms_repo.bump_position_qty('600000.SH',total_delta=200,avail_delta=0)
|
||||
assert pms_repo.get_position('600000.SH')['total_qty']==100
|
||||
with books.scope('micro30'):assert pms_repo.get_position('600000.SH')['total_qty']==200
|
||||
with books.scope('all'):assert len(pms_repo.list_positions())==2
|
||||
# 清除纯隔离样例的微盘虚拟数量,后续使用真实委托与成交路径。
|
||||
with books.scope('micro30'):pms_repo.update_position('600000.SH',total_qty=0)
|
||||
store.put_once('state:live','state',{'rotation':{'phase':'micro'}})
|
||||
cash=capital.seed(100000,{'original':2000,'micro30':0});store.put_once('capital:live','capital',cash)
|
||||
params={'PMS_ROTATION_MODE':'auto','PMS_ROTATION_CONTROL':'run','PMS_TOTAL_SCALE':200000.,'PMS_PORTFOLIO_CAP':.7,'PMS_CASH_RESERVE':0.,'PMS_MICRO30_COMMISSION_RATE':.0002}
|
||||
now=dt.datetime.now()
|
||||
funds={'seq':100,'age_sec':0,'received_at':now,'payload':{'data':{'available_cash':100000,'frozen_cash':0,'market_value':2000,'total_asset':102000}}}
|
||||
with patch('app.services.param_store.get',side_effect=lambda k,d=None:params.get(k,d)),patch('app.services.param_store.get_float',side_effect=lambda k,d=0:float(params.get(k,d))),patch('app.services.param_store.get_bool',side_effect=lambda k,d=False:bool(params.get(k,d))),patch('app.services.market.day_snapshot',return_value={'price':20,'bar_time':now.isoformat()}),patch('app.services.market.get_price',return_value=20),patch('app.repo.qmt_repo.latest_snapshot',return_value=funds):
|
||||
for i in range(30):
|
||||
pid='B'+str(i);code=str(600100+i)+'.SH'
|
||||
with books.scope('micro30'):pms_repo.insert_instruction(instruction_id=pid,origin_type='plan',origin_id=None,ts_code=code,action='OPEN',side='buy',qty=100)
|
||||
payload={'instruction_id':pid+'_D01','ts_code':code,'side':'buy','qty':100,'limit_price':10,'valid_until':int((now+dt.timedelta(hours=1)).timestamp()*1000),'intent':'OPEN'}
|
||||
execution.enqueue_with_reservation(payload,pid)
|
||||
execution.enqueue_with_reservation(payload,pid)
|
||||
assert db.fetch_one('SELECT COUNT(*) AS n FROM pms_qmt_order')['n']==30
|
||||
reserve,_=store.get('account-execution');assert len(reserve['orders'])==30
|
||||
assert 30000<sum(reserve['orders'].values())<30200
|
||||
# 大单资金不足不得进入出口。
|
||||
with books.scope('micro30'):pms_repo.insert_instruction(instruction_id='HUGE',origin_type='plan',origin_id=None,ts_code='600199.SH',action='OPEN',side='buy',qty=10000)
|
||||
try:execution.enqueue_with_reservation({**payload,'instruction_id':'HUGE_D01','ts_code':'600199.SH','qty':10000},'HUGE')
|
||||
except ValueError:pass
|
||||
else:raise AssertionError('超买未被拦截')
|
||||
assert qmt_repo.get_order('HUGE_D01') is None
|
||||
# 成交标记失败时,持仓、批次、现金和指令必须一起回滚。
|
||||
qmt_repo.inbox_put(seq=1,msg_id='trade1',msg_type='trade',payload={'trade_no':'T1'},msg_ts=int(now.timestamp()*1000),processed=0)
|
||||
act={'kind':'BUY','ts_code':'600100.SH','qty':100,'price':10.,'lot_type':'BASE','instruction_id':'B0','order_id':'T1'}
|
||||
fees=[{'amount':-5.,'ts_code':act['ts_code'],'estimated':False,'trade_no':'T1','instruction_id':'B0','note':'演练费用'}]
|
||||
with patch('app.repo.qmt_repo.inbox_mark',side_effect=RuntimeError('模拟落账尾部故障')):
|
||||
try:execution.book_trade(act,fees,1)
|
||||
except RuntimeError:pass
|
||||
else:raise AssertionError('应发生尾部故障')
|
||||
with books.scope('micro30'):
|
||||
assert pms_repo.get_position('600100.SH') is None
|
||||
assert pms_repo.get_instruction('B0')['exec_qty']==0
|
||||
assert execution.book_trade(act,fees,1)
|
||||
assert not execution.book_trade(act,fees,1)
|
||||
with books.scope('micro30'):
|
||||
assert pms_repo.get_position('600100.SH')['total_qty']==100
|
||||
assert pms_repo.get_instruction('B0')['exec_qty']==100
|
||||
assert len(pms_repo.list_lots('600100.SH'))==1
|
||||
assert pms_repo.get_position('600100.SH') is None
|
||||
# 子事务故障保留阶段,但不留下半条委托。
|
||||
with store.locked('savepoint-test','state',{'phase':'returning_original'}) as state:
|
||||
try:
|
||||
with db.savepoint():
|
||||
state['irrelevant']='local-memory-only'
|
||||
db.execute("INSERT INTO pms_rotation_document (doc_id,kind,payload_json,revision,updated_at) VALUES ('must-rollback','test','{}',0,NOW())")
|
||||
raise ValueError('演练回滚')
|
||||
except ValueError:pass
|
||||
assert store.get('must-rollback')[0] is None
|
||||
assert store.get('savepoint-test')[0]['phase']=='returning_original'
|
||||
result={'migration_tables':proof,'checks':['迁移字段哈希一致','重复迁移','同名股票分账','30股批量委托','重复委托幂等','超买拦截','成交尾部故障原子回滚','重复成交幂等','子事务回滚保留退出阶段'],'status':'PASS','environment':'155专用内部网络MariaDB,合成数据,无QMT与业务库连接'}
|
||||
print(json.dumps(result,ensure_ascii=False,default=str))
|
||||
if __name__=='__main__':main()
|
||||
|
|
@ -5,6 +5,7 @@
|
|||
运行: docker compose run --rm pms-web python scripts/run_tests.py
|
||||
|
||||
包含 (例数按 2026-10-26 隔离回归实跑校正):
|
||||
test_batch48_units.py 策略账本隔离、微盘轮换优先级、真实交易单位及纸面账本 (35例)
|
||||
test_batch47_units.py 账户资金去重、买入额度与账户展示和策略估值分离 (9 例)
|
||||
test_batch46_units.py 质地排序切换、档位准入、命令分配与白名单优先 (11 例)
|
||||
test_batch45_units.py 父指令总量:回放延迟、部分成交、跨日预留、故障守成与影子模式 (16 例)
|
||||
|
|
@ -202,6 +203,7 @@ SUITES = ["test_core_units.py", "test_batch2_units.py", "test_batch3_units.py",
|
|||
"test_batch45_units.py",
|
||||
"test_batch46_units.py",
|
||||
"test_batch47_units.py",
|
||||
"test_batch48_units.py",
|
||||
"test_page_copy_guard.py",
|
||||
"test_page_enum_guard.py",
|
||||
"test_page_wiring_guard.py",
|
||||
|
|
|
|||
|
|
@ -0,0 +1,208 @@
|
|||
"""策略账本隔离、轮换优先级、纸面现金及重复成交回归。"""
|
||||
import unit_test_guard # noqa: F401
|
||||
import copy
|
||||
import datetime as dt
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import unittest
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from app.core import strategy_book as b, micro_rotation as m
|
||||
|
||||
|
||||
def snapshot(day='2026-10-27', low=False, high=False, growth=False, ready=True):
|
||||
members = [{'code': f'{600000+i}.SH', 'rank': i+1, 'eligible': True, 'close': 10., 'total_mv_wan': i+1} for i in range(30)]
|
||||
value = {'rule_version': m.RULE, 'basket_size': 30, 'data_date': '2026-10-26',
|
||||
'effective_date': day, 'signal_ready': ready, 'basket_ready': ready,
|
||||
'signal': {'date': '2026-10-26', 'ready': ready, 'low': low, 'high': high, 'growth_break': growth},
|
||||
'members': members, 'source_hashes': {'source': 'fixed'}, 'errors': []}
|
||||
value['snapshot_version'] = hashlib.sha256(json.dumps(value, sort_keys=True, ensure_ascii=False, separators=(',', ':')).encode()).hexdigest()
|
||||
value['frozen'] = True
|
||||
return value
|
||||
|
||||
|
||||
class RotationTests(unittest.TestCase):
|
||||
def test_default_scope(self):
|
||||
self.assertEqual(b.CURRENT.get(), 'original')
|
||||
with b.scope('micro30'):
|
||||
self.assertIn("book_id = 'micro30'", b.scoped_sql('SELECT * FROM pms_position'))
|
||||
self.assertEqual(b.CURRENT.get(), 'original')
|
||||
|
||||
def test_where_or_is_wrapped(self):
|
||||
sql = b.scoped_sql('SELECT * FROM pms_lot WHERE qty>0 OR closed_qty>0 ORDER BY id LIMIT 5')
|
||||
self.assertIn("WHERE book_id = 'original' AND (qty>0 OR closed_qty>0) ORDER BY", sql)
|
||||
|
||||
def test_literals_and_nested_functions(self):
|
||||
sql = "SELECT * FROM pms_command WHERE note='ORDER BY x' AND id IN (:x,:y) ORDER BY id"
|
||||
self.assertIn("AND (note='ORDER BY x' AND id IN (:x,:y)) ORDER BY", b.scoped_sql(sql))
|
||||
|
||||
def test_insert_owns_book(self):
|
||||
self.assertEqual(b.scoped_sql('INSERT INTO pms_lot (ts_code,qty) VALUES (:c,:q)', 'micro30'),
|
||||
"INSERT INTO pms_lot (book_id, ts_code,qty) VALUES ('micro30', :c,:q)")
|
||||
|
||||
def test_all_read_only(self):
|
||||
self.assertEqual(b.scoped_sql('SELECT * FROM pms_position', 'all'), 'SELECT * FROM pms_position')
|
||||
with self.assertRaises(ValueError):
|
||||
b.scoped_sql('UPDATE pms_position SET total_qty=0', 'all')
|
||||
|
||||
def test_nonbook_untouched(self):
|
||||
sql = 'SELECT * FROM pms_qmt_order ORDER BY id'
|
||||
self.assertEqual(b.scoped_sql(sql, 'micro30'), sql)
|
||||
|
||||
def test_unknown_scope_rejected(self):
|
||||
with self.assertRaises(ValueError):
|
||||
with b.scope('anything'):
|
||||
pass
|
||||
|
||||
def test_snapshot_valid(self):
|
||||
self.assertEqual(m.snapshot_check(snapshot(), '2026-10-27'), [])
|
||||
|
||||
def test_snapshot_tamper(self):
|
||||
s = snapshot();s['signal']['low'] = True
|
||||
self.assertIn('快照摘要不一致', m.snapshot_check(s, '2026-10-27'))
|
||||
|
||||
def test_snapshot_date_and_freeze(self):
|
||||
s = snapshot();s['frozen'] = False
|
||||
self.assertGreaterEqual(len(m.snapshot_check(s, '2026-10-28')), 2)
|
||||
|
||||
def test_growth_or_low_enters(self):
|
||||
for s in [snapshot(low=True), snapshot(growth=True)]:
|
||||
r = m.transition(m.initial_state(), s, micro_qty=0, original_qty=300, budget=100000)
|
||||
self.assertEqual((r['phase'], r['cycle'], len(r['target'])), ('entering_micro', 1, 30))
|
||||
|
||||
def test_high_vetoes_entry(self):
|
||||
r = m.transition(m.initial_state(), snapshot(low=True, high=True, growth=True), micro_qty=0, original_qty=0, budget=100)
|
||||
self.assertEqual(r['phase'], 'original')
|
||||
|
||||
def test_exit_during_entry(self):
|
||||
s = m.initial_state();s['phase'] = 'entering_micro'
|
||||
r = m.transition(s, snapshot(high=True), micro_qty=100, original_qty=100, budget=100)
|
||||
self.assertEqual(r['phase'], 'returning_original')
|
||||
|
||||
def test_missing_signal_does_not_exit(self):
|
||||
s = m.initial_state();s['phase'] = 'micro'
|
||||
r = m.transition(s, None, micro_qty=100, original_qty=0, budget=100)
|
||||
self.assertEqual(r['phase'], 'micro')
|
||||
|
||||
def test_residual_prevents_new_cycle(self):
|
||||
s = m.initial_state();s['phase'] = 'returning_original'
|
||||
r = m.transition(s, snapshot(low=True), micro_qty=100, original_qty=0, budget=100)
|
||||
self.assertEqual(r['phase'], 'returning_original')
|
||||
|
||||
def test_finish_no_same_tick_reentry(self):
|
||||
s = m.initial_state();s['phase'] = 'returning_original'
|
||||
r = m.transition(s, snapshot(low=True), micro_qty=0, original_qty=0, budget=100)
|
||||
self.assertEqual(r['phase'], 'original')
|
||||
|
||||
def test_pause_and_exit_disable(self):
|
||||
s = m.initial_state();s['phase'] = 'micro'
|
||||
self.assertEqual(m.transition(s, snapshot(high=True), micro_qty=100, original_qty=0, budget=100, control='pause_all'), s)
|
||||
r = m.transition(s, snapshot(), micro_qty=100, original_qty=0, budget=100, control='exit_disable')
|
||||
self.assertTrue(r['disabled']);self.assertEqual(r['phase'], 'returning_original')
|
||||
|
||||
def test_lot_minimum_fee(self):
|
||||
self.assertEqual(m.buy_qty('600000.SH', 10, 1000, {}), 0)
|
||||
self.assertEqual(m.buy_qty('600000.SH', 10, 1006, {}), 100)
|
||||
self.assertEqual(m.buy_qty('688001.SH', 10, 1006, {}), 0)
|
||||
|
||||
def test_permissions_not_silent_subset(self):
|
||||
members = snapshot()['members'];members[0]['code'] = '688001.SH'
|
||||
self.assertTrue(any('权限' in s for s in m.preflight(members, 100000, {'MAIN'}, {})))
|
||||
|
||||
def test_unfilled_sells_do_not_fund_buys(self):
|
||||
snap = snapshot(low=True)
|
||||
state = m.transition(m.initial_state(), snap, micro_qty=0, original_qty=100, budget=100000)
|
||||
quotes = {r['code']: {'price': 10.} for r in snap['members']};quotes['000001.SZ'] = {'price': 10.}
|
||||
orders, _ = m.orders(state, snap, original=[{'ts_code':'000001.SZ','total_qty':100,'base_qty':100}], micro=[], quotes=quotes, cash=0, account_mv=1000, cap_amount=100000)
|
||||
self.assertTrue(orders);self.assertTrue(all(o['side'] == 'sell' for o in orders))
|
||||
|
||||
def test_same_ticker_and_inflight_block(self):
|
||||
snap = snapshot(low=True);state = m.transition(m.initial_state(), snap, micro_qty=0, original_qty=100, budget=100000)
|
||||
quotes = {r['code']: {'price': 10.} for r in snap['members']}
|
||||
orders, _ = m.orders(state, snap, original=[{'ts_code':'600000.SH','total_qty':100}], micro=[], quotes=quotes, cash=100000, account_mv=1000, cap_amount=100000, pending=[{'ts_code':'600001.SH'}])
|
||||
self.assertFalse(any(o['side']=='buy' and o['ts_code'] in ('600000.SH','600001.SH') for o in orders))
|
||||
|
||||
def test_no_reweight_retained(self):
|
||||
snap=snapshot();s=m.initial_state();s.update(phase='micro', target=[r['code'] for r in snap['members']])
|
||||
micro=[{'ts_code':c,'total_qty':100} for c in s['target']]
|
||||
orders,_=m.orders(s,snap,original=[],micro=micro,quotes={c:{'price':10} for c in s['target']},cash=100000,account_mv=30000,cap_amount=200000)
|
||||
self.assertEqual(orders,[])
|
||||
|
||||
def test_paper_idempotency_t1_and_cash(self):
|
||||
a={'cash':10000.,'books':{'original':{},'micro30':{}},'fills':{},'realized':{'original':0.,'micro30':0.},'fees':{'original':0.,'micro30':0.}}
|
||||
buy={'id':'1','book_id':'micro30','ts_code':'600000.SH','side':'buy','qty':100}
|
||||
self.assertTrue(m.apply_paper_fill(a,buy,10,'2026-10-26',{}))
|
||||
before=copy.deepcopy(a)
|
||||
self.assertFalse(m.apply_paper_fill(a,buy,10,'2026-10-26',{}));self.assertEqual(a,before)
|
||||
sell={**buy,'id':'2','side':'sell'}
|
||||
with self.assertRaises(ValueError):m.apply_paper_fill(a,sell,11,'2026-10-26',{})
|
||||
self.assertTrue(m.apply_paper_fill(a,sell,11,'2026-10-27',{}))
|
||||
self.assertAlmostEqual(a['cash']-10000,a['realized']['micro30'])
|
||||
self.assertEqual(a['books']['original'],{})
|
||||
|
||||
|
||||
class BoundaryTests(unittest.TestCase):
|
||||
def test_restart_does_not_reconsume_entry(self):
|
||||
snap=snapshot(low=True)
|
||||
a=m.transition(m.initial_state(),snap,micro_qty=0,original_qty=0,budget=100000)
|
||||
a.update(phase='returning_original',exit_date=snap['effective_date'])
|
||||
a=m.transition(a,snap,micro_qty=0,original_qty=0,budget=100000)
|
||||
a=json.loads(json.dumps(a))
|
||||
self.assertEqual(m.transition(a,snap,micro_qty=0,original_qty=0,budget=100000)['phase'],'original')
|
||||
def test_confirmed_exit_survives_bad_monitor(self):
|
||||
a=m.initial_state();a.update(phase='returning_original',target=['600000.SH'])
|
||||
orders,_=m.orders(a,None,original=[],micro=[{'ts_code':'600000.SH','total_qty':100}],quotes={},cash=0,account_mv=1000,cap_amount=100000)
|
||||
self.assertEqual([(r['side'],r['qty']) for r in orders],[('sell',100)])
|
||||
def test_partial_entry_does_not_reweight_after_price_fall(self):
|
||||
snap=snapshot(low=True);a=m.transition(m.initial_state(),snap,micro_qty=0,original_qty=0,budget=30000)
|
||||
a['entry_quantities']={'600000.SH':100}
|
||||
orders,_=m.orders(a,snap,original=[],micro=[{'ts_code':'600000.SH','total_qty':100}],quotes={'600000.SH':{'price':1}},cash=100000,account_mv=100,cap_amount=200000)
|
||||
self.assertFalse(any(r['ts_code']=='600000.SH' and r['side']=='buy' for r in orders))
|
||||
def test_partial_fill_only_buys_remaining_quantity(self):
|
||||
snap=snapshot(low=True);a=m.transition(m.initial_state(),snap,micro_qty=0,original_qty=0,budget=30000)
|
||||
a['entry_quantities']={'600000.SH':300}
|
||||
orders,_=m.orders(a,snap,original=[],micro=[{'ts_code':'600000.SH','total_qty':100}],quotes={'600000.SH':{'price':10}},cash=100000,account_mv=1000,cap_amount=200000)
|
||||
self.assertEqual(orders[0]['qty'],200)
|
||||
def test_target30_can_have31_actual_positions(self):
|
||||
snap=snapshot();a=m.initial_state();a.update(phase='micro',target=[r['code'] for r in snap['members']])
|
||||
held=[{'ts_code':c,'total_qty':100} for c in a['target']]+[{'ts_code':'000001.SZ','total_qty':100}]
|
||||
orders,_=m.orders(a,snap,original=[],micro=held,quotes={},cash=0,account_mv=31000,cap_amount=40000,pending=[{'ts_code':'000001.SZ'}])
|
||||
self.assertEqual(orders,[])
|
||||
def test_transfer_does_not_create_profit_or_change_nav(self):
|
||||
from app.core import rotation_account as a
|
||||
cash=a.seed(10000,{'original':10000,'micro30':0})
|
||||
a.transfer(cash,'original','micro30',5000,{'original':12000,'micro30':0},'1')
|
||||
view=a.metrics(cash,{'original':12000,'micro30':0})
|
||||
self.assertAlmostEqual(view['original']['profit'],2000)
|
||||
self.assertEqual(view['micro30']['profit'],0)
|
||||
self.assertAlmostEqual(view['original']['nav'],1.1)
|
||||
self.assertEqual(view['micro30']['nav'],1)
|
||||
self.assertEqual(view['account']['profit'],2000)
|
||||
def test_capital_duplicate_fill_and_cash_not_double_used(self):
|
||||
from app.core import rotation_account as a
|
||||
cash=a.seed(10000,{'original':0,'micro30':0})
|
||||
a.transfer(cash,'original','micro30',5000,{},'1')
|
||||
self.assertFalse(a.transfer(cash,'original','micro30',5000,{},'1'))
|
||||
a.fill(cash,'micro30','buy',4000,5,'x')
|
||||
self.assertFalse(a.fill(cash,'micro30','buy',4000,5,'x'))
|
||||
with self.assertRaises(ValueError):a.fill(cash,'micro30','buy',1000,5,'y')
|
||||
self.assertEqual(sum(cash['cash'].values()),5995)
|
||||
def test_cannot_liquidate_unknown_old_micro_as_new_cycle(self):
|
||||
with self.assertRaises(ValueError):m.transition(m.initial_state(),snapshot(low=True),micro_qty=100,original_qty=0,budget=100)
|
||||
def test_pause_new_still_sells_dropped_member(self):
|
||||
a=m.initial_state();a.update(phase='micro',target=[])
|
||||
orders,_=m.orders(a,snapshot(),original=[],micro=[{'ts_code':'000001.SZ','total_qty':100}],quotes={},cash=100000,account_mv=1000,cap_amount=100000,control='pause_new')
|
||||
self.assertEqual([r['side'] for r in orders],['sell'])
|
||||
def test_minimum_commission_for_small_budget(self):
|
||||
self.assertEqual(m.buy_qty('600000.SH',4.07,412.01,{'rate':.0002}),100)
|
||||
def test_risky_insert_rejected(self):
|
||||
with self.assertRaises(ValueError):b.scoped_sql('INSERT INTO pms_position (book_id,ts_code) VALUES (:b,:c)')
|
||||
def test_scope_restored_when_exception(self):
|
||||
with self.assertRaises(RuntimeError):
|
||||
with b.scope('micro30'):raise RuntimeError()
|
||||
self.assertEqual(b.CURRENT.get(),'original')
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
unittest.main()
|
||||
|
|
@ -748,7 +748,7 @@ def _():
|
|||
def _():
|
||||
from app.web.main import app
|
||||
paths = {r.path for r in app.routes}
|
||||
need = ["/", "/health", "/api/overview", "/api/params", "/api/commands",
|
||||
need = ["/", "/health", "/rotation", "/api/rotation", "/api/ops/rotation/control", "/api/overview", "/api/params", "/api/commands",
|
||||
"/api/commands/catalog", "/api/commands/active", "/api/commands/{command_id}",
|
||||
"/api/commands/{command_id}/cancel", "/api/plans", "/api/positions",
|
||||
"/api/positions/{ts_code}/lots", "/api/instructions", "/api/ledger", "/api/report",
|
||||
|
|
@ -777,7 +777,7 @@ def _():
|
|||
names = set(sch.celery_app.conf.beat_schedule)
|
||||
assert names == {"plan_pull", "premarket", "command_poll", "replay_fills",
|
||||
"intraday_exec", "signal_digest", "t0_close", "daily_settle",
|
||||
"daily_report",
|
||||
"daily_report", "rotation_tick",
|
||||
"macro_scan", # 2026-08-18 +宏观择时 09:35
|
||||
"strategy_attach", # 2026-08-25 +策略自动挂载 09:40
|
||||
"nav_snapshot", # 2026-08-28 +公示净值快照 15:20
|
||||
|
|
@ -786,7 +786,7 @@ def _():
|
|||
for t in ("pms.premarket", "pms.command_poll", "pms.replay_fills", "pms.daily_settle",
|
||||
"pms.daily_report", "pms.t0_close", "pms.intraday_exec", "pms.signal_digest",
|
||||
"pms.plan_pull", "pms.macro_scan", "pms.strategy_attach", "pms.nav_snapshot",
|
||||
"pms.tech_pull"):
|
||||
"pms.tech_pull", "pms.rotation_tick"):
|
||||
assert t in tasks, t
|
||||
assert sch.in_session(datetime(2026, 7, 27, 10, 0)) is True
|
||||
assert sch.in_session(datetime(2026, 7, 27, 12, 0)) is False
|
||||
|
|
|
|||
Loading…
Reference in New Issue