466faddd87
* feat(alma): add Arnaud Legoux Moving Average
Gaussian-weighted moving average with configurable centre (offset in
[0, 1]) and kernel width (sigma > 0). Pre-computes normalised weights
at construction so each update is a single rolling window dot product.
Reference: Arnaud Legoux and Dimitrios Kouzis-Loukas, 2009.
Touchpoints:
- crates/wickra-core: alma.rs + mod.rs + lib.rs re-export
- bindings/python: PyAlma + __init__.py + test_new_indicators +
test_known_values reference
- bindings/node: AlmaNode + index.d.ts/index.js + indicators.test.js
factory + reference value
- bindings/wasm: wasm_scalar_indicator! macro
- fuzz: indicator_update target covers ALMA(9, 0.85, 6.0)
- crates/wickra/benches: bench_scalar entry
- README + CHANGELOG: Moving Averages row + Unreleased entry
* feat(mcginley): add McGinley Dynamic moving average
John McGinley's self-adjusting moving average with the recurrence
MD + (price - MD) / (0.6 * period * (price / MD)^4). Speeds up when
price falls below the indicator and damps when price runs above the
indicator. Seeded with the simple average of the first period inputs.
Reference: McGinley, Technical Analysis of Stocks & Commodities, 1990.
Touchpoints:
- crates/wickra-core: mcginley_dynamic.rs + mod.rs + lib.rs re-export
- bindings/python: PyMcGinleyDynamic + __init__.py + test_new_indicators
+ test_known_values reference
- bindings/node: McGinleyDynamicNode (scalar macro) + index.d.ts/index.js
+ indicators.test.js factory + reference value
- bindings/wasm: wasm_scalar_indicator! macro
- fuzz: indicator_update target covers McGinleyDynamic(10)
- crates/wickra/benches: bench_scalar entry
- README + CHANGELOG: Moving Averages row + Unreleased entry
* feat(frama): add Fractal Adaptive Moving Average
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
* feat(vidya): add Variable Index Dynamic Average
Chande's VIDYA — an EMA whose alpha scales with |CMO(cmo_period)| / 100.
Strong directional momentum lifts the smoothing constant toward the
EMA-of-period rate; flat or choppy windows shrink it toward zero so
VIDYA coasts on its previous value. Two parameters: period (14) and
cmo_period (9). Reuses the existing wickra-core Cmo internally.
Reference: Chande, Stocks & Commodities, 1992.
Also fixes a silent gap from d37fbd1 (feat(frama)): the PyFrama Python
class wrapper and its add_class registration were dropped because the
two edits hit "File has not been read yet" errors that scrolled past
in a batch. Adds them here alongside VIDYA's bindings.
Touchpoints (VIDYA): vidya.rs + mod.rs + lib.rs re-export, PyVidya +
__init__.py + test_new_indicators + test_known_values reference,
VidyaNode (manual two-param binding) + index.d.ts/index.js +
indicators.test.js factory + reference, wasm_scalar_indicator! macro,
fuzz target, bench, README + CHANGELOG.
* feat(jma): add Jurik Moving Average
Three-stage filter reconstruction of Mark Jurik's adaptive MA (the
algorithm is proprietary; this is the form used by most open-source
ports since the 1999 TASC article). Parameters: period (14), phase in
[-100, 100] (0), power in 1..=4 (2). State is seeded by setting
e0 = JMA = first input so a constant input stream is reproduced exactly.
Touchpoints: jma.rs + mod.rs + lib.rs re-export, PyJma + __init__.py +
test_new_indicators + test_known_values reference, JmaNode (manual
three-param binding) + index.d.ts/index.js + indicators.test.js factory
+ reference, wasm_scalar_indicator! macro, fuzz target, bench, README +
CHANGELOG.
* feat(alligator): add Bill Williams Alligator
Three SMMA lines (Jaw / Teeth / Lips) over the median price
(high + low) / 2 with default periods 13 / 8 / 5. Multi-output
indicator returning AlligatorOutput { jaw, teeth, lips }. The
original chart variant shifts each line forward for display; we
publish the unshifted SMMA values and leave the visual shift to
the consumer.
Reference: Bill Williams, Trading Chaos, 1995.
Touchpoints: alligator.rs + mod.rs + lib.rs re-export, PyAlligator
(Candle input, returns 3-tuple, ndarray (n, 3) batch) + __init__.py
+ test_new_indicators + test_known_values reference, AlligatorNode +
AlligatorValue + index.d.ts/index.js + indicators.test.js multi
factory + reference, WasmAlligator (manual JsValue object) +
candle-fuzz target + README + CHANGELOG.
* feat(evwma): add Elastic Volume-Weighted Moving Average
Christian P. Fries' elastic recurrence where the smoothing weight is the
bar's volume relative to the running window total:
V_sum_t = sum of volumes over the last period candles
EVWMA_t = ((V_sum_t - v_t) * EVWMA_{t-1} + v_t * close_t) / V_sum_t
A bar whose volume is small barely moves the average; a bar that
dominates the window pulls it strongly toward that bar's close. Seeded
with the close of the first full window; holds its previous value if
the entire window has zero volume.
Reference: Fries, Wilmott Magazine, 2001.
Touchpoints: evwma.rs + mod.rs + lib.rs re-export, PyEvwma (close +
volume batch) + __init__.py + test_new_indicators CANDLE_SCALAR +
test_known_values reference, EvwmaNode + index.d.ts/index.js +
indicators.test.js candleScalar factory + reference, WasmEvwma,
candle-fuzz target + README + CHANGELOG.
* ci: Force local wheel install in Python jobs
Use --no-index --no-deps so the Python matrix installs the freshly
built wheel from dist/ and never falls back to PyPI. Previously pip
sometimes picked the released 0.2.x wheel on macOS / Windows when its
platform tag was a wider match than the local build, which made the
job test the released package and miss any new symbols added in the
PR (e.g. AttributeError: module 'wickra' has no attribute 'ALMA').
numpy is already installed by the preceding pip step, so --no-deps
is safe.
206 lines
7.5 KiB
Python
206 lines
7.5 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_jma_constant_series_yields_the_constant():
|
|
# JMA seeds e0 and the output to the first input, so a constant series
|
|
# is reproduced exactly from the first sample.
|
|
out = ta.JMA(14, 0.0, 2).batch(np.full(30, 42.0, dtype=np.float64))
|
|
np.testing.assert_allclose(out, 42.0, atol=1e-12)
|
|
|
|
|
|
def test_evwma_reference_value_period_2():
|
|
# EVWMA(2). Bars: (close, volume) = (10, 1), (20, 3), (30, 1).
|
|
# Bar 2: sum_v = 4, seeded prev = 20, EVWMA = (1*20 + 3*20)/4 = 20.
|
|
# Bar 3: sum_v = 4 (drops 1, gains 1), EVWMA = (3*20 + 1*30)/4 = 22.5.
|
|
out = ta.EVWMA(2).batch(np.array([10.0, 20.0, 30.0]), np.array([1.0, 3.0, 1.0]))
|
|
assert math.isnan(out[0])
|
|
assert math.isclose(out[1], 20.0, abs_tol=1e-12)
|
|
assert math.isclose(out[2], 22.5, abs_tol=1e-12)
|
|
|
|
|
|
def test_alligator_constant_series_holds_at_median_price():
|
|
# Median price = (11 + 9) / 2 = 10 on every candle, so all three SMMAs
|
|
# seed at 10 and stay there.
|
|
n = 30
|
|
high = np.full(n, 11.0)
|
|
low = np.full(n, 9.0)
|
|
out = ta.Alligator(13, 8, 5).batch(high, low)
|
|
assert out.shape == (n, 3)
|
|
for row in out[12:]:
|
|
assert math.isclose(row[0], 10.0, abs_tol=1e-12)
|
|
assert math.isclose(row[1], 10.0, abs_tol=1e-12)
|
|
assert math.isclose(row[2], 10.0, abs_tol=1e-12)
|
|
|
|
|
|
def test_vidya_constant_series_holds_seed():
|
|
# CMO = 0 on a flat series -> alpha = 0 -> VIDYA holds its seed value.
|
|
out = ta.VIDYA(14, 4).batch(np.full(20, 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_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])
|