feat: ticker search, watchlist momentum column, alpha vs S&P 500
Three usability fixes: 1. Global ticker search in the sidebar (TickerSearch) — typeahead over the tracked universe that opens a ticker's detail page without adding it to the watchlist. Also wired into the mobile nav. 2. Watchlist table shows the ticker's 12-1 momentum percentile (the top-pick selector) instead of the noisy full S/R-level list. Enriched from the setup already loaded in watchlist_service._enrich_entry — no extra query. 3. Alpha vs the S&P 500 on paper trades (open + closed). New benchmark_prices table + benchmark_service store SPY daily closes (a standalone series, not a Ticker, so it never enters the scanner / momentum ranking / rankings) via a new daily-pipeline step. paper_trade_service computes per-trade benchmark_return / alpha_pct / alpha_usd over each holding period; the open- trades table, dashboard, and closed-trades panel surface per-trade and total alpha. The list read path never makes a provider call. Deploy: alembic upgrade head, then run the benchmark/daily job once to populate SPY closes (alpha shows "—" until then). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
"""Tests for benchmark return / alpha helper (pure, no DB)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
|
||||
import pytest
|
||||
|
||||
from app.services.benchmark_service import benchmark_return_pct
|
||||
|
||||
|
||||
def test_benchmark_return_basic():
|
||||
closes = {date(2026, 1, 2): 100.0, date(2026, 1, 5): 110.0}
|
||||
assert benchmark_return_pct(closes, date(2026, 1, 2), date(2026, 1, 5)) == pytest.approx(10.0)
|
||||
|
||||
|
||||
def test_benchmark_return_uses_nearest_prior_trading_day():
|
||||
# No bar on the 4th (weekend) → falls back to the 2nd; as-of the 12th → the 9th.
|
||||
closes = {date(2026, 1, 2): 100.0, date(2026, 1, 9): 120.0}
|
||||
assert benchmark_return_pct(closes, date(2026, 1, 4), date(2026, 1, 12)) == pytest.approx(20.0)
|
||||
|
||||
|
||||
def test_benchmark_return_none_when_empty():
|
||||
assert benchmark_return_pct({}, date(2026, 1, 2), date(2026, 1, 5)) is None
|
||||
|
||||
|
||||
def test_benchmark_return_none_when_open_before_history():
|
||||
closes = {date(2026, 1, 10): 100.0}
|
||||
assert benchmark_return_pct(closes, date(2026, 1, 2), date(2026, 1, 12)) is None
|
||||
@@ -7,7 +7,9 @@ from datetime import date, datetime, timedelta, timezone
|
||||
import pytest
|
||||
|
||||
from app.exceptions import ValidationError
|
||||
from app.models.benchmark_price import BenchmarkPrice
|
||||
from app.models.ohlcv import OHLCVRecord
|
||||
from app.models.paper_trade import PaperTrade
|
||||
from app.models.ticker import Ticker
|
||||
from app.models.user import User
|
||||
from app.services import paper_trade_service as svc
|
||||
@@ -124,3 +126,48 @@ async def test_resolve_leaves_open_when_neither_hit(session):
|
||||
assert closed == 0
|
||||
rows = await svc.list_trades(session, 1, status="open")
|
||||
assert len(rows) == 1
|
||||
|
||||
|
||||
async def _seed_benchmark(session, points: dict) -> None:
|
||||
for d, close in points.items():
|
||||
session.add(BenchmarkPrice(symbol="SPY", date=d, close=close))
|
||||
await session.commit()
|
||||
|
||||
|
||||
async def _add_open_trade(session, ticker_id: int, direction: str, *, entry: float,
|
||||
shares: float, days_ago: int) -> None:
|
||||
session.add(PaperTrade(
|
||||
user_id=1, ticker_id=ticker_id, direction=direction, entry_price=entry,
|
||||
shares=shares, stop_loss=entry * 0.95, target=entry * 1.2, status="open",
|
||||
opened_at=datetime.now(timezone.utc) - timedelta(days=days_ago),
|
||||
))
|
||||
await session.commit()
|
||||
|
||||
|
||||
async def test_alpha_long_open(session):
|
||||
tid = await _seed(session, "AAA", close=110.0) # current price 110 → +10% on a 100 entry
|
||||
today = date.today()
|
||||
await _seed_benchmark(session, {today - timedelta(days=10): 400.0, today: 420.0}) # SPY +5%
|
||||
await _add_open_trade(session, tid, "long", entry=100.0, shares=10, days_ago=10)
|
||||
|
||||
row = (await svc.list_trades(session, 1, status="open"))[0]
|
||||
assert row["benchmark_return_pct"] == pytest.approx(5.0)
|
||||
assert row["alpha_pct"] == pytest.approx(5.0) # +10% trade − 5% bench
|
||||
assert row["alpha_usd"] == pytest.approx(50.0) # 5% of 100*10
|
||||
|
||||
|
||||
async def test_alpha_short_and_missing_benchmark(session):
|
||||
tid = await _seed(session, "BBB", close=90.0) # price fell to 90 → short +10%
|
||||
today = date.today()
|
||||
await _add_open_trade(session, tid, "short", entry=100.0, shares=4, days_ago=10)
|
||||
|
||||
# No benchmark data yet → alpha unset, not an error.
|
||||
row = (await svc.list_trades(session, 1, status="open"))[0]
|
||||
assert row["alpha_pct"] is None
|
||||
assert row["benchmark_return_pct"] is None
|
||||
|
||||
# Flat benchmark → alpha equals the (direction-signed) trade return.
|
||||
await _seed_benchmark(session, {today - timedelta(days=10): 400.0, today: 400.0})
|
||||
row = (await svc.list_trades(session, 1, status="open"))[0]
|
||||
assert row["benchmark_return_pct"] == pytest.approx(0.0)
|
||||
assert row["alpha_pct"] == pytest.approx(10.0)
|
||||
|
||||
@@ -80,6 +80,7 @@ class TestConfigureScheduler:
|
||||
assert job_ids == {
|
||||
"data_collector",
|
||||
"data_backfill",
|
||||
"benchmark_collector",
|
||||
"sentiment_collector",
|
||||
"fundamental_collector",
|
||||
"rr_scanner",
|
||||
@@ -103,6 +104,7 @@ class TestConfigureScheduler:
|
||||
assert sorted(job_ids) == sorted([
|
||||
"alerts",
|
||||
"backtest",
|
||||
"benchmark_collector",
|
||||
"daily_pipeline",
|
||||
"intraday_pipeline",
|
||||
"data_collector",
|
||||
|
||||
@@ -60,6 +60,25 @@ async def _make_ticker(session, symbol: str, *, score: float | None = None) -> i
|
||||
return t.id
|
||||
|
||||
|
||||
async def test_enrich_includes_momentum_percentile(session):
|
||||
"""The watchlist row carries the ticker's momentum percentile (from its setup),
|
||||
which replaces the old S/R-levels column in the UI."""
|
||||
from app.models.trade_setup import TradeSetup
|
||||
|
||||
user_id = await _make_user(session)
|
||||
tid = await _make_ticker(session, "AAA", score=70.0)
|
||||
session.add(TradeSetup(
|
||||
ticker_id=tid, direction="long", entry_price=100.0, stop_loss=95.0,
|
||||
target=110.0, rr_ratio=2.0, composite_score=70.0,
|
||||
momentum_percentile=88.0, detected_at=datetime.now(timezone.utc),
|
||||
))
|
||||
await session.commit()
|
||||
|
||||
await add_manual_entry(session, user_id, "AAA")
|
||||
rows = await get_watchlist(session, user_id)
|
||||
assert rows[0]["momentum_percentile"] == 88.0
|
||||
|
||||
|
||||
async def test_add_and_remove_sticks(session):
|
||||
user_id = await _make_user(session)
|
||||
await _make_ticker(session, "AAA", score=80.0)
|
||||
|
||||
Reference in New Issue
Block a user