feat: trailing-stop auto-exit for paper trades + close/digest alerts
Applies the backtest-validated trailing stop to live paper trading, and surfaces it transparently. Exit (A): - New paper-trade exit policy (paper_exit_mode=trailing, paper_trailing_pct=12), tunable in Admin → Paper-Trade Exit. resolve_open_trades runs a trailing stop (initial stop as floor, ratchets up from the peak; target ignored — the validated rule) and records close_reason (trailing|stop|target|manual; +migration 013). - list_trades enriches open trades with the live trailing-stop level + distance %. Open Trades panel shows the active tactic and a Trail Stop column. Alerts (B): - Daily digest now lists open trades with unrealized gain, trailing stop, and how far away it is. - New "trade closed" alert: one summary per auto-close (trailing/target/stop, not manual) — direction, reason, days held, P&L abs+%/R — covering wins AND stop-loss losses. Deduped by trade id; toggle in Admin alerts. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -3,11 +3,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from app.models.alert import AlertLog
|
||||
from app.models.ohlcv import OHLCVRecord
|
||||
from app.models.paper_trade import PaperTrade
|
||||
from app.models.score import CompositeScore
|
||||
from app.models.sr_level import SRLevel
|
||||
from app.models.ticker import Ticker
|
||||
@@ -168,3 +170,53 @@ async def test_dispatch_no_credentials(session):
|
||||
await svc.update_alert_config(session, enabled=True) # enabled but no token/chat
|
||||
res = await svc.dispatch_alerts(session)
|
||||
assert res["status"] == "no_credentials"
|
||||
|
||||
|
||||
async def _add_closed_trade(session, symbol: str, reason: str, *,
|
||||
close: float = 110.0, closed_hours_ago: float = 1.0) -> None:
|
||||
if await session.get(User, 1) is None:
|
||||
session.add(User(id=1, username="u", password_hash="x", role="user", has_access=True))
|
||||
await session.flush()
|
||||
t = Ticker(symbol=symbol)
|
||||
session.add(t)
|
||||
await session.flush()
|
||||
now = datetime.now(timezone.utc)
|
||||
session.add(PaperTrade(
|
||||
user_id=1, ticker_id=t.id, direction="long",
|
||||
entry_price=100.0, shares=10.0, stop_loss=95.0, target=120.0,
|
||||
status="closed", opened_at=now - timedelta(days=5),
|
||||
close_price=close, closed_at=now - timedelta(hours=closed_hours_ago),
|
||||
close_reason=reason,
|
||||
))
|
||||
await session.commit()
|
||||
|
||||
|
||||
async def test_config_includes_trade_closed_toggle(session):
|
||||
assert (await svc.get_alert_config(session))["trade_closed_enabled"] is True
|
||||
cfg = await svc.update_alert_config(session, trade_closed_enabled=False)
|
||||
assert cfg["trade_closed_enabled"] is False
|
||||
|
||||
|
||||
async def test_collect_closed_trades_filters_manual_and_old(session):
|
||||
await _add_closed_trade(session, "WIN", "trailing", close=110.0, closed_hours_ago=1)
|
||||
await _add_closed_trade(session, "MAN", "manual", close=110.0, closed_hours_ago=1) # manual → skip
|
||||
await _add_closed_trade(session, "OLD", "stop", close=95.0, closed_hours_ago=100) # too old → skip
|
||||
|
||||
out = await svc._collect_closed_trades(session)
|
||||
assert len(out) == 1
|
||||
_, text = out[0]
|
||||
assert "WIN" in text and "trailing stop" in text
|
||||
|
||||
|
||||
def test_format_closed_trade_win():
|
||||
now = datetime.now(timezone.utc)
|
||||
trade = SimpleNamespace(
|
||||
direction="long", entry_price=100.0, close_price=110.0, shares=10.0,
|
||||
stop_loss=95.0, opened_at=now - timedelta(days=12), closed_at=now,
|
||||
close_reason="trailing",
|
||||
)
|
||||
txt = svc._format_closed_trade(trade, "AAA")
|
||||
assert "✅" in txt # win
|
||||
assert "+10.0%" in txt
|
||||
assert "+2.00R" in txt # +10% over a 5% stop
|
||||
assert "held 12d" in txt
|
||||
|
||||
@@ -94,6 +94,7 @@ async def _add_bars(session, ticker_id: int, highs_lows: list[tuple[float, float
|
||||
|
||||
|
||||
async def test_resolve_closes_on_target(session):
|
||||
await svc.set_exit_policy(session, mode="target")
|
||||
tid = await _seed(session, "AAA", close=100.0)
|
||||
trade = await svc.create_trade(session, 1, symbol="AAA", direction="long",
|
||||
entry_price=100.0, shares=10, stop_loss=95.0, target=110.0)
|
||||
@@ -107,6 +108,7 @@ async def test_resolve_closes_on_target(session):
|
||||
|
||||
|
||||
async def test_resolve_closes_on_stop(session):
|
||||
await svc.set_exit_policy(session, mode="target")
|
||||
tid = await _seed(session, "AAA", close=100.0)
|
||||
trade = await svc.create_trade(session, 1, symbol="AAA", direction="long",
|
||||
entry_price=100.0, shares=10, stop_loss=95.0, target=110.0)
|
||||
@@ -118,6 +120,7 @@ async def test_resolve_closes_on_stop(session):
|
||||
|
||||
|
||||
async def test_resolve_leaves_open_when_neither_hit(session):
|
||||
await svc.set_exit_policy(session, mode="target")
|
||||
tid = await _seed(session, "AAA", close=100.0)
|
||||
await svc.create_trade(session, 1, symbol="AAA", direction="long",
|
||||
entry_price=100.0, shares=10, stop_loss=95.0, target=110.0)
|
||||
@@ -171,3 +174,71 @@ async def test_alpha_short_and_missing_benchmark(session):
|
||||
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)
|
||||
|
||||
|
||||
def _b(d: date, hi: float, lo: float):
|
||||
return svc.Bar(date=d, high=hi, low=lo)
|
||||
|
||||
|
||||
class TestTrailingClose:
|
||||
def test_long_locks_gain(self):
|
||||
# Runs to 120; the 12%-from-peak stop (120 → 105.6) is pierced on the drop.
|
||||
bars = [_b(date(2026, 1, 2), 120, 110), _b(date(2026, 1, 3), 130, 100)]
|
||||
hit = svc._trailing_close("long", 100.0, 95.0, 0.12, bars)
|
||||
assert hit is not None
|
||||
price, when, reason = hit
|
||||
assert price == pytest.approx(105.6)
|
||||
assert reason == "trailing"
|
||||
assert when == date(2026, 1, 3)
|
||||
|
||||
def test_initial_stop_caps_loss(self):
|
||||
bars = [_b(date(2026, 1, 2), 101, 94)] # through the initial stop before running
|
||||
hit = svc._trailing_close("long", 100.0, 95.0, 0.12, bars)
|
||||
assert hit is not None
|
||||
price, _, reason = hit
|
||||
assert price == pytest.approx(95.0)
|
||||
assert reason == "stop"
|
||||
|
||||
def test_none_when_neither_hit(self):
|
||||
bars = [_b(date(2026, 1, 2), 105, 99), _b(date(2026, 1, 3), 106, 100)]
|
||||
assert svc._trailing_close("long", 100.0, 95.0, 0.12, bars) is None
|
||||
|
||||
|
||||
async def test_exit_policy_defaults_and_round_trip(session):
|
||||
assert await svc.get_exit_policy(session) == {"mode": "trailing", "trailing_pct": 12.0}
|
||||
updated = await svc.set_exit_policy(session, mode="target", trailing_pct=15.0)
|
||||
assert updated == {"mode": "target", "trailing_pct": 15.0}
|
||||
assert (await svc.get_exit_policy(session))["mode"] == "target"
|
||||
|
||||
|
||||
async def test_exit_policy_rejects_bad_input(session):
|
||||
with pytest.raises(ValidationError):
|
||||
await svc.set_exit_policy(session, mode="bogus")
|
||||
with pytest.raises(ValidationError):
|
||||
await svc.set_exit_policy(session, trailing_pct=200.0)
|
||||
|
||||
|
||||
async def test_resolve_trailing_closes_with_reason(session):
|
||||
tid = await _seed(session, "AAA", close=100.0) # default policy: trailing 12%
|
||||
await _add_open_trade(session, tid, "long", entry=100.0, shares=10, days_ago=10)
|
||||
await _add_bars(session, tid, [(120, 110), (130, 100)], start=date.today()) # run up, pull back
|
||||
assert await svc.resolve_open_trades(session) == 1
|
||||
closed = await svc.list_trades(session, 1, status="closed")
|
||||
assert closed[0]["close_reason"] == "trailing"
|
||||
|
||||
|
||||
async def test_manual_close_sets_reason(session):
|
||||
await _seed(session, "AAA", close=112.0)
|
||||
trade = await svc.create_trade(session, 1, symbol="AAA", direction="long",
|
||||
entry_price=100.0, shares=5, stop_loss=95.0, target=120.0)
|
||||
await svc.close_trade(session, 1, trade.id)
|
||||
assert (await svc.list_trades(session, 1, status="closed"))[0]["close_reason"] == "manual"
|
||||
|
||||
|
||||
async def test_list_open_exposes_trailing_stop(session):
|
||||
tid = await _seed(session, "AAA", close=120.0)
|
||||
await _add_open_trade(session, tid, "long", entry=100.0, shares=10, days_ago=10)
|
||||
await _add_bars(session, tid, [(125, 118)], start=date.today()) # peak 125
|
||||
row = (await svc.list_trades(session, 1, status="open"))[0]
|
||||
assert row["trailing_stop"] == pytest.approx(110.0) # 125 * (1 - 0.12)
|
||||
assert row["trailing_distance_pct"] is not None
|
||||
|
||||
Reference in New Issue
Block a user