egui-charts 0.2.0

High-performance financial charting engine for egui — candlesticks, 95 drawing tools, 130+ indicators, and a full design-token theme system
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
//! Replay/playback UI module
//!
//! Historical data replay system for stepping through market data bar-by-bar.
//! This module provides:
//!
//! - Play/pause/step controls for navigating historical data
//! - Variable playback speeds (0.1x to 100x)
//! - Progress bar with seek functionality
//! - Markers for annotating specific bars
//! - Trading simulation integration
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │                    ReplayController                         │
//! │  (Orchestrates replay state, data feeding, and events)      │
//! └─────────────────────────┬───────────────────────────────────┘
//!//!          ┌────────────────┼────────────────┐
//!          ▼                ▼                ▼
//! ┌────────────────┐ ┌─────────────┐ ┌──────────────────┐
//! │  ReplayState   │ │ ReplayConfig│ │ ReplayControls   │
//! │ (Current pos,  │ │ (Colors,    │ │ (UI widget for   │
//! │  playback,     │ │  layout,    │ │  play/pause/     │
//! │  markers)      │ │  behavior)  │ │  progress)       │
//! └────────────────┘ └─────────────┘ └──────────────────┘
//! ```
//!
//! # Example
//!
//! ```rust,ignore
//! use egui_open_trading_charts::ui::replay::{
//!     ReplayController, ReplayControls, ReplayConfig, ReplayAction
//! };
//!
//! // Create replay controller with data
//! let mut controller = ReplayController::new(bars, ts);
//!
//! // In your UI loop:
//! egui::TopBottomPanel::bottom("replay").show(ctx, |ui| {
//!     let controls = ReplayControls::new(&controller.config);
//!     let action = controls.show(ui, &mut controller.state);
//!     controller.handle_action(action);
//! });
//!
//! // Get current visible bars for chart rendering
//! let visible_bars = controller.visible_bars();
//! ```

mod actions;
mod config;
mod controls;
mod state;
mod toolbar;

pub use actions::{ReplayAction, ReplaySpeed};
pub use config::{ReplayBehavior, ReplayColors, ReplayConfig, ReplayLayout};
pub use controls::{CompactReplayControls, ReplayControls, ReplayKeyboardHandler};
pub use state::{PlaybackState, ReplayMarker, ReplayState, TradingSimulationState};
pub use toolbar::{ReplayToolbar, ReplayToolbarAction, ReplayToolbarConfig, ReplayToolbarState};

use crate::model::Bar;
use chrono::{DateTime, Utc};

/// Replay controller
///
/// Orchestrates the replay system, managing state, data feeding, and action handling.
/// This is the main entry point for integrating replay functionality into your application.
#[derive(Default)]
pub struct ReplayController {
    /// Replay state
    pub state: ReplayState,
    /// Replay configuration
    pub config: ReplayConfig,
    /// Historical bar data
    bars: Vec<Bar>,
    /// Bar ts
    ts: Vec<DateTime<Utc>>,
}

impl ReplayController {
    /// Create a new replay controller with bar data
    pub fn new(bars: Vec<Bar>, ts: Vec<DateTime<Utc>>) -> Self {
        let mut state = ReplayState::new();

        // Initialize state if we have data
        if !bars.is_empty() && !ts.is_empty() {
            let start = ts.first().copied().unwrap_or_else(Utc::now);
            let end = ts.last().copied().unwrap_or_else(Utc::now);
            state.init(bars.len(), start, end, String::new());
        }

        Self {
            state,
            config: ReplayConfig::default(),
            bars,
            ts,
        }
    }

    /// Create with configuration
    pub fn with_config(mut self, config: ReplayConfig) -> Self {
        self.config = config;
        self
    }

    /// Set the symbol name
    pub fn with_symbol(mut self, symbol: impl Into<String>) -> Self {
        self.state.symbol = Some(symbol.into());
        self
    }

    /// Load new bar data
    pub fn load_data(&mut self, bars: Vec<Bar>, ts: Vec<DateTime<Utc>>, symbol: String) {
        self.bars = bars;
        self.ts = ts;

        if !self.bars.is_empty() && !self.ts.is_empty() {
            let start = self.ts.first().copied().unwrap_or_else(Utc::now);
            let end = self.ts.last().copied().unwrap_or_else(Utc::now);
            self.state.init(self.bars.len(), start, end, symbol);
        }
    }

