mylittleindicators 0.1.8

Multi-stream financial indicators library — 556 bar indicators + 21 event primitives across 35 categories. Consumes 27 stream kinds from digdigdig3 exchange connectors: OHLCV bars, ticks, orderbook (snapshot/delta/L3), funding/predicted funding/funding settlement, mark price, index price, open interest, liquidations, ticker, agg trades, long/short ratio, option greeks, volatility index, historical volatility, basis (derived), composite index, settlement events, block trades, insurance fund, risk limit, market warning, and three kline-family variants. Live-verified on 12 exchanges (89% pass-rate on a 150s BTC slice).
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//! Polynomial Regression
//! Полиномиальная регрессия для моделирования нелинейных трендов
//! y = β₀ + β₁x + β₂x² + ... + βₙxⁿ + ε

use crate::bar_indicators::indicator_value::IndicatorValue;
use crate::bar_indicators::ohlcv_field::OhlcvField;

/// Polynomial Regression Model
#[derive(Clone)]
pub struct PolynomialRegression {
    // Параметры модели
    degree: usize,                      // Степень полинома

    // Данные
    x_values: Vec<f64>,                  // Независимая переменная (обычно время/индекс)
    y_values: Vec<f64>,                  // Зависимая переменная (цена/значение)

    // Коэффициенты полинома
    coefficients: Vec<f64>,              // β₀, β₁, β₂, ..., βₙ

    // Подогнанные значения и остатки
    fitted_values: Vec<f64>,             // Предсказанные значения
    residuals: Vec<f64>,                 // Остатки

    // Статистики модели
    r_squared: f64,                     // Коэффициент детерминации
    adjusted_r_squared: f64,            // Скорректированный R²
    mse: f64,                          // Среднеквадратичная ошибка
    rmse: f64,                         // Корень из MSE

    // Производные для анализа тренда
    first_derivative: f64,              // Первая производная (скорость изменения)
    second_derivative: f64,             // Вторая производная (ускорение)

    // Прогноз
    forecast: f64,
    forecast_trend: TrendDirection,

    // Источник данных
    source: OhlcvField,                 // Поле OHLCV для расчета

    // Состояние
    is_fitted: bool,
    min_observations: usize,
    current_x: f64,                     // Текущее значение x (обычно индекс)
}

/// Направление тренда на основе производных
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TrendDirection {
    StrongUptrend,      // Первая производная > 0, вторая > 0 (ускоряющийся рост)
    Uptrend,           // Первая производная > 0, вторая ≤ 0 (замедляющийся рост)
    Sideways,          // Первая производная ≈ 0 (боковое движение)
    Downtrend,         // Первая производная < 0, вторая ≥ 0 (замедляющееся падение)
    StrongDowntrend,   // Первая производная < 0, вторая < 0 (ускоряющееся падение)
}

impl PolynomialRegression {
    pub fn new(degree: usize) -> Self {
        Self::with_source(degree, OhlcvField::Close)
    }

    /// Создать с настраиваемым источником данных
    pub fn with_source(degree: usize, source: OhlcvField) -> Self {
        let degree = degree.clamp(1, 8); // Ограничиваем степень от 1 до 8
        let min_obs = (degree + 2).max(10);

        Self {
            degree,
            x_values: Vec::with_capacity(512),
            y_values: Vec::with_capacity(512),
            coefficients: Vec::with_capacity(16),
            fitted_values: Vec::with_capacity(512),
            residuals: Vec::with_capacity(512),
            r_squared: 0.0,
            adjusted_r_squared: 0.0,
            mse: 0.0,
            rmse: 0.0,
            first_derivative: 0.0,
            second_derivative: 0.0,
            forecast: 0.0,
            forecast_trend: TrendDirection::Sideways,
            source,
            is_fitted: false,
            min_observations: min_obs,
            current_x: 0.0,
        }
    }

    /// Обновить модель с OHLCV баром
    pub fn update_bar(&mut self, open: f64, high: f64, low: f64, close: f64, volume: f64) -> f64 {
        let value = self.source.extract(open, high, low, close, volume);
        self.update(value)
    }

