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
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
//! # Average Directional Index (ADX) / +DI / −DI / DX
//!
//! Wilder directional movement system (period \(N\), classic **14**):
//!
//! ```text
//! +DM = up-move if up > down and up > 0 else 0
//! −DM = down-move if down > up and down > 0 else 0
//! TR  = true range
//! Smooth TR, +DM, −DM with Wilder (seed = SMA of first N, then Wilder)
//! +DI = 100 * smooth(+DM) / smooth(TR)
//! −DI = 100 * smooth(−DM) / smooth(TR)
//! DX  = 100 * |+DI − −DI| / (+DI + −DI)
//! ADX = Wilder smooth of DX (first ADX = SMA of first N DX values)
//! ```
//!
//! Warm-up: first bar has no prior close (TR = H−L; DM from first change needs bar 1).  
//! First DI/DX at index `N−1` after N TR/DM samples; first ADX after N DX values
//! (index roughly `2N−2` on a continuous path).
//!
//! ---
//!
//! ## Trading perspective
//!
//! | Signal | Habit (classic, not a rule) |
//! |--------|-----------------------------|
//! | ADX rising / \> ~25 | Trend strength — trend systems “allowed” |
//! | ADX low / falling | Range — oscillators / mean-reversion more natural |
//! | +DI \> −DI | Bullish directional bias |
//! | −DI \> +DI | Bearish directional bias |
//! | DI cross | Direction change screen (filter with ADX level) |
//!
//! **ADX is not direction** — only strength. Always read **+DI / −DI** (or price structure)
//! for side.
//!
//! ## vs ATR / Supertrend / MACD
//!
//! | | ADX/DI | ATR | Supertrend | MACD |
//! |--|--------|-----|------------|------|
//! | Measures | Trend *strength* + DI direction | Volatility size | Trail stop / side | Momentum of closes |
//! | Good at | Regime filter | Stops / size | In/out of trend | Timing / hist flips |
//!
//! ## Pairs well with
//!
//! - **Supertrend / SAR / Donchian** — take breakouts only if ADX confirms trend.
//! - **RSI / WillR / CCI** — fade extremes only if ADX is weak (range regime).
//! - **Moving averages** — DI side + MA slope agreement.
//!
//! ---
//!
//! ## Engineering
//!
//! [`AdxParams`] → [`adx`] / [`AdxState`] → [`adx_solution`].  
//! Batch uses [`AdxState`] end-to-end. After seeds, each push is **O(1)**.  
//! Smoothing uses the same **average-seed then Wilder** style as this crate’s ATR
//! (SMA of first N samples, then \((prev·(N−1)+x)/N\)).
//!
//! ## Word problem
//!
//! > Can ADX be defined on a series shorter than \(2N−1\) bars?
//!
//! Usually **no** for a full ADX value (need N DM/TR seeds then N DX for ADX seed).
//!
//! ```
//! use finance_solution::stocks::ta::{adx, AdxParams};
//! 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 = adx(&h, &l, &c, AdxParams::period_14()).unwrap();
//! assert!(s.adx.iter().any(|x| x.is_some()));
//! assert!(s.plus_di.iter().any(|x| x.is_some()));
//! ```

use crate::stocks::ta::common::{opt_cell, require_hlc, true_range};
use crate::util::error::{require_finite, FinanceError, FinanceResult};
use crate::util::primitives::PeriodLength;
use crate::{columns_with_strings, print_table_locale_opt};

/// ADX / DI Wilder period.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct AdxParams {
    pub period: usize,
}

impl AdxParams {
    pub const fn new(period: usize) -> Self {
        Self { period }
    }

