summaryrefslogtreecommitdiff
path: root/services/strategy-engine/tests/test_strategy_validation.py
blob: 0d9607aabee76ea8e241dfea9455617c5dd5a038 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import pytest
from strategies.bollinger_strategy import BollingerStrategy
from strategies.ema_crossover_strategy import EmaCrossoverStrategy
from strategies.grid_strategy import GridStrategy
from strategies.macd_strategy import MacdStrategy
from strategies.rsi_strategy import RsiStrategy
from strategies.volume_profile_strategy import VolumeProfileStrategy
from strategies.vwap_strategy import VwapStrategy

# ── RSI ──────────────────────────────────────────────────────────────────


class TestRsiValidation:
    def test_valid_params(self):
        s = RsiStrategy()
        s.configure({"period": 14, "oversold": 30, "overbought": 70, "quantity": "0.01"})

    def test_period_too_small(self):
        s = RsiStrategy()
        with pytest.raises(ValueError, match="period must be >= 2"):
            s.configure({"period": 1})

    def test_oversold_gte_overbought(self):
        s = RsiStrategy()
        with pytest.raises(ValueError, match="thresholds"):
            s.configure({"oversold": 70, "overbought": 30})

    def test_oversold_equals_overbought(self):
        s = RsiStrategy()
        with pytest.raises(ValueError, match="thresholds"):
            s.configure({"oversold": 50, "overbought": 50})

    def test_oversold_zero(self):
        s = RsiStrategy()
        with pytest.raises(ValueError, match="thresholds"):
            s.configure({"oversold": 0, "overbought": 70})

    def test_overbought_100(self):
        s = RsiStrategy()
        with pytest.raises(ValueError, match="thresholds"):
            s.configure({"oversold": 30, "overbought": 100})

    def test_quantity_zero(self):
        s = RsiStrategy()
        with pytest.raises(ValueError, match="Quantity must be positive"):
            s.configure({"quantity": "0"})

    def test_quantity_negative(self):
        s = RsiStrategy()
        with pytest.raises(ValueError, match="Quantity must be positive"):
            s.configure({"quantity": "-1"})


# ── MACD ─────────────────────────────────────────────────────────────────


class TestMacdValidation:
    def test_valid_params(self):
        s = MacdStrategy()
        s.configure({"fast_period": 12, "slow_period": 26, "signal_period": 9, "quantity": "0.05"})

    def test_fast_gte_slow(self):
        s = MacdStrategy()
        with pytest.raises(ValueError, match="fast_period must be < slow_period"):
            s.configure({"fast_period": 26, "slow_period": 12})

    def test_fast_equals_slow(self):
        s = MacdStrategy()
        with pytest.raises(ValueError, match="fast_period must be < slow_period"):
            s.configure({"fast_period": 12, "slow_period": 12})

    def test_fast_period_too_small(self):
        s = MacdStrategy()
        with pytest.raises(ValueError, match="fast_period must be >= 2"):
            s.configure({"fast_period": 1, "slow_period": 26})

    def test_signal_period_too_small(self):
        s = MacdStrategy()
        with pytest.raises(ValueError, match="signal_period must be >= 2"):
            s.configure({"signal_period": 1})

    def test_quantity_zero(self):
        s = MacdStrategy()
        with pytest.raises(ValueError, match="Quantity must be positive"):
            s.configure({"quantity": "0"})


# ── Bollinger ────────────────────────────────────────────────────────────


class TestBollingerValidation:
    def test_valid_params(self):
        s = BollingerStrategy()
        s.configure({"period": 20, "num_std": 2.0, "quantity": "0.01"})

    def test_period_too_small(self):
        s = BollingerStrategy()
        with pytest.raises(ValueError, match="period must be >= 2"):
            s.configure({"period": 1})

    def test_num_std_zero(self):
        s = BollingerStrategy()
        with pytest.raises(ValueError, match="num_std must be > 0"):
            s.configure({"num_std": 0})

    def test_num_std_negative(self):
        s = BollingerStrategy()
        with pytest.raises(ValueError, match="num_std must be > 0"):
            s.configure({"num_std": -1.0})

    def test_quantity_zero(self):
        s = BollingerStrategy()
        with pytest.raises(ValueError, match="Quantity must be positive"):
            s.configure({"quantity": "0"})


# ── EMA Crossover ────────────────────────────────────────────────────────


