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
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
//! Replay state
//!
//! Mutable state for replay mode UI and playback control.

use super::actions::ReplaySpeed;
use chrono::{DateTime, Utc};
use web_time::Instant;

/// Replay marker for annotating specific bars
#[derive(Debug, Clone)]
pub struct ReplayMarker {
    /// Bar index where marker is placed
    pub bar_idx: usize,
    /// Optional label text
    pub label: Option<String>,
    /// Marker color (RGBA)
    pub color: [u8; 4],
    /// Ts when marker was created
    pub created_at: DateTime<Utc>,
}

impl ReplayMarker {
    /// Create a new marker at the given bar index
    pub fn new(bar_idx: usize) -> Self {
        Self {
            bar_idx,
            label: None,
            color: [255, 165, 0, 255], // Orange default
            created_at: Utc::now(),
        }
    }

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

    /// Set the color
    pub fn with_color(mut self, color: [u8; 4]) -> Self {
        self.color = color;
        self
    }
}

/// Playback state for the replay
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PlaybackState {
    /// Not in replay mode
    #[default]
    Inactive,
    /// In replay mode but paused
    Paused,
    /// Currently playing
    Playing,
    /// Reached end of data
    Finished,
}

impl PlaybackState {
    /// Check if replay mode is active
    pub fn is_active(&self) -> bool {
        !matches!(self, PlaybackState::Inactive)
    }

    /// Check if currently playing
    pub fn is_playing(&self) -> bool {
        matches!(self, PlaybackState::Playing)
    }

    /// Check if paused (active but not playing)
    pub fn is_paused(&self) -> bool {
        matches!(self, PlaybackState::Paused)
    }
}

/// Trading simulation state during replay
#[derive(Debug, Clone, Default)]
pub struct TradingSimulationState {
    /// Whether trading simulation is enabled
    pub enabled: bool,
    /// Simulated cash balance
    pub cash: f64,
    /// Simulated position quantity
    pub pos_qty: f64,
    /// Simulated entry price
    pub entry_price: Option<f64>,
    /// Total simulated P&L
    pub total_pnl: f64,
    /// Number of trades executed
    pub trade_cnt: u32,
    /// Win rate percentage
    pub win_rate: f64,
}

impl TradingSimulationState {
    /// Create with initial capital
    pub fn new(initial_capital: f64) -> Self {
        Self {
            enabled: false,
            cash: initial_capital,
            pos_qty: 0.0,
            entry_price: None,
            total_pnl: 0.0,
            trade_cnt: 0,
            win_rate: 0.0,
        }
    }

    /// Reset to initial state
    pub fn reset(&mut self, initial_capital: f64) {
        self.cash = initial_capital;
        self.pos_qty = 0.0;
        self.entry_price = None;
        self.total_pnl = 0.0;
        self.trade_cnt = 0;
        self.win_rate = 0.0;
    }

    /// Calculate current equity given current price
    pub fn equity(&self, curr_price: f64) -> f64 {
        self.cash + (self.pos_qty * curr_price)
    }

    /// Calculate unrealized P&L
    pub fn unrealized_pnl(&self, curr_price: f64) -> f64 {
        if let Some(entry) = self.entry_price {
            (curr_price - entry) * self.pos_qty
        } else {
            0.0
        }
    }
}

/// Main replay state
#[derive(Debug, Clone)]
pub struct ReplayState {
    // Playback state
    /// Current playback state
    pub playback_state: PlaybackState,
    /// Current playback speed
    pub speed: ReplaySpeed,

    // Pos tracking
    /// Current bar index (0-based)
    pub curr_bar: usize,
    /// Total number of bars available
    pub total_bars: usize,
    /// Visible bar count (how many bars to show on chart)
    pub visible_bars: usize,

    // Time tracking
    /// Start date of replay data
    pub start_date: Option<DateTime<Utc>>,
    /// End date of replay data
    pub end_date: Option<DateTime<Utc>>,
    /// Current date/time at playback position
    pub curr_date: Option<DateTime<Utc>>,

    // Symbol info
    /// Symbol being replayed
    pub symbol: Option<String>,

    // UI state
    /// Whether speed dropdown is open
    pub speed_dropdown_open: bool,
    /// Whether date picker is open
    pub date_picker_open: bool,
    /// Whether settings panel is open
    pub settings_open: bool,
    /// Whether to show trading simulation panel
    pub show_trading_panel: bool,

    // Markers
    /// User-placed markers
    pub markers: Vec<ReplayMarker>,

    // Trading simulation
    /// Trading simulation state
    pub trading_sim: TradingSimulationState,

    // Internal timing
    /// Last update instant (for frame timing)
    last_update: Option<Instant>,
    /// Accumulated time since last bar advance
    accumulated_time: f64,
    /// Target time per bar in seconds (based on timeframe)
    bar_duration_secs: f64,
}

