Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dashboard/backend/api/routers/backtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3798,8 +3798,8 @@ class BacktestRunRequest(BaseModel):
# params or a JSON body, so validation runs on the merged effective values in the
# handler rather than only on the Pydantic body.
MAX_STRATEGY_PROMPT_CHARS = 4000
# Two weeks, matching the fortnight the Live Trading Leaderboard's seasons use
# so the product has one window vocabulary. It was 31, set when the fixed
# Two weeks, chosen to match the two-week seasons the Live Leaderboard had
# then (seasons are calendar months now). It was 31, set when the fixed
# parent budget was the only bound: a 31-day window is ~22 weekdays x ~7 hourly
# bars = ~154 decision bars, and a pipeline multiplies that by its step count,
# so the UI permitted a window roughly 3x the modal default against a constant
Expand Down
4 changes: 2 additions & 2 deletions dashboard/backend/api/routers/leaderboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def api_get_leaderboard(
Pass ?refresh=true to recompute (e.g. after config change); operators only,
with X-Leaderboard-Refresh-Secret or an admin session, else 403.
Pass ?period=daily for the rolling one-day board (weekends show Friday).
Pass ?period=live for the current calendar-month Live Trading Leaderboard
Pass ?period=live for the current calendar-month Live Leaderboard
(freeze-window snapshots under leaderboard-live; GET never deploys models).
"""
# Public, unauthenticated endpoint: no exception text reaches the caller.
Expand Down Expand Up @@ -181,7 +181,7 @@ def api_refresh_live_leaderboard(
force: bool = Query(default=False, description="Replay the whole month from the 1st."),
x_leaderboard_refresh_secret: str | None = Header(default=None, alias="X-Leaderboard-Refresh-Secret"),
):
"""Cron hook: enqueue a Live Trading Leaderboard increment (non-blocking).
"""Cron hook: enqueue a Live Leaderboard increment (non-blocking).

Uses the same ``LEADERBOARD_DAILY_REFRESH_SECRET`` as the daily board.
Returns **202 Accepted**; model steps run in a background thread. Public GET
Expand Down
10 changes: 5 additions & 5 deletions dashboard/backend/domain/leaderboard/live.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Calendar-month Live Trading Leaderboard.
"""Calendar-month Live Leaderboard.

This board is not Daily stretched to 30 days and not the fixed contest window.
One continuous paper-trading month per entry: the chart axis is the current
Expand Down Expand Up @@ -347,8 +347,8 @@ def live_freeze_config(
"end_date": freeze_end,
"reference_start_date": reference_start_date(month_start, None),
"period": "live",
"board_title": "Live Trading Leaderboard",
"phase_label": "Season 0",
"board_title": lb_service.LIVE_BOARD_TITLE,
"phase_label": lb_service.LIVE_PHASE_LABEL,
"standings_label": "Performance",
}

Expand Down Expand Up @@ -1939,8 +1939,8 @@ def get_live_leaderboard(

return {
"period": "live",
"board_title": "Live Trading Leaderboard",
"phase_label": "Season 0",
"board_title": lb_service.LIVE_BOARD_TITLE,
"phase_label": lb_service.LIVE_PHASE_LABEL,
"standings_label": "Performance",
"window": {
"start_date": start_date,
Expand Down
19 changes: 15 additions & 4 deletions dashboard/backend/domain/leaderboard/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,17 @@ def verify_daily_refresh_secret(provided: Optional[str]) -> None:
# explicitly the one whose results nobody should read as a standing. Mirrors
# PREVIEW_SEASON_NUMBER in dashboard/frontend/js/leaderboard.js.
PREVIEW_SEASON_NUMBER = 0
# The Live board's name and season label have exactly one owner: this pair.
# resolve_leaderboard_config("live"), live.live_freeze_config and the payload
# live.get_live_leaderboard serves all read them, and the static fallbacks in
# app.html / js/live-leaderboard.js are pinned to them by
# tests/test_frontend_live_leaderboard.py. "Trading" is dropped from the name
# because it read as real orders and collided with the Robinhood live-trading
# docs (docs/superpowers/specs/2026-10-14-atl-direction.md §6). The label is
# built from PREVIEW_SEASON_NUMBER so the number keeps one owner, and says
# "preview" until a season counts (Season 1 waits for the new engine).
LIVE_BOARD_TITLE = "Live Leaderboard"
LIVE_PHASE_LABEL = f"Season {PREVIEW_SEASON_NUMBER} · preview"
DEFAULT_SEASON_TRADING_DAYS = 10
# One calendar year of weekday sessions. An upper bound rather than a sanity
# check: `season_window` walks the calendar one day at a time and runs inside a
Expand Down Expand Up @@ -748,7 +759,7 @@ def build_season_payload(
config: Dict[str, Any],
as_of: Optional[Union[date, datetime]] = None,
) -> Dict[str, Any]:
"""The season block the Live Trading tab renders.
"""The season block the Live tab renders.

NOTHING HERE MAY CLAIM AN ADVANCE. ``last_advanced_date`` stays None and
``trading_days_elapsed`` stays 0 because no season has advanced -- there is
Expand Down Expand Up @@ -841,11 +852,11 @@ def resolve_leaderboard_config(period: Optional[str] = "contest") -> Dict[str, A
"ranking on this board counts. There is no advance engine yet."
),
"period": "live",
"board_title": "Live Trading Leaderboard",
"board_title": LIVE_BOARD_TITLE,
# Derived, never spelled out: a literal "Season 0" here is a second
# owner of the season number, and the first thing the advance engine
# does is bump PREVIEW_SEASON_NUMBER.
"phase_label": f"Season {PREVIEW_SEASON_NUMBER}",
# does is bump PREVIEW_SEASON_NUMBER, which LIVE_PHASE_LABEL is built from.
"phase_label": LIVE_PHASE_LABEL,
"standings_label": "Ranking",
}
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""NYSE full-day holidays and early closes, computed by rule (no data file,
no dependency).

Shared US market-data infrastructure: the Live Trading Leaderboard walks it
Shared US market-data infrastructure: the Live Leaderboard walks it
forward day by day, and the Run Backtest modal's default period
(``provider.default_backtest_window``) walks it back week by week. It lived
under ``domain/leaderboard/`` while the board was its only reader; a calendar
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
"""The legacy engine does not execute stop-loss or take-profit prices (D12).

The safe-trading prompt's JSON shape carries ``stop_loss_price`` and
``take_profit_price`` (infrastructure/llm/validator.py), so a model can write
them, but the single-prompt decision loop that both boards' LLM entries run
(``PortfolioManager.make_trading_decision_with_llm``, reached from
``LLMAgentStrategy``) copies only symbol, action, size, confidence and reasoning
out of each reply. Both boards say so in their Rules and About cards
(dashboard/frontend/app.html). These tests are the other half of that copy: the
day this path starts reading the fields, they fail, and the copy changes in the
same PR. The new core executes stops for real (direction spec 2026-10-14 §4);
when it lands, invert these tests and delete the four disclaimers together.
"""

import json
from datetime import datetime
from pathlib import Path

from dashboard.backend.domain.backtesting import portfolio_manager as pm
from dashboard.backend.domain.backtesting.portfolio_manager import PortfolioManager

_BACKEND = Path(__file__).resolve().parents[3]
_STOP_FIELDS = {"stop_loss_price": 240.0, "take_profit_price": 300.0}


class _Resp:
def __init__(self, text):
self._text = text
self.usage = None
self.content = []


def _script(monkeypatch, replies):
"""Answer each decision request with the next reply, in order."""
queue = [json.dumps(reply) for reply in replies]

def fake_request(client, **_kwargs):
return _Resp(queue.pop(0))

monkeypatch.setattr(pm, "_request_trading_decision", fake_request)
monkeypatch.setattr(pm, "_extract_response_text", lambda response: response._text)
monkeypatch.setattr(pm, "_extract_token_usage", lambda response: (100, 50))
return queue


def _state(manager, price, timestamp):
held = manager.positions.get("AAPL", 0)
positions = []
if held:
entry = manager.entry_prices.get("AAPL", price)
positions.append({
"symbol": "AAPL",
"shares": held,
"entry_price": entry,
"current_price": price,
"position_value": held * price,
"pnl_pct": (price / entry - 1.0) * 100.0,
})
return {
"timestamp": timestamp,
"cash": manager.cash,
"positions": positions,
"positions_value": held * price,
"total_equity": manager.cash + held * price,
"market_signals": {
"AAPL": {"price": price, "rsi": 50.0, "macd": 0.0,
"macd_signal": 0.0, "sma_20": price, "sma_50": price,
"bb_upper": price * 1.04, "bb_lower": price * 0.96,
"volume": 1000},
},
}


def _reply(action, **fields):
return {"actions": [{
"symbol": "AAPL",
"action": action,
"confidence": 0.8,
"reasoning": "scripted",
**_STOP_FIELDS,
**fields,
}]}


def _decide(manager, price, timestamp):
return manager.make_trading_decision_with_llm(
_state(manager, price, timestamp), llm_client=object(), model="m"
)


def test_stop_and_take_profit_fields_never_reach_the_executed_action(monkeypatch):
_script(monkeypatch, [_reply("buy", position_size=2)])
manager = PortfolioManager(initial_capital=1000.0, allowed_symbols=["AAPL"])

decision = _decide(manager, 250.0, datetime(2026, 4, 1, 14, 30))

assert manager.llm_decisions == 1, "the model drove this step"
assert len(decision["actions"]) == 1
action = decision["actions"][0]
assert set(action) == {"symbol", "action", "shares", "reason", "confidence"}
assert (action["symbol"], action["action"], action["shares"]) == ("AAPL", "buy", 2)


def test_a_held_position_is_not_sold_when_the_price_crosses_its_stop_or_target(monkeypatch):
remaining = _script(monkeypatch, [
_reply("buy", position_size=2),
_reply("hold"),
_reply("hold"),
])
manager = PortfolioManager(initial_capital=1000.0, allowed_symbols=["AAPL"])
opened = datetime(2026, 4, 1, 14, 30)
entry = _decide(manager, 250.0, opened)
manager.execute_actions(entry["actions"], {"AAPL": {"close": 250.0}}, opened)
# Each bar is decide, execute, then value the book, as llm_agent.py runs it.
manager.update_equity({"AAPL": {"close": 250.0}}, None, opened)
assert manager.positions == {"AAPL": 2}

# 200 is below the 240 stop; 320 is above the 300 target. Neither sells,
# and valuing the book at either price does not close the position either.
for hour, price in ((15, 200.0), (16, 320.0)):
timestamp = datetime(2026, 4, 1, hour, 30)
decision = _decide(manager, price, timestamp)
manager.execute_actions(decision["actions"], {"AAPL": {"close": price}}, timestamp)
manager.update_equity({"AAPL": {"close": price}}, None, timestamp)
assert decision["actions"] == []
assert manager.positions == {"AAPL": 2}

assert [row["positions_value"] for row in manager.get_equity_curve()] == [500.0, 400.0, 640.0]
assert remaining == []
assert [trade["side"] for trade in manager.trades] == ["BUY"]
assert manager.llm_decisions == 3


def test_the_legacy_order_path_has_no_stop_handling():
"""A source guard, because a stop could be wired in a branch the two cases
above never reach. Lifting it is a decision, not a refactor: update the
"not executed" copy in app.html (Live and Competition Rules + About cards)
in the same PR."""
for rel in (
"domain/backtesting/portfolio_manager.py",
"domain/trading/execution.py",
# update_equity's valuation step, run after every bar's orders.
"domain/trading/portfolio.py",
"domain/leaderboard/strategies/llm_agent.py",
):
text = (_BACKEND / rel).read_text(encoding="utf-8")
for field in ("stop_loss", "take_profit"):
assert field not in text, (
f"{rel} now mentions {field}: if stops execute on the legacy "
"engine, the 'not executed' disclaimers in app.html are false"
)
5 changes: 3 additions & 2 deletions dashboard/backend/tests/test_backtests_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -2363,8 +2363,9 @@ def test_backtest_window_cap_is_two_weeks():
counted LLM calls. A 31-day window is ~22 weekdays x ~7 hourly bars = ~154
decision bars, and a multi-step pipeline multiplies that by its step count
-- far past what the fixed 3600s budget can finish. Two weeks matches the
fortnight the Live Trading Leaderboard's seasons already use, so the
product has one window vocabulary rather than two.
two-week seasons the Live Leaderboard had when this cap was set (seasons
are calendar months now), so the product had one window vocabulary rather
than two.
"""
assert bt.MAX_BACKTEST_DAYS == 14

Expand Down
2 changes: 1 addition & 1 deletion dashboard/backend/tests/test_frontend_fast_boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def test_cache_busters_bumped():
assert "app.js?v=164" in APP_HTML
assert "js/agent-editor.js?v=33" in APP_HTML
assert "styles.css?v=160" in APP_HTML
assert "js/leaderboard.js?v=39" in APP_HTML
assert "js/leaderboard.js?v=40" in APP_HTML
assert "home-page.js?v=56" in APP_HTML
assert "js/credit-format.js?v=1" in APP_HTML
assert "js/credits.js?v=8" in APP_HTML
Expand Down
22 changes: 20 additions & 2 deletions dashboard/backend/tests/test_frontend_live_budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"Baselines keep updating."
)

# A board with prints, current, and models cached: the existing banner is hidden.
# A board with prints, current, and a printed model row (_banner_after adds
# it): the existing banner is hidden.
_HEALTHY = {
"has_prints": True,
"snapshot_stale": False,
Expand All @@ -67,8 +68,25 @@ def _attempt(outcome, kind=None, on="2026-08-27"):
}


# The banner reads the rows, not live_status.models_cached (#586): with no
# printed model row it says model curves have not appeared yet. A healthy board
# therefore needs one printed, current model row for the banner to stay hidden.
_PRINTED_MODEL = {
"entry_id": "deepseek_v4_pro",
"model": "DeepSeek V4 Pro",
"is_model": True,
"status": "frozen",
"portfolio_value": 100500,
"snapshot_end": "2026-08-27",
}


def _banner_after(status):
payload = {"window": {"label": "2026-08-01 — 2026-08-31"}, "entries": [], "live_status": status}
payload = {
"window": {"label": "2026-08-01 — 2026-08-31"},
"entries": [_PRINTED_MODEL],
"live_status": status,
}
return _run_live_js(
f"updateLiveHeader({json.dumps(payload)});"
"const b = document.getElementById('livePhaseBanner');"
Expand Down
Loading
Loading