Wickra 0.1.0: streaming-first technical indicators
A multi-language technical analysis library: 25 indicators across trend,
momentum, volatility, and volume families, every one a state machine with
O(1) per-tick updates. Batch evaluation is provided by a blanket extension
trait over the streaming primitive, so live trading bots and historical
backtests run the same code path.
What ships in this initial drop:
crates/wickra-core - 25 indicators, Indicator/BatchExt/Chain traits,
OHLCV types with validation; 171 unit tests,
property tests, Wilder/Bollinger textbook tests.
crates/wickra - top-level facade + criterion benches for every
indicator at 1K/10K/100K series sizes.
crates/wickra-data - streaming CSV reader, tick-to-candle aggregator,
multi-timeframe resampler, Binance Spot kline
WebSocket adapter behind feature live-binance;
11 unit + 1 doctest.
bindings/python - PyO3 + maturin, NumPy I/O, type stubs (.pyi),
56 pytest tests including streaming==batch
equivalence, Wilder reference values, lifecycle.
bindings/node - napi-rs native module, TypeScript .d.ts
auto-generated, 7 node --test cases.
bindings/wasm - wasm-bindgen ES module for browser/bundler/Node;
interactive HTML demo at examples/index.html.
examples/ - Python and Rust scripts: backtest, live trading,
parallel multi-asset, multi-timeframe, Binance.
benchmarks/ - cross-library comparison against TA-Lib,
pandas-ta, finta, talipp; Wickra wins every
category by 11-1030x (batch) and 17x+ streaming.
.github/workflows/ - CI matrix (Rust + Python + Node + WASM on
Linux/macOS/Windows), release pipeline for
PyPI wheels and npm.
Indicators (25):
Trend SMA EMA WMA DEMA TEMA HMA KAMA
Momentum RSI MACD Stochastic CCI ROC WilliamsR ADX MFI TRIX
AwesomeOscillator Aroon
Volatility BollingerBands ATR Keltner Donchian PSAR
Volume OBV VWAP (cumulative + rolling)
cargo clippy --workspace --all-targets -D warnings is clean. License: Apache-2.0.
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
//! Aroon Up / Down indicator.
|
||||
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::ohlcv::Candle;
|
||||
use crate::traits::Indicator;
|
||||
|
||||
/// Aroon output: up and down strengths in [0, 100].
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct AroonOutput {
|
||||
/// Time since the highest high, expressed as a percentage of the window.
|
||||
pub up: f64,
|
||||
/// Time since the lowest low, same convention.
|
||||
pub down: f64,
|
||||
}
|
||||
|
||||
/// Aroon indicator: tracks how many bars since the highest high and lowest low
|
||||
/// inside a `period + 1`-bar window. Returned as a percentage.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Aroon {
|
||||
period: usize,
|
||||
candles: VecDeque<Candle>,
|
||||
}
|
||||
|
||||
impl Aroon {
|
||||
/// # Errors
|
||||
/// Returns [`Error::PeriodZero`] if `period == 0`.
|
||||
pub fn new(period: usize) -> Result<Self> {
|
||||
if period == 0 {
|
||||
return Err(Error::PeriodZero);
|
||||
}
|
||||
Ok(Self {
|
||||
period,
|
||||
candles: VecDeque::with_capacity(period + 1),
|
||||
})
|
||||
}
|
||||
|
||||
/// Configured period.
|
||||
pub const fn period(&self) -> usize {
|
||||
self.period
|
||||
}
|
||||
}
|
||||
|
||||
impl Indicator for Aroon {
|
||||
type Input = Candle;
|
||||
type Output = AroonOutput;
|
||||
|
||||
fn update(&mut self, candle: Candle) -> Option<AroonOutput> {
|
||||
if self.candles.len() == self.period + 1 {
|
||||
self.candles.pop_front();
|
||||
}
|
||||
self.candles.push_back(candle);
|
||||
if self.candles.len() < self.period + 1 {
|
||||
return None;
|
||||
}
|
||||
// Find the index (0 = oldest) of the highest high and lowest low.
|
||||
let (mut hh_idx, mut ll_idx) = (0_usize, 0_usize);
|
||||
let (mut hh, mut ll) = (f64::NEG_INFINITY, f64::INFINITY);
|
||||
for (i, c) in self.candles.iter().enumerate() {
|
||||
if c.high >= hh {
|
||||
hh = c.high;
|
||||
hh_idx = i;
|
||||
}
|
||||
if c.low <= ll {
|
||||
ll = c.low;
|
||||
ll_idx = i;
|
||||
}
|
||||
}
|
||||
let n = self.period as f64;
|
||||
let up = 100.0 * hh_idx as f64 / n;
|
||||
let down = 100.0 * ll_idx as f64 / n;
|
||||
Some(AroonOutput { up, down })
|
||||
}
|
||||
|
||||
fn reset(&mut self) {
|
||||
self.candles.clear();
|
||||
}
|
||||
|
||||
fn warmup_period(&self) -> usize {
|
||||
self.period + 1
|
||||
}
|
||||
|
||||
fn is_ready(&self) -> bool {
|
||||
self.candles.len() == self.period + 1
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"Aroon"
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::traits::BatchExt;
|
||||
use approx::assert_relative_eq;
|
||||
|
||||
fn c(h: f64, l: f64, cl: f64) -> Candle {
|
||||
Candle::new(cl, h, l, cl, 1.0, 0).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pure_uptrend_aroon_up_100() {
|
||||
let candles: Vec<Candle> = (1..=15)
|
||||
.map(|i| c(f64::from(i) + 1.0, f64::from(i) - 1.0, f64::from(i)))
|
||||
.collect();
|
||||
let mut a = Aroon::new(14).unwrap();
|
||||
let last = a.batch(&candles).into_iter().flatten().last().unwrap();
|
||||
assert_relative_eq!(last.up, 100.0, epsilon = 1e-9);
|
||||
// The lowest low is at the oldest position (index 0).
|
||||
assert_relative_eq!(last.down, 0.0, epsilon = 1e-9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pure_downtrend_aroon_down_100() {
|
||||
let candles: Vec<Candle> = (1..=15)
|
||||
.rev()
|
||||
.map(|i| c(f64::from(i) + 1.0, f64::from(i) - 1.0, f64::from(i)))
|
||||
.collect();
|
||||
let mut a = Aroon::new(14).unwrap();
|
||||
let last = a.batch(&candles).into_iter().flatten().last().unwrap();
|
||||
assert_relative_eq!(last.down, 100.0, epsilon = 1e-9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn batch_equals_streaming() {
|
||||
let candles: Vec<Candle> = (0..40)
|
||||
.map(|i| {
|
||||
let m = 50.0 + (f64::from(i) * 0.3).sin() * 5.0;
|
||||
c(m + 1.0, m - 1.0, m)
|
||||
})
|
||||
.collect();
|
||||
let mut a = Aroon::new(14).unwrap();
|
||||
let mut b = Aroon::new(14).unwrap();
|
||||
assert_eq!(
|
||||
a.batch(&candles),
|
||||
candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn outputs_in_range() {
|
||||
let candles: Vec<Candle> = (0..200)
|
||||
.map(|i| {
|
||||
let m = 50.0 + (f64::from(i) * 0.2).sin() * 5.0;
|
||||
c(m + 1.0, m - 1.0, m)
|
||||
})
|
||||
.collect();
|
||||
let mut a = Aroon::new(14).unwrap();
|
||||
for o in a.batch(&candles).into_iter().flatten() {
|
||||
assert!((0.0..=100.0).contains(&o.up));
|
||||
assert!((0.0..=100.0).contains(&o.down));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user