ef523474ad
The 5-year backtest confirmed the EV gate adds negative value (high threshold = worst expectancy) and that 12-1 month momentum is the one price signal with a plausible, right-signed cross-sectional IC (~0.05). So "qualified" now means: clears the R:R + confidence floors AND the ticker ranks in the top `min_momentum_percentile` of the universe by 12-1 momentum that week. - qualification.py: drop expected_value_r / the EV gate; add a momentum-percentile gate (duck-typed `momentum_percentile`, only enforced when attached + threshold set, else defers to floors). Mirrored in frontend qualification.ts. - activation config/schema: min_expected_value -> min_momentum_percentile (default 80 = top quintile). ActivationSettings, DashboardPage (ranks/【shows】 momentum instead of EV), and the BacktestPanel sweep follow. - backtest: rank each ISO week's universe by 12-1 momentum, assign a percentile, and qualify the top slice; the sweep now sweeps the percentile cutoff. Also offload the backtest's per-ticker compute to a worker thread so the heavy ~5y run no longer blocks the API event loop (the "backend offline" flicker). Production setups don't carry momentum_percentile yet — wiring the scanner to attach it (a universe momentum-rank step) is the next step; until then the live gate defers to floors while the backtest measures the momentum selection. 330 backend tests pass; frontend build clean. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
78 lines
3.0 KiB
Python
78 lines
3.0 KiB
Python
"""Unit tests for activation threshold configuration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.exceptions import ValidationError
|
|
from app.services.admin_service import (
|
|
get_activation_config,
|
|
update_activation_config,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
async def session() -> AsyncSession:
|
|
"""DB session compatible with services that commit."""
|
|
from tests.conftest import _test_session_factory
|
|
|
|
async with _test_session_factory() as session:
|
|
yield session
|
|
|
|
|
|
class TestActivationConfig:
|
|
async def test_defaults_when_unset(self, session: AsyncSession):
|
|
config = await get_activation_config(session)
|
|
assert config == {
|
|
"min_momentum_percentile": 80.0,
|
|
"min_rr": 1.2,
|
|
"min_confidence": 55.0,
|
|
"min_target_probability": 0.0,
|
|
"require_high_conviction": False,
|
|
"exclude_conflicts": False,
|
|
}
|
|
|
|
async def test_update_and_read_back(self, session: AsyncSession):
|
|
updated = await update_activation_config(
|
|
session, {"min_momentum_percentile": 70.0, "min_confidence": 60.0}
|
|
)
|
|
assert updated["min_momentum_percentile"] == 70.0
|
|
assert updated["min_confidence"] == 60.0
|
|
|
|
config = await get_activation_config(session)
|
|
assert config["min_momentum_percentile"] == 70.0
|
|
assert config["min_confidence"] == 60.0
|
|
|
|
async def test_partial_update_keeps_other_value(self, session: AsyncSession):
|
|
await update_activation_config(session, {"min_confidence": 80.0})
|
|
config = await get_activation_config(session)
|
|
assert config["min_rr"] == 1.2 # default untouched
|
|
assert config["min_confidence"] == 80.0
|
|
|
|
async def test_rejects_out_of_range_momentum_percentile(self, session: AsyncSession):
|
|
with pytest.raises(ValidationError):
|
|
await update_activation_config(session, {"min_momentum_percentile": 150.0})
|
|
|
|
async def test_conviction_flags_round_trip(self, session: AsyncSession):
|
|
await update_activation_config(
|
|
session,
|
|
{"require_high_conviction": False, "exclude_conflicts": False, "min_target_probability": 45.0},
|
|
)
|
|
config = await get_activation_config(session)
|
|
assert config["require_high_conviction"] is False
|
|
assert config["exclude_conflicts"] is False
|
|
assert config["min_target_probability"] == 45.0
|
|
|
|
async def test_rejects_negative_rr(self, session: AsyncSession):
|
|
with pytest.raises(ValidationError):
|
|
await update_activation_config(session, {"min_rr": -1.0})
|
|
|
|
async def test_rejects_out_of_range_confidence(self, session: AsyncSession):
|
|
with pytest.raises(ValidationError):
|
|
await update_activation_config(session, {"min_confidence": 120.0})
|
|
|
|
async def test_rejects_out_of_range_target_probability(self, session: AsyncSession):
|
|
with pytest.raises(ValidationError):
|
|
await update_activation_config(session, {"min_target_probability": 150.0})
|