    /// Unload current data
    pub fn unload_data(&mut self) {
        self.bars.clear();
        self.ts.clear();
        self.state.stop();
        self.state.total_bars = 0;
    }

    /// Check if data is loaded
    pub fn has_data(&self) -> bool {
        !self.bars.is_empty()
    }

    /// Get the current bar
    pub fn curr_bar(&self) -> Option<&Bar> {
        self.bars.get(self.state.curr_bar)
    }

    /// Get the current ts
    pub fn curr_ts(&self) -> Option<&DateTime<Utc>> {
        self.ts.get(self.state.curr_bar)
    }

    /// Get visible bars up to current position
    pub fn visible_bars(&self) -> &[Bar] {
        let end = (self.state.curr_bar + 1).min(self.bars.len());
        &self.bars[..end]
    }

    /// Get visible ts up to current position
    pub fn visible_ts(&self) -> &[DateTime<Utc>] {
        let end = (self.state.curr_bar + 1).min(self.ts.len());
        &self.ts[..end]
    }

    /// Get bars in a window around current position
    pub fn bars_in_window(&self, window_size: usize) -> &[Bar] {
        let end = (self.state.curr_bar + 1).min(self.bars.len());
        let start = end.saturating_sub(window_size);
        &self.bars[start..end]
    }

    /// Handle a replay action
    pub fn handle_action(&mut self, action: ReplayAction) {
        match action {
            ReplayAction::None => {}

            // Playback controls
            ReplayAction::Play => self.state.play(),
            ReplayAction::Pause => self.state.pause(),
            ReplayAction::TogglePlayPause => self.state.toggle_play_pause(),
            ReplayAction::Stop => self.state.stop(),
            ReplayAction::Reset => self.state.reset(),

            // Navigation
            ReplayAction::StepForward => {
                self.state.pause();
                self.state.step_forward();
                self.update_curr_date();
            }
            ReplayAction::StepBackward => {
                self.state.pause();
                self.state.step_backward();
                self.update_curr_date();
            }
            ReplayAction::StepForwardN(n) => {
                self.state.pause();
                for _ in 0..n {
                    if !self.state.at_end() {
                        self.state.step_forward();
                    }
                }
                self.update_curr_date();
            }
            ReplayAction::StepBackwardN(n) => {
                self.state.pause();
                for _ in 0..n {
                    if !self.state.at_start() {
                        self.state.step_backward();
                    }
                }
                self.update_curr_date();
            }
            ReplayAction::JumpToBar(bar) => {
                self.state.jump_to_bar(bar);
                self.update_curr_date();
            }
            ReplayAction::JumpToDate(date) => {
                // Find the nearest bar to the given date
                if let Some(idx) = self.find_bar_at_date(&date) {
                    self.state.jump_to_bar(idx);
                    self.update_curr_date();
                }
            }
            ReplayAction::JumpToStart => {
                self.state.jump_to_bar(0);
                self.update_curr_date();
            }
            ReplayAction::JumpToEnd => {
                self.state.jump_to_bar(self.bars.len().saturating_sub(1));
                self.update_curr_date();
            }
            ReplayAction::JumpToPercent(pct) => {
                self.state.jump_to_percent(pct);
                self.update_curr_date();
            }

            // Speed control
            ReplayAction::SetSpeed(speed) => self.state.set_speed(ReplaySpeed::from(speed)),
            ReplayAction::SpeedUp => self.state.speed_up(),
            ReplayAction::SlowDown => self.state.slow_down(),

            // Mode control
            ReplayAction::EnterReplayMode => self.state.enter_replay_mode(),
            ReplayAction::ExitReplayMode => self.state.exit_replay_mode(),
            ReplayAction::ToggleReplayMode => {
                if self.state.is_active() {
                    self.state.exit_replay_mode();
                } else {
                    self.state.enter_replay_mode();
                }
            }

            // Data loading (handled externally, this just updates state)
            ReplayAction::LoadData { symbol, start, end } => {
                // Data loading would be handled by the application
                // This action signals intent to load data
                if let (Some(s), Some(e)) = (start, end) {
                    self.state.start_date = Some(s);
                    self.state.end_date = Some(e);
                }
                self.state.symbol = Some(symbol);
            }
            ReplayAction::UnloadData => {
                self.unload_data();
            }

            // Range selection
            ReplayAction::SetStartDate(date) => {
                self.state.start_date = Some(date);
            }
            ReplayAction::SetEndDate(date) => {
                self.state.end_date = Some(date);
            }
            ReplayAction::SetDateRange { start, end } => {
                self.state.start_date = Some(start);
                self.state.end_date = Some(end);
            }

            // Markers
            ReplayAction::AddMarker { label, color } => {
                self.state.add_marker(label, color);
            }
            ReplayAction::RemoveMarker(bar_idx) => {
                self.state.remove_marker(bar_idx);
            }
            ReplayAction::ClearMarkers => {
                self.state.clear_markers();
            }

            // Trading simulation
            ReplayAction::EnableTradingSimulation => {
                self.state.trading_sim.enabled = true;
                self.state.show_trading_panel = true;
            }
            ReplayAction::DisableTradingSimulation => {
                self.state.trading_sim.enabled = false;
            }
            ReplayAction::ResetTradingSimulation => {
                self.state
                    .trading_sim
                    .reset(self.config.behavior.initial_capital);
            }
        }
    }

