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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
use crate::common::{validate_inputs, validate_options};
pub use crate::indicator_types::TIndicatorState;
use crate::indicators::max::{calc as calc_max, calc_unchecked as calc_max_unchecked};
use crate::indicators::min::{calc as calc_min, calc_unchecked as calc_min_unchecked};
pub use crate::indicators::{max::State as MaxState, min::State as MinState};

use crate::ring_buffer::single_buffer::generic_buffer::{Buffer, RingBuffer};
use crate::types::{DisplayType, IndicatorError, IndicatorType, Info};
use serde::{Deserialize, Serialize};
/// Number of input price series required by this indicator.
pub const INPUTS_WIDTH: usize = 3;

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

/// 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::stoch_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::stoch_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::stoch_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::stoch_simd::indicator_by_options as indicator;
}

#[derive(Serialize, Deserialize)]
pub struct IndicatorState {
    state: State,
    high: Vec<f64>,
    low: Vec<f64>,
    multipliers: (f64, f64),
    k_period: usize,
}
impl IndicatorState {
    pub fn new(
        state: State,
        high: &[f64],
        low: &[f64],
        multipliers: (f64, f64),
        k_period: usize,
    ) -> Self {
        Self {
            state,
            multipliers,
            high: high[high.len() - k_period..].to_vec(),
            low: low[low.len() - k_period..].to_vec(),
            k_period,
        }
    }
}

impl TIndicatorState<3> for IndicatorState {
    fn batch_indicator(
        &mut self,
        inputs: &[&[f64]; INPUTS_WIDTH],
        _optional_outputs: Option<&[bool]>,
    ) -> Result<Vec<Vec<f64>>, IndicatorError> {
        validate_inputs(inputs, 1)?;

        self.high.extend_from_slice(inputs[0]);
        self.low.extend_from_slice(inputs[1]);

        let close = inputs[2];

        let (mut k_line, mut d_line) = {
            let capacity = inputs[0].len();
            (
                crate::uninit_vec!(f64, capacity),
                crate::uninit_vec!(f64, capacity),
            )
        };
        match self.k_period {
            1..=4 => {
                cycle::<1>(
                    (&self.high, &self.low, close),
                    self.k_period,
                    0,
                    self.multipliers,
                    &mut self.state,
                    (&mut k_line, &mut d_line),
                );
            }
            5..30 => {
                cycle::<4>(
                    (&self.high, &self.low, close),
                    self.k_period,
                    0,
                    self.multipliers,
                    &mut self.state,
                    (&mut k_line, &mut d_line),
                );
            }
            _ => {
                cycle::<8>(
                    (&self.high, &self.low, close),
                    self.k_period,
                    0,
                    self.multipliers,
                    &mut self.state,
                    (&mut k_line, &mut d_line),
                );
            }
        }

        self.high.drain(..self.high.len() - self.k_period);
        self.low.drain(..self.low.len() - self.k_period);

        Ok(vec![k_line, d_line])
    }
}
#[derive(Serialize, Deserialize)]
pub struct State {
    pub prev_k: Buffer,
    pub prev_d: Buffer,
    pub min_state: MinState,
    pub max_state: MaxState,
    pub k_sum: f64,
    pub d_sum: f64,
}

