62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
|
|
"""Tests for VolatilityRegimeDetector."""
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
import numpy as np
|
||
|
|
from unittest.mock import Mock
|
||
|
|
from nautilus_dolphin.nautilus.volatility_detector import VolatilityRegimeDetector
|
||
|
|
|
||
|
|
|
||
|
|
class TestVolatilityRegimeDetector:
|
||
|
|
|
||
|
|
def test_insufficient_data_returns_true(self):
|
||
|
|
"""Permissive default when insufficient data."""
|
||
|
|
detector = VolatilityRegimeDetector(min_history=100)
|
||
|
|
assert detector.is_high_regime() == True
|
||
|
|
|
||
|
|
def test_update_calculates_volatility(self):
|
||
|
|
"""Test volatility calculation from bars."""
|
||
|
|
detector = VolatilityRegimeDetector(lookback_bars=50, min_history=10)
|
||
|
|
|
||
|
|
# Generate synthetic bars with increasing volatility
|
||
|
|
for i in range(60):
|
||
|
|
bar = Mock()
|
||
|
|
bar.close = 100 + np.random.randn() * (1 + i * 0.1)
|
||
|
|
detector.update(bar)
|
||
|
|
|
||
|
|
assert detector._current_vol is not None
|
||
|
|
assert len(detector._volatility_history) > 0
|
||
|
|
|
||
|
|
def test_high_regime_detection(self):
|
||
|
|
"""Test dual-threshold regime detection."""
|
||
|
|
detector = VolatilityRegimeDetector(lookback_bars=50, min_history=10)
|
||
|
|
|
||
|
|
# Low volatility period
|
||
|
|
for i in range(30):
|
||
|
|
bar = Mock()
|
||
|
|
bar.close = 100 + np.random.randn() * 0.1
|
||
|
|
detector.update(bar)
|
||
|
|
|
||
|
|
# High volatility period
|
||
|
|
for i in range(30):
|
||
|
|
bar = Mock()
|
||
|
|
bar.close = 100 + np.random.randn() * 5.0
|
||
|
|
detector.update(bar)
|
||
|
|
|
||
|
|
# Should detect high regime
|
||
|
|
assert detector.is_high_regime() == True
|
||
|
|
|
||
|
|
def test_regime_info(self):
|
||
|
|
"""Test regime info dict."""
|
||
|
|
detector = VolatilityRegimeDetector(lookback_bars=50, min_history=10)
|
||
|
|
|
||
|
|
for i in range(60):
|
||
|
|
bar = Mock()
|
||
|
|
bar.close = 100 + np.random.randn()
|
||
|
|
detector.update(bar)
|
||
|
|
|
||
|
|
info = detector.get_regime_info()
|
||
|
|
assert 'status' in info
|
||
|
|
assert 'current_vol' in info
|
||
|
|
assert 'p50' in info
|
||
|
|
assert 'p75' in info
|