tulip_rs 0.1.6

High-performance technical analysis library — 100+ indicators and 60+ candlestick patterns with SIMD acceleration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use crate::common::{min_process, validate_inputs};
pub use crate::indicator_types::TIndicatorState;
use crate::indicators::ema::{
    calc as calc_ema, multiplier as ema_multiplier, output_length as ema_output_length,
};
use crate::types::{DisplayType, IndicatorError, IndicatorInfoOrInteger, IndicatorType, Info};
use serde::{Deserialize, Serialize};

/// Number of input price series required by this indicator.
pub const INPUTS_WIDTH: usize = 4;

/// Number of option parameters required by this indicator.
pub const OPTIONS_WIDTH: usize = 2;

/// SIMD-parallel variant that processes `N` assets with identical options simultaneously.
/// Requires the `simd_assets` Cargo feature. See [`by_assets`] for the module form.
#[cfg(feature = "simd_assets")]
pub use crate::indicators::simd_indicators::kvo_simd::indicator_by_assets;

/// SIMD-parallel variant that processes a single asset with `N` different option
/// sets simultaneously. Requires the `simd_options` Cargo feature. See [`by_options`].
#[cfg(feature = "simd_options")]
pub use crate::indicators::simd_indicators::kvo_simd::indicator_by_options;

/// Convenience module that re-exports [`indicator_by_assets`] as `indicator`,
/// allowing SIMD multi-asset computation to be used as a drop-in replacement
/// for the standard single-asset [`indicator`] function.
/// Requires the `simd_assets` Cargo feature.
#[cfg(feature = "simd_assets")]
pub mod by_assets {
    /// Processes `N` assets in parallel with shared options.
    /// See the parent module's [`super::indicator_by_assets`] for full documentation.
    pub use crate::indicators::simd_indicators::kvo_simd::indicator_by_assets as indicator;
}

/// Convenience module that re-exports [`indicator_by_options`] as `indicator`,
/// allowing SIMD multi-option computation to be used as a drop-in replacement
/// for the standard single-asset [`indicator`] function.
/// Requires the `simd_options` Cargo feature.
#[cfg(feature = "simd_options")]
pub mod by_options {
    /// Processes a single asset with `N` different option sets in parallel.
    /// See the parent module's [`super::indicator_by_options`] for full documentation.
    pub use crate::indicators::simd_indicators::kvo_simd::indicator_by_options as indicator;
}

/// Returns information about the Klinger Volume Oscillator (KVO) indicator.
///
/// # Returns
///
/// An `Info` struct containing metadata about the KVO indicator.
pub fn info() -> Info<'static> {
    Info {
        name: "kvo",
        display_type: DisplayType::Overlay,
        indicator_type: IndicatorType::Volume,
        full_name: "Klinger Volume Oscillator",
        inputs: &["high", "low", "close", "volume"],
        options: &["short_period", "long_period"],
        outputs: &["kvo"],
        optional_outputs: &["short_ema", "long_ema"],
    }
}

