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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
//! Point of Control (POC) Detector
//! Определяет уровни максимального объема и анализирует их динамику
//! Используется для выявления важных уровней поддержки/сопротивления

use crate::types::Bar;
use std::collections::BTreeMap;

/// Тип POC уровня
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PocType {
    Daily,      // Дневной POC
    Session,    // Сессионный POC  
    Weekly,     // Недельный POC
    Monthly,    // Месячный POC
    Rolling,    // Скользящий POC
}

/// Информация о POC уровне
#[derive(Debug, Clone)]
pub struct PocLevel {
    pub price: f64,                     // Цена POC уровня
    pub volume: f64,                    // Общий объем на уровне
    pub bar_count: u32,                 // Количество баров на уровне
    pub poc_type: PocType,              // Тип POC
    pub strength: f64,                  // Сила уровня (0.0 - 1.0)
    pub first_touch_time: i64,          // Время первого касания
    pub last_touch_time: i64,           // Время последнего касания
    pub touch_count: u32,               // Количество касаний
    pub breakout_probability: f64,      // Вероятность пробоя (0.0 - 1.0)
    pub is_active: bool,                // Активен ли уровень
}

impl PocLevel {
    pub fn new(price: f64, volume: f64, poc_type: PocType, timestamp: i64) -> Self {
        Self {
            price,
            volume,
            bar_count: 1,
            poc_type,
            strength: 0.0,
            first_touch_time: timestamp,
            last_touch_time: timestamp,
            touch_count: 1,
            breakout_probability: 0.0,
            is_active: true,
        }
    }
    
    /// Обновить уровень новыми данными
    pub fn update(&mut self, volume: f64, timestamp: i64) {
        self.volume += volume;
        self.bar_count += 1;
        self.last_touch_time = timestamp;
        self.touch_count += 1;
        self.recalculate_strength();
    }
    
    /// Пересчитать силу уровня
    fn recalculate_strength(&mut self) {
        // Сила зависит от объема, количества касаний и времени существования
        let volume_factor = (self.volume / 1000000.0).min(1.0); // Нормализация объема
        let touch_factor = (self.touch_count as f64 / 10.0).min(1.0); // Нормализация касаний
        let time_factor = ((self.last_touch_time - self.first_touch_time) as f64 / 86400.0 / 7.0).min(1.0); // Недели
        
        self.strength = (volume_factor * 0.5 + touch_factor * 0.3 + time_factor * 0.2).min(1.0);
        
        // Вероятность пробоя обратно пропорциональна силе
        self.breakout_probability = 1.0 - self.strength;
    }
    
    /// Проверить касание уровня
    pub fn is_touched(&self, high: f64, low: f64, tolerance: f64) -> bool {
        let upper_bound = self.price + tolerance;
        let lower_bound = self.price - tolerance;
        
        low <= upper_bound && high >= lower_bound
    }
    
    /// Проверить пробой уровня
    pub fn is_broken(&self, close: f64, tolerance: f64) -> bool {
        (close > self.price + tolerance) || (close < self.price - tolerance)
    }
}

/// Результат анализа POC
#[derive(Debug, Clone)]
pub struct PocAnalysis {
    pub current_poc: Option<PocLevel>,              // Текущий основной POC
    pub active_levels: Vec<PocLevel>,                // Активные POC уровни
    pub broken_levels: Vec<PocLevel>,               // Недавно пробитые уровни
    pub nearest_support: Option<f64>,               // Ближайшая поддержка
    pub nearest_resistance: Option<f64>,            // Ближайшее сопротивление
    pub market_structure: MarketStructure,          // Структура рынка
    pub volume_imbalance: f64,                      // Дисбаланс объемов (-1.0 до 1.0)
    pub poc_confluence: u32,                        // Количество совпадающих POC уровней
}

/// Структура рынка на основе POC анализа
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MarketStructure {
    Accumulation,   // Накопление (цена консолидируется около POC)
    Distribution,   // Распределение (высокий объем, но цена не растет)
    Trending,       // Тренд (POC смещается в направлении тренда)
    Breakout,       // Пробой (цена вышла за пределы основных POC уровней)
    Consolidation,  // Консолидация (цена торгуется между POC уровнями)
}

