Add trailing stops and parameter sweeps
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:
1 parent
d80e7b155e
commit
9dc00eba95
22 files changed
+575
-7
No files matched your search
@@ -7,6 +7,7 @@ from app.broker.paper import PaperBroker
|
||||
from app.engine.portfolio import (
|
||||
OpenPosition,
|
||||
account_equity,
|
||||
advance_trailing_stop,
|
||||
apply_close,
|
||||
apply_open,
|
||||
order_qty,
|
||||
@@ -91,6 +92,14 @@ class Backtester:
|
||||
for i, candle in enumerate(candles):
|
||||
mark = candle.close
|
||||
if position is not None:
|
||||
if position.trailing_stop_pct > 0:
|
||||
position.stop_loss = advance_trailing_stop(
|
||||
position.side,
|
||||
position.stop_loss,
|
||||
position.trailing_stop_pct,
|
||||
candle.high,
|
||||
candle.low,
|
||||
)
|
||||
hit = risk_exit(
|
||||
position.side,
|
||||
position.stop_loss,
|
||||
@@ -130,6 +139,7 @@ class Backtester:
|
||||
entry_fee=fill.fee,
|
||||
stop_loss=intent.stop_loss,
|
||||
take_profit=intent.take_profit,
|
||||
trailing_stop_pct=intent.trailing_stop_pct,
|
||||
entry_ts=candle.ts,
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user