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,337 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""Optional L3 post-ranking analyzers.
|
|
3
|
+
|
|
4
|
+
DSA is one analyzer backend. The pipeline can also use local scorecards or an
|
|
5
|
+
external HTTP scoring tool without making DSA part of the core selection path.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from dataclasses import asdict
|
|
11
|
+
|
|
12
|
+
import requests
|
|
13
|
+
|
|
14
|
+
from alphasift.config import Config
|
|
15
|
+
from alphasift.dsa import analyze_picks_with_dsa, apply_dsa_overlay
|
|
16
|
+
from alphasift.models import Pick
|
|
17
|
+
from alphasift.normalize import (
|
|
18
|
+
normalize_code,
|
|
19
|
+
safe_float as _safe_float,
|
|
20
|
+
safe_string_list as _safe_string_list,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _normalize_code(value: object) -> str:
|
|
25
|
+
# Pick codes and analyzer response code fields are structured, so
|
|
26
|
+
# US tickers may pass through (see normalize_code docstring).
|
|
27
|
+
return normalize_code(value, allow_ticker=True)
|
|
28
|
+
|
|
29
|
+
SUPPORTED_POST_ANALYZERS = {"dsa", "scorecard", "external_http"}
|
|
30
|
+
_DEFAULT_SCORECARD_PROFILE = {
|
|
31
|
+
"value_quality_value_min": 75.0,
|
|
32
|
+
"value_quality_stability_min": 65.0,
|
|
33
|
+
"value_quality_bonus": 2.4,
|
|
34
|
+
"capital_confirmed_momentum_min": 72.0,
|
|
35
|
+
"capital_confirmed_activity_min": 65.0,
|
|
36
|
+
"capital_confirmed_bonus": 1.8,
|
|
37
|
+
"controlled_reversal_min": 75.0,
|
|
38
|
+
"controlled_reversal_bonus": 1.2,
|
|
39
|
+
"hot_money_activity_min": 90.0,
|
|
40
|
+
"hot_money_stability_max": 45.0,
|
|
41
|
+
"hot_money_penalty": 2.5,
|
|
42
|
+
"volume_spike_ratio": 5.0,
|
|
43
|
+
"volume_spike_penalty": 1.2,
|
|
44
|
+
"high_llm_confidence": 0.75,
|
|
45
|
+
"high_llm_confidence_bonus": 0.8,
|
|
46
|
+
"low_llm_confidence": 0.40,
|
|
47
|
+
"low_llm_confidence_penalty": 1.0,
|
|
48
|
+
"catalyst_bonus": 0.5,
|
|
49
|
+
"catalyst_bonus_cap": 1.5,
|
|
50
|
+
"llm_risk_penalty": 0.8,
|
|
51
|
+
"llm_risk_penalty_cap": 2.4,
|
|
52
|
+
"score_delta_cap": 8.0,
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def normalize_post_analyzers(analyzers: list[str] | str | None) -> list[str]:
|
|
57
|
+
"""Normalize comma-separated/repeated analyzer names."""
|
|
58
|
+
if analyzers is None:
|
|
59
|
+
return []
|
|
60
|
+
raw_items: list[str]
|
|
61
|
+
if isinstance(analyzers, str):
|
|
62
|
+
raw_items = [analyzers]
|
|
63
|
+
else:
|
|
64
|
+
raw_items = list(analyzers)
|
|
65
|
+
|
|
66
|
+
result: list[str] = []
|
|
67
|
+
seen = set()
|
|
68
|
+
for raw in raw_items:
|
|
69
|
+
for item in str(raw).split(","):
|
|
70
|
+
name = item.strip().lower()
|
|
71
|
+
if not name or name in seen:
|
|
72
|
+
continue
|
|
73
|
+
if name not in SUPPORTED_POST_ANALYZERS:
|
|
74
|
+
raise ValueError(
|
|
75
|
+
f"Unknown post analyzer '{name}'. "
|
|
76
|
+
f"Supported: {', '.join(sorted(SUPPORTED_POST_ANALYZERS))}"
|
|
77
|
+
)
|
|
78
|
+
seen.add(name)
|
|
79
|
+
result.append(name)
|
|
80
|
+
return result
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def run_post_analyzers(
|
|
84
|
+
picks: list[Pick],
|
|
85
|
+
*,
|
|
86
|
+
analyzer_names: list[str],
|
|
87
|
+
run_id: str,
|
|
88
|
+
config: Config,
|
|
89
|
+
max_picks: int | None = None,
|
|
90
|
+
scorecard_profile: dict[str, object] | None = None,
|
|
91
|
+
) -> tuple[list[Pick], list[str]]:
|
|
92
|
+
"""Run selected post analyzers and re-rank by final_score."""
|
|
93
|
+
if not picks or not analyzer_names:
|
|
94
|
+
return picks, []
|
|
95
|
+
|
|
96
|
+
degradation: list[str] = []
|
|
97
|
+
result = picks
|
|
98
|
+
|
|
99
|
+
for analyzer in analyzer_names:
|
|
100
|
+
if analyzer == "dsa":
|
|
101
|
+
max_count = max_picks or config.post_analysis_max_picks
|
|
102
|
+
result, messages = _run_dsa_analyzer(result, run_id=run_id, config=config, max_picks=max_count)
|
|
103
|
+
elif analyzer == "scorecard":
|
|
104
|
+
max_count = max_picks or len(result)
|
|
105
|
+
result, messages = _run_scorecard_analyzer(
|
|
106
|
+
result,
|
|
107
|
+
max_picks=max_count,
|
|
108
|
+
profile=scorecard_profile,
|
|
109
|
+
)
|
|
110
|
+
elif analyzer == "external_http":
|
|
111
|
+
max_count = max_picks or config.post_analysis_max_picks
|
|
112
|
+
result, messages = _run_external_http_analyzer(result, run_id=run_id, config=config, max_picks=max_count)
|
|
113
|
+
else:
|
|
114
|
+
messages = [f"Unknown post analyzer skipped: {analyzer}"]
|
|
115
|
+
degradation.extend(messages)
|
|
116
|
+
|
|
117
|
+
result.sort(key=lambda item: item.final_score, reverse=True)
|
|
118
|
+
for i, pick in enumerate(result, start=1):
|
|
119
|
+
pick.rank = i
|
|
120
|
+
return result, degradation
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _run_dsa_analyzer(
|
|
124
|
+
picks: list[Pick],
|
|
125
|
+
*,
|
|
126
|
+
run_id: str,
|
|
127
|
+
config: Config,
|
|
128
|
+
max_picks: int,
|
|
129
|
+
) -> tuple[list[Pick], list[str]]:
|
|
130
|
+
if not config.dsa_api_url:
|
|
131
|
+
raise ValueError("post analyzer 'dsa' requested but DSA_API_URL is not configured")
|
|
132
|
+
|
|
133
|
+
before_scores = {pick.code: float(pick.final_score) for pick in picks}
|
|
134
|
+
analyzed, degradation = analyze_picks_with_dsa(
|
|
135
|
+
picks,
|
|
136
|
+
run_id=run_id,
|
|
137
|
+
api_url=config.dsa_api_url,
|
|
138
|
+
report_type=config.dsa_report_type,
|
|
139
|
+
max_picks=max_picks,
|
|
140
|
+
timeout_sec=config.dsa_timeout_sec,
|
|
141
|
+
force_refresh=config.dsa_force_refresh,
|
|
142
|
+
notify=config.dsa_notify,
|
|
143
|
+
)
|
|
144
|
+
analyzed = apply_dsa_overlay(analyzed)
|
|
145
|
+
for pick in analyzed:
|
|
146
|
+
status = pick.deep_analysis_status
|
|
147
|
+
summary = pick.deep_analysis_summary
|
|
148
|
+
delta = round(float(pick.final_score) - before_scores.get(pick.code, float(pick.final_score)), 4)
|
|
149
|
+
_record_post_result(
|
|
150
|
+
pick,
|
|
151
|
+
"dsa",
|
|
152
|
+
status=status,
|
|
153
|
+
summary=summary,
|
|
154
|
+
score_delta=delta,
|
|
155
|
+
payload=pick.deep_analysis_result or {},
|
|
156
|
+
risk_flags=pick.deep_analysis_risk_flags,
|
|
157
|
+
tags=["dsa"] if status == "completed" else [],
|
|
158
|
+
)
|
|
159
|
+
return analyzed, degradation
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _run_scorecard_analyzer(
|
|
163
|
+
picks: list[Pick],
|
|
164
|
+
*,
|
|
165
|
+
max_picks: int,
|
|
166
|
+
profile: dict[str, object] | None = None,
|
|
167
|
+
) -> tuple[list[Pick], list[str]]:
|
|
168
|
+
for idx, pick in enumerate(picks):
|
|
169
|
+
if idx >= max_picks:
|
|
170
|
+
_record_post_result(pick, "scorecard", status="skipped", summary="", score_delta=0.0)
|
|
171
|
+
continue
|
|
172
|
+
delta, flags, tags, summary = _scorecard_delta(pick, profile=profile)
|
|
173
|
+
pick.final_score = round(float(pick.final_score) + delta, 4)
|
|
174
|
+
pick.risk_flags = _unique([*pick.risk_flags, *flags])
|
|
175
|
+
_record_post_result(
|
|
176
|
+
pick,
|
|
177
|
+
"scorecard",
|
|
178
|
+
status="completed",
|
|
179
|
+
summary=summary,
|
|
180
|
+
score_delta=delta,
|
|
181
|
+
payload={"risk_flags": flags, "tags": tags},
|
|
182
|
+
risk_flags=flags,
|
|
183
|
+
tags=tags,
|
|
184
|
+
)
|
|
185
|
+
return picks, []
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _run_external_http_analyzer(
|
|
189
|
+
picks: list[Pick],
|
|
190
|
+
*,
|
|
191
|
+
run_id: str,
|
|
192
|
+
config: Config,
|
|
193
|
+
max_picks: int,
|
|
194
|
+
) -> tuple[list[Pick], list[str]]:
|
|
195
|
+
if not config.post_analyzer_url:
|
|
196
|
+
raise ValueError("post analyzer 'external_http' requested but POST_ANALYZER_URL is not configured")
|
|
197
|
+
|
|
198
|
+
candidates = [asdict(pick) for pick in picks[:max_picks]]
|
|
199
|
+
response = requests.post(
|
|
200
|
+
config.post_analyzer_url,
|
|
201
|
+
json={"run_id": run_id, "candidates": candidates},
|
|
202
|
+
timeout=config.post_analyzer_timeout_sec,
|
|
203
|
+
)
|
|
204
|
+
response.raise_for_status()
|
|
205
|
+
body = response.json()
|
|
206
|
+
if isinstance(body, list):
|
|
207
|
+
items = body
|
|
208
|
+
elif isinstance(body, dict):
|
|
209
|
+
items = body.get("ranked", [])
|
|
210
|
+
else:
|
|
211
|
+
items = []
|
|
212
|
+
if not isinstance(items, list):
|
|
213
|
+
raise ValueError("external_http analyzer response must be a list or contain ranked=list")
|
|
214
|
+
|
|
215
|
+
by_code = {_normalize_code(pick.code): pick for pick in picks}
|
|
216
|
+
for item in items:
|
|
217
|
+
if not isinstance(item, dict):
|
|
218
|
+
continue
|
|
219
|
+
code = _normalize_code(item.get("code", ""))
|
|
220
|
+
pick = by_code.get(code)
|
|
221
|
+
if pick is None:
|
|
222
|
+
continue
|
|
223
|
+
delta = _safe_float(item.get("score_delta"), 0.0)
|
|
224
|
+
summary = str(item.get("summary", "")).strip()
|
|
225
|
+
risk_flags = _safe_string_list(item.get("risk_flags"))
|
|
226
|
+
tags = _safe_string_list(item.get("tags"))
|
|
227
|
+
pick.final_score = round(float(pick.final_score) + delta, 4)
|
|
228
|
+
pick.risk_flags = _unique([*pick.risk_flags, *risk_flags])
|
|
229
|
+
_record_post_result(
|
|
230
|
+
pick,
|
|
231
|
+
"external_http",
|
|
232
|
+
status="completed",
|
|
233
|
+
summary=summary,
|
|
234
|
+
score_delta=delta,
|
|
235
|
+
payload=item,
|
|
236
|
+
risk_flags=risk_flags,
|
|
237
|
+
tags=tags,
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
for pick in picks[max_picks:]:
|
|
241
|
+
_record_post_result(pick, "external_http", status="skipped", summary="", score_delta=0.0)
|
|
242
|
+
return picks, []
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def _scorecard_delta(
|
|
246
|
+
pick: Pick,
|
|
247
|
+
*,
|
|
248
|
+
profile: dict[str, object] | None = None,
|
|
249
|
+
) -> tuple[float, list[str], list[str], str]:
|
|
250
|
+
profile = _scorecard_profile(profile)
|
|
251
|
+
factors = pick.factor_scores or {}
|
|
252
|
+
value = float(factors.get("value", 50))
|
|
253
|
+
stability = float(factors.get("stability", 50))
|
|
254
|
+
momentum = float(factors.get("momentum", 50))
|
|
255
|
+
activity = float(factors.get("activity", 50))
|
|
256
|
+
reversal = float(factors.get("reversal", 50))
|
|
257
|
+
|
|
258
|
+
delta = 0.0
|
|
259
|
+
flags: list[str] = []
|
|
260
|
+
tags: list[str] = []
|
|
261
|
+
|
|
262
|
+
if value >= profile["value_quality_value_min"] and stability >= profile["value_quality_stability_min"]:
|
|
263
|
+
delta += profile["value_quality_bonus"]
|
|
264
|
+
tags.append("value_quality")
|
|
265
|
+
if momentum >= profile["capital_confirmed_momentum_min"] and activity >= profile["capital_confirmed_activity_min"]:
|
|
266
|
+
delta += profile["capital_confirmed_bonus"]
|
|
267
|
+
tags.append("capital_confirmed")
|
|
268
|
+
if reversal >= profile["controlled_reversal_min"] and pick.change_pct < 0:
|
|
269
|
+
delta += profile["controlled_reversal_bonus"]
|
|
270
|
+
tags.append("controlled_reversal")
|
|
271
|
+
if activity >= profile["hot_money_activity_min"] and stability < profile["hot_money_stability_max"]:
|
|
272
|
+
delta -= profile["hot_money_penalty"]
|
|
273
|
+
flags.append("hot_money_instability")
|
|
274
|
+
if pick.volume_ratio is not None and pick.volume_ratio >= profile["volume_spike_ratio"]:
|
|
275
|
+
delta -= profile["volume_spike_penalty"]
|
|
276
|
+
flags.append("volume_spike")
|
|
277
|
+
if pick.llm_confidence is not None:
|
|
278
|
+
if pick.llm_confidence >= profile["high_llm_confidence"]:
|
|
279
|
+
delta += profile["high_llm_confidence_bonus"]
|
|
280
|
+
elif pick.llm_confidence < profile["low_llm_confidence"]:
|
|
281
|
+
delta -= profile["low_llm_confidence_penalty"]
|
|
282
|
+
flags.append("low_llm_confidence")
|
|
283
|
+
if pick.llm_catalysts:
|
|
284
|
+
delta += min(len(pick.llm_catalysts) * profile["catalyst_bonus"], profile["catalyst_bonus_cap"])
|
|
285
|
+
if pick.llm_risks:
|
|
286
|
+
delta -= min(len(pick.llm_risks) * profile["llm_risk_penalty"], profile["llm_risk_penalty_cap"])
|
|
287
|
+
flags.extend(pick.llm_risks)
|
|
288
|
+
|
|
289
|
+
cap = max(float(profile["score_delta_cap"]), 0.0)
|
|
290
|
+
delta = round(max(min(delta, cap), -cap), 4)
|
|
291
|
+
summary = "本地后置评分: " + (
|
|
292
|
+
"、".join(tags) if tags else "未发现额外加分项"
|
|
293
|
+
)
|
|
294
|
+
if flags:
|
|
295
|
+
summary += f";风险: {'、'.join(flags[:3])}"
|
|
296
|
+
return delta, _unique(flags), _unique(tags), summary
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def _scorecard_profile(profile: dict[str, object] | None) -> dict[str, float]:
|
|
300
|
+
result = dict(_DEFAULT_SCORECARD_PROFILE)
|
|
301
|
+
for key, value in (profile or {}).items():
|
|
302
|
+
if key in result:
|
|
303
|
+
result[key] = float(value)
|
|
304
|
+
return result
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def _record_post_result(
|
|
308
|
+
pick: Pick,
|
|
309
|
+
analyzer: str,
|
|
310
|
+
*,
|
|
311
|
+
status: str,
|
|
312
|
+
summary: str,
|
|
313
|
+
score_delta: float,
|
|
314
|
+
payload: dict | None = None,
|
|
315
|
+
risk_flags: list[str] | None = None,
|
|
316
|
+
tags: list[str] | None = None,
|
|
317
|
+
) -> None:
|
|
318
|
+
pick.post_analysis_status[analyzer] = status
|
|
319
|
+
pick.post_analysis_summaries[analyzer] = summary
|
|
320
|
+
pick.post_analysis_score_deltas[analyzer] = round(float(score_delta), 4)
|
|
321
|
+
if payload is not None:
|
|
322
|
+
pick.post_analysis_results[analyzer] = payload
|
|
323
|
+
if risk_flags:
|
|
324
|
+
pick.risk_flags = _unique([*pick.risk_flags, *risk_flags])
|
|
325
|
+
if tags:
|
|
326
|
+
pick.post_analysis_tags = _unique([*pick.post_analysis_tags, *tags])
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def _unique(items: list[str]) -> list[str]:
|
|
330
|
+
seen = set()
|
|
331
|
+
result = []
|
|
332
|
+
for item in items:
|
|
333
|
+
key = str(item).strip()
|
|
334
|
+
if key and key not in seen:
|
|
335
|
+
seen.add(key)
|
|
336
|
+
result.append(key)
|
|
337
|
+
return result
|