impl Default for PocAnalysis {
    fn default() -> Self {
        Self {
            current_poc: None,
            active_levels: Vec::with_capacity(32),
            broken_levels: Vec::with_capacity(16),
            nearest_support: None,
            nearest_resistance: None,
            market_structure: MarketStructure::Consolidation,
            volume_imbalance: 0.0,
            poc_confluence: 0,
        }
    }
}

/// Point of Control детектор
#[derive(Clone)]
pub struct PocDetector {
    // Параметры
    price_precision: u32,               // Точность цены (количество знаков после запятой)
    min_volume_threshold: f64,          // Минимальный объем для формирования POC
    level_tolerance: f64,               // Толерантность для группировки уровней
    max_levels: usize,                  // Максимальное количество отслеживаемых уровней
    
    // Данные volume profile
    volume_profile: BTreeMap<i64, f64>, // Цена (в тиках) -> Объем
    price_levels: BTreeMap<i64, PocLevel>, // Активные POC уровни
    
    // Временные окна
    daily_volume: BTreeMap<i64, f64>,   // Дневной объем по уровням
    session_volume: BTreeMap<i64, f64>, // Сессионный объем
    rolling_volume: BTreeMap<i64, f64>, // Скользящий объем (N баров)
    
    // Настройки временных окон
    rolling_period: usize,              // Период для скользящего POC
    session_start_hour: u32,            // Начало торговой сессии (UTC)
    session_end_hour: u32,              // Конец торговой сессии (UTC)
    
    // История баров для скользящего анализа
    bar_history: Vec<Bar>,              // История баров
    
    // Результаты анализа
    analysis: PocAnalysis,
    
    // Состояние
    is_ready: bool,
    min_bars: usize,
    current_day: i64,                   // Текущий день (timestamp дня)
    current_session: i64,               // Текущая сессия
}

impl PocDetector {
    pub fn new(
        price_precision: u32,
        min_volume_threshold: f64,
        rolling_period: usize,
    ) -> Self {
        Self {
            price_precision,
            min_volume_threshold,
            level_tolerance: 10.0_f64.powi(-(price_precision as i32)), // Автоматическая толерантность
            max_levels: 20,
            volume_profile: BTreeMap::new(),
            price_levels: BTreeMap::new(),
            daily_volume: BTreeMap::new(),
            session_volume: BTreeMap::new(),
            rolling_volume: BTreeMap::new(),
            rolling_period,
            session_start_hour: 9,  // 9:00 UTC
            session_end_hour: 17,   // 17:00 UTC
            bar_history: Vec::with_capacity(500),
            analysis: PocAnalysis::default(),
            is_ready: false,
            min_bars: rolling_period.max(20),
            current_day: 0,
            current_session: 0,
        }
    }
    
    /// Обновить детектор новым баром
    pub fn update(&mut self, bar: &Bar) -> &PocAnalysis {
        // Добавляем бар в историю
        if self.bar_history.len() >= 500 {
            self.bar_history.remove(0);
        }
        self.bar_history.push(*bar);
        
        // Обновляем volume profile
        self.update_volume_profile(bar);
        
        // Определяем временные окна
        self.update_time_windows(bar);
        
        // Обновляем POC уровни
        self.update_poc_levels(bar);
        
        // Анализируем структуру рынка
        self.analyze_market_structure(bar);
        
        // Обновляем состояние готовности
        if !self.is_ready && self.bar_history.len() >= self.min_bars {
            self.is_ready = true;
        }
        
        &self.analysis
    }
    
    /// Обновить volume profile
    fn update_volume_profile(&mut self, bar: &Bar) {
        let price_levels = self.get_price_levels_for_bar(bar);
        let volume_per_level = bar.volume / price_levels.len().max(1) as f64;

        for price_tick in price_levels {
            *self.rolling_volume.entry(price_tick).or_insert(0.0) += volume_per_level;

            // Дневной объем
            if self.is_same_day(bar.time, self.current_day) {
                *self.daily_volume.entry(price_tick).or_insert(0.0) += volume_per_level;
            }

            // Сессионный объем
            if self.is_same_session(bar.time, self.current_session) {
                *self.session_volume.entry(price_tick).or_insert(0.0) += volume_per_level;
            }
        }

        // Очистка старых данных для скользящего окна
        if self.bar_history.len() >= self.rolling_period {
            let old_bar = self.bar_history[self.bar_history.len() - self.rolling_period];
            self.remove_old_volume(&old_bar);
        }

        // Ограничиваем размер rolling_volume (держим только top N по объёму)
        const MAX_LEVELS: usize = 500;
        if self.rolling_volume.len() > MAX_LEVELS * 2 {
            // Оставляем только уровни с объёмом выше медианы
            let mut volumes: Vec<_> = self.rolling_volume.values().copied().collect();
            volumes.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
            let threshold = volumes.get(MAX_LEVELS).copied().unwrap_or(0.0);
            self.rolling_volume.retain(|_, v| *v >= threshold);
        }
    }
    
