"""Tests for the Grid strategy.""" from datetime import datetime, timezone from decimal import Decimal from shared.models import Candle, OrderSide from strategies.grid_strategy import GridStrategy def make_candle(close: float) -> Candle: return Candle( symbol="BTC/USDT", timeframe="1m", open_time=datetime(2024, 1, 1, tzinfo=timezone.utc), open=Decimal(str(close)), high=Decimal(str(close)), low=Decimal(str(close)), close=Decimal(str(close)), volume=Decimal("1.0"), ) def _configured_strategy() -> GridStrategy: strategy = GridStrategy() strategy.configure( { "lower_price": 48000, "upper_price": 52000, "grid_count": 5, "quantity": "0.01", } ) return strategy def test_grid_strategy_buy_at_lower_grid(): strategy = _configured_strategy() # First candle: establish zone at upper area strategy.on_candle(make_candle(51500)) # Second candle: price drops to lower zone → BUY signal = strategy.on_candle(make_candle(48100)) assert signal is not None assert signal.side == OrderSide.BUY def test_grid_strategy_sell_at_upper_grid(): strategy = _configured_strategy() # First candle: establish zone at lower area strategy.on_candle(make_candle(48100)) # Second candle: price rises to upper zone → SELL signal = strategy.on_candle(make_candle(51900)) assert signal is not None assert signal.side == OrderSide.SELL def test_grid_strategy_no_signal_in_same_zone(): strategy = _configured_strategy() # Both candles in approximately the same zone strategy.on_candle(make_candle(50000)) signal = strategy.on_candle(make_candle(50100)) assert signal is None