impl State {
    pub fn new(min: (f64, usize), max: (f64, usize), k_slow: usize, d_period: usize) -> Self {
        State {
            min_state: MinState::new(min.0, min.1),
            max_state: MaxState::new(max.0, max.1),
            prev_k: Buffer::new(k_slow),
            prev_d: Buffer::new(d_period),
            k_sum: 0.0,
            d_sum: 0.0,
        }
    }
    pub fn init_state(
        inputs: (&[f64], &[f64], &[f64]),
        k_period: usize,
        k_slow: usize,
        d_period: usize,
        k_line: &mut [f64],
    ) -> (Self, usize, usize) {
        let (high, low, _) = inputs;
        let mut state = Self::new((low[0], k_period), (high[0], k_period), k_slow, d_period);
        let (k_multiplier, _d_multiplier) = &multiplier(k_slow, d_period);
        let mut k_count = 0;
        let mut start = 0;
        for i in k_period + 1..k_period + k_slow + d_period {
            let k_fast = calc_kfast(
                &mut state.min_state,
                &mut state.max_state,
                inputs,
                i,
                k_period,
            );
            state.k_sum += k_fast;
            if let Some(k_old) = state.prev_k.push_with_info(k_fast) {
                state.k_sum -= k_old;
            }
            if state.prev_k.is_full() {
                // Buffer was full so a value was replaced.
                let k = state.k_sum * k_multiplier;
                k_line[k_count] = k;
                k_count += 1;
                state.d_sum += k;
                state.prev_d.push(k);
            }
            start = i;
        }
        start += 1;
        (state, k_count, start)
    }
}
/// Returns information about the Stochastic Oscillator indicator.
///
/// # Returns
///
/// An `Info` struct containing metadata about the Stochastic Oscillator indicator.
pub fn info() -> Info<'static> {
    Info {
        name: "stoch",
        full_name: "Stochastic Oscillator",
        display_type: DisplayType::Indicator,
        indicator_type: IndicatorType::Momentum,
        inputs: &["high", "low", "close"],
        options: &["k_period", "k_slow", "d_period"],
        outputs: &["stoch_k", "stoch_d"],
        optional_outputs: &[],
    }
}
/// Returns the minimum number of input bars required to produce accurate results.
///
/// For this indicator accuracy does not depend on decimal precision, so
/// this always returns the same value as [`min_data`].
///
/// # Arguments
///
/// * `options` - A slice containing the indicator options.
/// * `_decimals` - Unused. Accuracy is independent of decimal precision for this indicator.
///
/// # Returns
///
/// The minimum number of input bars required, identical to [`min_data`].
pub fn min_data_accuracy(options: &[f64], _decimals: usize) -> usize {
    min_data(options)
}
/// Returns the minimum amount of data required for the Stochastic Oscillator indicator.
///
/// # Arguments
///
/// * `options` - A slice containing the options for the Stochastic Oscillator calculation.
///
/// # Returns
///
/// The minimum amount of data required.
pub fn min_data(options: &[f64]) -> usize {
    (options[0] + options[1] + options[2]) as usize + 1
}

/// Calculates the output lengths for the Stochastic Oscillator given the input data length and options.
///
/// # Arguments
///
/// * `data_len` - The length of the input data.
/// * `options` - A slice containing the options for the Stochastic Oscillator calculation.
///
/// # Returns
///
/// A tuple `(k_capacity, d_capacity)` representing the total %K output length and the %D output length.
pub fn output_length(data_len: usize, options: &[f64]) -> (usize, usize) {
    let d_capacity = data_len - min_data(options) + 1;
    (d_capacity + options[2] as usize, d_capacity)
}

/// Calculates the Stochastic Oscillator indicator over the full input dataset.
///
/// # Inputs
///
/// * `inputs[0]` — high prices
/// * `inputs[1]` — low prices
/// * `inputs[2]` — close prices
///
/// # Options
///
/// * `options[0]` — k_period
/// * `options[1]` — k_slow
/// * `options[2]` — d_period
///
/// # Arguments
///
/// * `inputs` - Array of input price slices (see Inputs above).
/// * `options` - Array of indicator options (see Options above).
/// * `_optional_outputs` - Unused; this indicator has no optional outputs.
///
/// # Returns
///
/// `Ok((outputs, state))` where:
/// - `outputs[0]` — `stoch_k`
/// - `outputs[1]` — `stoch_d`
///
/// `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)?;
    let k_period = options[0] as usize;

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

    let (mut k_line, mut d_line, mut state, outputs, start, multipliers);
    {
        let (k_capacity, d_capacity) = output_length(high.len(), options);
        k_line = crate::uninit_vec!(f64, k_capacity);
        d_line = crate::uninit_vec!(f64, d_capacity);

        let k_slow = options[1] as usize;
        let d_period = options[2] as usize;
        multipliers = multiplier(k_slow, d_period);
        let k_count;
        (state, k_count, start) =
            State::init_state((high, low, close), k_period, k_slow, d_period, &mut k_line);
        outputs = (&mut k_line[k_count..], d_line.as_mut_slice());
    }
    //println!("k_line: {:?}, d_line: {:?}, start: {:?}, k_count: {:?}", k_line.len(), d_line.len(), start, k_count);
    match k_period {
        1..=4 => {
            cycle::<1>(
                (high, low, close),
                k_period,
                start,
                multipliers,
                &mut state,
                outputs,
            );
        }
        5..30 => {
            cycle::<4>(
                (high, low, close),
                k_period,
                start,
                multipliers,
                &mut state,
                outputs,
            );
        }
        _ => {
            cycle::<8>(
                (high, low, close),
                k_period,
                start,
                multipliers,
                &mut state,
                outputs,
            );
        }
    }

    Ok((
        vec![k_line, d_line],
        IndicatorState::new(state, high, low, multipliers, k_period),
    ))
}

