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,297 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""Saved evaluation performance summaries for UI and agent integrations."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import json
|
|
7
|
+
from datetime import datetime, timezone
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from statistics import median
|
|
10
|
+
|
|
11
|
+
from alphasift.models import EvaluationResult, PickEvaluation
|
|
12
|
+
from alphasift.store import evaluation_from_dict
|
|
13
|
+
from alphasift.timeutil import MIN_TIMESTAMP, parse_timestamp
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def build_strategy_performance_summary(
|
|
17
|
+
*,
|
|
18
|
+
data_dir: Path,
|
|
19
|
+
limit: int = 100,
|
|
20
|
+
strategy: str | None = None,
|
|
21
|
+
) -> dict[str, object]:
|
|
22
|
+
"""Summarize saved T+N evaluation files without re-running live evaluation."""
|
|
23
|
+
evaluations = _list_saved_evaluations(data_dir=data_dir, limit=limit, strategy=strategy)
|
|
24
|
+
strategy_rows = [_strategy_summary(items) for items in _group_by_strategy(evaluations).values()]
|
|
25
|
+
strategy_rows.sort(
|
|
26
|
+
key=lambda item: (
|
|
27
|
+
_score_value(item.get("performance_score")),
|
|
28
|
+
_parse_datetime(str(item.get("latest_evaluated_at") or "")),
|
|
29
|
+
str(item.get("strategy") or ""),
|
|
30
|
+
),
|
|
31
|
+
reverse=True,
|
|
32
|
+
)
|
|
33
|
+
return {
|
|
34
|
+
"schema_version": 1,
|
|
35
|
+
"generated_at": datetime.now(timezone.utc).isoformat(),
|
|
36
|
+
"evaluation_count": len(evaluations),
|
|
37
|
+
"strategy_count": len(strategy_rows),
|
|
38
|
+
"limit": int(limit),
|
|
39
|
+
"strategy_filter": strategy or "",
|
|
40
|
+
"summary": _history_summary(evaluations),
|
|
41
|
+
"strategies": strategy_rows,
|
|
42
|
+
"leaderboard": strategy_rows[:10],
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _list_saved_evaluations(
|
|
47
|
+
*,
|
|
48
|
+
data_dir: Path,
|
|
49
|
+
limit: int,
|
|
50
|
+
strategy: str | None,
|
|
51
|
+
) -> list[EvaluationResult]:
|
|
52
|
+
evaluations_dir = data_dir / "evaluations"
|
|
53
|
+
if not evaluations_dir.is_dir():
|
|
54
|
+
return []
|
|
55
|
+
limit = int(limit)
|
|
56
|
+
if limit <= 0:
|
|
57
|
+
return []
|
|
58
|
+
rows: list[EvaluationResult] = []
|
|
59
|
+
for path in sorted(evaluations_dir.glob("*.json"), key=lambda item: item.stat().st_mtime, reverse=True):
|
|
60
|
+
item = _load_evaluation(path)
|
|
61
|
+
if item is None:
|
|
62
|
+
continue
|
|
63
|
+
if strategy and item.strategy != strategy:
|
|
64
|
+
continue
|
|
65
|
+
item.saved_path = str(path)
|
|
66
|
+
rows.append(item)
|
|
67
|
+
if len(rows) >= limit:
|
|
68
|
+
break
|
|
69
|
+
return rows
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _load_evaluation(path: Path) -> EvaluationResult | None:
|
|
73
|
+
try:
|
|
74
|
+
data = json.loads(path.read_text(encoding="utf-8"))
|
|
75
|
+
except Exception:
|
|
76
|
+
return None
|
|
77
|
+
if not isinstance(data, dict):
|
|
78
|
+
return None
|
|
79
|
+
try:
|
|
80
|
+
return evaluation_from_dict(data)
|
|
81
|
+
except Exception:
|
|
82
|
+
return None
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _group_by_strategy(evaluations: list[EvaluationResult]) -> dict[str, list[EvaluationResult]]:
|
|
86
|
+
groups: dict[str, list[EvaluationResult]] = {}
|
|
87
|
+
for item in evaluations:
|
|
88
|
+
groups.setdefault(item.strategy or "unknown", []).append(item)
|
|
89
|
+
return groups
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _strategy_summary(evaluations: list[EvaluationResult]) -> dict[str, object]:
|
|
93
|
+
ordered = sorted(evaluations, key=_evaluation_sort_key, reverse=True)
|
|
94
|
+
latest = ordered[0] if ordered else None
|
|
95
|
+
pick_stats = _pick_stats(_all_picks(ordered))
|
|
96
|
+
run_returns = _run_returns(ordered)
|
|
97
|
+
missing_count = sum(len(item.missing_codes) for item in ordered)
|
|
98
|
+
source_error_count = sum(len(item.source_errors) for item in ordered)
|
|
99
|
+
degradation_count = sum(len(item.degradation) for item in ordered)
|
|
100
|
+
performance_score = _performance_score(
|
|
101
|
+
average_return_pct=pick_stats["average_return_pct"],
|
|
102
|
+
win_rate=pick_stats["win_rate"],
|
|
103
|
+
pick_count=pick_stats["pick_count"],
|
|
104
|
+
missing_count=missing_count,
|
|
105
|
+
)
|
|
106
|
+
outcome = _performance_outcome(
|
|
107
|
+
average_return_pct=pick_stats["average_return_pct"],
|
|
108
|
+
win_rate=pick_stats["win_rate"],
|
|
109
|
+
)
|
|
110
|
+
return {
|
|
111
|
+
"strategy": latest.strategy if latest else "unknown",
|
|
112
|
+
"evaluation_count": len(ordered),
|
|
113
|
+
"latest_run_id": latest.run_id if latest else "",
|
|
114
|
+
"latest_evaluated_at": latest.evaluated_at if latest else "",
|
|
115
|
+
"latest_elapsed_days": latest.elapsed_days if latest else None,
|
|
116
|
+
"latest_snapshot_source": latest.snapshot_source if latest else "",
|
|
117
|
+
"pick_count": pick_stats["pick_count"],
|
|
118
|
+
"evaluated_pick_count": pick_stats["evaluated_pick_count"],
|
|
119
|
+
"missing_count": missing_count,
|
|
120
|
+
"missing_rate": _rate(missing_count, pick_stats["pick_count"]),
|
|
121
|
+
"average_return_pct": pick_stats["average_return_pct"],
|
|
122
|
+
"median_return_pct": pick_stats["median_return_pct"],
|
|
123
|
+
"win_rate": pick_stats["win_rate"],
|
|
124
|
+
"average_max_drawdown_pct": pick_stats["average_max_drawdown_pct"],
|
|
125
|
+
"average_max_runup_pct": pick_stats["average_max_runup_pct"],
|
|
126
|
+
"average_run_return_pct": _average(run_returns),
|
|
127
|
+
"run_win_rate": _rate(sum(1 for value in run_returns if value > 0), len(run_returns), percent=True),
|
|
128
|
+
"performance_score": performance_score,
|
|
129
|
+
"outcome": outcome,
|
|
130
|
+
"source_error_count": source_error_count,
|
|
131
|
+
"degradation_count": degradation_count,
|
|
132
|
+
"next_actions": _performance_actions(outcome),
|
|
133
|
+
"recent_evaluations": [_compact_evaluation(item) for item in ordered[:5]],
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _history_summary(evaluations: list[EvaluationResult]) -> dict[str, object]:
|
|
138
|
+
ordered = sorted(evaluations, key=_evaluation_sort_key, reverse=True)
|
|
139
|
+
pick_stats = _pick_stats(_all_picks(ordered))
|
|
140
|
+
run_returns = _run_returns(ordered)
|
|
141
|
+
missing_count = sum(len(item.missing_codes) for item in ordered)
|
|
142
|
+
performance_score = _performance_score(
|
|
143
|
+
average_return_pct=pick_stats["average_return_pct"],
|
|
144
|
+
win_rate=pick_stats["win_rate"],
|
|
145
|
+
pick_count=pick_stats["pick_count"],
|
|
146
|
+
missing_count=missing_count,
|
|
147
|
+
)
|
|
148
|
+
outcome = _performance_outcome(
|
|
149
|
+
average_return_pct=pick_stats["average_return_pct"],
|
|
150
|
+
win_rate=pick_stats["win_rate"],
|
|
151
|
+
)
|
|
152
|
+
return {
|
|
153
|
+
"evaluation_count": len(ordered),
|
|
154
|
+
"pick_count": pick_stats["pick_count"],
|
|
155
|
+
"evaluated_pick_count": pick_stats["evaluated_pick_count"],
|
|
156
|
+
"missing_count": missing_count,
|
|
157
|
+
"missing_rate": _rate(missing_count, pick_stats["pick_count"]),
|
|
158
|
+
"average_return_pct": pick_stats["average_return_pct"],
|
|
159
|
+
"median_return_pct": pick_stats["median_return_pct"],
|
|
160
|
+
"win_rate": pick_stats["win_rate"],
|
|
161
|
+
"average_run_return_pct": _average(run_returns),
|
|
162
|
+
"run_win_rate": _rate(sum(1 for value in run_returns if value > 0), len(run_returns), percent=True),
|
|
163
|
+
"performance_score": performance_score,
|
|
164
|
+
"outcome": outcome,
|
|
165
|
+
"latest_evaluation": _compact_evaluation(ordered[0]) if ordered else {},
|
|
166
|
+
"next_actions": _performance_actions(outcome),
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _pick_stats(picks: list[PickEvaluation]) -> dict[str, object]:
|
|
171
|
+
returns = [float(item.return_pct) for item in picks if item.return_pct is not None]
|
|
172
|
+
drawdowns = [float(item.max_drawdown_pct) for item in picks if item.max_drawdown_pct is not None]
|
|
173
|
+
runups = [float(item.max_runup_pct) for item in picks if item.max_runup_pct is not None]
|
|
174
|
+
return {
|
|
175
|
+
"pick_count": len(picks),
|
|
176
|
+
"evaluated_pick_count": len(returns),
|
|
177
|
+
"average_return_pct": _average(returns),
|
|
178
|
+
"median_return_pct": _median(returns),
|
|
179
|
+
"win_rate": _rate(sum(1 for value in returns if value > 0), len(returns), percent=True),
|
|
180
|
+
"average_max_drawdown_pct": _average(drawdowns),
|
|
181
|
+
"average_max_runup_pct": _average(runups),
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _all_picks(evaluations: list[EvaluationResult]) -> list[PickEvaluation]:
|
|
186
|
+
picks: list[PickEvaluation] = []
|
|
187
|
+
for item in evaluations:
|
|
188
|
+
picks.extend(item.picks)
|
|
189
|
+
return picks
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def _run_returns(evaluations: list[EvaluationResult]) -> list[float]:
|
|
193
|
+
values: list[float] = []
|
|
194
|
+
for item in evaluations:
|
|
195
|
+
if item.average_return_pct is not None:
|
|
196
|
+
values.append(float(item.average_return_pct))
|
|
197
|
+
continue
|
|
198
|
+
returns = [float(pick.return_pct) for pick in item.picks if pick.return_pct is not None]
|
|
199
|
+
if returns:
|
|
200
|
+
values.append(sum(returns) / len(returns))
|
|
201
|
+
return values
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _performance_score(
|
|
205
|
+
*,
|
|
206
|
+
average_return_pct: object,
|
|
207
|
+
win_rate: object,
|
|
208
|
+
pick_count: object,
|
|
209
|
+
missing_count: int,
|
|
210
|
+
) -> float | None:
|
|
211
|
+
if average_return_pct is None or win_rate is None or int(pick_count or 0) <= 0:
|
|
212
|
+
return None
|
|
213
|
+
missing_rate = float(missing_count) / float(pick_count or 1)
|
|
214
|
+
score = 50.0 + (float(average_return_pct) * 2.0) + ((float(win_rate) - 50.0) * 0.5) - (missing_rate * 20.0)
|
|
215
|
+
return round(max(0.0, min(100.0, score)), 1)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def _performance_outcome(*, average_return_pct: object, win_rate: object) -> str:
|
|
219
|
+
if average_return_pct is None or win_rate is None:
|
|
220
|
+
return "insufficient_data"
|
|
221
|
+
avg = float(average_return_pct)
|
|
222
|
+
wins = float(win_rate)
|
|
223
|
+
if avg >= 2.0 and wins >= 60.0:
|
|
224
|
+
return "strong"
|
|
225
|
+
if avg > 0.0 and wins >= 50.0:
|
|
226
|
+
return "positive"
|
|
227
|
+
if avg < 0.0 and wins < 50.0:
|
|
228
|
+
return "negative"
|
|
229
|
+
return "mixed"
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def _performance_actions(outcome: str) -> list[str]:
|
|
233
|
+
if outcome == "insufficient_data":
|
|
234
|
+
return ["Save more T+N evaluations before comparing strategy performance."]
|
|
235
|
+
if outcome == "negative":
|
|
236
|
+
return ["Review failure samples and tighten risk/event filters before reusing this strategy."]
|
|
237
|
+
if outcome == "mixed":
|
|
238
|
+
return ["Compare performance by event signals, shape status, and market regime before scaling usage."]
|
|
239
|
+
if outcome == "positive":
|
|
240
|
+
return ["Keep monitoring saved evaluations and compare against stronger strategy candidates."]
|
|
241
|
+
if outcome == "strong":
|
|
242
|
+
return ["Consider prioritizing this strategy while continuing T+N validation."]
|
|
243
|
+
return []
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def _compact_evaluation(item: EvaluationResult) -> dict[str, object]:
|
|
247
|
+
return {
|
|
248
|
+
"run_id": item.run_id,
|
|
249
|
+
"strategy": item.strategy,
|
|
250
|
+
"evaluated_at": item.evaluated_at,
|
|
251
|
+
"elapsed_days": item.elapsed_days,
|
|
252
|
+
"snapshot_source": item.snapshot_source,
|
|
253
|
+
"pick_count": len(item.picks),
|
|
254
|
+
"evaluated_pick_count": sum(1 for pick in item.picks if pick.return_pct is not None),
|
|
255
|
+
"missing_count": len(item.missing_codes),
|
|
256
|
+
"average_return_pct": item.average_return_pct,
|
|
257
|
+
"win_rate": item.win_rate,
|
|
258
|
+
"saved_path": item.saved_path,
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _average(values: list[float]) -> float | None:
|
|
263
|
+
if not values:
|
|
264
|
+
return None
|
|
265
|
+
return round(sum(values) / len(values), 4)
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _median(values: list[float]) -> float | None:
|
|
269
|
+
if not values:
|
|
270
|
+
return None
|
|
271
|
+
return round(float(median(values)), 4)
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def _rate(count: int, total: int, *, percent: bool = False) -> float | None:
|
|
275
|
+
if total <= 0:
|
|
276
|
+
return None
|
|
277
|
+
value = float(count) / float(total)
|
|
278
|
+
if percent:
|
|
279
|
+
value *= 100.0
|
|
280
|
+
return round(value, 4)
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def _score_value(value: object) -> float:
|
|
284
|
+
if value is None:
|
|
285
|
+
return -1.0
|
|
286
|
+
try:
|
|
287
|
+
return float(value)
|
|
288
|
+
except (TypeError, ValueError):
|
|
289
|
+
return -1.0
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def _evaluation_sort_key(item: EvaluationResult) -> tuple[datetime, str]:
|
|
293
|
+
return (_parse_datetime(item.evaluated_at), item.run_id)
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def _parse_datetime(value: str) -> datetime:
|
|
297
|
+
return parse_timestamp(value) or MIN_TIMESTAMP
|