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
@@ -21,19 +21,22 @@ class Momentum(Strategy):
|
||||
"entry_threshold": 0.01,
|
||||
"atr_period": 14,
|
||||
"atr_stop_mult": 2.0,
|
||||
"trailing_stop_pct": 0.0,
|
||||
"size": 1.0,
|
||||
}
|
||||
param_help: ClassVar[dict] = {
|
||||
"period": "ROC lookback in bars",
|
||||
"entry_threshold": "Minimum ROC to enter (0.01 = 1%)",
|
||||
"atr_period": "ATR lookback for the stop",
|
||||
"atr_period": "ATR lookback for the initial stop",
|
||||
"atr_stop_mult": "Stop distance in ATRs (0 disables)",
|
||||
"trailing_stop_pct": "Trailing stop distance as a fraction of price (0 disables)",
|
||||
"size": "Fraction of equity per trade",
|
||||
}
|
||||
param_bounds: ClassVar[dict] = {
|
||||
"period": (2, 200),
|
||||
"entry_threshold": (0.0, 1.0),
|
||||
"atr_stop_mult": (0.0, 10.0),
|
||||
"trailing_stop_pct": (0.0, 0.5),
|
||||
"size": (0.01, 1.0),
|
||||
}
|
||||
|
||||
@@ -52,6 +55,7 @@ class Momentum(Strategy):
|
||||
ctx.enter_long(
|
||||
size=float(self.params["size"]),
|
||||
stop_loss=stop,
|
||||
trailing_stop_pct=float(self.params["trailing_stop_pct"]),
|
||||
reason=f"ROC {value:.2%} above threshold",
|
||||
)
|
||||
elif ctx.side == PositionSide.LONG and value < 0:
|
||||
|
||||
Reference in new issue
Block a user