impl ReplayState {
    /// Create new replay state
    pub fn new() -> Self {
        Self {
            playback_state: PlaybackState::Inactive,
            speed: ReplaySpeed::Normal,
            curr_bar: 0,
            total_bars: 0,
            visible_bars: 100,
            start_date: None,
            end_date: None,
            curr_date: None,
            symbol: None,
            speed_dropdown_open: false,
            date_picker_open: false,
            settings_open: false,
            show_trading_panel: false,
            markers: Vec::new(),
            trading_sim: TradingSimulationState::default(),
            last_update: None,
            accumulated_time: 0.0,
            bar_duration_secs: 1.0, // Default 1 second per bar at 1x
        }
    }

    /// Initialize replay with data params
    pub fn init(
        &mut self,
        total_bars: usize,
        start_date: DateTime<Utc>,
        end_date: DateTime<Utc>,
        symbol: String,
    ) {
        self.total_bars = total_bars;
        self.start_date = Some(start_date);
        self.end_date = Some(end_date);
        self.curr_date = Some(start_date);
        self.symbol = Some(symbol);
        self.curr_bar = 0;
        self.playback_state = PlaybackState::Paused;
        self.markers.clear();
    }

    /// Set the bar duration (time between bars at 1x speed)
    pub fn set_bar_duration(&mut self, seconds: f64) {
        self.bar_duration_secs = seconds.max(0.001);
    }

    /// Check if replay mode is active
    pub fn is_active(&self) -> bool {
        self.playback_state.is_active()
    }

    /// Check if currently playing
    pub fn is_playing(&self) -> bool {
        self.playback_state.is_playing()
    }

    /// Get progress as percentage (0.0 - 1.0)
    pub fn progress(&self) -> f32 {
        if self.total_bars == 0 {
            0.0
        } else {
            self.curr_bar as f32 / (self.total_bars - 1).max(1) as f32
        }
    }

    /// Get remaining bars
    pub fn remaining_bars(&self) -> usize {
        self.total_bars.saturating_sub(self.curr_bar + 1)
    }

    /// Check if at the end
    pub fn at_end(&self) -> bool {
        self.curr_bar >= self.total_bars.saturating_sub(1)
    }

    /// Check if at the beginning
    pub fn at_start(&self) -> bool {
        self.curr_bar == 0
    }

    // Playback control methods

    /// Start playing
    pub fn play(&mut self) {
        if self.total_bars > 0 && !self.at_end() {
            self.playback_state = PlaybackState::Playing;
            self.last_update = Some(Instant::now());
            self.accumulated_time = 0.0;
        }
    }

    /// Pause playback
    pub fn pause(&mut self) {
        if self.playback_state.is_playing() {
            self.playback_state = PlaybackState::Paused;
            self.last_update = None;
        }
    }

    /// Toggle play/pause
    pub fn toggle_play_pause(&mut self) {
        match self.playback_state {
            PlaybackState::Playing => self.pause(),
            PlaybackState::Paused => self.play(),
            PlaybackState::Finished => {
                // Restart from beginning
                self.curr_bar = 0;
                self.play();
            }
            PlaybackState::Inactive => {}
        }
    }

    /// Stop and exit replay mode
    pub fn stop(&mut self) {
        self.playback_state = PlaybackState::Inactive;
        self.curr_bar = 0;
        self.last_update = None;
        self.accumulated_time = 0.0;
    }

    /// Reset to beginning without exiting replay mode
    pub fn reset(&mut self) {
        self.pause();
        self.curr_bar = 0;
        self.curr_date = self.start_date;
        self.accumulated_time = 0.0;
    }

    /// Enter replay mode
    pub fn enter_replay_mode(&mut self) {
        if self.total_bars > 0 {
            self.playback_state = PlaybackState::Paused;
            self.curr_bar = 0;
            self.curr_date = self.start_date;
        }
    }

    /// Exit replay mode
    pub fn exit_replay_mode(&mut self) {
        self.stop();
    }

    // Navigation methods

    /// Step forward one bar
    pub fn step_forward(&mut self) {
        if self.curr_bar < self.total_bars.saturating_sub(1) {
            self.curr_bar += 1;
        } else {
            self.playback_state = PlaybackState::Finished;
        }
    }

    /// Step backward one bar
    pub fn step_backward(&mut self) {
        if self.curr_bar > 0 {
            self.curr_bar -= 1;
            // If we were finished, go back to paused
            if self.playback_state == PlaybackState::Finished {
                self.playback_state = PlaybackState::Paused;
            }
        }
    }

    /// Jump to specific bar
    pub fn jump_to_bar(&mut self, bar: usize) {
        self.curr_bar = bar.min(self.total_bars.saturating_sub(1));
        if self.at_end() && self.playback_state == PlaybackState::Playing {
            self.playback_state = PlaybackState::Finished;
        }
    }

    /// Jump to percentage position
    pub fn jump_to_percent(&mut self, percent: f32) {
        let bar = ((self.total_bars.saturating_sub(1)) as f32 * percent.clamp(0.0, 1.0)) as usize;
        self.jump_to_bar(bar);
    }

    // Speed control

    /// Set playback speed
    pub fn set_speed(&mut self, speed: ReplaySpeed) {
        self.speed = speed;
    }

