summaryrefslogtreecommitdiff
path: root/services/strategy-engine/tests/test_grid_strategy.py
diff options
context:
space:
mode:
Diffstat (limited to 'services/strategy-engine/tests/test_grid_strategy.py')
-rw-r--r--services/strategy-engine/tests/test_grid_strategy.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/services/strategy-engine/tests/test_grid_strategy.py b/services/strategy-engine/tests/test_grid_strategy.py
new file mode 100644
index 0000000..d96ebba
--- /dev/null
+++ b/services/strategy-engine/tests/test_grid_strategy.py
@@ -0,0 +1,60 @@
+"""Tests for the Grid strategy."""
+from datetime import datetime, timezone
+from decimal import Decimal
+
+import pytest
+
+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