Add trailing stops and parameter sweeps
CI/CD / backend (pull_request) Successful in 6m6s
CI/CD / frontend (pull_request) Successful in 5m17s
CI/CD / build (pull_request) Skipped
CI/CD / deploy (pull_request) Skipped
CI/CD / cleanup (pull_request) Skipped

Trailing stops: strategies can pass trailing_stop_pct to enter_long/enter_short.
advance_trailing_stop in app/engine/portfolio.py ratchets the stop with the price
(up for longs, down for shorts) and never loosens; the engine persists the
ratcheted level and the backtester applies the same logic. Position gains a
trailing_stop_pct column (additive migration). momentum exposes it as a param.

Parameter sweeps: app/backtest/sweep.py runs a cartesian grid through the same
backtester and ranks by return minus max drawdown. POST /api/sweep plus a
dashboard Parameter sweep panel.
This commit is contained in:
matitos committed 2026-09-19 23:04:20 +00:00
1 parent d80e7b155e
commit 9dc00eba95
22 files changed
+575 -7

No files matched your search

+19
View File
@@ -10,6 +10,7 @@ from app.broker.base import Broker
from app.config import Settings
from app.engine.portfolio import (
account_equity,
advance_trailing_stop,
apply_close,
apply_open,
order_qty,
@@ -202,6 +203,7 @@ class TradingEngine:
size=intent.size if intent.size is not None else 1.0,
stop_loss=intent.stop_loss,
take_profit=intent.take_profit,
trailing_stop_pct=intent.trailing_stop_pct,
reason=intent.reason or intent.action,
)
@@ -213,6 +215,7 @@ class TradingEngine:
stop_loss: float | None,
take_profit: float | None,
reason: str,
trailing_stop_pct: float = 0.0,
qty_override: float | None = None,
) -> None:
price = self.last_price
@@ -250,6 +253,7 @@ class TradingEngine:
entry_fee=fill.fee,
stop_loss=stop_loss,
take_profit=take_profit,
trailing_stop_pct=trailing_stop_pct,
)
self.store.record_order(
position_id=self.position.id,
@@ -317,6 +321,21 @@ class TradingEngine:
position = self.position
if position is None:
return
# Ratchet a trailing stop before testing levels, so a bar that both
# advances the trail and reverses is handled consistently.
if position.trailing_stop_pct and position.trailing_stop_pct > 0:
advanced = advance_trailing_stop(
position.side,
position.stop_loss,
position.trailing_stop_pct,
candle.high,
candle.low,
)
if advanced is not None and advanced != position.stop_loss:
position.stop_loss = advanced
self.store.update_stop_loss(position.id, advanced)
hit = risk_exit(
position.side,
position.stop_loss,