    /// Получить ценовые уровни для бара (OHLC точки)
    /// Используем только 4 точки вместо всех тиков в диапазоне
    fn get_price_levels_for_bar(&self, bar: &Bar) -> Vec<i64> {
        let tick_multiplier = 10_f64.powi(self.price_precision as i32);

        // Только OHLC точки - это предотвращает взрывной рост памяти
        vec![
            (bar.open * tick_multiplier).round() as i64,
            (bar.high * tick_multiplier).round() as i64,
            (bar.low * tick_multiplier).round() as i64,
            (bar.close * tick_multiplier).round() as i64,
        ]
    }
    
    /// Удалить старый объем из скользящего окна
    fn remove_old_volume(&mut self, old_bar: &Bar) {
        let price_levels = self.get_price_levels_for_bar(old_bar);
        let volume_per_level = old_bar.volume / price_levels.len() as f64;
        
        for price_tick in price_levels {
            if let Some(volume) = self.rolling_volume.get_mut(&price_tick) {
                *volume -= volume_per_level;
                if *volume <= 0.0 {
                    self.rolling_volume.remove(&price_tick);
                }
            }
        }
    }
    
    /// Обновить временные окна
    fn update_time_windows(&mut self, bar: &Bar) {
        let bar_day = self.get_day_timestamp(bar.time);
        let bar_session = self.get_session_timestamp(bar.time);
        
        // Новый день - очищаем дневной объем
        if bar_day != self.current_day {
            self.daily_volume.clear();
            self.current_day = bar_day;
        }
        
        // Новая сессия - очищаем сессионный объем
        if bar_session != self.current_session {
            self.session_volume.clear();
            self.current_session = bar_session;
        }
    }
    
    /// Обновить POC уровни
    fn update_poc_levels(&mut self, bar: &Bar) {
        // Находим POC для разных временных окон
        let daily_poc = self.find_poc(&self.daily_volume, PocType::Daily, bar.time);
        let session_poc = self.find_poc(&self.session_volume, PocType::Session, bar.time);
        let rolling_poc = self.find_poc(&self.rolling_volume, PocType::Rolling, bar.time);
        
        // Обновляем активные уровни
        self.update_active_levels(daily_poc.clone(), bar);
        self.update_active_levels(session_poc.clone(), bar);
        self.update_active_levels(rolling_poc.clone(), bar);

        // Определяем основной POC
        self.analysis.current_poc = rolling_poc.or(session_poc).or(daily_poc);
        
        // Очищаем неактивные уровни
        self.cleanup_inactive_levels(bar);
        
        // Обновляем поддержки и сопротивления
        self.update_support_resistance(bar);
    }
    
    /// Найти POC в volume profile
    fn find_poc(&self, volume_data: &BTreeMap<i64, f64>, poc_type: PocType, timestamp: i64) -> Option<PocLevel> {
        if volume_data.is_empty() {
            return None;
        }
        
        let mut max_volume = 0.0;
        let mut poc_price_tick = 0i64;
        
        for (&price_tick, &volume) in volume_data {
            if volume > max_volume && volume >= self.min_volume_threshold {
                max_volume = volume;
                poc_price_tick = price_tick;
            }
        }
        
        if max_volume >= self.min_volume_threshold {
            let price = poc_price_tick as f64 / 10_f64.powi(self.price_precision as i32);
            Some(PocLevel::new(price, max_volume, poc_type, timestamp))
        } else {
            None
        }
    }
    
