Crate indicator

source ·
Expand description

Abstractions for stream aggregation that we call “Indicator” s.

This crate provides abstractions of different levels to build indicators:

Example

use indicator::*;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use time::macros::offset;
use arrayvec::ArrayVec;

/// Return an indicator that calculates `hl2` and `ohlc4` simultaneously.
fn hl2_ohlc4(period: Period) -> impl Operator<TickValue<Decimal>, Output = (Decimal, Decimal)> {
    tumbling(
        period,
        |_w: &ArrayVec<[Decimal; 4], 0>, y: &mut Option<[Decimal; 4]>, x| match y {
            Some(ohlc) => {
                ohlc[1] = ohlc[1].max(x);
                ohlc[2] = ohlc[2].min(x);
                ohlc[3] = x;
                *ohlc
            }
            None => {
                let ohlc = [x; 4];
                *y = Some(ohlc);
                ohlc
            }
        },
    )
    .then(facet_t(
        map_t(|ohlc: [Decimal; 4]| (ohlc[1] + ohlc[2]) / dec!(2)),
        map_t(|ohlc: [Decimal; 4]| (ohlc[0] + ohlc[1] + ohlc[2] + ohlc[3]) / dec!(4)),
    ))
    .map(|v| v.value)
}

Re-exports

Modules