Add Supertrend and VWAP strategies plus a short-side example
- New indicators: vwap (rolling/cumulative) and supertrend (line + direction), exposed on StrategyContext. - New strategies: supertrend (flip-based, optional shorts), vwap_reversion (mean reversion below a rolling VWAP) and ema_cross_short (short-side example, mirroring the long EMA cross). 10 strategies total. - Adds indicator and strategy tests; smoke-tested all three on live BTC/USDT 5m data.
This commit is contained in:
1 parent
c1a7fb9c6b
commit
d1f66c95c4
8 files changed
+420
-3
No files matched your search
@@ -151,6 +151,87 @@ def roc(values: Sequence[float], period: int = 10) -> list[Number]:
|
||||
return out
|
||||
|
||||
|
||||
def typical_price(candle: Candle) -> float:
|
||||
return (candle.high + candle.low + candle.close) / 3
|
||||
|
||||
|
||||
def vwap(candles: Sequence[Candle], period: int | None = None) -> list[Number]:
|
||||
"""Volume-weighted average price, rolling over ``period`` bars.
|
||||
|
||||
With ``period=None`` it is cumulative from the first candle.
|
||||
"""
|
||||
out: list[Number] = [None] * len(candles)
|
||||
pv_sum = 0.0
|
||||
volume_sum = 0.0
|
||||
window: list[tuple[float, float]] = []
|
||||
for i, candle in enumerate(candles):
|
||||
price = typical_price(candle)
|
||||
volume = candle.volume
|
||||
pv_sum += price * volume
|
||||
volume_sum += volume
|
||||
if period is not None:
|
||||
window.append((price, volume))
|
||||
if len(window) > period:
|
||||
old_price, old_volume = window.pop(0)
|
||||
pv_sum -= old_price * old_volume
|
||||
volume_sum -= old_volume
|
||||
if len(window) < period:
|
||||
continue
|
||||
if volume_sum:
|
||||
out[i] = pv_sum / volume_sum
|
||||
return out
|
||||
|
||||
|
||||
def supertrend(
|
||||
candles: Sequence[Candle],
|
||||
period: int = 10,
|
||||
multiplier: float = 3.0,
|
||||
) -> tuple[list[float | None], list[bool | None]]:
|
||||
"""Supertrend line and trend direction (True = uptrend) per candle."""
|
||||
atr_series = atr(candles, period)
|
||||
line: list[float | None] = [None] * len(candles)
|
||||
up: list[bool | None] = [None] * len(candles)
|
||||
|
||||
final_upper: float | None = None
|
||||
final_lower: float | None = None
|
||||
prev_close: float | None = None
|
||||
direction_up = True
|
||||
|
||||
for i, candle in enumerate(candles):
|
||||
atr_value = atr_series[i]
|
||||
if atr_value is None:
|
||||
continue
|
||||
midpoint = (candle.high + candle.low) / 2
|
||||
basic_upper = midpoint + multiplier * atr_value
|
||||
basic_lower = midpoint - multiplier * atr_value
|
||||
|
||||
if final_upper is None or final_lower is None:
|
||||
final_upper, final_lower = basic_upper, basic_lower
|
||||
direction_up = candle.close >= midpoint
|
||||
else:
|
||||
assert prev_close is not None
|
||||
final_upper = (
|
||||
basic_upper
|
||||
if basic_upper < final_upper or prev_close > final_upper
|
||||
else final_upper
|
||||
)
|
||||
final_lower = (
|
||||
basic_lower
|
||||
if basic_lower > final_lower or prev_close < final_lower
|
||||
else final_lower
|
||||
)
|
||||
if direction_up:
|
||||
direction_up = candle.close >= final_lower
|
||||
else:
|
||||
direction_up = candle.close > final_upper
|
||||
|
||||
prev_close = candle.close
|
||||
line[i] = final_lower if direction_up else final_upper
|
||||
up[i] = direction_up
|
||||
|
||||
return line, up
|
||||
|
||||
|
||||
def last(series: Sequence[Number]) -> Number:
|
||||
for value in reversed(series):
|
||||
if value is not None:
|
||||
|
||||
@@ -5,7 +5,17 @@ from collections.abc import Sequence
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, ClassVar, Literal, Protocol
|
||||
|
||||
from app.engine.indicators import atr, ema, macd, roc, rolling_std, rsi, sma
|
||||
from app.engine.indicators import (
|
||||
atr,
|
||||
ema,
|
||||
macd,
|
||||
roc,
|
||||
rolling_std,
|
||||
rsi,
|
||||
sma,
|
||||
supertrend,
|
||||
vwap,
|
||||
)
|
||||
from app.models import PositionSide
|
||||
from app.schemas import Candle, ParamSpec, StrategyOut
|
||||
|
||||
@@ -116,6 +126,15 @@ class StrategyContext:
|
||||
def roc(self, period: int = 10) -> float | None:
|
||||
return roc(self.closes, period)[-1]
|
||||
|
||||
def vwap(self, period: int | None = None) -> float | None:
|
||||
return vwap(self._candles, period)[-1]
|
||||
|
||||
def supertrend(
|
||||
self, period: int = 10, multiplier: float = 3.0
|
||||
) -> tuple[float | None, bool | None]:
|
||||
line, up = supertrend(self._candles, period, multiplier)
|
||||
return line[-1], up[-1]
|
||||
|
||||
# --- order intents ------------------------------------------------
|
||||
def enter_long(
|
||||
self,
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import ClassVar
|
||||
|
||||
from app.engine.indicators import atr, ema
|
||||
from app.engine.strategies.base import Strategy, StrategyContext, register
|
||||
from app.models import PositionSide
|
||||
|
||||
|
||||
@register
|
||||
class EmaCrossShort(Strategy):
|
||||
"""Short-side example: sell EMA cross-downs, cover on cross-up."""
|
||||
|
||||
key: ClassVar[str] = "ema_cross_short"
|
||||
name: ClassVar[str] = "EMA Crossover (short)"
|
||||
description: ClassVar[str] = (
|
||||
"Open a short when the fast EMA crosses below the slow EMA and cover when "
|
||||
"it crosses back above. Demonstrates short trades; optional ATR stop."
|
||||
)
|
||||
default_params: ClassVar[dict] = {
|
||||
"fast": 9,
|
||||
"slow": 21,
|
||||
"atr_period": 14,
|
||||
"atr_stop_mult": 2.0,
|
||||
"size": 1.0,
|
||||
}
|
||||
param_help: ClassVar[dict] = {
|
||||
"fast": "Fast EMA period",
|
||||
"slow": "Slow EMA period",
|
||||
"atr_period": "ATR lookback for the stop",
|
||||
"atr_stop_mult": "Stop distance in ATRs (0 disables)",
|
||||
"size": "Fraction of equity per short",
|
||||
}
|
||||
param_bounds: ClassVar[dict] = {
|
||||
"fast": (2, 100),
|
||||
"slow": (3, 300),
|
||||
"atr_stop_mult": (0.0, 10.0),
|
||||
"size": (0.01, 1.0),
|
||||
}
|
||||
|
||||
def on_candle(self, ctx: StrategyContext) -> None:
|
||||
fast_period = int(self.params["fast"])
|
||||
slow_period = int(self.params["slow"])
|
||||
if len(ctx.closes) < slow_period + 2:
|
||||
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
|
||||
|
||||
crossed_down = f_prev >= s_prev and f_now < s_now
|
||||
crossed_up = f_prev <= s_prev and f_now > s_now
|
||||
|
||||
if ctx.is_flat and crossed_down:
|
||||
stop = None
|
||||
mult = float(self.params["atr_stop_mult"])
|
||||
if mult > 0:
|
||||
atr_value = atr(ctx.candles, int(self.params["atr_period"]))[-1]
|
||||
if atr_value:
|
||||
stop = ctx.price + mult * atr_value
|
||||
ctx.enter_short(
|
||||
size=float(self.params["size"]),
|
||||
stop_loss=stop,
|
||||
reason="EMA cross down",
|
||||
)
|
||||
elif ctx.side == PositionSide.SHORT and crossed_up:
|
||||
ctx.close(reason="EMA cross up")
|
||||
@@ -0,0 +1,63 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import ClassVar
|
||||
|
||||
from app.engine.indicators import supertrend as supertrend_series
|
||||
from app.engine.strategies.base import Strategy, StrategyContext, register
|
||||
from app.models import PositionSide
|
||||
|
||||
|
||||
@register
|
||||
class Supertrend(Strategy):
|
||||
"""Trend following on the Supertrend indicator, long or short."""
|
||||
|
||||
key: ClassVar[str] = "supertrend"
|
||||
name: ClassVar[str] = "Supertrend"
|
||||
description: ClassVar[str] = (
|
||||
"Go long when Supertrend flips to an uptrend and short when it flips to "
|
||||
"a downtrend. Uses the Supertrend line as a stop."
|
||||
)
|
||||
default_params: ClassVar[dict] = {
|
||||
"period": 10,
|
||||
"multiplier": 3.0,
|
||||
"allow_shorts": False,
|
||||
"size": 1.0,
|
||||
}
|
||||
param_help: ClassVar[dict] = {
|
||||
"period": "ATR period for the Supertrend bands",
|
||||
"multiplier": "ATR multiplier (band width)",
|
||||
"allow_shorts": "Also take short trades in downtrends",
|
||||
"size": "Fraction of equity per trade",
|
||||
}
|
||||
param_bounds: ClassVar[dict] = {
|
||||
"period": (2, 100),
|
||||
"multiplier": (0.5, 10.0),
|
||||
"size": (0.01, 1.0),
|
||||
}
|
||||
|
||||
def on_candle(self, ctx: StrategyContext) -> None:
|
||||
period = int(self.params["period"])
|
||||
multiplier = float(self.params["multiplier"])
|
||||
if len(ctx.candles) < period + 2:
|
||||
return
|
||||
|
||||
line_series, up_series = supertrend_series(ctx.candles, period, multiplier)
|
||||
line, up = line_series[-1], up_series[-1]
|
||||
prev_up = up_series[-2]
|
||||
if line is None or up is None or prev_up is None:
|
||||
return
|
||||
|
||||
allow_shorts = bool(self.params.get("allow_shorts", False))
|
||||
size = float(self.params["size"])
|
||||
|
||||
if ctx.side == PositionSide.LONG and not up:
|
||||
ctx.close(reason="Supertrend turned down")
|
||||
return
|
||||
if ctx.side == PositionSide.SHORT and up:
|
||||
ctx.close(reason="Supertrend turned up")
|
||||
return
|
||||
|
||||
if ctx.is_flat and up and not prev_up:
|
||||
ctx.enter_long(size=size, stop_loss=line, reason="Supertrend flipped up")
|
||||
elif ctx.is_flat and allow_shorts and not up and prev_up:
|
||||
ctx.enter_short(size=size, stop_loss=line, reason="Supertrend flipped down")
|
||||
@@ -0,0 +1,63 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import ClassVar
|
||||
|
||||
from app.engine.strategies.base import Strategy, StrategyContext, register
|
||||
from app.models import PositionSide
|
||||
|
||||
|
||||
@register
|
||||
class VwapReversion(Strategy):
|
||||
"""Mean reversion around VWAP, long-only."""
|
||||
|
||||
key: ClassVar[str] = "vwap_reversion"
|
||||
name: ClassVar[str] = "VWAP Reversion"
|
||||
description: ClassVar[str] = (
|
||||
"Open long when price falls this far below a rolling VWAP and exit when "
|
||||
"it returns to VWAP. Optional ATR stop."
|
||||
)
|
||||
default_params: ClassVar[dict] = {
|
||||
"period": 20,
|
||||
"deviation_pct": 0.005,
|
||||
"atr_period": 14,
|
||||
"atr_stop_mult": 2.0,
|
||||
"size": 1.0,
|
||||
}
|
||||
param_help: ClassVar[dict] = {
|
||||
"period": "Rolling VWAP window in bars",
|
||||
"deviation_pct": "Entry distance below VWAP (0.005 = 0.5%)",
|
||||
"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": (2, 200),
|
||||
"deviation_pct": (0.0, 0.2),
|
||||
"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.candles) < period + 1:
|
||||
return
|
||||
reference = ctx.vwap(period)
|
||||
if reference is None or reference <= 0:
|
||||
return
|
||||
|
||||
threshold = reference * (1 - float(self.params["deviation_pct"]))
|
||||
|
||||
if ctx.is_flat and ctx.price < threshold:
|
||||
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="price below VWAP band",
|
||||
)
|
||||
elif ctx.side == PositionSide.LONG and ctx.price >= reference:
|
||||
ctx.close(reason="price returned to VWAP")
|
||||
Reference in new issue
Block a user