    /// Обновить активные уровни
    fn update_active_levels(&mut self, new_poc: Option<PocLevel>, bar: &Bar) {
        if let Some(poc) = new_poc {
            let price_tick = (poc.price * 10_f64.powi(self.price_precision as i32)) as i64;
            
            // Проверяем, есть ли уже похожий уровень
            let mut found_similar = false;
            for level in &mut self.analysis.active_levels {
                let level_tick = (level.price * 10_f64.powi(self.price_precision as i32)) as i64;
                if (level_tick - price_tick).abs() <= (self.level_tolerance * 10_f64.powi(self.price_precision as i32)) as i64 {
                    level.update(poc.volume, bar.time);
                    found_similar = true;
                    break;
                }
            }
            
            // Если не нашли похожий, добавляем новый
            if !found_similar {
                self.analysis.active_levels.push(poc);
            }
        }
    }
    
    /// Очистить неактивные уровни
    fn cleanup_inactive_levels(&mut self, bar: &Bar) {
        let current_time = bar.time;
        let max_age = 86400 * 7; // 7 дней
        
        // Перемещаем старые уровни в пробитые
        let mut i = 0;
        while i < self.analysis.active_levels.len() {
            let level = &self.analysis.active_levels[i];
            
            if current_time - level.last_touch_time > max_age || level.is_broken(bar.close, self.level_tolerance) {
                let mut broken_level = level.clone();
                broken_level.is_active = false;
                self.analysis.broken_levels.push(broken_level);
                self.analysis.active_levels.remove(i);
            } else {
                i += 1;
            }
        }
        
        // Сортируем уровни по силе
        self.analysis.active_levels.sort_by(|a, b| b.strength.partial_cmp(&a.strength).unwrap_or(std::cmp::Ordering::Equal));
        
        // Ограничиваем количество уровней
        if self.analysis.active_levels.len() > self.max_levels {
            self.analysis.active_levels.truncate(self.max_levels);
        }
    }
    
    /// Обновить поддержки и сопротивления
    fn update_support_resistance(&mut self, bar: &Bar) {
        let current_price = bar.close;
        let mut nearest_support = None;
        let mut nearest_resistance = None;
        let mut support_distance = f64::INFINITY;
        let mut resistance_distance = f64::INFINITY;
        
        for level in &self.analysis.active_levels {
            if level.price < current_price {
                // Потенциальная поддержка
                let distance = current_price - level.price;
                if distance < support_distance {
                    support_distance = distance;
                    nearest_support = Some(level.price);
                }
            } else if level.price > current_price {
                // Потенциальное сопротивление
                let distance = level.price - current_price;
                if distance < resistance_distance {
                    resistance_distance = distance;
                    nearest_resistance = Some(level.price);
                }
            }
        }
        
        self.analysis.nearest_support = nearest_support;
        self.analysis.nearest_resistance = nearest_resistance;
    }
    
    /// Анализировать структуру рынка
    fn analyze_market_structure(&mut self, bar: &Bar) {
        if self.analysis.active_levels.is_empty() {
            self.analysis.market_structure = MarketStructure::Consolidation;
            return;
        }
        
        let current_price = bar.close;
        let volume = bar.volume;
        
        // Анализируем положение цены относительно POC уровней
        let mut _price_above_poc = 0;
        let mut _price_below_poc = 0;
        let mut total_volume_above = 0.0;
        let mut total_volume_below = 0.0;

        for level in &self.analysis.active_levels {
            if current_price > level.price {
                _price_above_poc += 1;
                total_volume_above += level.volume;
            } else if current_price < level.price {
                _price_below_poc += 1;
                total_volume_below += level.volume;
            }
        }
        
        // Дисбаланс объемов
        let total_volume = total_volume_above + total_volume_below;
        if total_volume > 0.0 {
            self.analysis.volume_imbalance = (total_volume_above - total_volume_below) / total_volume;
        }
        
        // Определяем структуру рынка
        self.analysis.market_structure = if self.is_near_poc_levels(current_price) {
            if volume > self.get_average_volume() * 1.5 {
                if self.analysis.volume_imbalance.abs() > 0.3 {
                    MarketStructure::Distribution
                } else {
                    MarketStructure::Accumulation
                }
            } else {
                MarketStructure::Consolidation
            }
        } else if self.is_trending() {
            MarketStructure::Trending
        } else {
            MarketStructure::Breakout
        };
        
        // Подсчитываем confluence
        self.analysis.poc_confluence = self.count_poc_confluence(current_price);
    }
    
