Add multi-factor conviction gate to activation
Make "qualified" mean an edge candidate, not just R:R + confidence. The gate now also requires (all admin-configurable, defaults on): - high conviction: recommended_action LONG_HIGH / SHORT_HIGH only - clean read: risk_level Low (no contradicting signals) - probable primary target: best target probability >= min (default 60) - Shared predicate: app/services/qualification.py + frontend/src/lib/qualification.ts (mirrored) - Activation config extended (min_target_probability, require_high_conviction, exclude_conflicts) with bool-aware get/update + validation - /trades/performance switched to ?qualified_only=true, applying the full gate server-side; confidence breakdown stays unfiltered - Dashboard "Qualified", Signals "Qualified only" toggle, and Track Record all use the one gate; Admin gains the new controls Sentiment provider runtime config (prior change) included. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -14,6 +14,8 @@ from app.schemas.admin import (
|
||||
DataCleanupRequest,
|
||||
JobToggle,
|
||||
RecommendationConfigUpdate,
|
||||
SentimentConfigUpdate,
|
||||
SentimentTestRequest,
|
||||
PasswordReset,
|
||||
RegistrationToggle,
|
||||
SystemSettingUpdate,
|
||||
@@ -22,6 +24,7 @@ from app.schemas.admin import (
|
||||
)
|
||||
from app.schemas.common import APIEnvelope
|
||||
from app.services import admin_service
|
||||
from app.services import sentiment_provider_service
|
||||
from app.services import ticker_universe_service
|
||||
|
||||
router = APIRouter(tags=["admin"])
|
||||
@@ -171,6 +174,41 @@ async def update_activation_settings(
|
||||
return APIEnvelope(status="success", data=updated)
|
||||
|
||||
|
||||
@router.get("/admin/settings/sentiment", response_model=APIEnvelope)
|
||||
async def get_sentiment_settings(
|
||||
_admin: User = Depends(require_admin),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
config = await sentiment_provider_service.get_sentiment_config(db)
|
||||
return APIEnvelope(status="success", data=config)
|
||||
|
||||
|
||||
@router.put("/admin/settings/sentiment", response_model=APIEnvelope)
|
||||
async def update_sentiment_settings(
|
||||
body: SentimentConfigUpdate,
|
||||
_admin: User = Depends(require_admin),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
config = await sentiment_provider_service.update_sentiment_config(
|
||||
db,
|
||||
provider=body.provider,
|
||||
model=body.model,
|
||||
api_key=body.api_key,
|
||||
)
|
||||
return APIEnvelope(status="success", data=config)
|
||||
|
||||
|
||||
@router.post("/admin/settings/sentiment/test", response_model=APIEnvelope)
|
||||
async def test_sentiment_settings(
|
||||
body: SentimentTestRequest,
|
||||
_admin: User = Depends(require_admin),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
"""Live credentials check: fetch sentiment for one ticker with current config."""
|
||||
result = await sentiment_provider_service.test_sentiment_provider(db, body.ticker)
|
||||
return APIEnvelope(status="success", data=result)
|
||||
|
||||
|
||||
@router.get("/admin/settings/ticker-universe", response_model=APIEnvelope)
|
||||
async def get_ticker_universe_setting(
|
||||
_admin: User = Depends(require_admin),
|
||||
|
||||
@@ -23,8 +23,8 @@ from app.models.ticker import Ticker
|
||||
from app.models.user import User
|
||||
from app.providers.alpaca import AlpacaOHLCVProvider
|
||||
from app.providers.fundamentals_chain import build_fundamental_provider_chain
|
||||
from app.providers.openai_sentiment import OpenAISentimentProvider
|
||||
from app.services.rr_scanner_service import scan_ticker
|
||||
from app.services.sentiment_provider_service import build_sentiment_provider
|
||||
from app.schemas.common import APIEnvelope
|
||||
from app.services import (
|
||||
fundamental_service,
|
||||
@@ -99,11 +99,14 @@ async def fetch_symbol(
|
||||
sources["ohlcv"] = {"status": "error", "records": 0, "message": str(exc)}
|
||||
|
||||
# --- Sentiment ---
|
||||
if settings.openai_api_key:
|
||||
try:
|
||||
sent_provider = await build_sentiment_provider(db)
|
||||
except ProviderError as exc:
|
||||
sent_provider = None
|
||||
sources["sentiment"] = {"status": "skipped", "message": str(exc)}
|
||||
|
||||
if sent_provider is not None:
|
||||
try:
|
||||
sent_provider = OpenAISentimentProvider(
|
||||
settings.openai_api_key, settings.openai_model
|
||||
)
|
||||
data = await sent_provider.fetch_sentiment(symbol_upper)
|
||||
await sentiment_service.store_sentiment(
|
||||
db,
|
||||
@@ -124,11 +127,6 @@ async def fetch_symbol(
|
||||
except Exception as exc:
|
||||
logger.error("Sentiment fetch failed for %s: %s", symbol_upper, exc)
|
||||
sources["sentiment"] = {"status": "error", "message": str(exc)}
|
||||
else:
|
||||
sources["sentiment"] = {
|
||||
"status": "skipped",
|
||||
"message": "OpenAI API key not configured",
|
||||
}
|
||||
|
||||
# --- Fundamentals ---
|
||||
if settings.fmp_api_key or settings.finnhub_api_key or settings.alpha_vantage_api_key:
|
||||
|
||||
@@ -67,9 +67,8 @@ async def get_activation_thresholds(
|
||||
|
||||
@router.get("/trades/performance", response_model=APIEnvelope)
|
||||
async def get_trade_performance(
|
||||
min_rr: float | None = Query(None, ge=0, description="Only setups with R:R >= this"),
|
||||
min_confidence: float | None = Query(
|
||||
None, ge=0, le=100, description="Only setups with confidence >= this"
|
||||
qualified_only: bool = Query(
|
||||
False, description="Restrict overall/direction/action stats to setups that clear the activation gate"
|
||||
),
|
||||
_user=Depends(require_access),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
@@ -78,11 +77,12 @@ async def get_trade_performance(
|
||||
|
||||
Outcomes are written by the nightly outcome_evaluator job (win = target
|
||||
hit first, loss = stop hit first, expired = neither within the window).
|
||||
Optional min_rr / min_confidence filters apply to the overall, direction
|
||||
and action breakdowns; the confidence breakdown always covers all setups
|
||||
so thresholds can be validated against it.
|
||||
With qualified_only, the overall/direction/action breakdowns cover only
|
||||
setups clearing the activation gate; the confidence breakdown always
|
||||
covers all setups so the gate can be validated against it.
|
||||
"""
|
||||
stats = await get_performance_stats(db, min_rr=min_rr, min_confidence=min_confidence)
|
||||
config = await admin_service.get_activation_config(db) if qualified_only else None
|
||||
stats = await get_performance_stats(db, config=config)
|
||||
return APIEnvelope(status="success", data=stats)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user