1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
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")))
if self._deviation_threshold <= 0:
raise ValueError(
f"VWAP deviation_threshold must be > 0, got {self._deviation_threshold}"
)
if self._quantity <= 0:
raise ValueError(f"Quantity must be positive, got {self._quantity}")
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
|