    /// Increase speed
    pub fn speed_up(&mut self) {
        self.speed = self.speed.faster();
    }

    /// Decrease speed
    pub fn slow_down(&mut self) {
        self.speed = self.speed.slower();
    }

    // Marker management

    /// Add a marker at current position
    pub fn add_marker(&mut self, label: Option<String>, color: Option<[u8; 4]>) {
        let mut marker = ReplayMarker::new(self.curr_bar);
        if let Some(l) = label {
            marker = marker.with_label(l);
        }
        if let Some(c) = color {
            marker = marker.with_color(c);
        }
        self.markers.push(marker);
    }

    /// Remove marker at bar index
    pub fn remove_marker(&mut self, bar_idx: usize) {
        self.markers.retain(|m| m.bar_idx != bar_idx);
    }

    /// Clear all markers
    pub fn clear_markers(&mut self) {
        self.markers.clear();
    }

    /// Get markers in visible range
    pub fn visible_markers(&self, start_bar: usize, end_bar: usize) -> Vec<&ReplayMarker> {
        self.markers
            .iter()
            .filter(|m| m.bar_idx >= start_bar && m.bar_idx <= end_bar)
            .collect()
    }

    // Update method for frame-based playback

    /// Update playback state. Call this each frame.
    /// Returns the number of bars to advance (usually 0 or 1).
    pub fn update(&mut self) -> usize {
        if !self.playback_state.is_playing() {
            return 0;
        }

        let now = Instant::now();
        let delta = if let Some(last) = self.last_update {
            now.duration_since(last).as_secs_f64()
        } else {
            0.0
        };
        self.last_update = Some(now);

        // Accumulate time adjusted by speed
        self.accumulated_time += delta * self.speed.multiplier() as f64;

        // Calculate how many bars to advance
        let bars_to_advance = (self.accumulated_time / self.bar_duration_secs) as usize;

        if bars_to_advance > 0 {
            self.accumulated_time -= bars_to_advance as f64 * self.bar_duration_secs;

            // Advance bars
            let new_bar = self.curr_bar + bars_to_advance;
            if new_bar >= self.total_bars.saturating_sub(1) {
                self.curr_bar = self.total_bars.saturating_sub(1);
                self.playback_state = PlaybackState::Finished;
                return self.total_bars.saturating_sub(1) - (self.curr_bar - bars_to_advance);
            } else {
                self.curr_bar = new_bar;
                return bars_to_advance;
            }
        }

        0
    }

    /// Update current date based on bar index
    pub fn update_curr_date(&mut self, bar_ts: &[DateTime<Utc>]) {
        if let Some(ts) = bar_ts.get(self.curr_bar) {
            self.curr_date = Some(*ts);
        }
    }

    /// Sync replay state from external values.
    ///
    /// `active` / `playing` drive the playback state machine,
    /// `current_bar` / `total_bars` sync position, and `speed` sets playback rate.
    pub fn sync_from(
        &mut self,
        active: bool,
        playing: bool,
        current_bar: usize,
        total_bars: usize,
        speed: ReplaySpeed,
    ) {
        // Update playback state based on central state
        if active {
            if playing {
                if self.playback_state != PlaybackState::Playing {
                    self.playback_state = PlaybackState::Playing;
                    self.last_update = Some(Instant::now());
                }
            } else if self.playback_state == PlaybackState::Playing {
                self.playback_state = PlaybackState::Paused;
            }
        } else if self.playback_state != PlaybackState::Inactive {
            self.playback_state = PlaybackState::Inactive;
        }

        // Sync position
        self.curr_bar = current_bar;
        self.total_bars = total_bars;

        // Sync speed
        self.speed = speed;
    }
}

impl Default for ReplayState {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_replay_state_creation() {
        let state = ReplayState::new();
        assert!(!state.is_active());
        assert!(!state.is_playing());
        assert_eq!(state.curr_bar, 0);
    }

    #[test]
    fn test_replay_progress() {
        let mut state = ReplayState::new();
        state.total_bars = 100;
        state.curr_bar = 50;
        assert!((state.progress() - 0.505).abs() < 0.01);
    }

    #[test]
    fn test_navigation() {
        let mut state = ReplayState::new();
        state.total_bars = 10;
        state.playback_state = PlaybackState::Paused;

        state.step_forward();
        assert_eq!(state.curr_bar, 1);

        state.jump_to_bar(5);
        assert_eq!(state.curr_bar, 5);

        state.step_backward();
        assert_eq!(state.curr_bar, 4);

        state.jump_to_percent(1.0);
        assert_eq!(state.curr_bar, 9);
    }

    #[test]
    fn test_markers() {
        let mut state = ReplayState::new();
        state.curr_bar = 5;

        state.add_marker(Some("Test".to_string()), None);
        assert_eq!(state.markers.len(), 1);
        assert_eq!(state.markers[0].bar_idx, 5);

        state.remove_marker(5);
        assert!(state.markers.is_empty());
    }
}