The comparison plotted both books in dollars and SPY on a hidden rescaled axis, stretched so its biggest move matched the books' biggest move. SPY's height therefore said nothing about which was ahead, and the stat cells showed R next to SPY's percent, which do not compare. Everything is now percent of the shadow book's equity: - shadow: actual P&L over equity at window start (start equity plus P&L realized before a pinned performance_start_date) - your picks: re-sized at the shadow book's rule (risk_pct, 20% notional cap), so the gap between books is selection only - SPY: buy & hold on the same axis A strategy-vs-SPY verdict measures SPY from the shadow book's first trade, so manual trades opening earlier no longer charge the strategy for SPY's run-up before it existed. R stats stay underneath for book-vs-book reads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
227 lines
10 KiB
Python
227 lines
10 KiB
Python
"""Performance comparison: per-book series, R-multiples, and the start-date window."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import date, datetime, timedelta, timezone
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from app.services import paper_trade_service as pts
|
|
from app.services.trade_policy import MANUAL_BOOK, SHADOW_BOOK
|
|
|
|
|
|
def _trade(*, book, entry=100.0, stop=95.0, close=None, shares=10.0, opened_days_ago=5):
|
|
now = datetime.now(timezone.utc)
|
|
return SimpleNamespace(
|
|
ticker_id=1,
|
|
direction="long",
|
|
entry_price=entry,
|
|
stop_loss=stop,
|
|
shares=shares,
|
|
book=book,
|
|
status="closed" if close is not None else "open",
|
|
close_price=close,
|
|
opened_at=now - timedelta(days=opened_days_ago),
|
|
closed_at=now if close is not None else None,
|
|
)
|
|
|
|
|
|
class TestRMultiple:
|
|
def test_winner_measured_in_units_of_initial_risk(self):
|
|
# Entry 100, stop 95 → 5 of risk. Exit 115 → +15 → +3R.
|
|
trade = _trade(book=SHADOW_BOOK, close=115.0)
|
|
assert pts.trade_r_multiple(trade, None) == pytest.approx(3.0)
|
|
|
|
def test_full_stop_is_minus_one_r(self):
|
|
trade = _trade(book=SHADOW_BOOK, close=95.0)
|
|
assert pts.trade_r_multiple(trade, None) == pytest.approx(-1.0)
|
|
|
|
def test_open_trade_marks_to_the_latest_close(self):
|
|
trade = _trade(book=SHADOW_BOOK)
|
|
assert pts.trade_r_multiple(trade, 110.0) == pytest.approx(2.0)
|
|
|
|
def test_no_risk_distance_has_no_r(self):
|
|
trade = _trade(book=SHADOW_BOOK, entry=100.0, stop=100.0, close=120.0)
|
|
assert pts.trade_r_multiple(trade, None) is None
|
|
|
|
|
|
class TestBookStats:
|
|
def test_r_is_independent_of_position_size(self):
|
|
"""The whole point: a 10-share and a 1000-share book compare equally."""
|
|
small = pts.book_stats([_trade(book=SHADOW_BOOK, close=115.0, shares=10)], {})
|
|
large = pts.book_stats([_trade(book=MANUAL_BOOK, close=115.0, shares=1000)], {})
|
|
assert small["total_r"] == large["total_r"] == pytest.approx(3.0)
|
|
|
|
def test_counts_and_win_rate(self):
|
|
trades = [
|
|
_trade(book=SHADOW_BOOK, close=115.0),
|
|
_trade(book=SHADOW_BOOK, close=95.0),
|
|
_trade(book=SHADOW_BOOK),
|
|
]
|
|
stats = pts.book_stats(trades, {1: 110.0})
|
|
assert stats["trades"] == 3
|
|
assert stats["closed"] == 2
|
|
assert stats["open"] == 1
|
|
# +3R, -1R, +2R marked → 2 of 3 positive.
|
|
assert stats["win_rate"] == pytest.approx(66.7)
|
|
assert stats["total_r"] == pytest.approx(4.0)
|
|
|
|
|
|
class TestStrategySizedPercent:
|
|
"""Both books and SPY share one unit — percent of equity — on the chart."""
|
|
|
|
def test_one_r_is_risk_pct_of_equity(self):
|
|
# Entry 100, stop 90 → 10 of risk; 1% risk → 0.001 shares per $ equity
|
|
# (10% notional, under the 20% cap). Exit 120 = +2R = +2% of equity.
|
|
trade = _trade(book=MANUAL_BOOK, entry=100.0, stop=90.0, close=120.0)
|
|
assert pts.strategy_sized_fraction(trade, 1.0, 0.20) == pytest.approx(0.001)
|
|
days = [trade.closed_at.date()]
|
|
assert pts._cumulative_sized_pct([trade], {}, days, 1.0, 0.20) == [2.0]
|
|
|
|
def test_notional_cap_shrinks_tight_stop_trades(self):
|
|
# Stop 1 away would need 100% of equity in notional for 1% risk; the
|
|
# 20% cap leaves 0.2% at risk, so +1R is only +0.2% of equity.
|
|
trade = _trade(book=MANUAL_BOOK, entry=100.0, stop=99.0, close=101.0)
|
|
days = [trade.closed_at.date()]
|
|
assert pts._cumulative_sized_pct([trade], {}, days, 1.0, 0.20) == [0.2]
|
|
|
|
def test_hand_sized_shares_do_not_matter(self):
|
|
small = _trade(book=MANUAL_BOOK, entry=100.0, stop=90.0, close=120.0, shares=1)
|
|
large = _trade(book=MANUAL_BOOK, entry=100.0, stop=90.0, close=120.0, shares=5000)
|
|
days = [small.closed_at.date()]
|
|
assert pts._cumulative_sized_pct(
|
|
[small], {}, days, 1.0, 0.20
|
|
) == pts._cumulative_sized_pct([large], {}, days, 1.0, 0.20)
|
|
|
|
|
|
class TestPerformanceSummaryPercent:
|
|
@pytest.fixture
|
|
async def session(self):
|
|
from tests.conftest import _test_session_factory
|
|
|
|
async with _test_session_factory() as session:
|
|
yield session
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_shadow_percent_is_measured_on_equity_at_window_start(self, session):
|
|
"""A pinned start date moves the base: P&L realized before it is equity."""
|
|
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
|
|
|
|
today = datetime.now(timezone.utc).date()
|
|
|
|
def at(d):
|
|
return datetime(d.year, d.month, d.day, 20, tzinfo=timezone.utc)
|
|
|
|
session.add(User(id=1, username="u", password_hash="x", role="user", has_access=True))
|
|
ticker = Ticker(symbol="AAA")
|
|
session.add(ticker)
|
|
await session.flush()
|
|
session.add(
|
|
OHLCVRecord(ticker_id=ticker.id, date=today, open=110, high=110, low=110,
|
|
close=110, volume=1)
|
|
)
|
|
for offset, close in ((10, 390.0), (3, 400.0), (0, 412.0)):
|
|
session.add(BenchmarkPrice(symbol="SPY", date=today - timedelta(days=offset),
|
|
close=close))
|
|
common = dict(user_id=1, ticker_id=ticker.id, direction="long", entry_price=100.0,
|
|
stop_loss=90.0, target=150.0, fill_mode="near_close")
|
|
# Before the window: +$5,000 realized, so the window's base is $105k.
|
|
session.add(PaperTrade(**common, book=SHADOW_BOOK, shares=100, status="closed",
|
|
close_price=150.0, opened_at=at(today - timedelta(days=10)),
|
|
closed_at=at(today - timedelta(days=8))))
|
|
# In the window: +$1,050 on 105 shares = +1.0% of $105k.
|
|
session.add(PaperTrade(**common, book=SHADOW_BOOK, shares=105, status="open",
|
|
opened_at=at(today - timedelta(days=3))))
|
|
# Hand-sized manual trade, +1R → +1.0% at strategy sizing whatever its shares.
|
|
session.add(PaperTrade(**common, book=MANUAL_BOOK, shares=7, status="open",
|
|
opened_at=at(today - timedelta(days=3))))
|
|
await session.commit()
|
|
await pts.settings_store.upsert_setting(
|
|
session, pts.KEY_PERFORMANCE_START, (today - timedelta(days=5)).isoformat()
|
|
)
|
|
|
|
summary = await pts.performance_summary(session, user_id=1)
|
|
|
|
assert summary["equity_base"] == pytest.approx(105_000.0)
|
|
assert summary["risk_pct"] == pytest.approx(1.0)
|
|
last = summary["series"][-1]
|
|
assert last["shadow_pnl"] == pytest.approx(1050.0)
|
|
assert last["shadow_pct"] == pytest.approx(1.0)
|
|
assert last["manual_pct"] == pytest.approx(1.0)
|
|
assert last["spy_pct"] == pytest.approx(3.0)
|
|
assert summary["stats"]["shadow"]["pct"] == pytest.approx(1.0)
|
|
assert summary["stats"]["manual"]["pct"] == pytest.approx(1.0)
|
|
assert summary["stats"]["shadow"]["spy_pct"] == pytest.approx(3.0)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_strategy_vs_spy_starts_at_the_first_shadow_trade(self, session):
|
|
"""Manual trades opening first must not charge the strategy for SPY's run-up."""
|
|
from app.models.benchmark_price import BenchmarkPrice
|
|
from app.models.paper_trade import PaperTrade
|
|
from app.models.ticker import Ticker
|
|
from app.models.user import User
|
|
|
|
today = datetime.now(timezone.utc).date()
|
|
|
|
def at(d):
|
|
return datetime(d.year, d.month, d.day, 20, tzinfo=timezone.utc)
|
|
|
|
session.add(User(id=1, username="u", password_hash="x", role="user", has_access=True))
|
|
ticker = Ticker(symbol="AAA")
|
|
session.add(ticker)
|
|
await session.flush()
|
|
for offset, close in ((6, 400.0), (2, 420.0), (0, 441.0)):
|
|
session.add(BenchmarkPrice(symbol="SPY", date=today - timedelta(days=offset),
|
|
close=close))
|
|
common = dict(user_id=1, ticker_id=ticker.id, direction="long", entry_price=100.0,
|
|
stop_loss=90.0, target=150.0, fill_mode="near_close", shares=10,
|
|
status="open")
|
|
session.add(PaperTrade(**common, book=MANUAL_BOOK, opened_at=at(today - timedelta(days=6))))
|
|
session.add(PaperTrade(**common, book=SHADOW_BOOK, opened_at=at(today - timedelta(days=2))))
|
|
await session.commit()
|
|
|
|
summary = await pts.performance_summary(session, user_id=1)
|
|
|
|
# Full window 400 → 441 = +10.25%; the strategy's own window 420 → 441 = +5%.
|
|
assert summary["stats"]["spy"]["pct"] == pytest.approx(10.25)
|
|
assert summary["stats"]["shadow"]["spy_pct"] == pytest.approx(5.0)
|
|
assert summary["stats"]["shadow"]["since"] == (today - timedelta(days=2)).isoformat()
|
|
|
|
|
|
class TestPerformanceStartDate:
|
|
@pytest.fixture
|
|
async def session(self):
|
|
from tests.conftest import _test_session_factory
|
|
|
|
async with _test_session_factory() as session:
|
|
yield session
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_unset_means_all_history(self, session):
|
|
assert await pts.get_performance_start(session) is None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_reads_an_iso_date(self, session):
|
|
await pts.settings_store.upsert_setting(
|
|
session, pts.KEY_PERFORMANCE_START, "2026-07-20"
|
|
)
|
|
assert await pts.get_performance_start(session) == date(2026, 7, 20)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_garbage_falls_back_to_all_history(self, session):
|
|
"""A bad setting must not blank the whole performance card."""
|
|
await pts.settings_store.upsert_setting(
|
|
session, pts.KEY_PERFORMANCE_START, "not-a-date"
|
|
)
|
|
assert await pts.get_performance_start(session) is None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_empty_string_means_all_history(self, session):
|
|
await pts.settings_store.upsert_setting(session, pts.KEY_PERFORMANCE_START, "")
|
|
assert await pts.get_performance_start(session) is None
|