88 lines
3.3 KiB
Python
88 lines
3.3 KiB
Python
"""最新有效盘中方向。按原始时间覆盖,不用当天第一条 BUY 代表全天方向。"""
|
|
from datetime import datetime, timedelta
|
|
|
|
from config.settings import settings
|
|
from app.core import risk_exit as rules, strategy_book as books
|
|
from app.repo import rotation_repo, pms_repo
|
|
from app.services import param_store
|
|
|
|
KIND = 'timing_direction'
|
|
|
|
|
|
def enabled():
|
|
return settings.INFRA_STRATEGY_BOOK_SCHEMA and param_store.get_bool('PMS_SIGNAL_RECOVERY_ENABLED', False)
|
|
|
|
|
|
def key(code):
|
|
return 'timing-direction:' + code
|
|
|
|
|
|
def valid(value, now=None):
|
|
if not value:
|
|
return None
|
|
now = rules.stamp(now or datetime.now())
|
|
at, expiry = rules.stamp(value.get('at')), rules.stamp(value.get('expires_at'))
|
|
if (not at or not expiry or at.date() != now.date() or not at <= now < expiry
|
|
or value.get('action') not in ('BUY', 'SELL')):
|
|
return None
|
|
return value
|
|
|
|
|
|
def latest(code, now=None):
|
|
if not enabled():
|
|
return None
|
|
return valid(rotation_repo.get(key(code))[0], now)
|
|
|
|
|
|
def record(sig, now=None):
|
|
if not enabled():
|
|
return None
|
|
now = now or datetime.now()
|
|
max_age = param_store.get_int('PMS_RISK_SIGNAL_MAX_AGE_SEC', 300)
|
|
event, _ = rules.signal_event(sig, now=now,
|
|
since=param_store.get('PMS_SIGNAL_RECOVERY_START_AT', ''),
|
|
threshold=param_store.get_float('PMS_SIGNAL_SELL_CONF_MIN', .75),
|
|
max_age=max_age, actions=('BUY', 'SELL'))
|
|
if not event:
|
|
return None
|
|
expiry = rules.stamp(event['at']) + timedelta(seconds=max_age)
|
|
explicit = rules.stamp(sig.get('expires_at'))
|
|
if explicit:
|
|
expiry = min(expiry, explicit)
|
|
event.update(action=sig['action'], expires_at=expiry.isoformat())
|
|
event['key'] = rules.digest([event['key'], event['action']])
|
|
code = sig['ts_code']
|
|
with rotation_repo.account_guard(), rotation_repo.locked(key(code), KIND, {}) as current:
|
|
if current:
|
|
before, after = rules.stamp(current['at']), rules.stamp(event['at'])
|
|
if after < before or (after == before and current['action'] == 'SELL'):
|
|
return None
|
|
if current.get('key') == event['key']:
|
|
return dict(current)
|
|
current.clear()
|
|
current.update(event)
|
|
with books.scope(books.ORIGINAL):
|
|
if pms_repo.insert_ledger(ts_code=code, action='SIGNAL', arbiter='system', verdict='NOTE',
|
|
price_at=float(sig.get('suggested_price') or 0),
|
|
hard_numbers={'timing_direction': event},
|
|
reason='决策系统最新盘中方向:' + ('看多' if event['action'] == 'BUY' else '看空'),
|
|
ref_id=event['key'][:64]) != 1:
|
|
raise RuntimeError('盘中方向依据未成功入账')
|
|
return event
|
|
|
|
|
|
def buy_map(now=None):
|
|
if not enabled():
|
|
return {}
|
|
today = rules.stamp(now or datetime.now()).replace(hour=0, minute=0, second=0, microsecond=0)
|
|
rows = rotation_repo.updated_since(KIND, today.replace(tzinfo=None), limit=6001)
|
|
if len(rows) >= 6001:
|
|
raise ValueError('盘中方向数量超过完整读取上限')
|
|
out = {}
|
|
for row in rows:
|
|
event = valid(row['value'], now)
|
|
if event and event['action'] == 'BUY':
|
|
code = row['id'][len('timing-direction:'):]
|
|
out[code] = {'at': event['at'], 'reason': event['reason'], 'event': event}
|
|
return out
|