diff --git a/app/services/outcome_service.py b/app/services/outcome_service.py index 42460d0..5bb6a79 100644 --- a/app/services/outcome_service.py +++ b/app/services/outcome_service.py @@ -16,7 +16,7 @@ from __future__ import annotations import logging from dataclasses import dataclass -from datetime import date, datetime, timezone +from datetime import date, datetime, timedelta, timezone from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession @@ -34,6 +34,13 @@ OUTCOME_EXPIRED = "expired" DEFAULT_MAX_BARS = 30 +# A setup's outcome is only unbiased once its full evaluation window has elapsed: +# until then, near stops resolve as losses within days while far targets are still +# pending, so a young sample skews sharply negative. Only count setups detected at +# least this many CALENDAR days ago (~max_bars trading days, ×1.5 to cover +# weekends/holidays). Younger setups are reported separately as "maturing". +_MATURITY_DAYS = int(DEFAULT_MAX_BARS * 1.5) + # Confidence buckets for the performance breakdown _CONFIDENCE_BUCKETS = [ ("<50%", 0.0, 50.0), @@ -183,7 +190,12 @@ async def get_performance_stats( db: AsyncSession, config: dict | None = None, ) -> dict: - """Aggregate outcome statistics over all evaluated trade setups. + """Aggregate outcome statistics over the *matured* evaluated trade setups. + + Only setups whose full evaluation window has elapsed (see ``_MATURITY_DAYS``) + are counted, so the headline isn't dominated by quick stop-outs while slower + winners are still in flight. ``maturing`` reports how many are excluded for + being too young. 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 @@ -197,13 +209,23 @@ async def get_performance_stats( result = await db.execute( select(TradeSetup).where(TradeSetup.actual_outcome.is_not(None)) ) - evaluated = list(result.scalars().all()) + evaluated_all = list(result.scalars().all()) + + # Matured cohort only — see _MATURITY_DAYS. Setups whose window hasn't fully + # elapsed are excluded so quick stop-outs can't drag the headline negative + # while their slower-to-resolve winners are still in flight. + cutoff_date = (datetime.now(timezone.utc) - timedelta(days=_MATURITY_DAYS)).date() + evaluated = [s for s in evaluated_all if s.detected_at.date() <= cutoff_date] pending_result = await db.execute( select(TradeSetup.id).where(TradeSetup.actual_outcome.is_(None)) ) pending_count = len(pending_result.scalars().all()) + # Still inside their measurement window (excluded above so they can't bias the + # stats): young setups that already resolved + everything still pending. + maturing_count = (len(evaluated_all) - len(evaluated)) + pending_count + if config is not None: qualified = [s for s in evaluated if setup_qualifies(s, config)] else: @@ -229,6 +251,7 @@ async def get_performance_stats( return { "overall": _bucket_stats(qualified), "pending": pending_count, + "maturing": maturing_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())}, "by_confidence": { diff --git a/frontend/src/components/signals/TrackRecordPanel.tsx b/frontend/src/components/signals/TrackRecordPanel.tsx index a536cc8..719a8ec 100644 --- a/frontend/src/components/signals/TrackRecordPanel.tsx +++ b/frontend/src/components/signals/TrackRecordPanel.tsx @@ -171,7 +171,10 @@ export function TrackRecordPanel() { neither level hit within 30 trading days expire at 0R. Avg R is the expectancy per trade: wins earn their R:R ratio, losses cost −1R — a positive value means the signals have been profitable on a risk-adjusted basis. The - evaluator runs nightly after OHLCV collection. + evaluator runs nightly after OHLCV collection. Only setups whose full 30-day window has + elapsed are counted — younger ones show as maturing, + since near stops resolve in days while far targets need time, so early numbers would skew + negative.