- 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).
61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import ClassVar
|
|
|
|
from app.engine.indicators import ema, sma
|
|
from app.engine.strategies.base import Strategy, StrategyContext, register
|
|
from app.models import PositionSide
|
|
|
|
|
|
@register
|
|
class EmaCross(Strategy):
|
|
"""EMA crossover with an optional long-term trend filter."""
|
|
|
|
key: ClassVar[str] = "ema_cross"
|
|
name: ClassVar[str] = "EMA Crossover"
|
|
description: ClassVar[str] = (
|
|
"Open long when the fast EMA crosses above the slow EMA (and price is "
|
|
"above the trend filter, if enabled); exit on the opposite cross."
|
|
)
|
|
default_params: ClassVar[dict] = {
|
|
"fast": 9,
|
|
"slow": 21,
|
|
"trend_filter": 200,
|
|
"size": 1.0,
|
|
}
|
|
param_help: ClassVar[dict] = {
|
|
"fast": "Fast EMA period",
|
|
"slow": "Slow EMA period",
|
|
"trend_filter": "SMA trend filter period (0 disables)",
|
|
"size": "Fraction of equity per trade",
|
|
}
|
|
param_bounds: ClassVar[dict] = {"fast": (2, 100), "slow": (3, 300), "size": (0.01, 1.0)}
|
|
|
|
def on_candle(self, ctx: StrategyContext) -> None:
|
|
fast_period = int(self.params["fast"])
|
|
slow_period = int(self.params["slow"])
|
|
trend_period = int(self.params["trend_filter"])
|
|
needed = max(slow_period, trend_period) + 2
|
|
if len(ctx.closes) < needed:
|
|
return
|
|
|
|
fast = ema(ctx.closes, fast_period)
|
|
slow = ema(ctx.closes, slow_period)
|
|
f_prev, f_now = fast[-2], fast[-1]
|
|
s_prev, s_now = slow[-2], slow[-1]
|
|
if None in (f_prev, f_now, s_prev, s_now):
|
|
return
|
|
|
|
trend_ok = True
|
|
if trend_period > 0:
|
|
trend = sma(ctx.closes, trend_period)[-1]
|
|
trend_ok = trend is not None and ctx.price > trend
|
|
|
|
crossed_up = f_prev <= s_prev and f_now > s_now
|
|
crossed_down = f_prev >= s_prev and f_now < s_now
|
|
|
|
if ctx.is_flat and crossed_up and trend_ok:
|
|
ctx.enter_long(size=float(self.params["size"]), reason="EMA cross up")
|
|
elif ctx.side == PositionSide.LONG and crossed_down:
|
|
ctx.close(reason="EMA cross down")
|