tulip_rs 0.1.15

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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
use crate::common::validate_inputs;
pub use crate::indicator_types::TIndicatorState;

pub use crate::indicators::atr::multiplier;
use crate::indicators::{
    atr::{output_length as atr_output_length, State as AtrState},
    max::{
        calc as calc_max, calc_unchecked as calc_max_unchecked, output_length as max_output_length,
        State as MaxState,
    },
    min::{calc as calc_min, calc_unchecked as calc_min_unchecked, State as MinState},
    tr::output_length as tr_output_length,
};

use crate::types::{DisplayGroup, 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 = 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::chandelierexit_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::chandelierexit_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::chandelierexit_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::chandelierexit_simd::indicator_by_options as indicator;
}

#[derive(Serialize, Deserialize)]
pub struct IndicatorState {
    high: Vec<f64>,
    low: Vec<f64>,
    state: State,
    periods: (usize, usize),
    multipliers: (f64, (f64, f64)),
}
impl IndicatorState {
    /// Creates a new `IndicatorState` for streaming continuation.
    ///
    /// Retains the trailing `period` high and low bars needed to maintain the sliding
    /// max/min windows across batch calls.
    ///
    /// # Arguments
    ///
    /// * `high` - Full high price slice from the just-completed batch.
    /// * `low` - Full low price slice from the just-completed batch.
    /// * `state` - Internal min/max and ATR state after the last computed bar.
    /// * `periods` - Tuple `(period, trail)` where `trail = period - 1`.
    /// * `multipliers` - Tuple `(step, (atr_alpha, atr_1m_alpha))` used in `calc`.
    pub fn new(
        high: &[f64],
        low: &[f64],
        state: State,
        periods: (usize, usize),
        multipliers: (f64, (f64, f64)),
    ) -> Self {
        Self {
            high: high[high.len() - periods.0..].to_vec(),
            low: low[low.len() - periods.0..].to_vec(),
            state,
            periods,
            multipliers,
        }
    }
}
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)?;

        let periods = self.periods;
        self.high.extend_from_slice(inputs[0]);
        self.low.extend_from_slice(inputs[1]);
        let close = inputs[2];
        let (
            mut long_line,
            mut short_line,
            (mut atr_line, mut tr_line, mut min_line, mut max_line),
        ) = {
            let capacity = inputs[0].len();
            (
                crate::uninit_vec!(f64, capacity),
                crate::uninit_vec!(f64, capacity),
                crate::init_optional_outputs_eff!(
                    optional_outputs, &[false, false, false, false],
                    atr_line: capacity,
                    tr_line: capacity,
                    min_line: capacity,
                    max_line: capacity
                ),
            )
        };
        match periods.0 {
            1..=4 => {
                cycle::<1>(
                    (&self.high, &self.low, close),
                    periods,
                    self.multipliers,
                    (&mut long_line, &mut short_line),
                    &mut self.state,
                    (&mut atr_line, &mut tr_line, &mut min_line, &mut max_line),
                );
            }
            5..25 => {
                cycle::<4>(
                    (&self.high, &self.low, close),
                    periods,
                    self.multipliers,
                    (&mut long_line, &mut short_line),
                    &mut self.state,
                    (&mut atr_line, &mut tr_line, &mut min_line, &mut max_line),
                );
            }
            _ => {
                cycle::<8>(
                    (&self.high, &self.low, close),
                    periods,
                    self.multipliers,
                    (&mut long_line, &mut short_line),
                    &mut self.state,
                    (&mut atr_line, &mut tr_line, &mut min_line, &mut max_line),
                );
            }
        }

        self.high.drain(..self.high.len() - periods.0);
        self.low.drain(..self.low.len() - periods.0);

        Ok(vec![
            long_line, short_line, atr_line, tr_line, min_line, max_line,
        ])
    }
}
#[derive(Serialize, Deserialize)]
pub struct State {
    pub min_state: MinState,
    pub max_state: MaxState,
    pub atr_state: AtrState,
}
impl State {
    /// Initialises the Chandelier Exit state from the seed bars.
    ///
    /// Starts the min/max windows at the first `low`/`high` value and seeds the ATR
    /// using a Wilder SMA over the first `period` bars.
    ///
    /// # Arguments
    ///
    /// * `high` - High prices; must contain at least `period` elements.
    /// * `low` - Low prices; must contain at least `period` elements.
    /// * `close` - Close prices; must contain at least `period` elements.
    /// * `period` - ATR lookback (Wilder smoothing period).
    /// * `trail` - Min/max sliding-window size (`= period - 1`).
    /// * `optional_outputs` - Output buffers `(tr_line, min_line, max_line)` written during warm-up; pass empty slices if not needed.
    pub fn new(
        high: &[f64],
        low: &[f64],
        close: &[f64],
        period: usize,
        trail: usize,
        optional_outputs: (&mut [f64], &mut [f64], &mut [f64]),
    ) -> Self {
        let (tr_line, min_line, max_line) = optional_outputs;
        let min_state = MinState::init_state(low, period, trail, min_line);
        let max_state = MaxState::init_state(high, period, trail, max_line);
        let atr_state = AtrState::init_state(high, low, close, period, tr_line, false);

        State {
            min_state,
            max_state,
            atr_state,
        }
    }
}
/// Returns information about the Chandelier Exit indicator.
///
/// # Returns
///
/// An `Info` struct containing metadata about the Chandelier Exit indicator.
pub const INFO: Info = Info {
    name: "chandelierexit",
    full_name: "Chandelier Exit",
    indicator_type: IndicatorType::Trend,
    inputs: &["high", "low", "close"],
    options: &["period", "step"],
    outputs: &["long", "short"],
    optional_outputs: &["atr", "tr", "min", "max"],
    display_groups: &[
        DisplayGroup {
            offset: None,
            id: "long_short",
            label: "Exit Positions",
            display_type: DisplayType::Overlay,
            outputs: &["long", "short"],
        },
        DisplayGroup {
            offset: None,
            id: "atr_tr",
            label: "True Range",
            display_type: DisplayType::Indicator,
            outputs: &["atr", "tr"],
        },
        DisplayGroup {
            offset: None,
            id: "min_max",
            label: "Min & Max",
            display_type: DisplayType::Overlay,
            outputs: &["min", "max"],
        },
    ],
};
pub(crate) fn validate_options(options: &[f64; OPTIONS_WIDTH]) -> Result<(), IndicatorError> {
    if options[0] < 1.0 || options[1] <= 0.0 {
        return Err(IndicatorError::InvalidOptions);
    }
    Ok(())
}
/// Returns the minimum amount of data required for the Chandelier Exit indicator.
///
/// # Arguments
///
/// * `options` - A slice containing the options for the Chandelier Exit calculation.
///
/// # Returns
///
/// The minimum amount of data required.
pub fn min_data(options: &[f64]) -> usize {
    options[0] as usize + 1
}