    pub const fn period_14() -> Self {
        Self { period: 14 }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ValidatedAdx {
    params: AdxParams,
}

impl ValidatedAdx {
    pub fn new(params: AdxParams) -> FinanceResult<Self> {
        PeriodLength::new(params.period)?;
        Ok(Self { params })
    }

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

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

#[derive(Clone, Debug, PartialEq)]
pub struct AdxSeries {
    pub plus_di: Vec<Option<f64>>,
    pub minus_di: Vec<Option<f64>>,
    pub dx: Vec<Option<f64>>,
    pub adx: Vec<Option<f64>>,
    pub params: AdxParams,
}

impl AdxSeries {
    pub fn last_adx(&self) -> Option<f64> {
        self.adx.iter().rev().find_map(|x| *x)
    }
}

/// One-bar ADX pack (any field may still be warming up).
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct AdxBarOutput {
    pub plus_di: Option<f64>,
    pub minus_di: Option<f64>,
    pub dx: Option<f64>,
    pub adx: Option<f64>,
}

/// Incremental ADX / DI / DX.
#[derive(Clone, Debug)]
pub struct AdxState {
    params: AdxParams,
    prev_high: Option<f64>,
    prev_low: Option<f64>,
    prev_close: Option<f64>,
    /// Seed buffers until length == period.
    seed_tr: Vec<f64>,
    seed_pdm: Vec<f64>,
    seed_mdm: Vec<f64>,
    atr: Option<f64>,
    pdm: Option<f64>,
    mdm: Option<f64>,
    seed_dx: Vec<f64>,
    adx: Option<f64>,
    last: Option<AdxBarOutput>,
}

impl AdxState {
    pub fn new(params: AdxParams) -> FinanceResult<Self> {
        PeriodLength::new(params.period)?;
        Ok(Self {
            params,
            prev_high: None,
            prev_low: None,
            prev_close: None,
            seed_tr: Vec::with_capacity(params.period),
            seed_pdm: Vec::with_capacity(params.period),
            seed_mdm: Vec::with_capacity(params.period),
            atr: None,
            pdm: None,
            mdm: None,
            seed_dx: Vec::with_capacity(params.period),
            adx: None,
            last: None,
        })
    }

    pub fn from_history(
        params: AdxParams,
        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) -> AdxParams {
        self.params
    }

    pub fn reset(&mut self) {
        self.prev_high = None;
        self.prev_low = None;
        self.prev_close = None;
        self.seed_tr.clear();
        self.seed_pdm.clear();
        self.seed_mdm.clear();
        self.atr = None;
        self.pdm = None;
        self.mdm = None;
        self.seed_dx.clear();
        self.adx = None;
        self.last = None;
    }

    pub fn push(&mut self, high: f64, low: f64, close: f64) -> FinanceResult<AdxBarOutput> {
        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 period = self.params.period;
        let pf = period as f64;

        let (plus_dm, minus_dm) = match (self.prev_high, self.prev_low) {
            (Some(ph), Some(pl)) => {
                let up = high - ph;
                let down = pl - low;
                let pdm = if up > down && up > 0.0 { up } else { 0.0 };
                let mdm = if down > up && down > 0.0 { down } else { 0.0 };
                (pdm, mdm)
            }
            _ => (0.0, 0.0),
        };
        let tr = true_range(high, low, self.prev_close);

        let mut plus_di = None;
        let mut minus_di = None;
        let mut dx = None;
        let mut adx_out = None;

        let smoothed = if self.atr.is_none() {
            self.seed_tr.push(tr);
            self.seed_pdm.push(plus_dm);
            self.seed_mdm.push(minus_dm);
            if self.seed_tr.len() == period {
                let atr = self.seed_tr.iter().sum::<f64>() / pf;
                let pdm = self.seed_pdm.iter().sum::<f64>() / pf;
                let mdm = self.seed_mdm.iter().sum::<f64>() / pf;
                self.atr = Some(atr);
                self.pdm = Some(pdm);
                self.mdm = Some(mdm);
                Some((atr, pdm, mdm))
            } else {
                None
            }
        } else {
            let atr = (self.atr.unwrap() * (pf - 1.0) + tr) / pf;
            let pdm = (self.pdm.unwrap() * (pf - 1.0) + plus_dm) / pf;
            let mdm = (self.mdm.unwrap() * (pf - 1.0) + minus_dm) / pf;
            self.atr = Some(atr);
            self.pdm = Some(pdm);
            self.mdm = Some(mdm);
            Some((atr, pdm, mdm))
        };

        if let Some((atr, pdm, mdm)) = smoothed {
            if atr > 0.0 {
                let pdi = 100.0 * pdm / atr;
                let mdi = 100.0 * mdm / atr;
                plus_di = Some(pdi);
                minus_di = Some(mdi);
                let den = pdi + mdi;
                if den > 0.0 {
                    let d = 100.0 * (pdi - mdi).abs() / den;
                    dx = Some(d);
                    if self.adx.is_none() {
                        self.seed_dx.push(d);
                        if self.seed_dx.len() == period {
                            let a = self.seed_dx.iter().sum::<f64>() / pf;
                            self.adx = Some(a);
                            adx_out = Some(a);
                        }
                    } else {
                        let a = (self.adx.unwrap() * (pf - 1.0) + d) / pf;
                        self.adx = Some(a);
                        adx_out = Some(a);
                    }
                }
            }
        }

        self.prev_high = Some(high);
        self.prev_low = Some(low);
        self.prev_close = Some(close);

        let out = AdxBarOutput {
            plus_di,
            minus_di,
            dx,
            adx: adx_out,
        };
        self.last = Some(out);
        Ok(out)
    }

    pub fn push_bars(
        &mut self,
        high: &[f64],
        low: &[f64],
        close: &[f64],
    ) -> FinanceResult<Vec<AdxBarOutput>> {
        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<AdxBarOutput> {
        self.last
    }
}

pub fn adx(
    high: &[f64],
    low: &[f64],
    close: &[f64],
    params: AdxParams,
) -> FinanceResult<AdxSeries> {
    ValidatedAdx::new(params)?.compute(high, low, close)
}

fn adx_validated(
    high: &[f64],
    low: &[f64],
    close: &[f64],
    eng: ValidatedAdx,
) -> FinanceResult<AdxSeries> {
    let mut st = AdxState::new(eng.params)?;
    let bars = st.push_bars(high, low, close)?;
    let n = bars.len();
    let mut plus_di = vec![None; n];
    let mut minus_di = vec![None; n];
    let mut dx = vec![None; n];
    let mut adx = vec![None; n];
    for (i, b) in bars.into_iter().enumerate() {
        plus_di[i] = b.plus_di;
        minus_di[i] = b.minus_di;
        dx[i] = b.dx;
        adx[i] = b.adx;
    }
    Ok(AdxSeries {
        plus_di,
        minus_di,
        dx,
        adx,
        params: eng.params,
    })
}

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

impl AdxSolution {
    pub fn series(&self) -> &AdxSeries {
        &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),
            ("plus_di", "f", true),
            ("minus_di", "f", true),
            ("dx", "f", true),
            ("adx", "f", true),
        ]);
        let data = self
            .close
            .iter()
            .enumerate()
            .map(|(i, c)| {
                vec![
                    i.to_string(),
                    c.to_string(),
                    opt_cell(self.series.plus_di[i]),
                    opt_cell(self.series.minus_di[i]),
                    opt_cell(self.series.dx[i]),
                    opt_cell(self.series.adx[i]),
                ]
            })
            .collect();
        print_table_locale_opt(&columns, data, locale, precision);
    }
}

/// # Examples
/// ```
/// use finance_solution::stocks::ta::{adx_solution, AdxParams};
/// let n = 50usize;
/// let h: Vec<_> = (0..n).map(|i| 101.0 + i as f64 * 0.1).collect();
/// let l: Vec<_> = (0..n).map(|i| 99.0 + i as f64 * 0.1).collect();
/// let c: Vec<_> = (0..n).map(|i| 100.0 + i as f64 * 0.1).collect();
/// let sol = adx_solution(&h, &l, &c, AdxParams::period_14()).unwrap();
/// assert!(sol.formula().contains("Wilder"));
/// ```
pub fn adx_solution(
    high: &[f64],
    low: &[f64],
    close: &[f64],
    params: AdxParams,
) -> FinanceResult<AdxSolution> {
    let series = adx(high, low, close, params)?;
    Ok(AdxSolution {
        series,
        close: close.to_vec(),
        formula: format!(
            "Wilder ADX/DI period={}: +DI/-DI from DM & TR; DX; ADX=Wilder(DX)",
            params.period
        ),
    })
}

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

