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
@@ -33,6 +33,7 @@ class Store:
|
||||
entry_fee: float,
|
||||
stop_loss: float | None = None,
|
||||
take_profit: float | None = None,
|
||||
trailing_stop_pct: float = 0.0,
|
||||
) -> Position:
|
||||
with self._session_factory() as session:
|
||||
position = Position(
|
||||
@@ -44,6 +45,7 @@ class Store:
|
||||
entry_fee=entry_fee,
|
||||
stop_loss=stop_loss,
|
||||
take_profit=take_profit,
|
||||
trailing_stop_pct=trailing_stop_pct,
|
||||
status=PositionStatus.OPEN,
|
||||
)
|
||||
session.add(position)
|
||||
@@ -88,6 +90,14 @@ class Store:
|
||||
session.commit()
|
||||
return len(positions)
|
||||
|
||||
def update_stop_loss(self, position_id: int, stop_loss: float) -> None:
|
||||
with self._session_factory() as session:
|
||||
position = session.get(Position, position_id)
|
||||
if position is None:
|
||||
return
|
||||
position.stop_loss = stop_loss
|
||||
session.commit()
|
||||
|
||||
def open_position_for(self, strategy_key: str) -> Position | None:
|
||||
with self._session_factory() as session:
|
||||
return session.scalars(
|
||||
|
||||
Reference in new issue
Block a user