summaryrefslogtreecommitdiff
path: root/services/strategy-engine/tests/test_rsi_strategy.py
blob: 2a2f4e7199c7c793bd6979fdea7bd0587b217b5c (plain)
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
"""Tests for the RSI strategy."""

from datetime import datetime, timezone
from decimal import Decimal


from shared.models import Candle, OrderSide
from strategies.rsi_strategy import RsiStrategy


def make_candle(close: float, idx: int = 0) -> 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 test_rsi_strategy_no_signal_insufficient_data():
    strategy = RsiStrategy()
    strategy.configure({})
    candle = make_candle(50000.0)
    result = strategy.on_candle(candle)
    assert result is None


def test_rsi_strategy_buy_signal_on_oversold():
    strategy = RsiStrategy()
    strategy.configure({"period": 14, "oversold": 30, "overbought": 70})

    # Feed 20 steadily declining prices to force RSI into oversold territory
    prices = [50000 - i * 500 for i in range(20)]
    signal = None
    for i, price in enumerate(prices):
        signal = strategy.on_candle(make_candle(price, i))

    # We may or may not get a signal depending on RSI calculation;
    # if a signal is returned, it must be a BUY
    if signal is not None:
        assert signal.side == OrderSide.BUY