    fn rising_path(n: usize) -> (Vec<f64>, Vec<f64>, Vec<f64>) {
        let h: Vec<_> = (0..n).map(|i| 101.0 + i as f64 * 0.3).collect();
        let l: Vec<_> = (0..n).map(|i| 99.0 + i as f64 * 0.3).collect();
        let c: Vec<_> = (0..n).map(|i| 100.0 + i as f64 * 0.3).collect();
        (h, l, c)
    }

    #[test]
    fn produces_adx_on_long_path() {
        let (h, l, c) = rising_path(50);
        let s = adx(&h, &l, &c, AdxParams::period_14()).unwrap();
        assert!(s.adx.iter().filter(|x| x.is_some()).count() > 5);
        assert!(s.plus_di.iter().any(|x| x.is_some()));
        // Strong uptrend: +DI should dominate when defined
        if let (Some(p), Some(m)) = (s.plus_di[49], s.minus_di[49]) {
            assert!(p > m, "+DI={p} −DI={m}");
        }
        let a = s.last_adx().unwrap();
        assert!(a >= 0.0 && a <= 100.0, "adx={a}");
    }

    #[test]
    fn di_before_adx() {
        let (h, l, c) = rising_path(30);
        let s = adx(&h, &l, &c, AdxParams::period_14()).unwrap();
        let first_di = s.plus_di.iter().position(|x| x.is_some()).unwrap();
        let first_adx = s.adx.iter().position(|x| x.is_some()).unwrap();
        assert!(first_di < first_adx);
    }

    #[test]
    fn state_parity() {
        let (h, l, c) = rising_path(45);
        let p = AdxParams::period_14();
        let batch = adx(&h, &l, &c, p).unwrap();
        let mut st = AdxState::new(p).unwrap();
        for i in 0..c.len() {
            let o = st.push(h[i], l[i], c[i]).unwrap();
            match (o.plus_di, batch.plus_di[i]) {
                (None, None) => {}
                (Some(a), Some(b)) => assert!((a - b).abs() < 1e-9, "pdi {i}"),
                other => panic!("pdi {i}: {other:?}"),
            }
            match (o.adx, batch.adx[i]) {
                (None, None) => {}
                (Some(a), Some(b)) => assert!((a - b).abs() < 1e-9, "adx {i}"),
                other => panic!("adx {i}: {other:?}"),
            }
        }
    }

    #[test]
    fn high_lt_low_err() {
        assert!(adx(&[1.0], &[2.0], &[1.5], AdxParams::period_14()).is_err());
    }
}