"""Reference-value tests that pin numerical behaviour from the Python side.""" from __future__ import annotations import math import numpy as np import pytest import wickra as ta def test_sma_constant_series(): out = ta.SMA(5).batch(np.full(20, 42.0, dtype=np.float64)) # First 4 are warmup -> NaN; rest equal 42. assert np.all(np.isnan(out[:4])) assert np.allclose(out[4:], 42.0) def test_sma_known_window(): # SMA(3) of [2, 4, 6, 8, 10] -> [_, _, 4, 6, 8] out = ta.SMA(3).batch(np.array([2.0, 4.0, 6.0, 8.0, 10.0])) assert math.isnan(out[0]) and math.isnan(out[1]) np.testing.assert_allclose(out[2:], [4.0, 6.0, 8.0]) def test_ema_seed_equals_simple_mean_of_first_window(): # EMA(5) seed = mean([10, 20, 30, 40, 50]) = 30 out = ta.EMA(5).batch(np.array([10.0, 20.0, 30.0, 40.0, 50.0])) assert math.isnan(out[0]) assert math.isclose(out[4], 30.0, abs_tol=1e-12) def test_wma_known_window(): # WMA(4) of [1, 2, 3, 4] = (1*1 + 2*2 + 3*3 + 4*4)/10 = 3 out = ta.WMA(4).batch(np.array([1.0, 2.0, 3.0, 4.0])) assert math.isnan(out[0]) and math.isnan(out[1]) and math.isnan(out[2]) assert math.isclose(out[3], 3.0, abs_tol=1e-12) def test_rsi_pure_uptrend_is_100(): out = ta.RSI(14).batch(np.arange(1.0, 21.0, dtype=np.float64)) np.testing.assert_allclose(out[14:], 100.0) def test_rsi_pure_downtrend_is_0(): out = ta.RSI(14).batch(np.arange(20.0, 0.0, -1.0)) np.testing.assert_allclose(out[14:], 0.0) def test_rsi_flat_series_is_50(): out = ta.RSI(14).batch(np.full(30, 100.0)) np.testing.assert_allclose(out[14:], 50.0) def test_rsi_wilder_textbook_first_value(): """Wilder's original 14-period example, ~70.46 at the first emit.""" prices = np.array( [ 44.34, 44.09, 44.15, 43.61, 44.33, 44.83, 45.10, 45.42, 45.84, 46.08, 45.89, 46.03, 45.61, 46.28, 46.28, ], dtype=np.float64, ) out = ta.RSI(14).batch(prices) assert math.isclose(out[14], 70.464, abs_tol=0.05) def test_macd_constant_series_converges_to_zero(): out = ta.MACD().batch(np.full(200, 100.0)) # Last row's MACD and signal must be ~0. last = out[-1] assert math.isclose(last[0], 0.0, abs_tol=1e-9) assert math.isclose(last[1], 0.0, abs_tol=1e-9) assert math.isclose(last[2], 0.0, abs_tol=1e-9) def test_bollinger_constant_series_zero_width(): out = ta.BollingerBands(20, 2.0).batch(np.full(50, 100.0)) row = out[-1] np.testing.assert_allclose(row, [100.0, 100.0, 100.0, 0.0], atol=1e-12) def test_bollinger_upper_middle_lower_ordering(): out = ta.BollingerBands(20, 2.0).batch(np.linspace(50.0, 150.0, 100)) ready = out[~np.isnan(out[:, 0])] assert np.all(ready[:, 0] >= ready[:, 1]) assert np.all(ready[:, 1] >= ready[:, 2]) assert np.all(ready[:, 3] >= 0.0) def test_atr_constant_range_constant_output(): high = np.full(30, 11.0) low = np.full(30, 9.0) close = np.full(30, 10.0) out = ta.ATR(14).batch(high, low, close) # Once seeded, ATR equals the constant TR of 2. np.testing.assert_allclose(out[13:], 2.0, atol=1e-12) def test_stochastic_extremes(): # Close at the top of a 3-period range -> %K = 100. high = np.array([10.0, 11.0, 12.0]) low = np.array([8.0, 9.0, 10.0]) close = np.array([9.0, 10.0, 12.0]) out = ta.Stochastic(3, 1).batch(high, low, close) assert math.isclose(out[2, 0], 100.0, abs_tol=1e-12) def test_obv_cumulative_known_sequence(): close = np.array([10.0, 11.0, 10.5, 10.5, 12.0]) volume = np.array([100.0, 20.0, 30.0, 40.0, 10.0]) out = ta.OBV().batch(close, volume) np.testing.assert_allclose(out, [0.0, 20.0, -10.0, -10.0, 0.0])