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
+44
-2
@@ -6,7 +6,7 @@ from collections.abc import Callable
|
||||
from contextlib import suppress
|
||||
from typing import Any
|
||||
|
||||
from app.backtest import Backtester
|
||||
from app.backtest import Backtester, run_sweep
|
||||
from app.broker import build_broker
|
||||
from app.broker.base import Broker
|
||||
from app.config import Settings
|
||||
@@ -15,7 +15,14 @@ from app.engine.strategies import get_strategy
|
||||
from app.market import build_market_data
|
||||
from app.market.base import MarketData
|
||||
from app.notify import Notifier, build_notifier
|
||||
from app.schemas import BacktestRequest, BacktestResult, StatusOut
|
||||
from app.schemas import (
|
||||
BacktestRequest,
|
||||
BacktestResult,
|
||||
StatusOut,
|
||||
SweepRequest,
|
||||
SweepResult,
|
||||
SweepRowOut,
|
||||
)
|
||||
from app.store import Store
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -139,6 +146,41 @@ class Runtime:
|
||||
)
|
||||
return backtester.run(candles, strategy, symbol, timeframe)
|
||||
|
||||
async def run_sweep(self, request: SweepRequest) -> SweepResult:
|
||||
symbol = request.symbol or self.settings.symbol
|
||||
timeframe = request.timeframe or self.settings.timeframe
|
||||
strategy_cls = get_strategy(request.strategy_key)
|
||||
|
||||
feed = self._feed_factory(self.settings)
|
||||
try:
|
||||
candles = await feed.fetch_candles(symbol, timeframe, request.limit)
|
||||
finally:
|
||||
await feed.close()
|
||||
|
||||
rows = run_sweep(
|
||||
request.strategy_key,
|
||||
request.base_params,
|
||||
request.grid,
|
||||
candles,
|
||||
symbol,
|
||||
timeframe,
|
||||
fee_bps=self.settings.fee_bps,
|
||||
slippage_bps=self.settings.slippage_bps,
|
||||
starting_balance=request.starting_balance or self.settings.starting_balance,
|
||||
top_n=request.top_n,
|
||||
)
|
||||
combinations = 1
|
||||
for values in request.grid.values():
|
||||
combinations *= max(1, len(values))
|
||||
return SweepResult(
|
||||
strategy_key=request.strategy_key,
|
||||
strategy_name=strategy_cls.name or request.strategy_key,
|
||||
symbol=symbol,
|
||||
timeframe=timeframe,
|
||||
combinations=combinations,
|
||||
rows=[SweepRowOut(params=row.params, metrics=row.metrics) for row in rows],
|
||||
)
|
||||
|
||||
async def shutdown(self) -> None:
|
||||
if self.engine is not None:
|
||||
await self.engine.shutdown()
|
||||
|
||||
Reference in new issue
Block a user