    /// Обновить модель новым значением
    pub fn update(&mut self, value: f64) -> f64 {
        // Добавляем новое значение
        if self.y_values.len() >= 512 {
            self.y_values.remove(0);
            self.x_values.remove(0);
            // Сдвигаем x значения
            for x in &mut self.x_values {
                *x -= 1.0;
            }
            self.current_x -= 1.0;
        }
        
        self.y_values.push(value);
        self.x_values.push(self.current_x);
        
        self.current_x += 1.0;
        
        // Если достаточно данных, переоцениваем модель
        if self.y_values.len() >= self.min_observations {
            self.fit_model();
            self.calculate_derivatives();
            self.generate_forecast();
            self.determine_trend_direction();
        }
        
        self.forecast
    }
    
    /// Подгонка полиномиальной модели
    fn fit_model(&mut self) {
        if self.y_values.len() < self.min_observations {
            return;
        }
        
        // 1. Создаем матрицу Вандермонда
        let vandermonde_matrix = self.create_vandermonde_matrix();
        
        // 2. Решаем систему нормальных уравнений
        self.solve_normal_equations(&vandermonde_matrix);
        
        // 3. Рассчитываем подогнанные значения и остатки
        self.calculate_fitted_and_residuals();
        
        // 4. Рассчитываем статистики модели
        self.calculate_model_statistics();
        
        self.is_fitted = true;
    }
    
    /// Создание матрицы Вандермонда
    fn create_vandermonde_matrix(&self) -> Vec<Vec<f64>> {
        let n = self.x_values.len();
        let mut matrix = vec![vec![0.0; self.degree + 1]; n];
        
        for (row, &x) in matrix.iter_mut().zip(self.x_values.iter()) {
            for (j, cell) in row.iter_mut().enumerate() {
                *cell = x.powi(j as i32);
            }
        }
        
        matrix
    }
    
    /// Решение системы нормальных уравнений (X'X)β = X'y
    fn solve_normal_equations(&mut self, x_matrix: &[Vec<f64>]) {
        self.coefficients.clear();
        
        let n = x_matrix.len();
        let p = self.degree + 1;
        
        if n == 0 || p == 0 {
            return;
        }
        
        // Создаем X'X матрицу
        let mut xtx = vec![vec![0.0; p]; p];
        for i in 0..p {
            for j in 0..p {
                for row in &x_matrix[..n] {
                    xtx[i][j] += row[i] * row[j];
                }
            }
        }

        // Создаем X'y вектор
        let mut xty = vec![0.0; p];
        for i in 0..p {
            for (row, &y) in x_matrix[..n].iter().zip(self.y_values.iter()) {
                xty[i] += row[i] * y;
            }
        }
        
        // Решаем систему (упрощенный метод - диагональное приближение)
        for i in 0..p {
            if xtx[i][i].abs() > 1e-12 {
                let coeff = xty[i] / xtx[i][i];
                self.coefficients.push(coeff);
            } else {
                self.coefficients.push(0.0);
            }
        }
        
        // Если не удалось решить систему, используем простую линейную регрессию
        if self.coefficients.len() < 2 {
            self.fallback_to_linear_regression();
        }
    }
    
    /// Резервная линейная регрессия если полиномиальная не работает
    fn fallback_to_linear_regression(&mut self) {
        self.coefficients.clear();
        
        if self.x_values.len() < 2 || self.y_values.len() < 2 {
            return;
        }
        
        let n = self.x_values.len() as f64;
        let sum_x: f64 = self.x_values.iter().sum();
        let sum_y: f64 = self.y_values.iter().sum();
        let sum_xy: f64 = self.x_values.iter().zip(self.y_values.iter())
            .map(|(&x, &y)| x * y).sum();
        let sum_x2: f64 = self.x_values.iter().map(|&x| x * x).sum();
        
        let mean_x = sum_x / n;
        let mean_y = sum_y / n;
        
        // Slope (β₁)
        let denominator = sum_x2 - n * mean_x * mean_x;
        let slope = if denominator.abs() > 1e-12 {
            (sum_xy - n * mean_x * mean_y) / denominator
        } else {
            0.0
        };
        
        // Intercept (β₀)
        let intercept = mean_y - slope * mean_x;
        
        self.coefficients.push(intercept);
        self.coefficients.push(slope);
    }
    
    /// Рассчитать подогнанные значения и остатки
    fn calculate_fitted_and_residuals(&mut self) {
        self.fitted_values.clear();
        self.residuals.clear();
        
        for (i, &x) in self.x_values.iter().enumerate() {
            let fitted_value = self.evaluate_polynomial(x);
            self.fitted_values.push(fitted_value);

            if i < self.y_values.len() {
                let residual = self.y_values[i] - fitted_value;
                self.residuals.push(residual);
            }
        }
    }
    
