add backtest harness (Phase 1): historical replay + hit-rate & calibration reports
Replays the price-derived engine over stored OHLCV: at each weekly as-of date, rebuild the setup from bars <= D (no lookahead) and walk the actual forward bars for the realized outcome. Reports realized hit-rate/expectancy of qualified setups (and all setups, by direction) plus a probability calibration curve (predicted target prob vs realized hit rate). Reuses pure functions throughout; extracted compute_technical_from_arrays / compute_momentum_from_closes from scoring_service so live and backtest stay in sync. Runs as a weekly/triggerable 'backtest' job caching the report in a SystemSetting; GET /backtest/report serves it. Sentiment/fundamentals held neutral (no point-in-time history) — calibrates the price/S-R/probability machinery. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -88,11 +88,28 @@ async def _save_weights(db: AsyncSession, weights: dict[str, float]) -> None:
|
||||
async def _compute_technical_score(
|
||||
db: AsyncSession, symbol: str
|
||||
) -> tuple[float | None, dict | None]:
|
||||
"""Compute technical dimension score from ADX, EMA, RSI, EMA Cross,
|
||||
Volume Profile and Pivot Points.
|
||||
"""Compute technical dimension score from stored OHLCV (DB wrapper)."""
|
||||
from app.services.indicator_service import _extract_ohlcv
|
||||
from app.services.price_service import query_ohlcv
|
||||
|
||||
Returns (score, breakdown) where breakdown follows the ScoreBreakdown
|
||||
TypedDict shape: {sub_scores, formula, unavailable}.
|
||||
records = await query_ohlcv(db, symbol)
|
||||
if not records:
|
||||
return None, None
|
||||
|
||||
_, highs, lows, closes, volumes = _extract_ohlcv(records)
|
||||
return compute_technical_from_arrays(highs, lows, closes, volumes)
|
||||
|
||||
|
||||
def compute_technical_from_arrays(
|
||||
highs: list[float],
|
||||
lows: list[float],
|
||||
closes: list[float],
|
||||
volumes: list[int],
|
||||
) -> tuple[float | None, dict | None]:
|
||||
"""Technical score from raw OHLCV arrays — ADX, EMA, RSI, EMA Cross, Volume
|
||||
Profile, Pivot Points. Pure (no DB) so the backtest can compute it as-of-date.
|
||||
|
||||
Returns (score, breakdown).
|
||||
"""
|
||||
from app.services.indicator_service import (
|
||||
compute_adx,
|
||||
@@ -101,16 +118,11 @@ async def _compute_technical_score(
|
||||
compute_pivot_points,
|
||||
compute_rsi,
|
||||
compute_volume_profile,
|
||||
_extract_ohlcv,
|
||||
)
|
||||
from app.services.price_service import query_ohlcv
|
||||
|
||||
records = await query_ohlcv(db, symbol)
|
||||
if not records:
|
||||
if not closes:
|
||||
return None, None
|
||||
|
||||
_, highs, lows, closes, volumes = _extract_ohlcv(records)
|
||||
|
||||
formula = (
|
||||
"Weighted average: 0.30*ADX + 0.20*EMA + 0.20*RSI + 0.15*EMA_Cross "
|
||||
"+ 0.10*Volume_Profile + 0.05*Pivot_Points, re-normalized if any "
|
||||
@@ -514,13 +526,21 @@ async def _compute_momentum_score(
|
||||
"""
|
||||
from app.services.price_service import query_ohlcv
|
||||
|
||||
formula = "Weighted average: 0.5 * ROC_5 + 0.5 * ROC_20, re-normalized if any sub-score unavailable."
|
||||
|
||||
records = await query_ohlcv(db, symbol)
|
||||
if not records or len(records) < 6:
|
||||
return None, None
|
||||
|
||||
closes = [float(r.close) for r in records]
|
||||
return compute_momentum_from_closes(closes)
|
||||
|
||||
|
||||
def compute_momentum_from_closes(closes: list[float]) -> tuple[float | None, dict | None]:
|
||||
"""Momentum score (5- and 20-day ROC) from a close series. Pure (no DB)."""
|
||||
formula = "Weighted average: 0.5 * ROC_5 + 0.5 * ROC_20, re-normalized if any sub-score unavailable."
|
||||
|
||||
if not closes or len(closes) < 6:
|
||||
return None, None
|
||||
|
||||
latest = closes[-1]
|
||||
|
||||
scores: list[tuple[float, float]] = [] # (weight, score)
|
||||
|
||||
Reference in New Issue
Block a user