    /// Проверить, находится ли цена рядом с POC уровнями
    fn is_near_poc_levels(&self, price: f64) -> bool {
        for level in &self.analysis.active_levels {
            if (price - level.price).abs() <= self.level_tolerance * 2.0 {
                return true;
            }
        }
        false
    }
    
    /// Проверить, находится ли рынок в тренде
    fn is_trending(&self) -> bool {
        if self.bar_history.len() < 20 {
            return false;
        }
        
        let recent_bars = &self.bar_history[self.bar_history.len().saturating_sub(20)..];
        let first_price = recent_bars[0].close;
        let last_price = recent_bars[recent_bars.len() - 1].close;
        
        let price_change_pct = ((last_price - first_price) / first_price).abs();
        price_change_pct > 0.05 // 5% изменение считаем трендом
    }
    
    /// Получить средний объем
    fn get_average_volume(&self) -> f64 {
        if self.bar_history.is_empty() {
            return 0.0;
        }
        
        let total_volume: f64 = self.bar_history.iter().map(|bar| bar.volume).sum();
        total_volume / self.bar_history.len() as f64
    }
    
    /// Подсчитать confluence POC уровней
    fn count_poc_confluence(&self, price: f64) -> u32 {
        let mut confluence = 0;
        let tolerance = self.level_tolerance * 3.0; // Расширенная толерантность для confluence
        
        for level in &self.analysis.active_levels {
            if (price - level.price).abs() <= tolerance {
                confluence += 1;
            }
        }
        
        confluence
    }
    
    /// Получить timestamp дня (начало дня в UTC)
    fn get_day_timestamp(&self, timestamp: i64) -> i64 {
        timestamp / 86400 * 86400
    }
    
    /// Получить timestamp сессии
    fn get_session_timestamp(&self, timestamp: i64) -> i64 {
        let day = self.get_day_timestamp(timestamp);
        let hour = (timestamp % 86400) / 3600;
        
        if hour >= self.session_start_hour as i64 && hour < self.session_end_hour as i64 {
            day + self.session_start_hour as i64 * 3600
        } else {
            // Предыдущая или следующая сессия
            if hour < self.session_start_hour as i64 {
                day - 86400 + self.session_start_hour as i64 * 3600
            } else {
                day + 86400 + self.session_start_hour as i64 * 3600
            }
        }
    }
    
    /// Проверить, тот же ли день
    fn is_same_day(&self, timestamp1: i64, timestamp2: i64) -> bool {
        self.get_day_timestamp(timestamp1) == self.get_day_timestamp(timestamp2)
    }
    
    /// Проверить, та же ли сессия
    fn is_same_session(&self, timestamp1: i64, timestamp2: i64) -> bool {
        self.get_session_timestamp(timestamp1) == self.get_session_timestamp(timestamp2)
    }
    
    /// Получить результаты анализа
    pub fn analysis(&self) -> &PocAnalysis {
        &self.analysis
    }
    
    /// Проверить готовность индикатора
    pub fn is_ready(&self) -> bool {
        self.is_ready
    }
    
    /// Получить активные POC уровни
    pub fn get_active_levels(&self) -> &Vec<PocLevel> {
        &self.analysis.active_levels
    }
    
    /// Получить текущий основной POC
    pub fn get_current_poc(&self) -> Option<&PocLevel> {
        self.analysis.current_poc.as_ref()
    }
    
    /// Получить структуру рынка
    pub fn get_market_structure(&self) -> MarketStructure {
        self.analysis.market_structure
    }
    
    /// Получить дисбаланс объемов
    pub fn get_volume_imbalance(&self) -> f64 {
        self.analysis.volume_imbalance
    }
    
    /// Проверить, есть ли confluence на текущем уровне
    pub fn has_confluence(&self, price: f64, min_confluence: u32) -> bool {
        self.count_poc_confluence(price) >= min_confluence
    }
    
    /// Получить силу уровня на заданной цене
    pub fn get_level_strength(&self, price: f64) -> f64 {
        let tolerance = self.level_tolerance * 2.0;
        
        for level in &self.analysis.active_levels {
            if (price - level.price).abs() <= tolerance {
                return level.strength;
            }
        }
        
        0.0
    }
    