#[derive(Serialize, Deserialize)]
pub struct IndicatorState {
    state: State,
    multipliers: ((f64, f64), (f64, f64)),
}
impl IndicatorState {
    pub fn new(multipliers: ((f64, f64), (f64, f64)), state: State) -> Self {
        Self { multipliers, state }
    }
}
impl TIndicatorState<4> for IndicatorState {
    fn batch_indicator(
        &mut self,
        inputs: &[&[f64]; INPUTS_WIDTH],
        optional_outputs: Option<&[bool]>,
    ) -> Result<Vec<Vec<f64>>, IndicatorError> {
        validate_inputs(inputs, 1)?;

        let (mut kvo_line, mut short_ema_line, mut long_ema_line);
        {
            let capacity = inputs[0].len();
            (short_ema_line, long_ema_line) = crate::init_optional_outputs_eff!(
                optional_outputs, &[false, false],
                short_ema_line: capacity,
                long_ema_line: capacity
            );

            kvo_line = crate::uninit_vec!(f64, capacity);
        }
        cycle_kvo(
            (inputs[0], inputs[1], inputs[2], inputs[3]),
            self.multipliers,
            &mut kvo_line,
            &mut self.state,
            (&mut short_ema_line, &mut long_ema_line),
        );

        Ok(vec![kvo_line, short_ema_line, long_ema_line])
    }
}
#[derive(Serialize, Deserialize)]
pub struct State {
    pub short_ema: f64,
    pub long_ema: f64,
    pub cm: f64,
    pub trend: f64,
    pub prev_hlc: f64,
    pub prev_high: f64,
    pub prev_low: f64,
}
impl State {
    pub fn new(
        short_ema: f64,
        long_ema: f64,
        trend: f64,
        cm: f64,
        prev_hlc: f64,
        prev_high: f64,
        prev_low: f64,
    ) -> Self {
        Self {
            short_ema,
            long_ema,
            trend,
            cm,
            prev_hlc,
            prev_high,
            prev_low,
        }
    }
    pub fn init_state(
        inputs: (&[f64], &[f64], &[f64], &[f64]),
        kvo_line: &Vec<f64>,
        periods: (usize, usize),
        short_ema_line: &mut [f64],
    ) -> Self {
        let capacity = kvo_line.capacity();
        let (high, low, close, volume) = inputs;
        let output_start = high.len() - capacity;
        let mut state = Self::new(
            0.0,
            0.0,
            -2.0,
            0.0,
            high[0] + low[0] + close[0],
            high[0],
            low[0],
        );
        let (short_period, long_period) = periods;
        let (short_multiplier, long_multiplier) = multiplier(short_period, long_period);
        for i in 1..output_start {
            let inputs = unsafe {
                (
                    *high.get_unchecked(i),
                    *low.get_unchecked(i),
                    *close.get_unchecked(i),
                    *volume.get_unchecked(i),
                )
            };

            let vf = calc_vf(&mut state, inputs);
            if i == 1 {
                // Initialize EMAs only once, just like C
                state.short_ema = vf;
                state.long_ema = vf;
            } else {
                // Use normal EMA calculation for subsequent points
                state.short_ema = calc_ema(&vf, state.short_ema, short_multiplier);
                state.long_ema = calc_ema(&vf, state.long_ema, long_multiplier);
            }
            crate::init_store_optional_outputs!(i, high.len(),
                short_ema_line => state.short_ema
            );
        }

        state
    }
}
/// Returns the minimum number of input bars required to produce results
/// accurate to `decimals` decimal places.
///
/// For indicators with exponential smoothing the seed value's influence
/// must decay below the requested precision, so this value grows with
/// `decimals`. Internally uses `min_process` with the smoothing
/// multiplier to calculate the required lookback.
///
/// # Arguments
///
/// * `options` - A slice containing the indicator options (e.g. period).
/// * `decimals` - The number of decimal places of accuracy required.
///
/// # Returns
///
/// The minimum number of input bars needed for the requested accuracy.
pub fn min_data_accuracy(options: &[f64], decimals: usize) -> usize {
    let multipliers = multiplier(options[0] as usize, options[1] as usize);
    min_process(
        options,
        Some((decimals, 0)),
        &[multipliers.1 .0],
        IndicatorInfoOrInteger::Info(&info()),
        min_data,
    )
}
/// Returns the minimum amount of data required for the KVO indicator.
///
/// # Arguments
///
/// * `options` - A slice containing the options for the KVO calculation.
///
/// # Returns
///
/// The minimum amount of data required.
pub fn min_data(options: &[f64]) -> usize {
    options[1] as usize + 1
}

/// Returns the number of output values produced by the KVO indicator given input data length and options.
///
/// # Arguments
///
/// * `data_len` - The length of the input data.
/// * `options` - A slice containing the options for the KVO calculation.
///
/// # Returns
///
/// The number of output values.
pub fn output_length(data_len: usize, options: &[f64]) -> usize {
    data_len - min_data(options) + 1
}
pub(crate) fn validate_options(options: &[f64; OPTIONS_WIDTH]) -> Result<(), IndicatorError> {
    if options[0] < 1.0 || options[1] <= options[0] {
        return Err(IndicatorError::InvalidOptions);
    }
    Ok(())
}
/// Calculates the Klinger Volume Oscillator (KVO) indicator for an entire dataset.
///
/// # Inputs
///
/// * `inputs[0]` — high prices
/// * `inputs[1]` — low prices
/// * `inputs[2]` — close prices
/// * `inputs[3]` — volume
///
/// # Options
///
/// * `options[0]` — short_period
/// * `options[1]` — long_period
///
/// # Outputs
///
/// * `outputs[0]` — `kvo` line
/// * `outputs[1]` — `short_ema` (optional, if requested)
/// * `outputs[2]` — `long_ema` (optional, if requested)
///
/// # Arguments
///
/// * `inputs` - Array of input price slices (see Inputs above).
/// * `options` - Array of indicator options (see Options above).
/// * `optional_outputs` - Optional slice selecting which extra outputs to compute:
///   index `0` = `short_ema`, index `1` = `long_ema`.
///
/// # Returns
///
/// `Ok((outputs, state))` where `outputs[0]` is the `kvo` line and
/// `state` can be passed to `IndicatorState::batch_indicator` for streaming.
/// Returns `Err(IndicatorError)` if inputs are too short or options are invalid.
pub fn indicator(
    inputs: &[&[f64]; INPUTS_WIDTH],
    options: &[f64; OPTIONS_WIDTH],
    optional_outputs: Option<&[bool]>,
) -> Result<(Vec<Vec<f64>>, IndicatorState), IndicatorError> {
    validate_options(options)?;

    validate_inputs(inputs, min_data(options))?;
    let [high, low, close, volume] = inputs;

    let (mut kvo_line, mut short_ema_line, mut long_ema_line, mut state, multipliers, inputs);
    {
        let capacity = output_length(high.len(), options);
        let short_capacity = ema_output_length(high.len(), &[options[0]]);
        kvo_line = crate::uninit_vec!(f64, capacity);

        (short_ema_line, long_ema_line) = crate::init_optional_outputs_eff!(
            optional_outputs, &[false, false],
            short_ema_line: short_capacity,
            long_ema_line: capacity
        );
        let short_period = options[0] as usize;
        let long_period = options[1] as usize;
        multipliers = multiplier(short_period, long_period);
        // Perform the main KVO calculation
        state = State::init_state(
            (&high, &low, &close, &volume),
            &kvo_line,
            (short_period, long_period),
            &mut short_ema_line,
        );
        let from = high.len() - capacity;
        inputs = (&high[from..], &low[from..], &close[from..], &volume[from..])
    }
    let optional_outputs = {
        let offset = crate::slice_outputs_start!(kvo_line.len(), short_ema_line);
        (&mut short_ema_line[offset..], long_ema_line.as_mut_slice())
    };

    cycle_kvo(
        inputs,
        multipliers,
        &mut kvo_line,
        &mut state,
        optional_outputs,
    );

    Ok((
        vec![kvo_line, short_ema_line, long_ema_line],
        IndicatorState { multipliers, state },
    ))
}