class TestEmaCrossoverValidation:
    def test_valid_params(self):
        s = EmaCrossoverStrategy()
        s.configure({"short_period": 9, "long_period": 21, "quantity": "0.01"})

    def test_short_gte_long(self):
        s = EmaCrossoverStrategy()
        with pytest.raises(ValueError, match="short_period must be < long_period"):
            s.configure({"short_period": 21, "long_period": 9})

    def test_short_equals_long(self):
        s = EmaCrossoverStrategy()
        with pytest.raises(ValueError, match="short_period must be < long_period"):
            s.configure({"short_period": 10, "long_period": 10})

    def test_short_period_too_small(self):
        s = EmaCrossoverStrategy()
        with pytest.raises(ValueError, match="short_period must be >= 2"):
            s.configure({"short_period": 1, "long_period": 21})

    def test_quantity_zero(self):
        s = EmaCrossoverStrategy()
        with pytest.raises(ValueError, match="Quantity must be positive"):
            s.configure({"quantity": "0"})


# ── Grid ─────────────────────────────────────────────────────────────────


class TestGridValidation:
    def test_valid_params(self):
        s = GridStrategy()
        s.configure({"lower_price": 100, "upper_price": 200, "grid_count": 5, "quantity": "0.01"})

    def test_lower_gte_upper(self):
        s = GridStrategy()
        with pytest.raises(ValueError, match="lower_price must be < upper_price"):
            s.configure({"lower_price": 200, "upper_price": 100})

    def test_lower_equals_upper(self):
        s = GridStrategy()
        with pytest.raises(ValueError, match="lower_price must be < upper_price"):
            s.configure({"lower_price": 100, "upper_price": 100})

    def test_grid_count_too_small(self):
        s = GridStrategy()
        with pytest.raises(ValueError, match="grid_count must be >= 2"):
            s.configure({"lower_price": 100, "upper_price": 200, "grid_count": 1})

    def test_quantity_zero(self):
        s = GridStrategy()
        with pytest.raises(ValueError, match="Quantity must be positive"):
            s.configure({"lower_price": 100, "upper_price": 200, "quantity": "0"})


# ── VWAP ─────────────────────────────────────────────────────────────────


class TestVwapValidation:
    def test_valid_params(self):
        s = VwapStrategy()
        s.configure({"deviation_threshold": 0.002, "quantity": "0.01"})

    def test_deviation_threshold_zero(self):
        s = VwapStrategy()
        with pytest.raises(ValueError, match="deviation_threshold must be > 0"):
            s.configure({"deviation_threshold": 0})

    def test_deviation_threshold_negative(self):
        s = VwapStrategy()
        with pytest.raises(ValueError, match="deviation_threshold must be > 0"):
            s.configure({"deviation_threshold": -0.01})

    def test_quantity_zero(self):
        s = VwapStrategy()
        with pytest.raises(ValueError, match="Quantity must be positive"):
            s.configure({"quantity": "0"})


# ── Volume Profile ───────────────────────────────────────────────────────


class TestVolumeProfileValidation:
    def test_valid_params(self):
        s = VolumeProfileStrategy()
        s.configure(
            {
                "lookback_period": 100,
                "num_bins": 50,
                "value_area_pct": 0.7,
                "quantity": "0.01",
            }
        )

    def test_lookback_too_small(self):
        s = VolumeProfileStrategy()
        with pytest.raises(ValueError, match="lookback_period must be >= 2"):
            s.configure({"lookback_period": 1})

    def test_num_bins_too_small(self):
        s = VolumeProfileStrategy()
        with pytest.raises(ValueError, match="num_bins must be >= 2"):
            s.configure({"num_bins": 1})

    def test_value_area_pct_zero(self):
        s = VolumeProfileStrategy()
        with pytest.raises(ValueError, match="value_area_pct"):
            s.configure({"value_area_pct": 0})

    def test_value_area_pct_negative(self):
        s = VolumeProfileStrategy()
        with pytest.raises(ValueError, match="value_area_pct"):
            s.configure({"value_area_pct": -0.5})

    def test_value_area_pct_above_one(self):
        s = VolumeProfileStrategy()
        with pytest.raises(ValueError, match="value_area_pct"):
            s.configure({"value_area_pct": 1.5})

    def test_value_area_pct_exactly_one(self):
        """value_area_pct=1.0 is valid (100% of volume)."""
        s = VolumeProfileStrategy()
        s.configure({"value_area_pct": 1.0})

    def test_quantity_zero(self):
        s = VolumeProfileStrategy()
        with pytest.raises(ValueError, match="Quantity must be positive"):
            s.configure({"quantity": "0"})