diff options
Diffstat (limited to 'services/strategy-engine/tests')
| -rw-r--r-- | services/strategy-engine/tests/test_vwap_strategy.py | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/services/strategy-engine/tests/test_vwap_strategy.py b/services/strategy-engine/tests/test_vwap_strategy.py new file mode 100644 index 0000000..37d35bc --- /dev/null +++ b/services/strategy-engine/tests/test_vwap_strategy.py @@ -0,0 +1,101 @@ +"""Tests for the VWAP strategy.""" +from datetime import datetime, timezone +from decimal import Decimal + +import pytest + +from shared.models import Candle, OrderSide +from strategies.vwap_strategy import VwapStrategy + + +def make_candle( + close: float, + high: float | None = None, + low: float | None = None, + volume: float = 1.0, +) -> Candle: + if high is None: + high = close + if low is None: + low = close + return Candle( + symbol="BTC/USDT", + timeframe="1m", + open_time=datetime(2024, 1, 1, tzinfo=timezone.utc), + open=Decimal(str(close)), + high=Decimal(str(high)), + low=Decimal(str(low)), + close=Decimal(str(close)), + volume=Decimal(str(volume)), + ) + + +def _configured_strategy() -> VwapStrategy: + strategy = VwapStrategy() + strategy.configure({"deviation_threshold": 0.01, "quantity": "0.01"}) + return strategy + + +def test_vwap_warmup_period(): + strategy = VwapStrategy() + assert strategy.warmup_period == 30 + + +def test_vwap_no_signal_insufficient_data(): + strategy = _configured_strategy() + # Feed fewer candles than warmup_period + for _ in range(29): + signal = strategy.on_candle(make_candle(100.0)) + assert signal is None + + +def test_vwap_buy_signal_below_vwap_recovery(): + strategy = _configured_strategy() + + # Build VWAP around 100 with 30 candles (satisfy warmup) + for _ in range(30): + strategy.on_candle(make_candle(100.0, high=101.0, low=99.0)) + + # Drop price well below VWAP to trigger _was_below_vwap + for _ in range(3): + strategy.on_candle(make_candle(95.0, high=96.0, low=94.0)) + + # Recover back to VWAP (close ~100, deviation within threshold) + signal = strategy.on_candle(make_candle(100.0, high=101.0, low=99.0)) + assert signal is not None + assert signal.side == OrderSide.BUY + assert "VWAP" in signal.reason + + +def test_vwap_sell_signal_above_vwap_recovery(): + strategy = _configured_strategy() + + # Build VWAP around 100 with 30 candles (satisfy warmup) + for _ in range(30): + strategy.on_candle(make_candle(100.0, high=101.0, low=99.0)) + + # Rise price well above VWAP to trigger _was_above_vwap + for _ in range(3): + strategy.on_candle(make_candle(105.0, high=106.0, low=104.0)) + + # Recover back to VWAP (close ~100, deviation within threshold) + signal = strategy.on_candle(make_candle(100.0, high=101.0, low=99.0)) + assert signal is not None + assert signal.side == OrderSide.SELL + assert "VWAP" in signal.reason + + +def test_vwap_reset_clears_state(): + strategy = _configured_strategy() + + # Build some state + for _ in range(35): + strategy.on_candle(make_candle(100.0)) + + strategy.reset() + + assert strategy._cumulative_tp_vol == 0.0 + assert strategy._cumulative_vol == 0.0 + assert strategy._candle_count == 0 + assert strategy._was_below_vwap is False + assert strategy._was_above_vwap is False |
