d37fbd10f6
Ehlers' FRAMA adapts its smoothing constant to the fractal dimension of the recent window: tight tracking in trends, heavy smoothing in chop. Uses the close-only variant where max/min over each window half drive the dimension estimate. Period must be even (default 16). Reference: Ehlers, Fractal Adaptive Moving Average, 2005. Touchpoints: - crates/wickra-core: frama.rs + mod.rs + lib.rs re-export - bindings/python: PyFrama + __init__.py + test_new_indicators + test_known_values reference (constant series + uptrend tracking) - bindings/node: FramaNode (scalar macro) + index.d.ts/index.js + indicators.test.js factory + reference value - bindings/wasm: wasm_scalar_indicator! macro - fuzz: indicator_update target covers Frama(16) - crates/wickra/benches: bench_scalar entry - README + CHANGELOG: Moving Averages row + Unreleased entry
168 lines
5.9 KiB
Python
168 lines
5.9 KiB
Python
"""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_alma_constant_series_yields_the_constant():
|
|
# ALMA's Gaussian weights are normalised, so any constant series is
|
|
# reproduced exactly after warmup.
|
|
out = ta.ALMA(9, 0.85, 6.0).batch(np.full(30, 42.0, dtype=np.float64))
|
|
assert np.all(np.isnan(out[:8]))
|
|
np.testing.assert_allclose(out[8:], 42.0, atol=1e-12)
|
|
|
|
|
|
def test_alma_reference_value_period_3():
|
|
# ALMA(period=3, offset=0.85, sigma=6) on [10, 20, 30].
|
|
# m = 0.85 * 2 = 1.7; s = 3 / 6 = 0.5; 2*s^2 = 0.5.
|
|
out = ta.ALMA(3, 0.85, 6.0).batch(np.array([10.0, 20.0, 30.0]))
|
|
assert math.isnan(out[0]) and math.isnan(out[1])
|
|
# Independently compute the expected Gaussian-weighted sum.
|
|
w = np.exp(-((np.arange(3, dtype=np.float64) - 1.7) ** 2) / 0.5)
|
|
expected = float(np.dot([10.0, 20.0, 30.0], w) / w.sum())
|
|
assert math.isclose(out[2], expected, abs_tol=1e-12)
|
|
# Sanity: heavy offset toward the newest sample lifts the average above
|
|
# the simple mean of 20.
|
|
assert out[2] > 20.0
|
|
|
|
|
|
def test_mcginley_dynamic_constant_series_yields_the_constant():
|
|
# ratio = 1, so the recurrence collapses to MD + 0 / divisor = MD.
|
|
out = ta.McGinleyDynamic(5).batch(np.full(30, 42.0, dtype=np.float64))
|
|
assert np.all(np.isnan(out[:4]))
|
|
np.testing.assert_allclose(out[4:], 42.0, atol=1e-12)
|
|
|
|
|
|
def test_mcginley_dynamic_reference_value():
|
|
# Period 3, seed = SMA([10, 20, 30]) = 20.0. Next price 40.0:
|
|
# ratio = 2; divisor = 0.6 * 3 * 16 = 28.8; next = 20 + 20/28.8.
|
|
out = ta.McGinleyDynamic(3).batch(np.array([10.0, 20.0, 30.0, 40.0]))
|
|
assert math.isnan(out[0]) and math.isnan(out[1])
|
|
assert math.isclose(out[2], 20.0, abs_tol=1e-12)
|
|
expected = 20.0 + 20.0 / (0.6 * 3.0 * 16.0)
|
|
assert math.isclose(out[3], expected, abs_tol=1e-12)
|
|
|
|
|
|
def test_frama_constant_series_yields_the_constant():
|
|
# Flat input -> degenerate ranges -> alpha clamps to 0.01 and the EMA
|
|
# recurrence holds the seed value.
|
|
out = ta.FRAMA(4).batch(np.full(20, 42.0, dtype=np.float64))
|
|
assert np.all(np.isnan(out[:3]))
|
|
np.testing.assert_allclose(out[3:], 42.0, atol=1e-12)
|
|
|
|
|
|
def test_frama_pure_uptrend_hugs_latest():
|
|
# Monotonic uptrend -> alpha pushed toward 1.0, FRAMA tracks close.
|
|
out = ta.FRAMA(4).batch(np.arange(1.0, 9.0, dtype=np.float64))
|
|
assert math.isclose(out[-1], 8.0, 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])
|