/// Performs the main calculation loop for the KVO indicator.
///
/// # Arguments
///
/// * `inputs` - A tuple of four price slices: `(high, low, close, volume)`.
/// * `multipliers` - A tuple of EMA multiplier pairs for the short and long EMAs.
/// * `kvo_line` - A mutable slice for storing the KVO output values.
/// * `state` - A mutable reference to the indicator state.
/// * `out_vecs` - A tuple of mutable optional output slices: `(short_ema_line, long_ema_line)`.
fn cycle_kvo(
    inputs: (&[f64], &[f64], &[f64], &[f64]),
    multipliers: ((f64, f64), (f64, f64)),
    kvo_line: &mut [f64],
    state: &mut State,
    out_vecs: (&mut [f64], &mut [f64]),
) {
    let (high, low, close, volume) = inputs;
    let (short_ema_line, long_ema_line) = out_vecs;
    let (has_optional, want_short, want_long) =
        crate::calc_want_flags!(short_ema_line, long_ema_line);

    for i in 0..high.len() {
        let inputs = unsafe {
            (
                *high.get_unchecked(i),
                *low.get_unchecked(i),
                *close.get_unchecked(i),
                *volume.get_unchecked(i),
            )
        };
        let kvo = calc(state, inputs, multipliers);
        unsafe { *kvo_line.get_unchecked_mut(i) = kvo };

        if has_optional {
            crate::store_optional_outputs!(i,
                want_short, short_ema_line => state.short_ema,
                want_long, long_ema_line => state.long_ema
            );
        }
    }
}

/// Calculates the Klinger Volume Oscillator (KVO) value for a single bar.
///
/// # Arguments
///
/// * `state` - A mutable reference to the indicator state.
/// * `inputs` - A tuple `(high, low, close, volume)` for the current bar.
/// * `multipliers` - A tuple of EMA multiplier pairs for the short and long EMAs.
///
/// # Returns
///
/// The calculated KVO value (`short_ema - long_ema`).
#[inline(always)]
pub fn calc(
    state: &mut State,
    inputs: (f64, f64, f64, f64),
    multipliers: ((f64, f64), (f64, f64)),
) -> f64 {
    // Extract multipliers once (minor optimization)

    let vf = calc_vf(state, inputs);
    let (short_multiplier, long_multiplier) = multipliers;
    state.short_ema = calc_ema(&vf, state.short_ema, short_multiplier);
    state.long_ema = calc_ema(&vf, state.long_ema, long_multiplier);
    state.short_ema - state.long_ema
}

#[inline(always)]
pub(crate) fn calc_vf(state: &mut State, inputs: (f64, f64, f64, f64)) -> f64 {
    let (high, low, close, volume) = inputs;

    let hlc = high + low + close;
    let dm = high - low;

    // Update trend and cm
    if state.trend != 1.0 && hlc > state.prev_hlc {
        state.trend = 1.0;
        state.cm = state.prev_high - state.prev_low;
    } else if state.trend != -1.0 && hlc < state.prev_hlc {
        state.trend = -1.0;
        state.cm = state.prev_high - state.prev_low;
    }
    state.cm += dm.max(f64::EPSILON);

    state.prev_hlc = hlc;
    state.prev_high = high;
    state.prev_low = low;

    (dm / state.cm).mul_add(2.0, -1.0).abs() * volume * 100.0 * state.trend
}

#[inline(always)]
pub fn multiplier(short_period: usize, long_period: usize) -> ((f64, f64), (f64, f64)) {
    (ema_multiplier(short_period), ema_multiplier(long_period))
}