    /// Вычислить значение полинома в точке x
    fn evaluate_polynomial(&self, x: f64) -> f64 {
        let mut result = 0.0;
        for (i, &coeff) in self.coefficients.iter().enumerate() {
            result += coeff * x.powi(i as i32);
        }
        result
    }
    
    /// Рассчитать статистики модели
    fn calculate_model_statistics(&mut self) {
        if self.y_values.is_empty() || self.fitted_values.is_empty() {
            return;
        }
        
        let n = self.y_values.len() as f64;
        let p = self.coefficients.len() as f64;
        
        // Среднее значение y
        let y_mean: f64 = self.y_values.iter().sum::<f64>() / n;
        
        // Сумма квадратов
        let mut ss_tot = 0.0; // Общая сумма квадратов
        let mut ss_res = 0.0; // Остаточная сумма квадратов
        
        for (i, &y_actual) in self.y_values.iter().enumerate() {
            ss_tot += (y_actual - y_mean).powi(2);
            
            if i < self.fitted_values.len() {
                let y_fitted = self.fitted_values[i];
                ss_res += (y_actual - y_fitted).powi(2);
            }
        }
        
        // R-squared
        self.r_squared = if ss_tot > 0.0 {
            1.0 - (ss_res / ss_tot)
        } else {
            0.0
        };
        
        // Adjusted R-squared
        if n > p + 1.0 {
            self.adjusted_r_squared = 1.0 - ((ss_res / (n - p)) / (ss_tot / (n - 1.0)));
        } else {
            self.adjusted_r_squared = self.r_squared;
        }
        
        // MSE и RMSE
        self.mse = ss_res / n;
        self.rmse = self.mse.sqrt();
    }
    
    /// Рассчитать производные в текущей точке
    fn calculate_derivatives(&mut self) {
        if self.coefficients.len() < 2 {
            self.first_derivative = 0.0;
            self.second_derivative = 0.0;
            return;
        }
        
        let x = self.current_x - 1.0; // Последняя точка
        
        // Первая производная: d/dx(Σ βᵢxⁱ) = Σ i*βᵢxⁱ⁻¹
        self.first_derivative = 0.0;
        for (i, &coeff) in self.coefficients.iter().enumerate().skip(1) {
            self.first_derivative += (i as f64) * coeff * x.powi(i as i32 - 1);
        }
        
        // Вторая производная: d²/dx²(Σ βᵢxⁱ) = Σ i*(i-1)*βᵢxⁱ⁻²
        self.second_derivative = 0.0;
        for (i, &coeff) in self.coefficients.iter().enumerate().skip(2) {
            self.second_derivative += (i as f64) * ((i - 1) as f64) * coeff * x.powi(i as i32 - 2);
        }
    }
    
    /// Генерация прогноза на следующую точку
    fn generate_forecast(&mut self) {
        if !self.is_fitted {
            return;
        }
        
        self.forecast = self.evaluate_polynomial(self.current_x);
    }
    
    /// Определение направления тренда
    fn determine_trend_direction(&mut self) {
        let first_deriv_threshold = self.rmse * 0.1; // Порог для определения значимости
        
        self.forecast_trend = if self.first_derivative.abs() < first_deriv_threshold {
            TrendDirection::Sideways
        } else if self.first_derivative > 0.0 {
            if self.second_derivative > 0.0 {
                TrendDirection::StrongUptrend
            } else {
                TrendDirection::Uptrend
            }
        } else if self.second_derivative < 0.0 {
            TrendDirection::StrongDowntrend
        } else {
            TrendDirection::Downtrend
        };
    }
    
    /// Получить прогноз
    pub fn forecast(&self) -> f64 {
        self.forecast
    }
    
    /// Получить направление тренда
    pub fn trend_direction(&self) -> TrendDirection {
        self.forecast_trend
    }
    
    /// Получить производные
    pub fn derivatives(&self) -> (f64, f64) {
        (self.first_derivative, self.second_derivative)
    }
    
    /// Получить коэффициенты полинома
    pub fn coefficients(&self) -> &[f64] {
        &self.coefficients
    }
    
    /// Получить статистики модели
    pub fn statistics(&self) -> (f64, f64, f64, f64) {
        (self.r_squared, self.adjusted_r_squared, self.mse, self.rmse)
    }
    