/// Calculates the output length for the Chandelier Exit indicator.
///
/// # Arguments
///
/// * `data_len` - The length of the input data.
/// * `options` - A slice containing the options for the Chandelier Exit calculation.
///
/// # Returns
///
/// The number of output values produced by the Chandelier Exit calculation.
pub fn output_length(data_len: usize, options: &[f64]) -> usize {
    data_len - min_data(options) + 1
}

/// Calculates the Chandelier Exit indicator over the full input dataset.
///
/// # Inputs
///
/// * `inputs[0]` — high prices
/// * `inputs[1]` — low prices
/// * `inputs[2]` — close prices
///
/// # Options
///
/// * `options[0]` — period (ATR and highest-high / lowest-low lookback)
/// * `options[1]` — multiplier applied to ATR for the exit distance
///
/// # Outputs
///
/// * `outputs[0]` — `long` exit line  (`highest_high - multiplier × ATR`)
/// * `outputs[1]` — `short` exit line (`lowest_low  + multiplier × ATR`)
///
/// # Optional Outputs
///
/// * `atr` — the Wilder ATR series used in the calculation
/// * `tr`  — the True Range series
/// * `min` — the rolling lowest-low series (same window as the short exit)
/// * `max` — the rolling highest-high series (same window as the long exit)
///
/// # Arguments
///
/// * `inputs` - Array of input price slices (see Inputs above).
/// * `options` - Array of indicator options (see Options above).
/// * `optional_outputs` - Optional flags `[want_atr, want_tr, want_min, want_max]` to enable extra outputs.
///
/// # Returns
///
/// `Ok((outputs, state))` where `outputs[0]` is `long`, `outputs[1]` is `short`,
/// `outputs[2]` is `atr` (empty unless requested), `outputs[3]` is `tr` (empty unless
/// requested), `outputs[4]` is `min` (empty unless requested), `outputs[5]` is `max`
/// (empty unless requested), 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 periods = (options[0] as usize, options[0] as usize - 1);
    let multipliers = (options[1], multiplier(periods.0));
    let [high, low, close] = inputs;

    let (mut long_line, mut short_line, (mut atr_line, mut tr_line, mut min_line, mut max_line)) = {
        let len = high.len();
        let capacity = output_length(len, options);
        let min_max_capacity = max_output_length(len, options);
        (
            crate::uninit_vec!(f64, capacity),
            crate::uninit_vec!(f64, capacity),
            crate::init_optional_outputs_eff!(
                optional_outputs, &[false, false, false, false],
                atr_line: atr_output_length(len, &[options[0]]),
                tr_line: tr_output_length(len, &[]),
                min_line: min_max_capacity,
                max_line: min_max_capacity
            ),
        )
    };

    let mut state = State::new(
        high,
        low,
        close,
        periods.0,
        periods.1,
        (&mut tr_line, &mut min_line, &mut max_line),
    );
    let optional_outputs = {
        let (tr_offset, min_offset, max_offset) =
            crate::slice_outputs_start!(long_line.len(), tr_line, min_line, max_line);
        (
            atr_line.as_mut_slice(),
            &mut tr_line[tr_offset..],
            &mut min_line[min_offset..],
            &mut max_line[max_offset..],
        )
    };
    match periods.0 {
        1..=10 => {
            cycle::<1>(
                (high, low, &close[periods.0..]),
                periods,
                multipliers,
                (&mut long_line, &mut short_line),
                &mut state,
                optional_outputs,
            );
        }
        11..=25 => {
            cycle::<4>(
                (high, low, &close[periods.0..]),
                periods,
                multipliers,
                (&mut long_line, &mut short_line),
                &mut state,
                optional_outputs,
            );
        }
        _ => {
            cycle::<8>(
                (high, low, &close[periods.0..]),
                periods,
                multipliers,
                (&mut long_line, &mut short_line),
                &mut state,
                optional_outputs,
            );
        }
    }
    Ok((
        vec![long_line, short_line, atr_line, tr_line, min_line, max_line],
        IndicatorState::new(high, low, state, periods, multipliers),
    ))
}

