diff options
| author | TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> | 2026-04-01 16:16:31 +0900 |
|---|---|---|
| committer | TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> | 2026-04-01 16:16:31 +0900 |
| commit | 49e5baaebf2f9ca1ba7b85a80c3451c5789edde4 (patch) | |
| tree | a756ccbf4166df6ee6b3cabd9b17e6c485dc5fe2 /services/strategy-engine/strategies | |
| parent | 2faec2f1f1631bd286a5c55051e582f8abe2898c (diff) | |
feat(strategy): add VWAP strategy
Diffstat (limited to 'services/strategy-engine/strategies')
| -rw-r--r-- | services/strategy-engine/strategies/config/vwap_strategy.yaml | 2 | ||||
| -rw-r--r-- | services/strategy-engine/strategies/vwap_strategy.py | 83 |
2 files changed, 85 insertions, 0 deletions
diff --git a/services/strategy-engine/strategies/config/vwap_strategy.yaml b/services/strategy-engine/strategies/config/vwap_strategy.yaml new file mode 100644 index 0000000..e12d5e7 --- /dev/null +++ b/services/strategy-engine/strategies/config/vwap_strategy.yaml @@ -0,0 +1,2 @@ +deviation_threshold: 0.002 +quantity: "0.01" diff --git a/services/strategy-engine/strategies/vwap_strategy.py b/services/strategy-engine/strategies/vwap_strategy.py new file mode 100644 index 0000000..d1b86b5 --- /dev/null +++ b/services/strategy-engine/strategies/vwap_strategy.py @@ -0,0 +1,83 @@ +from decimal import Decimal + +from shared.models import Candle, Signal, OrderSide +from strategies.base import BaseStrategy + + +class VwapStrategy(BaseStrategy): + name: str = "vwap" + + def __init__(self) -> None: + self._deviation_threshold: float = 0.002 + self._quantity: Decimal = Decimal("0.01") + self._cumulative_tp_vol: float = 0.0 + self._cumulative_vol: float = 0.0 + self._candle_count: int = 0 + self._was_below_vwap: bool = False + self._was_above_vwap: bool = False + + @property + def warmup_period(self) -> int: + return 30 + + def configure(self, params: dict) -> None: + self._deviation_threshold = float(params.get("deviation_threshold", 0.002)) + self._quantity = Decimal(str(params.get("quantity", "0.01"))) + + def reset(self) -> None: + self._cumulative_tp_vol = 0.0 + self._cumulative_vol = 0.0 + self._candle_count = 0 + self._was_below_vwap = False + self._was_above_vwap = False + + def on_candle(self, candle: Candle) -> Signal | None: + high = float(candle.high) + low = float(candle.low) + close = float(candle.close) + volume = float(candle.volume) + + typical_price = (high + low + close) / 3.0 + self._cumulative_tp_vol += typical_price * volume + self._cumulative_vol += volume + self._candle_count += 1 + + if self._candle_count < self.warmup_period: + return None + + if self._cumulative_vol == 0.0: + return None + + vwap = self._cumulative_tp_vol / self._cumulative_vol + deviation = (close - vwap) / vwap + + if deviation < -self._deviation_threshold: + self._was_below_vwap = True + if deviation > self._deviation_threshold: + self._was_above_vwap = True + + # Mean reversion from below: was below VWAP, now back near it + if self._was_below_vwap and abs(deviation) <= self._deviation_threshold: + self._was_below_vwap = False + return Signal( + strategy=self.name, + symbol=candle.symbol, + side=OrderSide.BUY, + price=candle.close, + quantity=self._quantity, + reason=f"VWAP mean reversion BUY: deviation {deviation:.4f} within threshold {self._deviation_threshold}", + ) + + # Mean reversion from above: was above VWAP, now back near it + if self._was_above_vwap and abs(deviation) <= self._deviation_threshold: + self._was_above_vwap = False + return Signal( + strategy=self.name, + symbol=candle.symbol, + side=OrderSide.SELL, + price=candle.close, + quantity=self._quantity, + reason=f"VWAP mean reversion SELL: deviation {deviation:.4f} within threshold {self._deviation_threshold}", + ) + + return None |
