85 lines
4.2 KiB
Python
85 lines
4.2 KiB
Python
|
|
"""本地:仅离线测试。桥机容器:python run.py plan-refresh [--date 数据日]。
|
|||
|
|
|
|||
|
|
统一调度每五分钟触发独立 plan-refresh 步骤;北京时间 07:20—15:10 的
|
|||
|
|
交易日才工作。只替换评析/区制,重放其余输入,绝不调用模型或写日频状态表。
|
|||
|
|
"""
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
import copy
|
|||
|
|
import datetime as dt
|
|||
|
|
from zoneinfo import ZoneInfo
|
|||
|
|
|
|||
|
|
import config
|
|||
|
|
import plan_inputs as pi
|
|||
|
|
import plan_versions as pv
|
|||
|
|
|
|||
|
|
BJ = ZoneInfo('Asia/Shanghai')
|
|||
|
|
|
|||
|
|
|
|||
|
|
def in_window(now):
|
|||
|
|
from chinese_calendar import is_holiday
|
|||
|
|
local = now.astimezone(BJ)
|
|||
|
|
return (local.weekday() < 5 and not is_holiday(local.date())
|
|||
|
|
and dt.time(7, 20) <= local.time().replace(tzinfo=None) <= dt.time(15, 10))
|
|||
|
|
|
|||
|
|
|
|||
|
|
def refresh(day=None, *, now=None, regime_value=None):
|
|||
|
|
import plan
|
|||
|
|
import sources
|
|||
|
|
now = now or dt.datetime.now(dt.timezone.utc)
|
|||
|
|
if not in_window(now):
|
|||
|
|
return {'status': 'outside_window'}
|
|||
|
|
root = config.PLAN_SNAPSHOT_DIR
|
|||
|
|
with pv.writer(root):
|
|||
|
|
try:
|
|||
|
|
old = pv.load(root, day)
|
|||
|
|
morning = pv.load(root, old['date'], morning=True)
|
|||
|
|
except (FileNotFoundError, ValueError):
|
|||
|
|
return {'status': 'no_morning'}
|
|||
|
|
# 行情日常是昨天;晨版截止时刻才界定属于哪一个决策日。
|
|||
|
|
if dt.datetime.fromisoformat(morning['decision']['cutoff_at']).astimezone(BJ).date() != now.astimezone(BJ).date():
|
|||
|
|
return {'status': 'no_morning_today'}
|
|||
|
|
day = old['date']
|
|||
|
|
pending = set(pv.health(root, day).get('pending_codes') or [])
|
|||
|
|
try:
|
|||
|
|
env = pv.envelope(root, old['decision']['id'])
|
|||
|
|
inputs = copy.deepcopy(env['inputs'])
|
|||
|
|
for rec in inputs['records'].values():
|
|||
|
|
if rec['name'] != 'sources.company_reviews':
|
|||
|
|
continue
|
|||
|
|
args, kw = pi.unpack(rec['args'])
|
|||
|
|
before = pi.unpack(rec['value']) if 'value' in rec else {}
|
|||
|
|
# 读取失败时无法确定哪些股票变化,保守暂停整个本次观察范围。
|
|||
|
|
pending.update(args[0])
|
|||
|
|
pv.health(root, day, pending_codes=sorted(pending), error=None)
|
|||
|
|
after = sources.company_reviews(*args, **dict(kw, cutoff_at=now.isoformat(), strict=True))
|
|||
|
|
pending.difference_update(args[0])
|
|||
|
|
pending.update(c for c in set(before) | set(after) if before.get(c) != after.get(c))
|
|||
|
|
rec.pop('error', None)
|
|||
|
|
rec['value'] = pi.pack(after)
|
|||
|
|
if regime_value is not None:
|
|||
|
|
# 抓取时刻不构成区制变化;保留最后一次语义变化的抓取时刻。
|
|||
|
|
def meaningful(x):
|
|||
|
|
return {k: v for k, v in (x or {}).items() if k != 'fetched_at'}
|
|||
|
|
if meaningful(inputs.get('regime')) != meaningful(regime_value):
|
|||
|
|
inputs['regime'] = regime_value
|
|||
|
|
pv.health(root, day, pending_codes=sorted(pending), error=None)
|
|||
|
|
if pv.digest(inputs) == old['decision']['input_hash']:
|
|||
|
|
pv.health(root, day, pending_codes=[], error=None, decision_id=old['decision']['id'])
|
|||
|
|
return {'status': 'unchanged', 'decision': old['decision']}
|
|||
|
|
bundle = pi.Bundle(records=inputs['records'], params=env['params'], cutoff_at=now.isoformat())
|
|||
|
|
shown = old.get('shown_params') or {'top': 20, 'obs_top': 10, 'theme_cap': 5}
|
|||
|
|
with bundle.use():
|
|||
|
|
data = plan.collect(day, **shown)
|
|||
|
|
full = data.pop('_full')
|
|||
|
|
data.update(main_shown=data['main'], observe_shown=data['observe'], shown_params=shown,
|
|||
|
|
main=full['main'], observe=full['observe'], market=inputs.get('market'),
|
|||
|
|
regime=inputs.get('regime'))
|
|||
|
|
published = pv.publish(root, data, inputs, env['params'], cutoff_at=now.isoformat(),
|
|||
|
|
reason='评析更新' if pending else '市场区制更新')
|
|||
|
|
plan.write_published_md(published)
|
|||
|
|
return {'status': 'published', 'decision': published['decision']}
|
|||
|
|
except Exception as e:
|
|||
|
|
pv.health(root, day, pending_codes=sorted(pending), error=f'{type(e).__name__}: {e}')
|
|||
|
|
raise
|