2026-11-11 15:50:39 +08:00
|
|
|
|
"""周期采纳Bionic合成方向,原始证据随状态和账本留存。"""
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
from config.settings import settings
|
|
|
|
|
|
from app.core import timing_fusion_contract as contract, risk_exit as rules, strategy_book as books
|
|
|
|
|
|
from app.repo import rotation_repo, pms_repo
|
|
|
|
|
|
from app.services import param_store
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def enabled():
|
|
|
|
|
|
return (settings.INFRA_STRATEGY_BOOK_SCHEMA and not settings.PMS_TABLE_PREFIX
|
|
|
|
|
|
and param_store.get_bool('PMS_TIMING_FUSION_ENABLED', False))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def request_fields():
|
|
|
|
|
|
return {'timing_fusion_scope': 'pms-sim-155'} if enabled() else {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def refresh(*, now=None, dry_run=False):
|
|
|
|
|
|
if not enabled():
|
|
|
|
|
|
return {'enabled': False}
|
|
|
|
|
|
import requests
|
|
|
|
|
|
from app.services import exec_advisor, timing_state, risk_exit
|
|
|
|
|
|
response = requests.get(exec_advisor.base_url()+'/api/intraday/timing_fusion', timeout=(2,5))
|
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
|
data = response.json()
|
2026-11-18 18:03:41 +08:00
|
|
|
|
received_at = now or datetime.now()
|
2026-11-11 15:50:39 +08:00
|
|
|
|
if data.get('version') != contract.VERSION or data.get('enabled') is not True:
|
|
|
|
|
|
raise ValueError('合成择时来源未就绪')
|
2026-11-18 18:03:41 +08:00
|
|
|
|
if not isinstance(data.get('errors', []), list):
|
|
|
|
|
|
raise ValueError('合成择时来源错误清单格式无效')
|
2026-11-11 15:50:39 +08:00
|
|
|
|
states = data.get('states')
|
|
|
|
|
|
if not isinstance(states, list) or len(states)>6000:
|
|
|
|
|
|
raise ValueError('合成择时响应超过完整读取边界')
|
2026-11-11 16:05:59 +08:00
|
|
|
|
# 广域v3f覆盖上千只票。缺少第二组的无方向记录不进入PMS账本;
|
|
|
|
|
|
# 已有方向必须接收WAIT,及时撤销过期或冲突的旧结论。
|
2026-11-18 18:03:41 +08:00
|
|
|
|
known = rotation_repo.document_ids(timing_state.KIND, limit=6001)
|
|
|
|
|
|
if len(known) >= 6001:
|
2026-11-11 16:05:59 +08:00
|
|
|
|
raise ValueError('本地方向状态超过完整读取上限')
|
2026-11-18 18:03:41 +08:00
|
|
|
|
out = {'enabled': True, 'accepted': 0, 'invalid': 0, 'source_errors': data.get('errors', []),
|
|
|
|
|
|
'directions': 0, 'waiting': 0, 'no_evidence': 0, 'actions': []}
|
|
|
|
|
|
since = param_store.get('PMS_TIMING_FUSION_START_AT', '')
|
2026-11-11 15:50:39 +08:00
|
|
|
|
for state in states:
|
2026-11-18 18:03:41 +08:00
|
|
|
|
event = contract.valid(state, received_at, since=since)
|
2026-11-11 15:50:39 +08:00
|
|
|
|
if not event:
|
|
|
|
|
|
out['invalid'] += 1
|
|
|
|
|
|
continue
|
|
|
|
|
|
code = event['ts_code']
|
2026-11-18 18:03:41 +08:00
|
|
|
|
if event['action'] == 'WAIT':
|
|
|
|
|
|
out['waiting'] += 1
|
|
|
|
|
|
if not any(group['evidence'] for group in event['groups'].values()):
|
|
|
|
|
|
out['no_evidence'] += 1
|
|
|
|
|
|
if timing_state.key(code) not in known:
|
|
|
|
|
|
continue
|
|
|
|
|
|
else:
|
|
|
|
|
|
out['directions'] += 1
|
2026-11-11 15:50:39 +08:00
|
|
|
|
if dry_run:
|
|
|
|
|
|
out['accepted'] += 1
|
|
|
|
|
|
continue
|
|
|
|
|
|
with rotation_repo.account_guard(), rotation_repo.locked(timing_state.key(code), timing_state.KIND, {}) as current:
|
|
|
|
|
|
if current and rules.stamp(current.get('verified_at') or current.get('at')) > rules.stamp(event['verified_at']):
|
|
|
|
|
|
continue
|
|
|
|
|
|
changed = current.get('key') != event['key']
|
|
|
|
|
|
current.clear()
|
|
|
|
|
|
current.update(event)
|
|
|
|
|
|
if changed:
|
|
|
|
|
|
with books.scope(books.ORIGINAL):
|
|
|
|
|
|
if pms_repo.insert_ledger(ts_code=code, action='SIGNAL', arbiter='system', verdict='NOTE',
|
|
|
|
|
|
price_at=0, hard_numbers={'timing_direction':event}, reason=event['reason'],
|
|
|
|
|
|
ref_id=event['key'][:64]) != 1:
|
|
|
|
|
|
raise RuntimeError('合成择时依据未成功入账')
|
|
|
|
|
|
out['accepted'] += 1
|
|
|
|
|
|
if event['action'] == 'BUY':
|
2026-11-18 18:03:41 +08:00
|
|
|
|
result = risk_exit.recover_on_buy(code, event, now=now or datetime.now())
|
2026-11-11 15:50:39 +08:00
|
|
|
|
elif event['action'] == 'SELL':
|
2026-11-18 18:03:41 +08:00
|
|
|
|
result = risk_exit.on_signal(event, now=now or datetime.now())
|
2026-11-11 15:50:39 +08:00
|
|
|
|
else:
|
|
|
|
|
|
continue
|
|
|
|
|
|
out['actions'].append({'ts_code':code, 'action':event['action'], 'result':result})
|
2026-11-18 18:03:41 +08:00
|
|
|
|
if out['source_errors'] or out['invalid']:
|
|
|
|
|
|
out['status'] = 'DEGRADED' if out['directions'] or out['waiting'] > out['no_evidence'] else 'FAILED'
|
|
|
|
|
|
elif out['directions']:
|
|
|
|
|
|
out['status'] = 'DIRECTION'
|
|
|
|
|
|
elif out['waiting'] > out['no_evidence']:
|
|
|
|
|
|
out['status'] = 'NO_DIRECTION'
|
|
|
|
|
|
else:
|
|
|
|
|
|
out['status'] = 'NO_EVIDENCE'
|
2026-11-11 15:50:39 +08:00
|
|
|
|
return out
|