    /// Получить степень полинома
    pub fn degree(&self) -> usize {
        self.degree
    }
    
    /// Проверить готовность модели
    pub fn is_fitted(&self) -> bool {
        self.is_fitted
    }
    
    /// Вычислить значение полинома для произвольной точки
    pub fn predict(&self, x: f64) -> f64 {
        if !self.is_fitted {
            return 0.0;
        }
        self.evaluate_polynomial(x)
    }
    
    /// Получить производную в произвольной точке
    pub fn derivative_at(&self, x: f64) -> f64 {
        if self.coefficients.len() < 2 {
            return 0.0;
        }
        
        let mut derivative = 0.0;
        for (i, &coeff) in self.coefficients.iter().enumerate().skip(1) {
            derivative += (i as f64) * coeff * x.powi(i as i32 - 1);
        }
        derivative
    }
    
    /// Найти экстремумы полинома (корни первой производной)
    pub fn find_extrema(&self) -> Vec<f64> {
        let mut extrema: Vec<f64> = Vec::with_capacity(8);

        if self.coefficients.len() < 3 {
            return extrema; // Линейная функция не имеет экстремумов
        }

        // Для простоты находим экстремумы численно в текущем диапазоне данных
        if let (Some(&x_min), Some(&x_max)) = (self.x_values.first(), self.x_values.last()) {
            let step = (x_max - x_min) / 100.0;
            let mut prev_deriv = self.derivative_at(x_min);

            for i in 1..=100 {
                let x = x_min + i as f64 * step;
                let curr_deriv = self.derivative_at(x);

                // Смена знака производной указывает на экстремум
                if prev_deriv * curr_deriv < 0.0 {
                    extrema.push(x - step * 0.5); // Приблизительная позиция экстремума
                }

                prev_deriv = curr_deriv;
            }
        }

        extrema
    }
    
    /// Сбросить модель
    pub fn reset(&mut self) {
        self.x_values.clear();
        self.y_values.clear();
        self.coefficients.clear();
        self.fitted_values.clear();
        self.residuals.clear();
        self.r_squared = 0.0;
        self.adjusted_r_squared = 0.0;
        self.mse = 0.0;
        self.rmse = 0.0;
        self.first_derivative = 0.0;
        self.second_derivative = 0.0;
        self.forecast = 0.0;
        self.forecast_trend = TrendDirection::Sideways;
        self.is_fitted = false;
        self.current_x = 0.0;
    }

    #[inline]
    pub fn is_ready(&self) -> bool {
        self.y_values.len() >= self.min_observations
    }

    pub fn value(&self) -> IndicatorValue {
        IndicatorValue::Single(self.forecast)
    }
}

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

    #[test]
    fn test_polynomial_regression_creation() {
        let ind = PolynomialRegression::new(2);
        assert!(!ind.is_ready());
        assert_eq!(ind.forecast(), 0.0);
    }

    #[test]
    fn test_polynomial_regression_warmup() {
        let mut ind = PolynomialRegression::new(2);
        for i in 0..15 {
            let value = 100.0 + (i as f64 * 0.1).sin() * 5.0;
            ind.update(value);
        }
        assert!(ind.is_ready());
    }

    #[test]
    fn test_polynomial_regression_forecast_finite() {
        let mut ind = PolynomialRegression::new(2);
        for i in 0..20 {
            let value = 100.0 + i as f64 * 0.5;
            ind.update(value);
        }
        assert!(ind.forecast().is_finite());
        let (r2, _, _, rmse) = ind.statistics();
        assert!(r2.is_finite());
        assert!(rmse.is_finite());
    }

    #[test]
    fn test_polynomial_regression_trend() {
        let mut ind = PolynomialRegression::new(2);
        // Uptrend data
        for i in 0..20 {
            let value = 100.0 + i as f64 * 2.0;
            ind.update(value);
        }
        let (first_deriv, _) = ind.derivatives();
        assert!(first_deriv > 0.0);
    }

    #[test]
    fn test_polynomial_regression_reset() {
        let mut ind = PolynomialRegression::new(2);
        for i in 0..20 {
            let value = 100.0 + i as f64;
            ind.update(value);
        }
        ind.reset();
        assert!(!ind.is_ready());
        assert_eq!(ind.forecast(), 0.0);
    }
}