    /// Update state each frame (call in your render loop)
    pub fn update(&mut self) -> usize {
        let bars_advanced = self.state.update();
        if bars_advanced > 0 {
            self.update_curr_date();
        }
        bars_advanced
    }

    /// Find the bar index closest to the given date
    fn find_bar_at_date(&self, target: &DateTime<Utc>) -> Option<usize> {
        if self.ts.is_empty() {
            return None;
        }

        // Binary search for the closest ts
        let result = self.ts.binary_search(target);
        match result {
            Ok(idx) => Some(idx),
            Err(idx) => {
                if idx == 0 {
                    Some(0)
                } else if idx >= self.ts.len() {
                    Some(self.ts.len() - 1)
                } else {
                    // Compare distances to adjacent ts
                    let before_diff = *target - self.ts[idx - 1];
                    let after_diff = self.ts[idx] - *target;
                    if before_diff < after_diff {
                        Some(idx - 1)
                    } else {
                        Some(idx)
                    }
                }
            }
        }
    }

    /// Update current date from bar ts
    fn update_curr_date(&mut self) {
        self.state.update_curr_date(&self.ts);
    }
}

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

    fn create_test_bars(count: usize) -> (Vec<Bar>, Vec<DateTime<Utc>>) {
        let mut bars = Vec::with_capacity(count);
        let mut timestamps = Vec::with_capacity(count);

        for i in 0..count {
            // Use different days to avoid hour overflow (hours 0-23 only)
            let day = (i / 24) as u32 + 1;
            let hour = (i % 24) as u32;
            let ts = Utc
                .with_ymd_and_hms(2024, 1, day.min(28), hour, 0, 0)
                .unwrap();
            bars.push(Bar {
                time: ts,
                open: 100.0 + i as f64,
                high: 101.0 + i as f64,
                low: 99.0 + i as f64,
                close: 100.5 + i as f64,
                volume: 1000.0,
            });
            timestamps.push(ts);
        }

        (bars, timestamps)
    }

    #[test]
    fn test_controller_creation() {
        let (bars, ts) = create_test_bars(100);
        let controller = ReplayController::new(bars, ts);

        assert!(controller.has_data());
        assert_eq!(controller.state.total_bars, 100);
    }

    #[test]
    fn test_navigation() {
        let (bars, ts) = create_test_bars(100);
        let mut controller = ReplayController::new(bars, ts);

        controller.handle_action(ReplayAction::EnterReplayMode);
        assert!(controller.state.is_active());

        controller.handle_action(ReplayAction::JumpToBar(50));
        assert_eq!(controller.state.curr_bar, 50);

        controller.handle_action(ReplayAction::StepForward);
        assert_eq!(controller.state.curr_bar, 51);

        controller.handle_action(ReplayAction::StepBackward);
        assert_eq!(controller.state.curr_bar, 50);
    }

    #[test]
    fn test_visible_bars() {
        let (bars, ts) = create_test_bars(100);
        let mut controller = ReplayController::new(bars, ts);

        controller.state.curr_bar = 25;
        let visible = controller.visible_bars();
        assert_eq!(visible.len(), 26); // 0 through 25 inclusive
    }

    #[test]
    fn test_find_bar_at_date() {
        let (bars, ts) = create_test_bars(24);
        let controller = ReplayController::new(bars, ts);

        let target = Utc.with_ymd_and_hms(2024, 1, 1, 12, 0, 0).unwrap();
        let idx = controller.find_bar_at_date(&target);
        assert_eq!(idx, Some(12));
    }
}