/// Performs the main calculation loop for the Stochastic Oscillator indicator.
///
/// # Arguments
///
/// * `inputs` - A tuple of three slices: `(high, low, close)`.
/// * `k_period` - The lookback period for the fast %K calculation.
/// * `start` - The starting index within `close` to begin output from.
/// * `multipliers` - A tuple `(k_multiplier, d_multiplier)` for the slow %K and %D averages.
/// * `state` - A mutable reference to the current `State`.
/// * `outputs` - A mutable tuple `(k_line, d_line)` of output slices.
fn cycle<const N: usize>(
    inputs: (&[f64], &[f64], &[f64]),
    k_period: usize,
    start: usize,
    multipliers: (f64, f64),
    state: &mut State,
    outputs: (&mut [f64], &mut [f64]),
) {
    let close = inputs.2;
    let (k_line, d_line) = outputs;

    for (j, i) in (start..close.len()).enumerate() {
        unsafe {
            (*k_line.get_unchecked_mut(j), *d_line.get_unchecked_mut(j)) =
                calc_unchecked::<N>(state, inputs, i, k_period, multipliers);
        }
        //k_count += 1;
    }
}
/// Calculates the Stochastic Oscillator %K and %D values for a single data point.
///
/// # Arguments
///
/// * `state` - A mutable reference to the current `State`.
/// * `inputs` - A tuple of three slices: `(high, low, close)`.
/// * `i` - The current index within `close`.
/// * `k_period` - The lookback period for the fast %K calculation.
/// * `multipliers` - A tuple `(k_multiplier, d_multiplier)` for the slow %K and %D averages.
///
/// # Returns
///
/// A tuple `(k, d)` — the slow %K and %D values for the current bar.
#[inline(always)]
pub fn calc(
    state: &mut State,
    inputs: (&[f64], &[f64], &[f64]),
    i: usize,
    k_period: usize,
    multipliers: (f64, f64),
) -> (f64, f64) {
    let (k_multiplier, d_multiplier) = multipliers;

    let kfast = calc_kfast(
        &mut state.min_state,
        &mut state.max_state,
        inputs,
        i,
        k_period,
    );

    if let Some(old_k) = state.prev_k.push_with_info(kfast) {
        state.k_sum += kfast - old_k;
    } else {
        state.k_sum += kfast;
    }
    let k = state.k_sum * k_multiplier;
    if let Some(old_d) = state.prev_d.push_with_info(k) {
        state.d_sum += k - old_d;
    } else {
        state.d_sum += k;
    }

    (k, state.d_sum * d_multiplier)
}
#[inline(always)]
unsafe fn calc_unchecked<const N: usize>(
    state: &mut State,
    inputs: (&[f64], &[f64], &[f64]),
    i: usize,
    k_period: usize,
    multipliers: (f64, f64),
) -> (f64, f64) {
    let (k_multiplier, d_multiplier) = multipliers;

    let kfast = calc_kfast_unchecked::<N>(
        &mut state.min_state,
        &mut state.max_state,
        inputs,
        i,
        k_period,
    );

    let old_k = state.prev_k.push_with_info_unchecked(kfast);
    state.k_sum += kfast - old_k;
    let k = state.k_sum * k_multiplier;
    let old_d = state.prev_d.push_with_info_unchecked(k);
    state.d_sum += k - old_d;

    (k, state.d_sum * d_multiplier)
}

#[inline(always)]
pub fn calc_kfast(
    min_state: &mut MinState,
    max_state: &mut MaxState,
    inputs: (&[f64], &[f64], &[f64]),
    i: usize,
    period: usize,
) -> f64 {
    let (high, low, close) = inputs;
    let shift = low.len() - close.len();

    let (min, _) = calc_min(min_state, low, i + shift, (period, period - 1));
    let (max, _) = calc_max(max_state, high, i + shift, (period, period - 1));

    100.0 * (close[i] - min) / (max - min).max(f64::EPSILON)
}
#[inline(always)]
pub unsafe fn calc_kfast_unchecked<const N: usize>(
    min_state: &mut MinState,
    max_state: &mut MaxState,
    inputs: (&[f64], &[f64], &[f64]),
    i: usize,
    period: usize,
) -> f64 {
    let (high, low, close) = inputs;
    let shift = low.len() - close.len();

    let (min, _) = calc_min_unchecked::<N>(min_state, low, i + shift, (period, period - 1));
    let (max, _) = calc_max_unchecked::<N>(max_state, high, i + shift, (period, period - 1));

    100.0 * (close.get_unchecked(i) - min) / (max - min).max(f64::EPSILON)
}

#[inline(always)]
pub fn multiplier(k_slow: usize, d_period: usize) -> (f64, f64) {
    (1.0 / k_slow as f64, 1.0 / d_period as f64)
}