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
|
"""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
|