alphasift 0.3.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- alphasift/__init__.py +52 -0
- alphasift/__main__.py +7 -0
- alphasift/audit.py +191 -0
- alphasift/candidate_context.py +738 -0
- alphasift/cli.py +2797 -0
- alphasift/config.py +705 -0
- alphasift/context.py +672 -0
- alphasift/daily.py +1256 -0
- alphasift/daily_brief.py +593 -0
- alphasift/daily_report.py +1115 -0
- alphasift/daily_report_html.py +1257 -0
- alphasift/doctor.py +1123 -0
- alphasift/dsa.py +302 -0
- alphasift/dsa_adapter.py +178 -0
- alphasift/dsa_provider.py +192 -0
- alphasift/evaluate.py +2418 -0
- alphasift/filter.py +517 -0
- alphasift/hotspot.py +1713 -0
- alphasift/industry.py +959 -0
- alphasift/llm_client.py +349 -0
- alphasift/market_digest.py +284 -0
- alphasift/market_news.py +446 -0
- alphasift/market_regime.py +578 -0
- alphasift/markets.py +160 -0
- alphasift/mcp.py +720 -0
- alphasift/models.py +315 -0
- alphasift/normalize.py +113 -0
- alphasift/notify.py +2608 -0
- alphasift/overview.py +225 -0
- alphasift/pack.py +1470 -0
- alphasift/pack_features.py +431 -0
- alphasift/pattern_replay.py +1449 -0
- alphasift/patterns/bottom_volume_yang.yaml +32 -0
- alphasift/patterns/box_breakout.yaml +36 -0
- alphasift/patterns/double_bottom_breakout.yaml +38 -0
- alphasift/patterns/ma20_shrink_pullback.yaml +37 -0
- alphasift/patterns/shooting_star_confirm.yaml +49 -0
- alphasift/patterns/single_yang_hold.yaml +44 -0
- alphasift/patterns/steady_trend_breakout.yaml +39 -0
- alphasift/patterns/three_white_soldiers.yaml +72 -0
- alphasift/patterns/three_white_soldiers_pullback.yaml +91 -0
- alphasift/patterns.py +1233 -0
- alphasift/performance_history.py +297 -0
- alphasift/pipeline.py +1010 -0
- alphasift/post_analysis.py +337 -0
- alphasift/ranker.py +990 -0
- alphasift/report.py +379 -0
- alphasift/result_schema.py +179 -0
- alphasift/risk.py +452 -0
- alphasift/run_history.py +164 -0
- alphasift/scorer.py +597 -0
- alphasift/server.py +680 -0
- alphasift/snapshot.py +1025 -0
- alphasift/snapshot_us.py +280 -0
- alphasift/snapshot_yahoo.py +358 -0
- alphasift/source_guard.py +70 -0
- alphasift/source_history.py +306 -0
- alphasift/store.py +294 -0
- alphasift/strategies/balanced_alpha.yaml +76 -0
- alphasift/strategies/blue_chip_income.yaml +87 -0
- alphasift/strategies/capital_heat.yaml +74 -0
- alphasift/strategies/dual_low.yaml +80 -0
- alphasift/strategies/global_value.yaml +65 -0
- alphasift/strategies/low_volatility_quality.yaml +95 -0
- alphasift/strategies/momentum_quality.yaml +81 -0
- alphasift/strategies/oversold_reversal.yaml +75 -0
- alphasift/strategies/quality_value.yaml +80 -0
- alphasift/strategies/shrink_pullback.yaml +77 -0
- alphasift/strategies/us_momentum_quality.yaml +74 -0
- alphasift/strategies/volume_breakout.yaml +78 -0
- alphasift/strategy.py +1090 -0
- alphasift/strategy_cards.py +424 -0
- alphasift/strategy_templates.py +291 -0
- alphasift/timeutil.py +38 -0
- alphasift/valuation_kr.py +149 -0
- alphasift/workbench_ui.py +1051 -0
- alphasift-0.3.1.data/data/.env.example +151 -0
- alphasift-0.3.1.data/data/LICENSE +184 -0
- alphasift-0.3.1.data/data/SKILL.md +190 -0
- alphasift-0.3.1.data/data/agents/openai.yaml +7 -0
- alphasift-0.3.1.dist-info/METADATA +312 -0
- alphasift-0.3.1.dist-info/RECORD +86 -0
- alphasift-0.3.1.dist-info/WHEEL +5 -0
- alphasift-0.3.1.dist-info/entry_points.txt +2 -0
- alphasift-0.3.1.dist-info/licenses/LICENSE +184 -0
- alphasift-0.3.1.dist-info/top_level.txt +1 -0
alphasift/report.py
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""Run report builders for human and UI surfaces."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import json
|
|
7
|
+
from datetime import datetime, timezone
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from alphasift.models import EvaluationResult, Pick, PickEvaluation, ScreenResult
|
|
12
|
+
|
|
13
|
+
RUN_REPORT_SCHEMA_VERSION = 1
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def build_run_report_payload(
|
|
17
|
+
result: ScreenResult,
|
|
18
|
+
*,
|
|
19
|
+
evaluation: EvaluationResult | None = None,
|
|
20
|
+
max_picks: int = 10,
|
|
21
|
+
) -> dict[str, Any]:
|
|
22
|
+
"""Build a stable, UI-friendly report payload for a saved screen run."""
|
|
23
|
+
max_picks = max(1, int(max_picks))
|
|
24
|
+
payload: dict[str, Any] = {
|
|
25
|
+
"schema_version": RUN_REPORT_SCHEMA_VERSION,
|
|
26
|
+
"object": "RunReport",
|
|
27
|
+
"generated_at": datetime.now(timezone.utc).isoformat(),
|
|
28
|
+
"run": _run_summary(result),
|
|
29
|
+
"summary_cards": _summary_cards(result, evaluation),
|
|
30
|
+
"source_health": _source_health_summary(result),
|
|
31
|
+
"top_picks": [_pick_card(pick) for pick in _sorted_picks(result.picks)[:max_picks]],
|
|
32
|
+
}
|
|
33
|
+
if evaluation is not None:
|
|
34
|
+
payload["evaluation"] = _evaluation_summary(evaluation, max_picks=max_picks)
|
|
35
|
+
return payload
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def render_run_report_markdown(payload: dict[str, Any]) -> str:
|
|
39
|
+
"""Render a run report payload as Markdown."""
|
|
40
|
+
run = payload.get("run", {}) or {}
|
|
41
|
+
lines = [
|
|
42
|
+
"# AlphaSift Run Report",
|
|
43
|
+
"",
|
|
44
|
+
f"- run_id: {_fmt(run.get('run_id'))}",
|
|
45
|
+
f"- strategy: {_fmt(run.get('strategy'))} v{_fmt(run.get('strategy_version'))}",
|
|
46
|
+
f"- market: {_fmt(run.get('market'))}",
|
|
47
|
+
f"- created_at: {_fmt(run.get('created_at'))}",
|
|
48
|
+
f"- generated_at: {_fmt(payload.get('generated_at'))}",
|
|
49
|
+
"",
|
|
50
|
+
"## Summary",
|
|
51
|
+
"",
|
|
52
|
+
"| Metric | Value | Status |",
|
|
53
|
+
"|---|---:|---|",
|
|
54
|
+
]
|
|
55
|
+
for card in payload.get("summary_cards", []) or []:
|
|
56
|
+
lines.append(
|
|
57
|
+
f"| {_md(card.get('label'))} | {_md(card.get('value'))} | {_md(card.get('status'))} |"
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
source = payload.get("source_health", {}) or {}
|
|
61
|
+
lines.extend([
|
|
62
|
+
"",
|
|
63
|
+
"## Source Health",
|
|
64
|
+
"",
|
|
65
|
+
f"- snapshot_source: {_fmt(source.get('snapshot_source'))}",
|
|
66
|
+
f"- daily_enriched: {_fmt(source.get('daily_enriched'))}",
|
|
67
|
+
f"- daily_enrich_count: {_fmt(source.get('daily_enrich_count'))}",
|
|
68
|
+
])
|
|
69
|
+
if source.get("source_errors"):
|
|
70
|
+
lines.append("- source_errors: " + "; ".join(str(item) for item in source["source_errors"]))
|
|
71
|
+
if source.get("degradation"):
|
|
72
|
+
lines.append("- degradation: " + "; ".join(str(item) for item in source["degradation"]))
|
|
73
|
+
|
|
74
|
+
lines.extend([
|
|
75
|
+
"",
|
|
76
|
+
"## Top Picks",
|
|
77
|
+
"",
|
|
78
|
+
"| # | Code | Name | Final | Screen | Risk | Industry | Notes |",
|
|
79
|
+
"|---:|---|---|---:|---:|---|---|---|",
|
|
80
|
+
])
|
|
81
|
+
for pick in payload.get("top_picks", []) or []:
|
|
82
|
+
notes = _pick_notes(pick)
|
|
83
|
+
lines.append(
|
|
84
|
+
"| "
|
|
85
|
+
f"{_md(pick.get('rank'))} | {_md(pick.get('code'))} | {_md(pick.get('name'))} | "
|
|
86
|
+
f"{_md(pick.get('final_score'))} | {_md(pick.get('screen_score'))} | "
|
|
87
|
+
f"{_md(pick.get('risk_level'))} | {_md(pick.get('industry'))} | {_md(notes)} |"
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
evaluation = payload.get("evaluation")
|
|
91
|
+
if isinstance(evaluation, dict):
|
|
92
|
+
lines.extend(_render_evaluation_markdown(evaluation))
|
|
93
|
+
return "\n".join(lines).rstrip() + "\n"
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def write_run_report(
|
|
97
|
+
path: str | Path,
|
|
98
|
+
payload: dict[str, Any],
|
|
99
|
+
*,
|
|
100
|
+
json_output: bool = False,
|
|
101
|
+
) -> Path:
|
|
102
|
+
"""Write a run report payload as Markdown or JSON."""
|
|
103
|
+
output = Path(path)
|
|
104
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
|
105
|
+
if json_output:
|
|
106
|
+
output.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
107
|
+
else:
|
|
108
|
+
output.write_text(render_run_report_markdown(payload), encoding="utf-8")
|
|
109
|
+
return output
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _run_summary(result: ScreenResult) -> dict[str, Any]:
|
|
113
|
+
return {
|
|
114
|
+
"run_id": result.run_id,
|
|
115
|
+
"strategy": result.strategy,
|
|
116
|
+
"market": result.market,
|
|
117
|
+
"strategy_version": result.strategy_version,
|
|
118
|
+
"strategy_category": result.strategy_category,
|
|
119
|
+
"created_at": result.created_at,
|
|
120
|
+
"snapshot_count": result.snapshot_count,
|
|
121
|
+
"after_filter_count": result.after_filter_count,
|
|
122
|
+
"pick_count": len(result.picks),
|
|
123
|
+
"llm_ranked": result.llm_ranked,
|
|
124
|
+
"llm_coverage": result.llm_coverage,
|
|
125
|
+
"post_analyzers": list(result.post_analyzers),
|
|
126
|
+
"saved_path": result.saved_path,
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _summary_cards(
|
|
131
|
+
result: ScreenResult,
|
|
132
|
+
evaluation: EvaluationResult | None,
|
|
133
|
+
) -> list[dict[str, Any]]:
|
|
134
|
+
cards = [
|
|
135
|
+
_card("Snapshot Rows", result.snapshot_count, _count_status(result.snapshot_count)),
|
|
136
|
+
_card("After Filter", result.after_filter_count, _count_status(result.after_filter_count)),
|
|
137
|
+
_card("Picks", len(result.picks), _count_status(len(result.picks))),
|
|
138
|
+
_card("LLM Ranked", result.llm_ranked, "ok" if result.llm_ranked else "skipped"),
|
|
139
|
+
_card("Daily Enriched", result.daily_enriched, "ok" if result.daily_enriched else "skipped"),
|
|
140
|
+
_card(
|
|
141
|
+
"Degradation",
|
|
142
|
+
len(result.degradation),
|
|
143
|
+
"degraded" if result.degradation else "ok",
|
|
144
|
+
),
|
|
145
|
+
]
|
|
146
|
+
if evaluation is not None:
|
|
147
|
+
cards.extend([
|
|
148
|
+
_card("Average Return %", evaluation.average_return_pct, _return_status(evaluation.average_return_pct)),
|
|
149
|
+
_card("Win Rate %", evaluation.win_rate, _win_rate_status(evaluation.win_rate)),
|
|
150
|
+
_card("Missing Quotes", len(evaluation.missing_codes), "degraded" if evaluation.missing_codes else "ok"),
|
|
151
|
+
])
|
|
152
|
+
return cards
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _source_health_summary(result: ScreenResult) -> dict[str, Any]:
|
|
156
|
+
return {
|
|
157
|
+
"snapshot_source": result.snapshot_source,
|
|
158
|
+
"source_errors": list(result.source_errors),
|
|
159
|
+
"degradation": list(result.degradation),
|
|
160
|
+
"daily_enriched": result.daily_enriched,
|
|
161
|
+
"daily_enrich_count": result.daily_enrich_count,
|
|
162
|
+
"post_analyzers": list(result.post_analyzers),
|
|
163
|
+
"portfolio_concentration_notes": list(result.portfolio_concentration_notes),
|
|
164
|
+
"portfolio_topic_exposure": dict(result.portfolio_topic_exposure),
|
|
165
|
+
"overconcentrated_topics": list(result.overconcentrated_topics),
|
|
166
|
+
"replacement_candidates": list(result.replacement_candidates),
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _sorted_picks(picks: list[Pick]) -> list[Pick]:
|
|
171
|
+
return sorted(picks, key=lambda pick: (pick.rank or 999999, -float(pick.final_score or 0)))
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _pick_card(pick: Pick) -> dict[str, Any]:
|
|
175
|
+
return {
|
|
176
|
+
"rank": pick.rank,
|
|
177
|
+
"code": pick.code,
|
|
178
|
+
"name": pick.name,
|
|
179
|
+
"final_score": _round_value(pick.final_score),
|
|
180
|
+
"screen_score": _round_value(pick.screen_score),
|
|
181
|
+
"price": _round_value(pick.price),
|
|
182
|
+
"change_pct": _round_value(pick.change_pct),
|
|
183
|
+
"industry": pick.industry,
|
|
184
|
+
"concepts": pick.concepts,
|
|
185
|
+
"topic_alignment_status": pick.topic_alignment_status,
|
|
186
|
+
"topic_alignment_role": pick.topic_alignment_role,
|
|
187
|
+
"topic_alignment_reason": pick.topic_alignment_reason,
|
|
188
|
+
"matched_topics": list(pick.matched_topics),
|
|
189
|
+
"unmatched_topics": list(pick.unmatched_topics),
|
|
190
|
+
"board_heat_score": _round_value(pick.board_heat_score),
|
|
191
|
+
"board_heat_state": pick.board_heat_state,
|
|
192
|
+
"board_heat_summary": pick.board_heat_summary,
|
|
193
|
+
"daily_quality_score": _round_value(pick.daily_quality_score),
|
|
194
|
+
"daily_quality_flags": pick.daily_quality_flags,
|
|
195
|
+
"daily_source": pick.daily_source,
|
|
196
|
+
"signal_score": _round_value(pick.signal_score),
|
|
197
|
+
"change_60d": _round_value(pick.change_60d),
|
|
198
|
+
"volatility_20d_pct": _round_value(pick.volatility_20d_pct),
|
|
199
|
+
"max_drawdown_20d_pct": _round_value(pick.max_drawdown_20d_pct),
|
|
200
|
+
"atr_20_pct": _round_value(pick.atr_20_pct),
|
|
201
|
+
"risk_score": _round_value(pick.risk_score),
|
|
202
|
+
"risk_level": pick.risk_level,
|
|
203
|
+
"risk_flags": list(pick.risk_flags),
|
|
204
|
+
"portfolio_flags": list(pick.portfolio_flags),
|
|
205
|
+
"ranking_reason": pick.ranking_reason,
|
|
206
|
+
"llm_thesis": pick.llm_thesis,
|
|
207
|
+
"llm_watch_items": list(pick.llm_watch_items),
|
|
208
|
+
"llm_invalidators": list(pick.llm_invalidators),
|
|
209
|
+
"post_analysis_status": dict(pick.post_analysis_status),
|
|
210
|
+
"post_analysis_summaries": dict(pick.post_analysis_summaries),
|
|
211
|
+
"post_analysis_tags": list(pick.post_analysis_tags),
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def _evaluation_summary(
|
|
216
|
+
evaluation: EvaluationResult,
|
|
217
|
+
*,
|
|
218
|
+
max_picks: int,
|
|
219
|
+
) -> dict[str, Any]:
|
|
220
|
+
return {
|
|
221
|
+
"run_id": evaluation.run_id,
|
|
222
|
+
"strategy": evaluation.strategy,
|
|
223
|
+
"evaluated_at": evaluation.evaluated_at,
|
|
224
|
+
"elapsed_days": evaluation.elapsed_days,
|
|
225
|
+
"snapshot_source": evaluation.snapshot_source,
|
|
226
|
+
"average_return_pct": evaluation.average_return_pct,
|
|
227
|
+
"median_return_pct": evaluation.median_return_pct,
|
|
228
|
+
"win_rate": evaluation.win_rate,
|
|
229
|
+
"entry_mode": evaluation.entry_mode,
|
|
230
|
+
"horizon_days": evaluation.horizon_days,
|
|
231
|
+
"pending_count": evaluation.pending_count,
|
|
232
|
+
"untradable_count": evaluation.untradable_count,
|
|
233
|
+
"untradable_share_pct": evaluation.untradable_share_pct,
|
|
234
|
+
"missing_count": len(evaluation.missing_codes),
|
|
235
|
+
"missing_codes": list(evaluation.missing_codes),
|
|
236
|
+
"degradation": list(evaluation.degradation),
|
|
237
|
+
"picks": [
|
|
238
|
+
_evaluation_pick_card(pick)
|
|
239
|
+
for pick in sorted(
|
|
240
|
+
evaluation.picks,
|
|
241
|
+
key=lambda item: (
|
|
242
|
+
item.return_pct is None,
|
|
243
|
+
-(float(item.return_pct) if item.return_pct is not None else -999999.0),
|
|
244
|
+
item.rank,
|
|
245
|
+
),
|
|
246
|
+
)[:max_picks]
|
|
247
|
+
],
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def _evaluation_pick_card(pick: PickEvaluation) -> dict[str, Any]:
|
|
252
|
+
return {
|
|
253
|
+
"rank": pick.rank,
|
|
254
|
+
"code": pick.code,
|
|
255
|
+
"name": pick.name,
|
|
256
|
+
"entry_price": _round_value(pick.entry_price),
|
|
257
|
+
"entry_open": _round_value(pick.entry_open),
|
|
258
|
+
"current_price": _round_value(pick.current_price),
|
|
259
|
+
"return_pct": _round_value(pick.return_pct),
|
|
260
|
+
"status": pick.status,
|
|
261
|
+
"llm_tags": list(pick.llm_tags),
|
|
262
|
+
"llm_catalysts": list(pick.llm_catalysts),
|
|
263
|
+
"llm_risks": list(pick.llm_risks),
|
|
264
|
+
"post_analysis_tags": list(pick.post_analysis_tags),
|
|
265
|
+
"risk_level": pick.risk_level,
|
|
266
|
+
"risk_flags": list(pick.risk_flags),
|
|
267
|
+
"shape_status": pick.shape_status,
|
|
268
|
+
"shape_tags": list(pick.shape_tags),
|
|
269
|
+
"path_status": pick.path_status,
|
|
270
|
+
"entry_date": pick.entry_date,
|
|
271
|
+
"exit_date": pick.exit_date,
|
|
272
|
+
"tradability_flags": list(pick.tradability_flags),
|
|
273
|
+
"max_drawdown_pct": _round_value(pick.max_drawdown_pct),
|
|
274
|
+
"max_runup_pct": _round_value(pick.max_runup_pct),
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def _render_evaluation_markdown(evaluation: dict[str, Any]) -> list[str]:
|
|
279
|
+
lines = [
|
|
280
|
+
"",
|
|
281
|
+
"## Evaluation",
|
|
282
|
+
"",
|
|
283
|
+
f"- evaluated_at: {_fmt(evaluation.get('evaluated_at'))}",
|
|
284
|
+
f"- elapsed_days: {_fmt(evaluation.get('elapsed_days'))}",
|
|
285
|
+
f"- average_return_pct: {_fmt(evaluation.get('average_return_pct'))}",
|
|
286
|
+
f"- median_return_pct: {_fmt(evaluation.get('median_return_pct'))}",
|
|
287
|
+
f"- win_rate: {_fmt(evaluation.get('win_rate'))}",
|
|
288
|
+
f"- missing_count: {_fmt(evaluation.get('missing_count'))}",
|
|
289
|
+
]
|
|
290
|
+
next_open = evaluation.get("entry_mode") == "next_open"
|
|
291
|
+
if evaluation.get("horizon_days") is not None:
|
|
292
|
+
lines.extend([
|
|
293
|
+
f"- window: T+{evaluation['horizon_days']}",
|
|
294
|
+
f"- entry_mode: {_fmt(evaluation.get('entry_mode'))}",
|
|
295
|
+
f"- pending_count: {_fmt(evaluation.get('pending_count'))}",
|
|
296
|
+
])
|
|
297
|
+
if next_open:
|
|
298
|
+
lines.extend([
|
|
299
|
+
f"- untradable_count: {_fmt(evaluation.get('untradable_count'))}",
|
|
300
|
+
f"- untradable_share_pct: {_fmt(evaluation.get('untradable_share_pct'))}",
|
|
301
|
+
])
|
|
302
|
+
lines.extend(f"- {_md(note)}" for note in evaluation.get("degradation", []))
|
|
303
|
+
lines.extend([
|
|
304
|
+
"",
|
|
305
|
+
"| # | Code | Name | Return % | Status | Shape | Max DD | Max Runup |"
|
|
306
|
+
+ (" Path status | Tradability flags |" if next_open else ""),
|
|
307
|
+
"|---:|---|---|---:|---|---|---:|---:|" + ("---|---|" if next_open else ""),
|
|
308
|
+
])
|
|
309
|
+
for pick in evaluation.get("picks", []) or []:
|
|
310
|
+
lines.append(
|
|
311
|
+
"| "
|
|
312
|
+
f"{_md(pick.get('rank'))} | {_md(pick.get('code'))} | {_md(pick.get('name'))} | "
|
|
313
|
+
f"{_md(pick.get('return_pct'))} | {_md(pick.get('status'))} | "
|
|
314
|
+
f"{_md(pick.get('shape_status'))} | {_md(pick.get('max_drawdown_pct'))} | "
|
|
315
|
+
f"{_md(pick.get('max_runup_pct'))} |"
|
|
316
|
+
+ (
|
|
317
|
+
f" {_md(pick.get('path_status'))} | {_md(', '.join(pick.get('tradability_flags', [])))} |"
|
|
318
|
+
if next_open else ""
|
|
319
|
+
)
|
|
320
|
+
)
|
|
321
|
+
return lines
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def _pick_notes(pick: dict[str, Any]) -> str:
|
|
325
|
+
notes = []
|
|
326
|
+
if pick.get("daily_quality_flags"):
|
|
327
|
+
notes.append(f"daily={pick['daily_quality_flags']}")
|
|
328
|
+
if pick.get("risk_flags"):
|
|
329
|
+
notes.append("risk=" + ",".join(str(item) for item in pick["risk_flags"]))
|
|
330
|
+
if pick.get("post_analysis_tags"):
|
|
331
|
+
notes.append("post=" + ",".join(str(item) for item in pick["post_analysis_tags"]))
|
|
332
|
+
if pick.get("ranking_reason"):
|
|
333
|
+
notes.append(str(pick["ranking_reason"]))
|
|
334
|
+
return "; ".join(notes)
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def _card(label: str, value: Any, status: str) -> dict[str, Any]:
|
|
338
|
+
return {"label": label, "value": value, "status": status}
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
def _count_status(value: int | float | None) -> str:
|
|
342
|
+
if value is None:
|
|
343
|
+
return "unknown"
|
|
344
|
+
return "ok" if float(value) > 0 else "degraded"
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def _return_status(value: float | None) -> str:
|
|
348
|
+
if value is None:
|
|
349
|
+
return "unknown"
|
|
350
|
+
return "ok" if float(value) > 0 else "degraded"
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def _win_rate_status(value: float | None) -> str:
|
|
354
|
+
if value is None:
|
|
355
|
+
return "unknown"
|
|
356
|
+
return "ok" if float(value) >= 50 else "degraded"
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
def _round_value(value: Any) -> Any:
|
|
360
|
+
if isinstance(value, float):
|
|
361
|
+
return round(value, 4)
|
|
362
|
+
return value
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def _fmt(value: Any) -> str:
|
|
366
|
+
if value is None or value == "":
|
|
367
|
+
return "-"
|
|
368
|
+
if isinstance(value, (dict, list)):
|
|
369
|
+
return json.dumps(value, ensure_ascii=False)
|
|
370
|
+
return str(value)
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
def _md(value: Any) -> str:
|
|
374
|
+
return _fmt(value).replace("|", "\\|").replace("\n", " ")
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
def report_payload_to_json(payload: dict[str, Any]) -> str:
|
|
378
|
+
"""Serialize report payload with stable JSON formatting."""
|
|
379
|
+
return json.dumps(payload, ensure_ascii=False, indent=2)
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""Stable machine-readable result schema metadata."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import copy
|
|
7
|
+
|
|
8
|
+
SCREEN_RESULT_SCHEMA_VERSION = 1
|
|
9
|
+
DAILY_REPORT_SCHEMA_VERSION = 1
|
|
10
|
+
|
|
11
|
+
SCREEN_RESULT_SCHEMA: dict[str, object] = {
|
|
12
|
+
"schema_version": SCREEN_RESULT_SCHEMA_VERSION,
|
|
13
|
+
"object": "ScreenResult",
|
|
14
|
+
"top_level_fields": [
|
|
15
|
+
"strategy",
|
|
16
|
+
"market",
|
|
17
|
+
"track",
|
|
18
|
+
"strategy_version",
|
|
19
|
+
"strategy_category",
|
|
20
|
+
"snapshot_count",
|
|
21
|
+
"after_filter_count",
|
|
22
|
+
"picks",
|
|
23
|
+
"run_id",
|
|
24
|
+
"llm_ranked",
|
|
25
|
+
"llm_coverage",
|
|
26
|
+
"llm_parse_errors",
|
|
27
|
+
"degradation",
|
|
28
|
+
"snapshot_source",
|
|
29
|
+
"source_errors",
|
|
30
|
+
"deep_analysis_requested",
|
|
31
|
+
"post_analyzers",
|
|
32
|
+
"daily_enriched",
|
|
33
|
+
"daily_enrich_count",
|
|
34
|
+
"risk_enabled",
|
|
35
|
+
"portfolio_diversity_enabled",
|
|
36
|
+
"portfolio_concentration_notes",
|
|
37
|
+
"portfolio_topic_exposure",
|
|
38
|
+
"overconcentrated_topics",
|
|
39
|
+
"replacement_candidates",
|
|
40
|
+
"daily_source_counts",
|
|
41
|
+
"daily_quality_flag_counts",
|
|
42
|
+
"created_at",
|
|
43
|
+
],
|
|
44
|
+
"pick_fields": [
|
|
45
|
+
"rank",
|
|
46
|
+
"code",
|
|
47
|
+
"name",
|
|
48
|
+
"final_score",
|
|
49
|
+
"screen_score",
|
|
50
|
+
"factor_scores",
|
|
51
|
+
"ranking_reason",
|
|
52
|
+
"risk_summary",
|
|
53
|
+
"risk_score",
|
|
54
|
+
"risk_level",
|
|
55
|
+
"risk_flags",
|
|
56
|
+
"portfolio_penalty",
|
|
57
|
+
"portfolio_flags",
|
|
58
|
+
"industry",
|
|
59
|
+
"concepts",
|
|
60
|
+
"topic_alignment_status",
|
|
61
|
+
"topic_alignment_role",
|
|
62
|
+
"topic_alignment_reason",
|
|
63
|
+
"matched_topics",
|
|
64
|
+
"unmatched_topics",
|
|
65
|
+
"board_heat_score",
|
|
66
|
+
"board_heat_summary",
|
|
67
|
+
"daily_quality_score",
|
|
68
|
+
"daily_quality_flags",
|
|
69
|
+
"daily_source",
|
|
70
|
+
"post_analysis_status",
|
|
71
|
+
"post_analysis_summaries",
|
|
72
|
+
"post_analysis_score_deltas",
|
|
73
|
+
"post_analysis_tags",
|
|
74
|
+
"dsa_context",
|
|
75
|
+
"dsa_news",
|
|
76
|
+
"dsa_analysis_summary",
|
|
77
|
+
"deep_analysis_status",
|
|
78
|
+
"deep_analysis_query_id",
|
|
79
|
+
"deep_analysis_summary",
|
|
80
|
+
"deep_analysis_error",
|
|
81
|
+
"deep_analysis_signal_score",
|
|
82
|
+
"deep_analysis_sentiment_score",
|
|
83
|
+
"deep_analysis_operation_advice",
|
|
84
|
+
"deep_analysis_trend_prediction",
|
|
85
|
+
"deep_analysis_risk_flags",
|
|
86
|
+
"llm_watch_items",
|
|
87
|
+
"llm_invalidators",
|
|
88
|
+
],
|
|
89
|
+
"ui_card_fields": {
|
|
90
|
+
"identity": ["rank", "code", "name", "final_score", "screen_score"],
|
|
91
|
+
"source_health": [
|
|
92
|
+
"snapshot_source",
|
|
93
|
+
"source_errors",
|
|
94
|
+
"daily_source",
|
|
95
|
+
"daily_quality_flags",
|
|
96
|
+
"daily_source_counts",
|
|
97
|
+
"daily_quality_flag_counts",
|
|
98
|
+
],
|
|
99
|
+
"risk": ["risk_level", "risk_flags", "portfolio_flags", "risk_summary"],
|
|
100
|
+
"topic_alignment": [
|
|
101
|
+
"topic_alignment_status",
|
|
102
|
+
"topic_alignment_role",
|
|
103
|
+
"topic_alignment_reason",
|
|
104
|
+
"matched_topics",
|
|
105
|
+
"unmatched_topics",
|
|
106
|
+
],
|
|
107
|
+
"portfolio_exposure": [
|
|
108
|
+
"portfolio_topic_exposure",
|
|
109
|
+
"overconcentrated_topics",
|
|
110
|
+
"replacement_candidates",
|
|
111
|
+
],
|
|
112
|
+
"watch": [
|
|
113
|
+
"llm_watch_items",
|
|
114
|
+
"llm_invalidators",
|
|
115
|
+
"deep_analysis_operation_advice",
|
|
116
|
+
],
|
|
117
|
+
"post_analysis": [
|
|
118
|
+
"post_analysis_status",
|
|
119
|
+
"post_analysis_summaries",
|
|
120
|
+
"post_analysis_tags",
|
|
121
|
+
],
|
|
122
|
+
},
|
|
123
|
+
"non_goals": [
|
|
124
|
+
"AlphaSift does not execute trades.",
|
|
125
|
+
"AlphaSift does not provide full portfolio accounting.",
|
|
126
|
+
"DSA is optional and must not be required for core screening.",
|
|
127
|
+
],
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def screen_result_schema() -> dict[str, object]:
|
|
132
|
+
"""Return a copy of the stable ScreenResult schema metadata."""
|
|
133
|
+
return copy.deepcopy(SCREEN_RESULT_SCHEMA)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
DAILY_REPORT_SCHEMA: dict[str, object] = {
|
|
137
|
+
"schema_version": DAILY_REPORT_SCHEMA_VERSION,
|
|
138
|
+
"object": "DailyReport",
|
|
139
|
+
"top_level_fields": [
|
|
140
|
+
"schema_version",
|
|
141
|
+
"object",
|
|
142
|
+
"trade_date",
|
|
143
|
+
"pack_trade_date",
|
|
144
|
+
"expected_trade_date",
|
|
145
|
+
"pack_is_stale",
|
|
146
|
+
"stale_reason",
|
|
147
|
+
"detail_url",
|
|
148
|
+
"market",
|
|
149
|
+
"generated_at",
|
|
150
|
+
"usage_policy",
|
|
151
|
+
"market_regime",
|
|
152
|
+
"strategies",
|
|
153
|
+
"ai_highlights",
|
|
154
|
+
"cross_strategy_synthesis",
|
|
155
|
+
"ai_brief",
|
|
156
|
+
"patterns",
|
|
157
|
+
"announcement_risks",
|
|
158
|
+
"market_news",
|
|
159
|
+
"degradation",
|
|
160
|
+
"im_card_markdown",
|
|
161
|
+
"im_card_chars",
|
|
162
|
+
"non_goals",
|
|
163
|
+
],
|
|
164
|
+
"pattern_sections": ["active", "experimental"],
|
|
165
|
+
"im_card_max_chars": 1199,
|
|
166
|
+
"non_goals": [
|
|
167
|
+
"量化形态观测与回测基准,非投资建议。",
|
|
168
|
+
"Market regime stage is reporting and slice metadata only; never adjusts strategy weights.",
|
|
169
|
+
"Relay statistics do not generate stock-picking signals.",
|
|
170
|
+
],
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def daily_report_schema() -> dict[str, object]:
|
|
175
|
+
"""Return a copy of the stable DailyReport schema metadata."""
|
|
176
|
+
return {
|
|
177
|
+
key: list(value) if isinstance(value, list) else value
|
|
178
|
+
for key, value in DAILY_REPORT_SCHEMA.items()
|
|
179
|
+
}
|