- 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).
51 lines
1.8 KiB
Python
51 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import ClassVar
|
|
|
|
from app.engine.indicators import macd
|
|
from app.engine.strategies.base import Strategy, StrategyContext, register
|
|
from app.models import PositionSide
|
|
|
|
|
|
@register
|
|
class MacdTrend(Strategy):
|
|
"""MACD crossover trend following, long-only."""
|
|
|
|
key: ClassVar[str] = "macd_trend"
|
|
name: ClassVar[str] = "MACD Trend"
|
|
description: ClassVar[str] = (
|
|
"Open long when the MACD line crosses above its signal line and close when "
|
|
"it crosses back below."
|
|
)
|
|
default_params: ClassVar[dict] = {"fast": 12, "slow": 26, "signal": 9, "size": 1.0}
|
|
param_help: ClassVar[dict] = {
|
|
"fast": "Fast EMA period",
|
|
"slow": "Slow EMA period",
|
|
"signal": "Signal EMA period",
|
|
"size": "Fraction of equity per trade",
|
|
}
|
|
param_bounds: ClassVar[dict] = {"fast": (2, 100), "slow": (3, 200), "signal": (2, 100)}
|
|
|
|
def on_candle(self, ctx: StrategyContext) -> None:
|
|
closes = ctx.closes
|
|
if len(closes) < int(self.params["slow"]) + int(self.params["signal"]) + 2:
|
|
return
|
|
macd_line, signal_line, _ = macd(
|
|
closes,
|
|
int(self.params["fast"]),
|
|
int(self.params["slow"]),
|
|
int(self.params["signal"]),
|
|
)
|
|
m_prev, m_now = macd_line[-2], macd_line[-1]
|
|
s_prev, s_now = signal_line[-2], signal_line[-1]
|
|
if None in (m_prev, m_now, s_prev, s_now):
|
|
return
|
|
|
|
crossed_up = m_prev <= s_prev and m_now > s_now
|
|
crossed_down = m_prev >= s_prev and m_now < s_now
|
|
|
|
if ctx.is_flat and crossed_up:
|
|
ctx.enter_long(size=float(self.params["size"]), reason="MACD crossed above signal")
|
|
elif ctx.side == PositionSide.LONG and crossed_down:
|
|
ctx.close(reason="MACD crossed below signal")
|