finance-solution 0.5.1

Finance math: TVM, cashflow, amortization, equity path metrics, technical analysis (SMA/EMA/WMA/HMA/RMA/DEMA/TEMA/KAMA/MACD, BB/Keltner/Donchian/Stoch/VWAP/RVOL/RSI/ATR/LinReg, WillR/OBV/CCI/ADX/MOM/MFI/Supertrend/SAR), risk (Sharpe/Sortino/Calmar/Ulcer/IR), and options (BSM, Black76, GK, CRR American) with Result-only APIs and incremental state.
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
//! # Supertrend
//!
//! ATR-based trailing band (common breakout / stop overlay):
//!
//! ```text
//! mid = (high + low) / 2
//! basic_upper = mid + mult * ATR
//! basic_lower = mid − mult * ATR
//! final bands stick until price closes through the opposite band
//! Supertrend = final_lower in uptrend, final_upper in downtrend
//! ```
//!
//! Default: ATR period **10**, mult **3.0** ([`SupertrendParams::standard`]).
//!
//! Output: [`SupertrendBar`] with `value` and `direction` (`+1` uptrend / `−1` downtrend).
//!
//! ---
//!
//! ## Trading perspective
//!
//! | Event | Habit (classic) |
//! |-------|-----------------|
//! | Direction flips to +1 | Long regime / trail under price |
//! | Direction flips to −1 | Short regime / trail over price |
//! | Price pulls to ST line | Support/resistance screen in trend |
//!
//! Supertrend is a **stop + regime** tool, not an oscillator. Mult↑ → fewer flips, wider trail.
//!
//! ## vs Parabolic SAR / Donchian / Keltner
//!
//! | | Supertrend | SAR | Donchian | Keltner |
//! |--|------------|-----|----------|---------|
//! | Driver | ATR × mid | Acceleration on extremes | Pure HH/LL | EMA + ATR |
//! | Flip style | Close through band | Touch SAR | Channel break | Band tag |
//! | Best for | ATR-scaled trail | Fast reverse systems | Breakout structure | Channel mean reversion |
//!
//! ## Pairs well with
//!
//! - **ADX** — enter Supertrend flips only when ADX rising / above threshold.
//! - **Volume (OBV/RVOL)** — confirm breakout volume.
//! - **Higher-timeframe MA** — only long Supertrend with HTF trend.
//!
//! ---
//!
//! ## Engineering
//!
//! [`SupertrendParams`] → [`supertrend`] / [`SupertrendState`] → [`supertrend_solution`].  
//! Uses shared [`AtrState`] (Wilder). Batch via state.  
//! First defined bar seeds direction from close vs mid (implementation convention).

use crate::stocks::ta::atr::{AtrParams, AtrState};
use crate::stocks::ta::common::{opt_cell, require_hlc};
use crate::util::error::{require_finite, FinanceError, FinanceResult};
use crate::{columns_with_strings, print_table_locale_opt};

/// Supertrend pack: Wilder ATR period + band multiplier.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SupertrendParams {
    pub atr_period: usize,
    pub multiplier: f64,
}

impl SupertrendParams {
    pub const fn new(atr_period: usize, multiplier: f64) -> Self {
        Self {
            atr_period,
            multiplier,
        }
    }

