- Add an SVG Bitcoin favicon and link it in index.html. - New indicators: macd() and roc(). - Five new strategies: macd_trend, bollinger_reversion, donchian_breakout, ema_cross, momentum (plus the existing sma_cross, rsi_reversion). - Default TIMEFRAME is now 5m (config, .env.example, compose, workflow, UI).
64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import ClassVar
|
|
|
|
from app.engine.strategies.base import Strategy, StrategyContext, register
|
|
from app.models import PositionSide
|
|
|
|
|
|
@register
|
|
class BollingerReversion(Strategy):
|
|
"""Buy the lower Bollinger band, exit at the middle band."""
|
|
|
|
key: ClassVar[str] = "bollinger_reversion"
|
|
name: ClassVar[str] = "Bollinger Reversion"
|
|
description: ClassVar[str] = (
|
|
"Enter long when the close falls below the lower Bollinger band and exit "
|
|
"when it returns to the middle band (SMA). Optional ATR stop."
|
|
)
|
|
default_params: ClassVar[dict] = {
|
|
"period": 20,
|
|
"num_std": 2.0,
|
|
"atr_period": 14,
|
|
"atr_stop_mult": 0.0,
|
|
"size": 1.0,
|
|
}
|
|
param_help: ClassVar[dict] = {
|
|
"period": "Bollinger lookback",
|
|
"num_std": "Band width in standard deviations",
|
|
"atr_period": "ATR lookback for the stop",
|
|
"atr_stop_mult": "Stop distance in ATRs (0 disables)",
|
|
"size": "Fraction of equity per trade",
|
|
}
|
|
param_bounds: ClassVar[dict] = {
|
|
"period": (5, 100),
|
|
"num_std": (0.5, 4.0),
|
|
"atr_stop_mult": (0.0, 10.0),
|
|
"size": (0.01, 1.0),
|
|
}
|
|
|
|
def on_candle(self, ctx: StrategyContext) -> None:
|
|
period = int(self.params["period"])
|
|
if len(ctx.closes) < period:
|
|
return
|
|
middle = ctx.sma(period)
|
|
deviation = ctx.std(period)
|
|
if middle is None or deviation is None:
|
|
return
|
|
lower = middle - float(self.params["num_std"]) * deviation
|
|
|
|
if ctx.is_flat and ctx.price < lower:
|
|
stop = None
|
|
mult = float(self.params["atr_stop_mult"])
|
|
if mult > 0:
|
|
atr_value = ctx.atr(int(self.params["atr_period"]))
|
|
if atr_value:
|
|
stop = ctx.price - mult * atr_value
|
|
ctx.enter_long(
|
|
size=float(self.params["size"]),
|
|
stop_loss=stop,
|
|
reason="close below lower band",
|
|
)
|
|
elif ctx.side == PositionSide.LONG and ctx.price >= middle:
|
|
ctx.close(reason="close back at middle band")
|