Add activation thresholds: qualified-signal defaults and views
Admin-configurable thresholds (min R:R, default 2.0; min confidence, default 70%) defining what counts as an actionable signal: - Admin Settings: new Activation Thresholds panel (GET/PUT /admin/settings/activation) - GET /trades/activation exposes values to all users with access - Signals/Setups: filters initialize from activation values - Track Record: "Qualified signals only" toggle (default on) via min_rr/min_confidence params on /trades/performance; the confidence breakdown always covers the full population so the thresholds can be validated against outcomes - Dashboard: "Qualified" metric and qualified-first Top Setups - Outcome evaluator unchanged: every setup is still evaluated Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -31,6 +31,16 @@ RECOMMENDATION_CONFIG_DEFAULTS: dict[str, float] = {
|
||||
DEFAULT_TICKER_UNIVERSE = "sp500"
|
||||
SUPPORTED_TICKER_UNIVERSES = {"sp500", "nasdaq100", "nasdaq_all"}
|
||||
|
||||
# Activation thresholds: what counts as a signal worth acting on.
|
||||
# Used as Signals-page default filters, the Dashboard's qualified-setup
|
||||
# metrics, and the Track Record's "qualified only" view. The outcome
|
||||
# evaluator deliberately ignores these — every setup gets evaluated so the
|
||||
# thresholds themselves can be validated against outcomes.
|
||||
ACTIVATION_DEFAULTS: dict[str, float] = {
|
||||
"activation_min_rr": 2.0,
|
||||
"activation_min_confidence": 70.0,
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# User management
|
||||
@@ -143,6 +153,48 @@ async def update_setting(db: AsyncSession, key: str, value: str) -> SystemSettin
|
||||
return setting
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Activation thresholds
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
async def get_activation_config(db: AsyncSession) -> dict[str, float]:
|
||||
"""Return activation thresholds with public keys (min_rr, min_confidence)."""
|
||||
result = await db.execute(
|
||||
select(SystemSetting).where(SystemSetting.key.like("activation_%"))
|
||||
)
|
||||
config = dict(ACTIVATION_DEFAULTS)
|
||||
for setting in result.scalars().all():
|
||||
if setting.key in config:
|
||||
try:
|
||||
config[setting.key] = float(setting.value)
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
return {
|
||||
"min_rr": config["activation_min_rr"],
|
||||
"min_confidence": config["activation_min_confidence"],
|
||||
}
|
||||
|
||||
|
||||
async def update_activation_config(
|
||||
db: AsyncSession, updates: dict[str, float]
|
||||
) -> dict[str, float]:
|
||||
"""Update activation thresholds. Accepts public keys min_rr / min_confidence."""
|
||||
if "min_rr" in updates and updates["min_rr"] < 0:
|
||||
raise ValidationError("min_rr must be >= 0")
|
||||
if "min_confidence" in updates and not 0 <= updates["min_confidence"] <= 100:
|
||||
raise ValidationError("min_confidence must be between 0 and 100")
|
||||
|
||||
key_map = {
|
||||
"min_rr": "activation_min_rr",
|
||||
"min_confidence": "activation_min_confidence",
|
||||
}
|
||||
for public_key, storage_key in key_map.items():
|
||||
if public_key in updates:
|
||||
await update_setting(db, storage_key, str(float(updates[public_key])))
|
||||
|
||||
return await get_activation_config(db)
|
||||
|
||||
|
||||
def _recommendation_public_to_storage_key(key: str) -> str:
|
||||
return f"recommendation_{key}"
|
||||
|
||||
|
||||
@@ -178,12 +178,20 @@ def _confidence_bucket(score: float | None) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
async def get_performance_stats(db: AsyncSession) -> dict:
|
||||
async def get_performance_stats(
|
||||
db: AsyncSession,
|
||||
min_rr: float | None = None,
|
||||
min_confidence: float | None = None,
|
||||
) -> dict:
|
||||
"""Aggregate outcome statistics over all evaluated trade setups.
|
||||
|
||||
avg_r is the expectancy per trade in R-multiples (win = +rr_ratio,
|
||||
loss = -1R, expired = 0R). A positive avg_r means the signals have
|
||||
been profitable on a risk-adjusted basis.
|
||||
|
||||
min_rr / min_confidence filter the overall, direction and action
|
||||
breakdowns. The confidence breakdown deliberately stays unfiltered:
|
||||
it is the instrument for validating the thresholds themselves.
|
||||
"""
|
||||
result = await db.execute(
|
||||
select(TradeSetup).where(TradeSetup.actual_outcome.is_not(None))
|
||||
@@ -195,14 +203,26 @@ async def get_performance_stats(db: AsyncSession) -> dict:
|
||||
)
|
||||
pending_count = len(pending_result.scalars().all())
|
||||
|
||||
def qualifies(setup: TradeSetup) -> bool:
|
||||
if min_rr is not None and setup.rr_ratio < min_rr:
|
||||
return False
|
||||
if min_confidence is not None and (setup.confidence_score or 0.0) < min_confidence:
|
||||
return False
|
||||
return True
|
||||
|
||||
qualified = [s for s in evaluated if qualifies(s)]
|
||||
|
||||
by_direction: dict[str, list[TradeSetup]] = {}
|
||||
by_action: dict[str, list[TradeSetup]] = {}
|
||||
by_confidence: dict[str, list[TradeSetup]] = {}
|
||||
|
||||
for setup in evaluated:
|
||||
for setup in qualified:
|
||||
by_direction.setdefault(setup.direction, []).append(setup)
|
||||
action = setup.recommended_action or "NONE"
|
||||
by_action.setdefault(action, []).append(setup)
|
||||
|
||||
# Confidence buckets always cover the full evaluated population
|
||||
for setup in evaluated:
|
||||
bucket = _confidence_bucket(setup.confidence_score)
|
||||
if bucket is not None:
|
||||
by_confidence.setdefault(bucket, []).append(setup)
|
||||
@@ -210,7 +230,7 @@ async def get_performance_stats(db: AsyncSession) -> dict:
|
||||
bucket_order = [label for label, _, _ in _CONFIDENCE_BUCKETS]
|
||||
|
||||
return {
|
||||
"overall": _bucket_stats(evaluated),
|
||||
"overall": _bucket_stats(qualified),
|
||||
"pending": pending_count,
|
||||
"by_direction": {k: _bucket_stats(v) for k, v in sorted(by_direction.items())},
|
||||
"by_action": {k: _bucket_stats(v) for k, v in sorted(by_action.items())},
|
||||
|
||||
Reference in New Issue
Block a user