/// Performs the main calculation loop for the Chandelier Exit indicator.
///
/// # Arguments
///
/// * `inputs` - A tuple of `(high, low, close)` price slices.
/// * `periods` - A tuple of `(period, trail)` where `period` is the ATR lookback and
///   `trail` (`= period - 1`) is the sliding window size passed to min/max states.
/// * `multipliers` - A tuple of `(step, atr_multipliers)` where `step` is the ATR multiplier
///   option and `atr_multipliers` are the Wilder smoothing constants.
/// * `output_lines` - A tuple of mutable slices for storing the `long` and `short` exit lines.
/// * `state` - A mutable reference to the current indicator state.
/// * `optional_outputs` - A tuple of mutable slices for optional `atr`, `tr`, `min`, and `max` outputs.
fn cycle<const N: usize>(
    inputs: (&[f64], &[f64], &[f64]),
    periods: (usize, usize),
    multipliers: (f64, (f64, f64)),
    output_lines: (&mut [f64], &mut [f64]),
    state: &mut State,
    optional_outputs: (&mut [f64], &mut [f64], &mut [f64], &mut [f64]),
) {
    let (high, low, close) = inputs;
    let (long_line, short_line) = output_lines;
    let (atr_line, tr_line, min_line, max_line) = optional_outputs;
    let (has_optional, want_atr, want_tr, want_min, want_max) =
        crate::calc_want_flags!(atr_line, tr_line, min_line, max_line);
    for (j, i) in (periods.0..inputs.0.len()).enumerate() {
        let (long, short, atr, tr, min, max);
        unsafe {
            (long, short, atr, tr, min, max) = calc_unchecked::<N>(
                state,
                (high, low, *close.get_unchecked(j)),
                i,
                periods,
                multipliers,
            );
            *long_line.get_unchecked_mut(j) = long;
            *short_line.get_unchecked_mut(j) = short;
        }
        if has_optional {
            crate::store_optional_outputs!(j,
                want_atr, atr_line => atr,
                want_tr, tr_line => tr,
                want_min, min_line => min,
                want_max, max_line => max
            );
        }
    }
}
/// Computes one step of the Chandelier Exit indicator.
///
/// Advances the rolling min, max, and ATR states by one bar and returns the long and short
/// exit lines along with the current ATR and True Range values.
///
/// # Arguments
///
/// * `state` - Mutable reference to the current min/max/ATR state.
/// * `inputs` - A tuple of `(high_slice, low_slice, close)` where `close` is the scalar
///   close for the current bar and `high_slice`/`low_slice` cover the full lookback window.
/// * `i` - Current bar index into `high_slice`/`low_slice`.
/// * `periods` - Tuple `(period, trail)` where `trail = period - 1`.
/// * `multipliers` - Tuple `(step, (atr_alpha, atr_1m_alpha))`.
///
/// # Returns
///
/// A tuple `(long, short, atr, tr)` for the current bar.
#[inline(always)]
pub fn calc(
    state: &mut State,
    inputs: (&[f64], &[f64], f64),
    i: usize,
    periods: (usize, usize),
    multipliers: (f64, (f64, f64)),
) -> (f64, f64, f64, f64, f64, f64) {
    let (high, low, close) = inputs;
    let (step, atr_multipliers) = multipliers;
    let (min, _) = calc_min(&mut state.min_state, low, i, periods);
    let (max, _) = calc_max(&mut state.max_state, high, i, periods);

    let (atr, tr) = state
        .atr_state
        .calc(high[i], low[i], close, atr_multipliers);

    let long = atr.mul_add(-step, max);
    let short = atr.mul_add(step, min);

    (long, short, atr, tr, min, max)
}
/// Unchecked version of [`calc`] that uses SIMD-hint size `N` for the min/max windows.
///
/// Identical to [`calc`] but uses `get_unchecked` for all slice accesses and passes the
/// const generic `N` as a prefetch/SIMD-hint to the min/max helpers.
///
/// # Safety
///
/// Callers must ensure that `i` is a valid index into `high` and `low` and that the
/// slice lengths are sufficient for the lookback window (`trail + 1` elements before `i`).
///
/// # Arguments
///
/// * `state` - Mutable reference to the current min/max/ATR state.
/// * `inputs` - A tuple of `(high_slice, low_slice, close)` where `close` is the scalar
///   close for the current bar.
/// * `i` - Current bar index into `high_slice`/`low_slice`.
/// * `periods` - Tuple `(period, trail)` where `trail = period - 1`.
/// * `multipliers` - Tuple `(step, (atr_alpha, atr_1m_alpha))`.
///
/// # Returns
///
/// A tuple `(long, short, atr, tr)` for the current bar.
#[inline(always)]
pub(crate) unsafe fn calc_unchecked<const N: usize>(
    state: &mut State,
    inputs: (&[f64], &[f64], f64),
    i: usize,
    periods: (usize, usize),
    multipliers: (f64, (f64, f64)),
) -> (f64, f64, f64, f64, f64, f64) {
    let (high, low, close) = inputs;
    let (step, atr_multipliers) = multipliers;
    let (min, _) = calc_min_unchecked::<N>(&mut state.min_state, low, i, periods);
    let (max, _) = calc_max_unchecked::<N>(&mut state.max_state, high, i, periods);

    let (atr, tr) = state.atr_state.calc(
        *high.get_unchecked(i),
        *low.get_unchecked(i),
        close,
        atr_multipliers,
    );
    let long = atr.mul_add(-step, max);
    let short = atr.mul_add(step, min);
    (long, short, atr, tr, min, max)
}