Files
crypto_trader/tests/unit/strategies/test_rsi_strategy.py

68 lines
2.2 KiB
Python

"""Tests for RSI strategy."""
import pytest
import pandas as pd
from src.strategies.technical.rsi_strategy import RSIStrategy
class TestRSIStrategy:
"""Tests for RSIStrategy."""
@pytest.fixture
def strategy(self):
"""Create RSI strategy instance."""
return RSIStrategy(
strategy_id=1,
name="test_rsi",
symbol="BTC/USD",
timeframe="1h",
parameters={
"rsi_period": 14,
"overbought": 70,
"oversold": 30
}
)
@pytest.fixture
def sample_data(self):
"""Create sample price data."""
dates = pd.date_range(start='2025-01-01', periods=50, freq='1H')
# Create data with clear trend for RSI calculation
prices = [100 - i * 0.5 for i in range(50)] # Downward trend
return pd.DataFrame({
'timestamp': dates,
'open': prices,
'high': [p + 1 for p in prices],
'low': [p - 1 for p in prices],
'close': prices,
'volume': [1000.0] * 50
})
@pytest.mark.asyncio
async def test_rsi_strategy_initialization(self, strategy):
"""Test RSI strategy initialization."""
assert strategy.rsi_period == 14
assert strategy.overbought == 70
assert strategy.oversold == 30
@pytest.mark.asyncio
async def test_on_data(self, strategy, sample_data):
"""Test on_data method."""
await strategy.on_data(sample_data)
assert len(strategy.current_data) > 0
@pytest.mark.asyncio
async def test_generate_signal_oversold(self, strategy, sample_data):
"""Test signal generation for oversold condition."""
await strategy.on_data(sample_data)
# Calculate RSI - should be low for downward trend
from src.data.indicators import get_indicators
indicators = get_indicators()
rsi = indicators.rsi(strategy.current_data['close'], period=14)
# If RSI is low, should generate buy signal
signal = await strategy.generate_signal()
assert "signal" in signal
assert signal["signal"] in ["buy", "sell", "hold"]