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
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""UI-ready strategy cards combining catalog, readiness, and run history."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from dataclasses import asdict
|
|
7
|
+
from datetime import datetime, timezone
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from alphasift.config import Config
|
|
11
|
+
from alphasift.doctor import doctor_data_sources
|
|
12
|
+
from alphasift.models import StrategyInfo
|
|
13
|
+
from alphasift.performance_history import build_strategy_performance_summary
|
|
14
|
+
from alphasift.run_history import build_strategy_run_summary
|
|
15
|
+
from alphasift.strategy import list_strategies
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def build_strategy_cards(
|
|
19
|
+
config: Config,
|
|
20
|
+
*,
|
|
21
|
+
strategy_name: str | None = None,
|
|
22
|
+
runs_limit: int = 100,
|
|
23
|
+
live_data_check: bool = False,
|
|
24
|
+
) -> dict[str, Any]:
|
|
25
|
+
"""Build strategy cards for dashboards and agents."""
|
|
26
|
+
strategies = _filtered_strategies(
|
|
27
|
+
list_strategies(config.strategies_dir),
|
|
28
|
+
strategy_name=strategy_name,
|
|
29
|
+
)
|
|
30
|
+
doctor = doctor_data_sources(
|
|
31
|
+
config,
|
|
32
|
+
run_live=live_data_check,
|
|
33
|
+
strategy_name=strategy_name,
|
|
34
|
+
all_strategies=strategy_name is None,
|
|
35
|
+
).to_dict()
|
|
36
|
+
run_history = build_strategy_run_summary(
|
|
37
|
+
data_dir=config.data_dir,
|
|
38
|
+
limit=runs_limit,
|
|
39
|
+
strategy=strategy_name,
|
|
40
|
+
)
|
|
41
|
+
performance = build_strategy_performance_summary(
|
|
42
|
+
data_dir=config.data_dir,
|
|
43
|
+
limit=runs_limit,
|
|
44
|
+
strategy=strategy_name,
|
|
45
|
+
)
|
|
46
|
+
return build_strategy_cards_from_parts(
|
|
47
|
+
strategies,
|
|
48
|
+
strategy_coverage=doctor.get("strategy_coverage", []),
|
|
49
|
+
run_history_summary=run_history,
|
|
50
|
+
performance_summary=performance,
|
|
51
|
+
live_data_check=live_data_check,
|
|
52
|
+
strategy_filter=strategy_name or "",
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def build_strategy_cards_from_parts(
|
|
57
|
+
strategies: list[StrategyInfo],
|
|
58
|
+
*,
|
|
59
|
+
strategy_coverage: list[dict[str, Any]] | None = None,
|
|
60
|
+
run_history_summary: dict[str, Any] | None = None,
|
|
61
|
+
performance_summary: dict[str, Any] | None = None,
|
|
62
|
+
live_data_check: bool = False,
|
|
63
|
+
strategy_filter: str = "",
|
|
64
|
+
) -> dict[str, Any]:
|
|
65
|
+
"""Build cards from data already gathered by overview or the API layer."""
|
|
66
|
+
coverage_by_strategy = {
|
|
67
|
+
str(item.get("strategy") or ""): item
|
|
68
|
+
for item in (strategy_coverage or [])
|
|
69
|
+
}
|
|
70
|
+
history_by_strategy = {
|
|
71
|
+
str(item.get("strategy") or ""): item
|
|
72
|
+
for item in ((run_history_summary or {}).get("strategies", []) or [])
|
|
73
|
+
}
|
|
74
|
+
performance_by_strategy = {
|
|
75
|
+
str(item.get("strategy") or ""): item
|
|
76
|
+
for item in ((performance_summary or {}).get("strategies", []) or [])
|
|
77
|
+
}
|
|
78
|
+
cards = [
|
|
79
|
+
_strategy_card(
|
|
80
|
+
strategy,
|
|
81
|
+
coverage=coverage_by_strategy.get(strategy.name, {}),
|
|
82
|
+
history=history_by_strategy.get(strategy.name, {}),
|
|
83
|
+
performance=performance_by_strategy.get(strategy.name, {}),
|
|
84
|
+
live_data_check=live_data_check,
|
|
85
|
+
)
|
|
86
|
+
for strategy in strategies
|
|
87
|
+
]
|
|
88
|
+
lanes = _card_lanes(cards)
|
|
89
|
+
return {
|
|
90
|
+
"schema_version": 1,
|
|
91
|
+
"generated_at": datetime.now(timezone.utc).isoformat(),
|
|
92
|
+
"strategy_filter": strategy_filter,
|
|
93
|
+
"summary": _cards_summary(cards, lanes=lanes, live_data_check=live_data_check),
|
|
94
|
+
"lanes": lanes,
|
|
95
|
+
"cards": cards,
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _filtered_strategies(
|
|
100
|
+
strategies: list[StrategyInfo],
|
|
101
|
+
*,
|
|
102
|
+
strategy_name: str | None,
|
|
103
|
+
) -> list[StrategyInfo]:
|
|
104
|
+
if not strategy_name:
|
|
105
|
+
return strategies
|
|
106
|
+
matches = [item for item in strategies if item.name == strategy_name]
|
|
107
|
+
if not matches:
|
|
108
|
+
raise ValueError(f"Strategy '{strategy_name}' not found")
|
|
109
|
+
return matches
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _strategy_card(
|
|
113
|
+
strategy: StrategyInfo,
|
|
114
|
+
*,
|
|
115
|
+
coverage: dict[str, Any],
|
|
116
|
+
history: dict[str, Any],
|
|
117
|
+
performance: dict[str, Any],
|
|
118
|
+
live_data_check: bool,
|
|
119
|
+
) -> dict[str, Any]:
|
|
120
|
+
readiness_status = str(coverage.get("status") or "skipped")
|
|
121
|
+
history_payload = _history_payload(history)
|
|
122
|
+
performance_payload = _performance_payload(performance)
|
|
123
|
+
return {
|
|
124
|
+
"name": strategy.name,
|
|
125
|
+
"display_name": strategy.display_name,
|
|
126
|
+
"description": strategy.description,
|
|
127
|
+
"version": strategy.version,
|
|
128
|
+
"category": strategy.category,
|
|
129
|
+
"tags": list(strategy.tags),
|
|
130
|
+
"style": dict(strategy.style),
|
|
131
|
+
"use_case": _use_case(strategy),
|
|
132
|
+
"data": {
|
|
133
|
+
"requirements": list(strategy.data_requirements),
|
|
134
|
+
"requires_daily_features": bool(strategy.requires_daily_features),
|
|
135
|
+
"required_snapshot_fields": list(strategy.required_snapshot_fields),
|
|
136
|
+
"required_daily_fields": list(strategy.required_daily_fields),
|
|
137
|
+
"active_filters": list(strategy.active_filters),
|
|
138
|
+
},
|
|
139
|
+
"scoring": {
|
|
140
|
+
"factor_weights": dict(strategy.factor_weights),
|
|
141
|
+
"top_factors": _top_factors(strategy.factor_weights),
|
|
142
|
+
"profile_keys": dict(strategy.profile_keys),
|
|
143
|
+
},
|
|
144
|
+
"readiness": {
|
|
145
|
+
"status": readiness_status,
|
|
146
|
+
"live_data_check": bool(live_data_check),
|
|
147
|
+
"snapshot_missing_fields": list(coverage.get("snapshot_missing_fields", []) or []),
|
|
148
|
+
"daily_missing_fields": list(coverage.get("daily_missing_fields", []) or []),
|
|
149
|
+
},
|
|
150
|
+
"history": history_payload,
|
|
151
|
+
"performance": performance_payload,
|
|
152
|
+
"actions": _card_actions(
|
|
153
|
+
strategy,
|
|
154
|
+
readiness_status=readiness_status,
|
|
155
|
+
history=history_payload,
|
|
156
|
+
performance=performance_payload,
|
|
157
|
+
),
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _cards_summary(
|
|
162
|
+
cards: list[dict[str, Any]],
|
|
163
|
+
*,
|
|
164
|
+
lanes: dict[str, dict[str, Any]],
|
|
165
|
+
live_data_check: bool,
|
|
166
|
+
) -> dict[str, Any]:
|
|
167
|
+
status_counts: dict[str, int] = {}
|
|
168
|
+
for card in cards:
|
|
169
|
+
status = str((card.get("readiness") or {}).get("status") or "unknown")
|
|
170
|
+
status_counts[status] = status_counts.get(status, 0) + 1
|
|
171
|
+
return {
|
|
172
|
+
"strategy_count": len(cards),
|
|
173
|
+
"daily_strategy_count": sum(
|
|
174
|
+
1 for card in cards if bool((card.get("data") or {}).get("requires_daily_features"))
|
|
175
|
+
),
|
|
176
|
+
"snapshot_only_strategy_count": sum(
|
|
177
|
+
1 for card in cards if not bool((card.get("data") or {}).get("requires_daily_features"))
|
|
178
|
+
),
|
|
179
|
+
"ready_strategy_count": status_counts.get("ok", 0),
|
|
180
|
+
"attention_strategy_count": status_counts.get("degraded", 0) + status_counts.get("failed", 0),
|
|
181
|
+
"unchecked_strategy_count": status_counts.get("skipped", 0),
|
|
182
|
+
"history_seeded_strategy_count": sum(
|
|
183
|
+
1 for card in cards if _int_value((card.get("history") or {}).get("run_count")) > 0
|
|
184
|
+
),
|
|
185
|
+
"evaluated_strategy_count": sum(
|
|
186
|
+
1 for card in cards if _int_value((card.get("performance") or {}).get("evaluation_count")) > 0
|
|
187
|
+
),
|
|
188
|
+
"needs_history_count": _lane_count(lanes, "needs_history"),
|
|
189
|
+
"needs_evaluation_count": _lane_count(lanes, "needs_evaluation"),
|
|
190
|
+
"performance_leader_count": _lane_count(lanes, "performance_leaders"),
|
|
191
|
+
"operational_attention_count": _lane_count(lanes, "attention"),
|
|
192
|
+
"status_counts": status_counts,
|
|
193
|
+
"live_data_check": bool(live_data_check),
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def _card_lanes(cards: list[dict[str, Any]]) -> dict[str, dict[str, Any]]:
|
|
198
|
+
needs_history = [
|
|
199
|
+
card for card in cards if _int_value((card.get("history") or {}).get("run_count")) <= 0
|
|
200
|
+
]
|
|
201
|
+
needs_evaluation = [
|
|
202
|
+
card
|
|
203
|
+
for card in cards
|
|
204
|
+
if _int_value((card.get("history") or {}).get("run_count")) > 0
|
|
205
|
+
and _int_value((card.get("performance") or {}).get("evaluation_count")) <= 0
|
|
206
|
+
]
|
|
207
|
+
performance_leaders = [
|
|
208
|
+
card
|
|
209
|
+
for card in cards
|
|
210
|
+
if str((card.get("performance") or {}).get("outcome") or "") in {"strong", "positive"}
|
|
211
|
+
]
|
|
212
|
+
attention = [
|
|
213
|
+
card for card in cards if _needs_attention(card)
|
|
214
|
+
]
|
|
215
|
+
return {
|
|
216
|
+
"needs_history": _lane_payload(
|
|
217
|
+
sorted(needs_history, key=_card_name),
|
|
218
|
+
reason="no_saved_runs",
|
|
219
|
+
),
|
|
220
|
+
"needs_evaluation": _lane_payload(
|
|
221
|
+
sorted(needs_evaluation, key=_card_name),
|
|
222
|
+
reason="runs_without_saved_evaluations",
|
|
223
|
+
),
|
|
224
|
+
"performance_leaders": _lane_payload(
|
|
225
|
+
sorted(performance_leaders, key=_performance_card_sort_key, reverse=True),
|
|
226
|
+
reason="positive_saved_evaluation_outcome",
|
|
227
|
+
),
|
|
228
|
+
"attention": _lane_payload(
|
|
229
|
+
sorted(attention, key=_attention_card_sort_key, reverse=True),
|
|
230
|
+
reason="readiness_source_or_performance_attention",
|
|
231
|
+
),
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def _lane_payload(cards: list[dict[str, Any]], *, reason: str) -> dict[str, Any]:
|
|
236
|
+
return {
|
|
237
|
+
"reason": reason,
|
|
238
|
+
"count": len(cards),
|
|
239
|
+
"cards": [_compact_card(card) for card in cards[:10]],
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def _compact_card(card: dict[str, Any]) -> dict[str, Any]:
|
|
244
|
+
history = card.get("history") or {}
|
|
245
|
+
performance = card.get("performance") or {}
|
|
246
|
+
readiness = card.get("readiness") or {}
|
|
247
|
+
return {
|
|
248
|
+
"name": str(card.get("name") or ""),
|
|
249
|
+
"display_name": str(card.get("display_name") or ""),
|
|
250
|
+
"category": str(card.get("category") or ""),
|
|
251
|
+
"readiness_status": str(readiness.get("status") or "unknown"),
|
|
252
|
+
"run_count": _int_value(history.get("run_count")),
|
|
253
|
+
"evaluation_count": _int_value(performance.get("evaluation_count")),
|
|
254
|
+
"performance_score": performance.get("performance_score"),
|
|
255
|
+
"outcome": str(performance.get("outcome") or "insufficient_data"),
|
|
256
|
+
"latest_run_id": str(history.get("latest_run_id") or performance.get("latest_run_id") or ""),
|
|
257
|
+
"actions": list(card.get("actions", []) or [])[:2],
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def _needs_attention(card: dict[str, Any]) -> bool:
|
|
262
|
+
readiness = card.get("readiness") or {}
|
|
263
|
+
history = card.get("history") or {}
|
|
264
|
+
performance = card.get("performance") or {}
|
|
265
|
+
return (
|
|
266
|
+
str(readiness.get("status") or "") in {"degraded", "failed"}
|
|
267
|
+
or _int_value(history.get("source_error_count")) > 0
|
|
268
|
+
or _int_value(history.get("degradation_count")) > 0
|
|
269
|
+
or str(performance.get("outcome") or "") in {"negative", "mixed"}
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def _lane_count(lanes: dict[str, dict[str, Any]], lane_name: str) -> int:
|
|
274
|
+
return _int_value((lanes.get(lane_name) or {}).get("count"))
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def _card_name(card: dict[str, Any]) -> str:
|
|
278
|
+
return str(card.get("name") or "")
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def _performance_card_sort_key(card: dict[str, Any]) -> tuple[float, str]:
|
|
282
|
+
performance = card.get("performance") or {}
|
|
283
|
+
return (_float_value(performance.get("performance_score")), _card_name(card))
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def _attention_card_sort_key(card: dict[str, Any]) -> tuple[int, int, float, str]:
|
|
287
|
+
readiness = card.get("readiness") or {}
|
|
288
|
+
history = card.get("history") or {}
|
|
289
|
+
performance = card.get("performance") or {}
|
|
290
|
+
status_rank = {"failed": 4, "degraded": 3, "skipped": 1, "ok": 0}.get(
|
|
291
|
+
str(readiness.get("status") or ""),
|
|
292
|
+
0,
|
|
293
|
+
)
|
|
294
|
+
source_issues = _int_value(history.get("source_error_count")) + _int_value(history.get("degradation_count"))
|
|
295
|
+
performance_rank = {"negative": 3, "mixed": 2}.get(str(performance.get("outcome") or ""), 0)
|
|
296
|
+
return (
|
|
297
|
+
status_rank + performance_rank,
|
|
298
|
+
source_issues,
|
|
299
|
+
_float_value(performance.get("performance_score")),
|
|
300
|
+
_card_name(card),
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def _use_case(strategy: StrategyInfo) -> dict[str, object]:
|
|
305
|
+
style = strategy.style or {}
|
|
306
|
+
regimes = list(style.get("market_regime", []) or [])
|
|
307
|
+
risk = str(style.get("risk_profile") or "balanced")
|
|
308
|
+
holding = str(style.get("holding_period") or "watchlist")
|
|
309
|
+
execution = str(style.get("execution_style") or strategy.category)
|
|
310
|
+
data_mode = "daily-confirmed" if strategy.requires_daily_features else "snapshot-only"
|
|
311
|
+
return {
|
|
312
|
+
"risk_profile": risk,
|
|
313
|
+
"holding_period": holding,
|
|
314
|
+
"execution_style": execution,
|
|
315
|
+
"market_regime": regimes,
|
|
316
|
+
"capital_profile": str(style.get("capital_profile") or ""),
|
|
317
|
+
"ui_badge": str(style.get("ui_badge") or ""),
|
|
318
|
+
"summary": (
|
|
319
|
+
f"{risk} {holding} strategy for {','.join(regimes) or 'general'} "
|
|
320
|
+
f"using {execution}; {data_mode}"
|
|
321
|
+
),
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
def _history_payload(history: dict[str, Any]) -> dict[str, Any]:
|
|
326
|
+
return {
|
|
327
|
+
"run_count": _int_value(history.get("run_count")),
|
|
328
|
+
"latest_run_id": str(history.get("latest_run_id") or ""),
|
|
329
|
+
"latest_created_at": str(history.get("latest_created_at") or ""),
|
|
330
|
+
"latest_report_path": str(history.get("latest_report_path") or ""),
|
|
331
|
+
"latest_snapshot_source": str(history.get("latest_snapshot_source") or ""),
|
|
332
|
+
"total_picks": _int_value(history.get("total_picks")),
|
|
333
|
+
"average_picks": history.get("average_picks"),
|
|
334
|
+
"source_error_count": _int_value(history.get("source_error_count")),
|
|
335
|
+
"source_error_samples": _string_list(history.get("source_error_samples", []))[:5],
|
|
336
|
+
"degradation_count": _int_value(history.get("degradation_count")),
|
|
337
|
+
"degradation_samples": _string_list(history.get("degradation_samples", []))[:5],
|
|
338
|
+
"llm_ranked_runs": _int_value(history.get("llm_ranked_runs")),
|
|
339
|
+
"daily_enriched_runs": _int_value(history.get("daily_enriched_runs")),
|
|
340
|
+
"recent_runs": list(history.get("recent_runs", []) or [])[:3],
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def _performance_payload(performance: dict[str, Any]) -> dict[str, Any]:
|
|
345
|
+
return {
|
|
346
|
+
"evaluation_count": _int_value(performance.get("evaluation_count")),
|
|
347
|
+
"latest_run_id": str(performance.get("latest_run_id") or ""),
|
|
348
|
+
"latest_evaluated_at": str(performance.get("latest_evaluated_at") or ""),
|
|
349
|
+
"latest_elapsed_days": performance.get("latest_elapsed_days"),
|
|
350
|
+
"pick_count": _int_value(performance.get("pick_count")),
|
|
351
|
+
"evaluated_pick_count": _int_value(performance.get("evaluated_pick_count")),
|
|
352
|
+
"missing_count": _int_value(performance.get("missing_count")),
|
|
353
|
+
"average_return_pct": performance.get("average_return_pct"),
|
|
354
|
+
"median_return_pct": performance.get("median_return_pct"),
|
|
355
|
+
"win_rate": performance.get("win_rate"),
|
|
356
|
+
"average_run_return_pct": performance.get("average_run_return_pct"),
|
|
357
|
+
"run_win_rate": performance.get("run_win_rate"),
|
|
358
|
+
"performance_score": performance.get("performance_score"),
|
|
359
|
+
"outcome": str(performance.get("outcome") or "insufficient_data"),
|
|
360
|
+
"next_actions": _string_list(performance.get("next_actions", []))[:3],
|
|
361
|
+
"recent_evaluations": list(performance.get("recent_evaluations", []) or [])[:3],
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def _top_factors(weights: dict[str, float]) -> list[dict[str, object]]:
|
|
366
|
+
rows = [
|
|
367
|
+
{"name": str(name), "weight": float(weight)}
|
|
368
|
+
for name, weight in weights.items()
|
|
369
|
+
if float(weight) > 0
|
|
370
|
+
]
|
|
371
|
+
rows.sort(key=lambda item: (-float(item["weight"]), str(item["name"])))
|
|
372
|
+
return rows[:3]
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
def _card_actions(
|
|
376
|
+
strategy: StrategyInfo,
|
|
377
|
+
*,
|
|
378
|
+
readiness_status: str,
|
|
379
|
+
history: dict[str, Any],
|
|
380
|
+
performance: dict[str, Any],
|
|
381
|
+
) -> list[str]:
|
|
382
|
+
actions: list[str] = []
|
|
383
|
+
if readiness_status == "skipped":
|
|
384
|
+
actions.append(f"Run `alphasift doctor data-sources --strategy {strategy.name} --explain`.")
|
|
385
|
+
elif readiness_status in {"degraded", "failed"}:
|
|
386
|
+
actions.append(f"Review missing fields before screening `{strategy.name}`.")
|
|
387
|
+
if strategy.requires_daily_features:
|
|
388
|
+
actions.append(f"Check daily data quality before running `{strategy.name}`.")
|
|
389
|
+
if _int_value(history.get("run_count")) <= 0:
|
|
390
|
+
actions.append(f"Run `alphasift screen {strategy.name} --save-run` to seed history.")
|
|
391
|
+
elif _int_value(history.get("degradation_count")) > 0:
|
|
392
|
+
actions.append(f"Review latest report for `{strategy.name}` data degradation.")
|
|
393
|
+
if _int_value(performance.get("evaluation_count")) <= 0:
|
|
394
|
+
actions.append(f"Run `alphasift evaluate <run_id> --save` to seed `{strategy.name}` performance.")
|
|
395
|
+
elif str(performance.get("outcome") or "") in {"negative", "mixed"}:
|
|
396
|
+
actions.extend(_string_list(performance.get("next_actions", []))[:1])
|
|
397
|
+
return list(dict.fromkeys(actions))
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
def _int_value(value: object) -> int:
|
|
401
|
+
try:
|
|
402
|
+
return int(value or 0)
|
|
403
|
+
except (TypeError, ValueError):
|
|
404
|
+
return 0
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
def _float_value(value: object) -> float:
|
|
408
|
+
try:
|
|
409
|
+
return float(value or 0.0)
|
|
410
|
+
except (TypeError, ValueError):
|
|
411
|
+
return 0.0
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
def _string_list(value: object) -> list[str]:
|
|
415
|
+
if isinstance(value, list):
|
|
416
|
+
return [str(item) for item in value if str(item)]
|
|
417
|
+
if isinstance(value, str) and value:
|
|
418
|
+
return [item.strip() for item in value.split(",") if item.strip()]
|
|
419
|
+
return []
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
def strategy_cards_to_jsonable(payload: dict[str, Any]) -> dict[str, Any]:
|
|
423
|
+
"""Normalize dataclass-like values if callers extend this payload later."""
|
|
424
|
+
return asdict(payload) if hasattr(payload, "__dataclass_fields__") else payload
|