    /// Настроить параметры сессии
    pub fn set_session_hours(&mut self, start_hour: u32, end_hour: u32) {
        self.session_start_hour = start_hour;
        self.session_end_hour = end_hour;
    }
    
    /// Настроить минимальный порог объема
    pub fn set_min_volume_threshold(&mut self, threshold: f64) {
        self.min_volume_threshold = threshold;
    }
    
    /// Сбросить состояние детектора
    pub fn reset(&mut self) {
        self.volume_profile.clear();
        self.price_levels.clear();
        self.daily_volume.clear();
        self.session_volume.clear();
        self.rolling_volume.clear();
        self.bar_history.clear();
        self.analysis = PocAnalysis::default();
        self.is_ready = false;
        self.current_day = 0;
        self.current_session = 0;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    fn create_test_bar(time: i64, open: f64, high: f64, low: f64, close: f64, volume: f64) -> Bar {
        Bar {
            time,
            open,
            high,
            low,
            close,
            volume,
        }
    }
    
    #[test]
    fn test_poc_detector_creation() {
        let detector = PocDetector::new(2, 1000.0, 20);
        assert!(!detector.is_ready());
        assert_eq!(detector.price_precision, 2);
        assert_eq!(detector.min_volume_threshold, 1000.0);
    }
    
    #[test]
    fn test_poc_level_creation() {
        let level = PocLevel::new(100.0, 5000.0, PocType::Daily, 1234567890);
        assert_eq!(level.price, 100.0);
        assert_eq!(level.volume, 5000.0);
        assert_eq!(level.poc_type, PocType::Daily);
        assert!(level.is_active);
    }
    
    #[test]
    fn test_poc_level_touch_detection() {
        let level = PocLevel::new(100.0, 5000.0, PocType::Daily, 1234567890);
        
        // Касание уровня
        assert!(level.is_touched(100.5, 99.5, 1.0));
        assert!(level.is_touched(101.0, 100.0, 1.0));
        
        // Нет касания
        assert!(!level.is_touched(102.0, 101.5, 1.0));
        assert!(!level.is_touched(98.5, 98.0, 1.0));
    }
    
    #[test]
    fn test_poc_level_breakout_detection() {
        let level = PocLevel::new(100.0, 5000.0, PocType::Daily, 1234567890);
        
        // Пробой вверх
        assert!(level.is_broken(102.0, 1.0));
        
        // Пробой вниз
        assert!(level.is_broken(98.0, 1.0));
        
        // Нет пробоя
        assert!(!level.is_broken(100.5, 1.0));
        assert!(!level.is_broken(99.5, 1.0));
    }
    
    #[test]
    fn test_poc_detector_basic_update() {
        let mut detector = PocDetector::new(2, 100.0, 5);

        // Добавляем несколько баров
        for i in 0..10 {
            let bar = create_test_bar(
                1234567890 + i * 60,
                100.0 + i as f64,
                101.0 + i as f64,
                99.0 + i as f64,
                100.5 + i as f64,
                1000.0 + i as f64 * 100.0,
            );
            detector.update(&bar);
        }

        // Базовые проверки работоспособности
        // Analysis объект должен существовать (может не иметь уровней с такими данными)
        let _ = detector.get_current_poc();
        let _ = detector.get_market_structure();
    }
    
    #[test]
    fn test_market_structure_detection() {
        let mut detector = PocDetector::new(2, 100.0, 10);
        
        // Создаем трендовые данные
        for i in 0..250 {
            let price_trend = i as f64 * 0.5; // Растущий тренд
            let bar = create_test_bar(
                1234567890 + i * 300,
                100.0 + price_trend,
                101.0 + price_trend,
                99.0 + price_trend,
                100.5 + price_trend,
                1000.0,
            );
            detector.update(&bar);
        }
        
        // После достаточного количества баров должен определить структуру
        // is_ready depends on minimum bars
        // assert!(detector.is_ready());
        
        // Структура может быть разной в зависимости от логики
        let structure = detector.get_market_structure();
        assert!(matches!(structure, 
            MarketStructure::Trending | 
            MarketStructure::Breakout | 
            MarketStructure::Consolidation
        ));
    }
}