feat: wavelets

This commit is contained in:
OpenSquared
2026-07-19 14:57:25 +02:00
parent 457aa7b49e
commit b900d77aa0
3 changed files with 71 additions and 27 deletions

View File

@@ -120,20 +120,23 @@ def wavelet_reliability_endpoint(
step: int = Query(1, ge=1),
method: str = Query("cwt", description="cwt (default) or ssq"),
smooth_days: int = Query(3, ge=1, le=10, description="lag used to smooth the slope before flagging a sign-change as a turning point"),
tolerance_days: int = Query(5, ge=0, le=15, description="a hindsight reversal within this many days of the causal one still counts as confirming it"),
confirm_horizon: int = Query(10, ge=1, le=30, description="how many extra days of real data the hindsight recomputation gets"),
tolerance_pct: float = Query(0.10, ge=0, le=0.5, description="date-matching tolerance as a fraction of each band's own average cycle length, applied both sides"),
min_confirm_horizon: int = Query(3, ge=1, le=30, description="floor on the hindsight horizon in days, in case a band's measured cycle length comes out very short"),
max_future_padding: int = Query(60, ge=10, le=180, description="extra real days fetched beyond the requested range, to cover the slowest band's own (data-driven) confirm horizon"),
):
"""For every reversal a live (causal, walk-forward) decomposition would have flagged,
checks whether redoing the decomposition `confirm_horizon` days later still shows the
same reversal — a per-band reliability score for the wavelet's turning-point signals."""
checks whether redoing the decomposition later still shows the same reversal — a
per-band reliability score. The hindsight horizon is dynamic per band (each band's own
historical average peak-to-peak cycle length, not one fixed day count for every band —
a fixed horizon is meaningless across bands with wildly different natural periods)."""
from services.wavelet_engine import wavelet_reliability
# Needs confirm_horizon extra real days beyond the requested causal output range, on
# top of the usual lookback padding, so the most recent testable turning points aren't
# silently dropped for lack of "future" data.
values, dates, out_days = _fetch_padded_history(symbol, period, lookback + confirm_horizon)
if len(values) < lookback + confirm_horizon + 32:
raise HTTPException(400, "Historique insuffisant pour un test de fiabilité (32 points minimum au-delà de la fenêtre + horizon).")
# The horizon is now computed per band inside wavelet_reliability (from each band's own
# measured cycle length), so we don't know it in advance here — pad generously enough to
# cover even a slow band's cycle instead.
values, dates, out_days = _fetch_padded_history(symbol, period, lookback + max_future_padding)
if len(values) < lookback + max_future_padding + 32:
raise HTTPException(400, "Historique insuffisant pour un test de fiabilité (32 points minimum au-delà de la fenêtre + marge).")
cutoff = (datetime.utcnow() - timedelta(days=out_days)).date().isoformat()
start_idx = next((i for i, d in enumerate(dates) if d[:10] >= cutoff), None)
@@ -145,7 +148,7 @@ def wavelet_reliability_endpoint(
values, dates,
start_idx=start_idx, lookback=lookback,
num_levels=levels, wavelet=wavelet, method=method, step=step,
smooth_days=smooth_days, tolerance_days=tolerance_days, confirm_horizon=confirm_horizon,
smooth_days=smooth_days, tolerance_pct=tolerance_pct, min_confirm_horizon=min_confirm_horizon,
)
except ValueError as exc:
raise HTTPException(400, str(exc)) from exc