    /// Common `(10, 3.0)`.
    pub const fn standard() -> Self {
        Self {
            atr_period: 10,
            multiplier: 3.0,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ValidatedSupertrend {
    params: SupertrendParams,
}

impl ValidatedSupertrend {
    pub fn new(params: SupertrendParams) -> FinanceResult<Self> {
        crate::util::primitives::PeriodLength::new(params.atr_period)?;
        require_finite("multiplier", params.multiplier)?;
        if params.multiplier <= 0.0 {
            return Err(FinanceError::Unsolvable {
                message: "supertrend multiplier must be positive",
            });
        }
        Ok(Self { params })
    }

    pub fn params(self) -> SupertrendParams {
        self.params
    }

    pub fn compute(
        self,
        high: &[f64],
        low: &[f64],
        close: &[f64],
    ) -> FinanceResult<SupertrendSeries> {
        supertrend_validated(high, low, close, self)
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SupertrendBar {
    pub value: f64,
    /// `+1` uptrend (line under price), `−1` downtrend (line over price).
    pub direction: i8,
}

#[derive(Clone, Debug, PartialEq)]
pub struct SupertrendSeries {
    pub value: Vec<Option<f64>>,
    pub direction: Vec<Option<i8>>,
    pub params: SupertrendParams,
}

/// Incremental Supertrend.
#[derive(Clone, Debug)]
pub struct SupertrendState {
    params: SupertrendParams,
    atr: AtrState,
    prev_close: Option<f64>,
    final_upper: Option<f64>,
    final_lower: Option<f64>,
    /// Last direction: +1 / −1
    direction: Option<i8>,
    last: Option<SupertrendBar>,
}

impl SupertrendState {
    pub fn new(params: SupertrendParams) -> FinanceResult<Self> {
        let _ = ValidatedSupertrend::new(params)?;
        Ok(Self {
            params,
            atr: AtrState::new(AtrParams::new(params.atr_period))?,
            prev_close: None,
            final_upper: None,
            final_lower: None,
            direction: None,
            last: None,
        })
    }

    pub fn from_history(
        params: SupertrendParams,
        high: &[f64],
        low: &[f64],
        close: &[f64],
    ) -> FinanceResult<Self> {
        let mut s = Self::new(params)?;
        let _ = s.push_bars(high, low, close)?;
        Ok(s)
    }

    pub fn params(&self) -> SupertrendParams {
        self.params
    }

    pub fn reset(&mut self) {
        self.atr.reset();
        self.prev_close = None;
        self.final_upper = None;
        self.final_lower = None;
        self.direction = None;
        self.last = None;
    }

    pub fn push(
        &mut self,
        high: f64,
        low: f64,
        close: f64,
    ) -> FinanceResult<Option<SupertrendBar>> {
        require_finite("high", high)?;
        require_finite("low", low)?;
        require_finite("close", close)?;
        if high < low {
            return Err(FinanceError::InvalidCashflow {
                message: "high must be >= low for each bar",
            });
        }
        let atr_v = self.atr.push(high, low, close)?;
        let out = match atr_v {
            None => {
                self.prev_close = Some(close);
                self.last = None;
                None
            }
            Some(atr) => {
                let mid = 0.5 * (high + low);
                let basic_u = mid + self.params.multiplier * atr;
                let basic_l = mid - self.params.multiplier * atr;
                let prev_c = self.prev_close.unwrap_or(close);

                let fu = match self.final_upper {
                    None => basic_u,
                    Some(prev_u) => {
                        if basic_u < prev_u || prev_c > prev_u {
                            basic_u
                        } else {
                            prev_u
                        }
                    }
                };
                let fl = match self.final_lower {
                    None => basic_l,
                    Some(prev_l) => {
                        if basic_l > prev_l || prev_c < prev_l {
                            basic_l
                        } else {
                            prev_l
                        }
                    }
                };
                self.final_upper = Some(fu);
                self.final_lower = Some(fl);

                let dir = match self.direction {
                    None => {
                        // First defined bar: close above mid → up
                        if close >= mid {
                            1
                        } else {
                            -1
                        }
                    }
                    Some(1) => {
                        if close < fl {
                            -1
                        } else {
                            1
                        }
                    }
                    Some(_) => {
                        if close > fu {
                            1
                        } else {
                            -1
                        }
                    }
                };
                self.direction = Some(dir);
                let value = if dir > 0 { fl } else { fu };
                let bar = SupertrendBar {
                    value,
                    direction: dir,
                };
                self.prev_close = Some(close);
                self.last = Some(bar);
                Some(bar)
            }
        };
        Ok(out)
    }

    pub fn push_bars(
        &mut self,
        high: &[f64],
        low: &[f64],
        close: &[f64],
    ) -> FinanceResult<Vec<Option<SupertrendBar>>> {
        require_hlc(high, low, close)?;
        let mut out = Vec::with_capacity(close.len());
        for i in 0..close.len() {
            out.push(self.push(high[i], low[i], close[i])?);
        }
        Ok(out)
    }

    pub fn last(&self) -> Option<SupertrendBar> {
        self.last
    }
}

pub fn supertrend(
    high: &[f64],
    low: &[f64],
    close: &[f64],
    params: SupertrendParams,
) -> FinanceResult<SupertrendSeries> {
    ValidatedSupertrend::new(params)?.compute(high, low, close)
}

fn supertrend_validated(
    high: &[f64],
    low: &[f64],
    close: &[f64],
    eng: ValidatedSupertrend,
) -> FinanceResult<SupertrendSeries> {
    let mut st = SupertrendState::new(eng.params)?;
    let bars = st.push_bars(high, low, close)?;
    let n = bars.len();
    let mut value = vec![None; n];
    let mut direction = vec![None; n];
    for (i, b) in bars.into_iter().enumerate() {
        if let Some(bar) = b {
            value[i] = Some(bar.value);
            direction[i] = Some(bar.direction);
        }
    }
    Ok(SupertrendSeries {
        value,
        direction,
        params: eng.params,
    })
}

#[derive(Clone, Debug)]
pub struct SupertrendSolution {
    series: SupertrendSeries,
    close: Vec<f64>,
    formula: String,
}

impl SupertrendSolution {
    pub fn series(&self) -> &SupertrendSeries {
        &self.series
    }
    pub fn formula(&self) -> &str {
        &self.formula
    }

    pub fn print_table(&self) {
        self.print_table_locale_opt(None, None);
    }

    pub fn print_table_locale(&self, locale: &num_format::Locale, precision: usize) {
        self.print_table_locale_opt(Some(locale), Some(precision));
    }

    fn print_table_locale_opt(
        &self,
        locale: Option<&num_format::Locale>,
        precision: Option<usize>,
    ) {
        let columns = columns_with_strings(&[
            ("period", "i", true),
            ("close", "f", true),
            ("st", "f", true),
            ("dir", "i", true),
        ]);
        let data = self
            .close
            .iter()
            .enumerate()
            .map(|(i, c)| {
                let d = self.series.direction[i]
                    .map(|x| x.to_string())
                    .unwrap_or_else(|| "n/a".to_string());
                vec![
                    i.to_string(),
                    c.to_string(),
                    opt_cell(self.series.value[i]),
                    d,
                ]
            })
            .collect();
        print_table_locale_opt(&columns, data, locale, precision);
    }
}

/// # Examples
/// ```
/// use finance_solution::stocks::ta::{supertrend_solution, SupertrendParams};
/// let n = 40usize;
/// let h: Vec<_> = (0..n).map(|i| 101.0 + i as f64 * 0.2).collect();
/// let l: Vec<_> = (0..n).map(|i| 99.0 + i as f64 * 0.2).collect();
/// let c: Vec<_> = (0..n).map(|i| 100.0 + i as f64 * 0.2).collect();
/// let sol = supertrend_solution(&h, &l, &c, SupertrendParams::standard()).unwrap();
/// assert!(sol.formula().contains("ATR"));
/// ```
pub fn supertrend_solution(
    high: &[f64],
    low: &[f64],
    close: &[f64],
    params: SupertrendParams,
) -> FinanceResult<SupertrendSolution> {
    let series = supertrend(high, low, close, params)?;
    Ok(SupertrendSolution {
        series,
        close: close.to_vec(),
        formula: format!(
            "Supertrend ATR({}) x {}; ST = final lower (up) / upper (down)",
            params.atr_period, params.multiplier
        ),
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn produces_values() {
        let n = 40usize;
        let h: Vec<_> = (0..n).map(|i| 101.0 + i as f64 * 0.2).collect();
        let l: Vec<_> = (0..n).map(|i| 99.0 + i as f64 * 0.2).collect();
        let c: Vec<_> = (0..n).map(|i| 100.0 + i as f64 * 0.2).collect();
        let s = supertrend(&h, &l, &c, SupertrendParams::standard()).unwrap();
        assert!(s.value.iter().filter(|x| x.is_some()).count() > 10);
        // Strong uptrend → mostly +1
        let up = s.direction.iter().filter(|d| **d == Some(1)).count();
        assert!(up > 5);
    }

    #[test]
    fn state_parity() {
        let n = 35usize;
        let h: Vec<_> = (0..n).map(|i| 12.0 + i as f64 * 0.05).collect();
        let l: Vec<_> = (0..n).map(|i| 10.0 + i as f64 * 0.05).collect();
        let c: Vec<_> = (0..n).map(|i| 11.0 + i as f64 * 0.05).collect();
        let p = SupertrendParams::standard();
        let batch = supertrend(&h, &l, &c, p).unwrap();
        let mut st = SupertrendState::new(p).unwrap();
        for i in 0..n {
            let o = st.push(h[i], l[i], c[i]).unwrap();
            match (o, batch.value[i], batch.direction[i]) {
                (None, None, None) => {}
                (Some(bar), Some(v), Some(d)) => {
                    assert!((bar.value - v).abs() < 1e-9);
                    assert_eq!(bar.direction, d);
                }
                other => panic!("{other:?}"),
            }
        }
    }

    #[test]
    fn bad_mult_err() {
        assert!(ValidatedSupertrend::new(SupertrendParams::new(10, 0.0)).is_err());
    }
}