54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
|
|
"""Tests for Consensus (ensemble) strategy."""
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
from decimal import Decimal
|
||
|
|
from src.strategies.ensemble.consensus_strategy import ConsensusStrategy
|
||
|
|
from src.strategies.base import SignalType
|
||
|
|
|
||
|
|
|
||
|
|
class TestConsensusStrategy:
|
||
|
|
"""Tests for ConsensusStrategy."""
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def strategy(self):
|
||
|
|
"""Create Consensus strategy instance."""
|
||
|
|
return ConsensusStrategy(
|
||
|
|
name="test_consensus",
|
||
|
|
parameters={
|
||
|
|
'strategy_names': ['rsi', 'macd'],
|
||
|
|
'min_consensus': 2,
|
||
|
|
'use_weights': True,
|
||
|
|
'min_weight': 0.3,
|
||
|
|
'exclude_strategies': []
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
def test_initialization(self, strategy):
|
||
|
|
"""Test strategy initialization."""
|
||
|
|
assert strategy.min_consensus == 2
|
||
|
|
assert strategy.use_weights is True
|
||
|
|
assert strategy.min_weight == 0.3
|
||
|
|
|
||
|
|
def test_on_tick_no_strategies(self, strategy):
|
||
|
|
"""Test that strategy handles empty strategy list."""
|
||
|
|
# Strategy should handle cases where no strategies are available
|
||
|
|
signal = strategy.on_tick(
|
||
|
|
symbol="BTC/USD",
|
||
|
|
price=Decimal("50000"),
|
||
|
|
timeframe="1h",
|
||
|
|
data={'volume': 1000}
|
||
|
|
)
|
||
|
|
# May return None if no strategies available or no consensus
|
||
|
|
assert signal is None or isinstance(signal, (type(None), object))
|
||
|
|
|
||
|
|
def test_strategy_metadata(self, strategy):
|
||
|
|
"""Test strategy metadata."""
|
||
|
|
assert strategy.name == "test_consensus"
|
||
|
|
assert strategy.enabled is False
|
||
|
|
|
||
|
|
def test_consensus_calculation(self, strategy):
|
||
|
|
"""Test consensus calculation parameters."""
|
||
|
|
assert strategy.min_consensus == 2
|
||
|
|
assert strategy.use_weights is True
|
||
|
|
|