148 lines
8.4 KiB
Python
148 lines
8.4 KiB
Python
"""高股息独立候选契约。仅读完整研究归档,不混入每日计划。"""
|
|
import datetime as dt
|
|
import re
|
|
|
|
from dividend_watch import BJ, day, digest, instant, number, required_period, risk_name
|
|
|
|
SOURCE = 'dividend'
|
|
POLICY_VERSION = '2026.10-1'
|
|
QUESTIONS = ('ordinary_dividend', 'earnings_resilience', 'payment_capacity',
|
|
'balance_sheet_constraints', 'policy_commitment', 'downside_resilience')
|
|
|
|
|
|
def qualification(row, *, now):
|
|
"""有支持只允许进入建仓评估;不生成仓位、买点或投资批准。"""
|
|
reasons = []
|
|
special = row.get('dividend_research') or {}
|
|
research = special.get('research') or {}
|
|
local = instant(now).astimezone(BJ)
|
|
if row.get('status') != 'priority' or row.get('quality') != '好':
|
|
reasons.append('公司质地尚未达到优先核验条件')
|
|
if not row.get('name') or risk_name(row['name']):
|
|
reasons.append('证券名称或上市资格不满足要求')
|
|
if (row.get('report_capture') or {}).get('status') != 'ok':
|
|
reasons.append('个股评析原文尚未完整保存')
|
|
if (number(row.get('yield_ttm_pct')) or 0) < 5:
|
|
reasons.append('滚动股息不足5%或缺失')
|
|
try:
|
|
checked = instant(row['review_at']).astimezone(BJ)
|
|
if checked > local:
|
|
reasons.append('个股评析时间异常')
|
|
if (checked.year, (checked.month-1)//3) != (local.year, (local.month-1)//3):
|
|
reasons.append('个股评析尚未完成本季度复核')
|
|
except (KeyError, TypeError, ValueError):
|
|
reasons.append('个股评析时间不明')
|
|
periods = row.get('material_periods') or {}
|
|
if any(not isinstance(periods.get(k), str) or not re.fullmatch(r'\d{4}Q[1-4]', periods[k])
|
|
or periods[k] < required_period(local.date()) for k in ('period_fi', 'period_rf')):
|
|
reasons.append('财务材料尚未覆盖应披露报告期')
|
|
for value, label in (((row.get('fundamentals') or {}).get('checked_at'), '财务与分红资料'),
|
|
(special.get('last_checked_at'), '分红专项研究')):
|
|
try:
|
|
checked = instant(value).astimezone(BJ)
|
|
if checked > local or (checked.year, checked.month) != (local.year, local.month):
|
|
reasons.append(label+'尚未完成本月复核')
|
|
except (TypeError, ValueError):
|
|
reasons.append(label+'复核时间不明')
|
|
if special.get('status') != 'ok':
|
|
reasons.append('分红专项研究未完成或正在更新')
|
|
if research.get('conclusion') != '有支持':
|
|
reasons.append('分红持续性'+str(research.get('conclusion') or '尚未核验'))
|
|
if (research.get('ts_code') != row.get('code') or research.get('schema_version') != 1
|
|
or research.get('kind') != 'dividend_sustainability'
|
|
or not re.fullmatch('[0-9a-f]{64}', str(research.get('ref', '')))):
|
|
reasons.append('缺少有效的固定研究来源')
|
|
for k in ('generated_at', 'cutoff_at'):
|
|
try:
|
|
if instant(research[k]) > instant(now):
|
|
reasons.append('专项研究时间异常')
|
|
except (KeyError, TypeError, ValueError):
|
|
reasons.append('专项研究时间不完整')
|
|
assessments = research.get('assessments') or {}
|
|
if any(not isinstance(assessments.get(k), dict) or assessments[k].get('state') != 'support'
|
|
or not assessments[k].get('evidence') for k in QUESTIONS):
|
|
reasons.append('分红持续性六项核验尚未全部获得证据支持')
|
|
if research.get('cashflow_conflicts'):
|
|
reasons.append('现金流来源存在未解决差异')
|
|
if (not isinstance(research.get('validation_warnings'), list)
|
|
or not isinstance(research.get('unknowns'), list)
|
|
or not research.get('validation_version')):
|
|
reasons.append('专项研究核验记录不完整')
|
|
if research.get('validation_warnings') or research.get('unknowns'):
|
|
reasons.append('专项研究仍有未解决的材料或引用问题')
|
|
if not research.get('risks'):
|
|
reasons.append('反方研究尚未完整记录')
|
|
if row.get('needs'):
|
|
reasons.append('个股资料仍有待核验事项')
|
|
return list(dict.fromkeys(reasons))
|
|
|
|
|
|
def contract(bundle, check, expected, *, now=None, code=None):
|
|
now = instant(now or dt.datetime.now(dt.timezone.utc))
|
|
if not bundle or not re.fullmatch('[0-9a-f]{64}', str(bundle.get('id', ''))):
|
|
raise ValueError('高股息研究尚未完整发布')
|
|
result = bundle['result']
|
|
if result.get('schema_version') != 1:
|
|
raise ValueError('高股息研究格式暂不支持')
|
|
if (day(result['data_date']) != day(expected) or instant(result['cutoff_at']) > now
|
|
or day(result['data_date']) >= now.astimezone(BJ).date()):
|
|
raise ValueError('高股息资料尚未更新到上一交易日')
|
|
if not check.get('ok') or check.get('id') != bundle['id']:
|
|
raise ValueError('高股息检查尚未完成,暂不能发起建仓')
|
|
reviews = {r['ts_code']: r for r in bundle['source'].get('reviews', [])}
|
|
decision = {'id': 'dividend_'+digest([bundle['id'], POLICY_VERSION]),
|
|
'research_snapshot': bundle['id'], 'policy_version': POLICY_VERSION,
|
|
'cutoff_at': result['cutoff_at'], 'source': SOURCE}
|
|
rows = []
|
|
for row in sorted(result['rows'], key=lambda r:r['code']):
|
|
if (number(row.get('yield_ttm_pct')) or 0) < 5 or row.get('status') == 'excluded':
|
|
continue
|
|
reasons = qualification(row, now=now)
|
|
special = row.get('dividend_research') or {}
|
|
research = special.get('research') or {}
|
|
rv = reviews.get(row['code']) or {}
|
|
summary = dict(rv.get('summary') or {})
|
|
if summary:
|
|
summary.update(age_days=max(0, (now-instant(rv['ran_at'])).days),
|
|
report_url=rv.get('report_url'), ran_at=rv.get('ran_at'))
|
|
reference = row.get('review_ref') or {'capture_sha256': (row.get('report_capture') or {}).get('content_hash')}
|
|
fixed = {'ref': research.get('ref'), 'conclusion': research.get('conclusion'),
|
|
'cutoff_at': research.get('cutoff_at'), 'generated_at': research.get('generated_at'),
|
|
'assessments': research.get('assessments'), 'risks': research.get('risks'),
|
|
'unknowns': research.get('unknowns'), 'url': special.get('url')}
|
|
plan_row = {'ts_code':row['code'], 'name':row.get('name'), 'candidate_source':SOURCE,
|
|
'src':SOURCE, 'bucket':'dividend', 'decision':decision,
|
|
'verdict':'候选' if not reasons else '仅展示',
|
|
'company_review':summary, 'company_review_text':row.get('thesis'),
|
|
'review_ref':reference, 'dividend_research':fixed,
|
|
'reasons':['高股息研究核验通过'] if not reasons else reasons,
|
|
'risk':[r.get('reason') for r in research.get('risks', []) if isinstance(r, dict)],
|
|
'basis':row.get('thesis'), 'yield_ttm_pct':row.get('yield_ttm_pct'),
|
|
'research':{'thesis':row.get('thesis'), 'antithesis':row.get('antithesis'),
|
|
'needs':row.get('needs') or []}}
|
|
rows.append({'ts_code':row['code'], 'name':row.get('name'),
|
|
'yield_ttm_pct':row.get('yield_ttm_pct'), 'quality':row.get('quality'),
|
|
'conclusion':research.get('conclusion') or '待核验', 'eligible':not reasons,
|
|
'reasons':reasons, 'needs':row.get('needs') or [],
|
|
'thesis':row.get('thesis'), 'antithesis':row.get('antithesis'),
|
|
'research_ref':research.get('ref'), 'candidate':plan_row})
|
|
for rank, row in enumerate(rows, 1):
|
|
row['candidate'].update(rank=rank, selection_rank=rank)
|
|
if code:
|
|
rows = [r for r in rows if r['ts_code'] == code]
|
|
return {'schema_version':1, 'source':SOURCE, 'policy_version':POLICY_VERSION,
|
|
'data_date':result['data_date'], 'checked_at':check.get('at'),
|
|
'decision':decision, 'eligible_count':sum(r['eligible'] for r in rows),
|
|
'rows':rows, 'investment_approved':False}
|
|
|
|
|
|
def read(*, code=None):
|
|
import json
|
|
from pathlib import Path
|
|
import dividend_watch as watch
|
|
from build_readiness import expected_data_day
|
|
bundle = watch.read_latest(watch.DEFAULT_ROOT)
|
|
path = Path(watch.DEFAULT_ROOT)/'last_check.json'
|
|
check = json.loads(path.read_text()) if path.exists() else {}
|
|
return contract(bundle, check, expected_data_day(), code=code)
|