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
//! Long/short position tool rendering

use crate::drawings::domain::Drawing;
use crate::styles::{stroke, typography};
use crate::tokens::DESIGN_TOKENS;
use egui::{Color32, Pos2, Rect, Stroke};

impl Drawing {
    pub(crate) fn render_pos_tool(&self, painter: &egui::Painter, is_long: bool) {
        if self.points.len() < 2 {
            if !self.points.is_empty() {
                painter.circle_filled(
                    self.points[0],
                    DESIGN_TOKENS.rounding.md,
                    Color32::from_rgba_unmultiplied(
                        self.color[0],
                        self.color[1],
                        self.color[2],
                        self.color[3],
                    ),
                );
            }
            return;
        }

        let entry = self.points[0];
        let target = self.points[1];

        // Position colors
        let profit_color = DESIGN_TOKENS.semantic.extended.bullish;
        let loss_color = DESIGN_TOKENS.semantic.extended.bearish;
        let entry_color = DESIGN_TOKENS.semantic.extended.chart_text_secondary;

        // Get actual price values from chart_points
        let (entry_price, target_price) = if self.chart_points.len() >= 2 {
            (self.chart_points[0].price, self.chart_points[1].price)
        } else {
            (0.0, 0.0)
        };

        // Calculate target and stop prices based on position direction
        let (target_price_actual, stop_price, target_y, stop_y) = if is_long {
            // Long: target above entry, stop below entry
            let tp = entry_price.max(target_price);
            let sl = entry_price - (tp - entry_price); // Symmetric stop
            let ty = entry.y.min(target.y);
            let sy = entry.y + (entry.y - ty); // Symmetric stop y
            (tp, sl, ty, sy)
        } else {
            // Short: target below entry, stop above entry
            let tp = entry_price.min(target_price);
            let sl = entry_price + (entry_price - tp); // Symmetric stop
            let ty = entry.y.max(target.y);
            let sy = entry.y - (ty - entry.y); // Symmetric stop y
            (tp, sl, ty, sy)
        };

        // Calculate P&L percentages
        let profit_pct = if entry_price != 0.0 {
            ((target_price_actual - entry_price) / entry_price * 100.0).abs()
        } else {
            0.0
        };
        let loss_pct = if entry_price != 0.0 {
            ((stop_price - entry_price) / entry_price * 100.0).abs()
        } else {
            0.0
        };
        let risk_reward = if loss_pct != 0.0 {
            profit_pct / loss_pct
        } else {
            0.0
        };

        // Calculate real-time P&L
        let qty = self.quantity.unwrap_or(100.0);
        let curr_price = self.curr_price.unwrap_or(entry_price);

        let pnl_per_unit = if is_long {
            curr_price - entry_price
        } else {
            entry_price - curr_price
        };
        let total_pnl = pnl_per_unit * qty as f64;

        // Calculate width - extend across chart
        let width = (target.x - entry.x)
            .abs()
            .max(DESIGN_TOKENS.sizing.position_tool.min_width);
        let right = entry.x + width;

        // Draw filled zones
        self.draw_profit_zone(painter, is_long, entry, target_y, right);
        self.draw_loss_zone(painter, is_long, entry, stop_y, right);

        // Draw horizontal lines (thicker)
        self.draw_position_lines(
            painter,
            entry,
            target_y,
            stop_y,
            right,
            entry_color,
            profit_color,
            loss_color,
        );

        // Draw handle circles at endpoints
        self.draw_position_handles(
            painter,
            entry,
            target_y,
            stop_y,
            right,
            entry_color,
            profit_color,
            loss_color,
        );

        // Draw labels
        self.draw_position_labels(
            painter,
            entry,
            target_y,
            stop_y,
            right,
            entry_price,
            target_price_actual,
            stop_price,
            profit_pct,
            loss_pct,
            risk_reward,
            total_pnl,
            qty as f64,
            profit_color,
            loss_color,
        );
    }

    fn draw_profit_zone(
        &self,
        painter: &egui::Painter,
        is_long: bool,
        entry: Pos2,
        target_y: f32,
        right: f32,
    ) {
        let profit_rect = if is_long {
            Rect::from_min_max(Pos2::new(entry.x, target_y), Pos2::new(right, entry.y))
        } else {
            Rect::from_min_max(Pos2::new(entry.x, entry.y), Pos2::new(right, target_y))
        };
        // Visibility check: skip rendering if rect is outside clip region
        if !painter.clip_rect().intersects(profit_rect) {
            return;
        }
        painter.rect_filled(
            profit_rect,
            0.0,
            DESIGN_TOKENS
                .semantic
                .extended
                .bullish
                .gamma_multiply(30_f32 / 255.0),
        );
    }

    fn draw_loss_zone(
        &self,
        painter: &egui::Painter,
        is_long: bool,
        entry: Pos2,
        stop_y: f32,
        right: f32,
    ) {
        let loss_rect = if is_long {
            Rect::from_min_max(Pos2::new(entry.x, entry.y), Pos2::new(right, stop_y))
        } else {
            Rect::from_min_max(Pos2::new(entry.x, stop_y), Pos2::new(right, entry.y))
        };
        // Visibility check: skip rendering if rect is outside clip region
        if !painter.clip_rect().intersects(loss_rect) {
            return;
        }
        painter.rect_filled(
            loss_rect,
            0.0,
            DESIGN_TOKENS
                .semantic
                .extended
                .bearish
                .gamma_multiply(30_f32 / 255.0),
        );
    }

    #[allow(clippy::too_many_arguments)]
    fn draw_position_lines(
        &self,
        painter: &egui::Painter,
        entry: Pos2,
        target_y: f32,
        stop_y: f32,
        right: f32,
        entry_color: Color32,
        profit_color: Color32,
        loss_color: Color32,
    ) {
        // Visibility check: compute bounding rect for all lines
        let min_y = target_y.min(stop_y).min(entry.y);
        let max_y = target_y.max(stop_y).max(entry.y);
        let lines_rect = Rect::from_min_max(Pos2::new(entry.x, min_y), Pos2::new(right, max_y));
        if !painter.clip_rect().intersects(lines_rect) {
            return;
        }

        // Entry line (gray, thicker)
        painter.hline(
            entry.x..=right,
            entry.y,
            Stroke::new(stroke::THICK, entry_color),
        );

        // Target line (green, thicker)
        painter.hline(
            entry.x..=right,
            target_y,
            Stroke::new(stroke::THICK, profit_color),
        );

        // Stop line (red, thicker)
        painter.hline(
            entry.x..=right,
            stop_y,
            Stroke::new(stroke::THICK, loss_color),
        );
    }

    #[allow(clippy::too_many_arguments)]
    fn draw_position_handles(
        &self,
        painter: &egui::Painter,
        entry: Pos2,
        target_y: f32,
        stop_y: f32,
        right: f32,
        entry_color: Color32,
        profit_color: Color32,
        loss_color: Color32,
    ) {
        let handle_radius = DESIGN_TOKENS.rounding.lg;

        // Visibility check: compute bounding rect for all handles (including radius)
        let min_y = target_y.min(stop_y).min(entry.y) - handle_radius;
        let max_y = target_y.max(stop_y).max(entry.y) + handle_radius;
        let handles_rect = Rect::from_min_max(
            Pos2::new(entry.x - handle_radius, min_y),
            Pos2::new(right + handle_radius, max_y),
        );
        if !painter.clip_rect().intersects(handles_rect) {
            return;
        }

        // Entry handles
        painter.circle_filled(Pos2::new(entry.x, entry.y), handle_radius, entry_color);
        painter.circle_filled(Pos2::new(right, entry.y), handle_radius, entry_color);

        // Target handles
        painter.circle_filled(Pos2::new(entry.x, target_y), handle_radius, profit_color);
        painter.circle_filled(Pos2::new(right, target_y), handle_radius, profit_color);

        // Stop handles
        painter.circle_filled(Pos2::new(entry.x, stop_y), handle_radius, loss_color);
        painter.circle_filled(Pos2::new(right, stop_y), handle_radius, loss_color);
    }

    #[allow(clippy::too_many_arguments)]
    fn draw_position_labels(
        &self,
        painter: &egui::Painter,
        entry: Pos2,
        target_y: f32,
        stop_y: f32,
        right: f32,
        entry_price: f64,
        target_price_actual: f64,
        stop_price: f64,
        profit_pct: f64,
        loss_pct: f64,
        risk_reward: f64,
        total_pnl: f64,
        qty: f64,
        profit_color: Color32,
        loss_color: Color32,
    ) {
        let font_small = egui::FontId::proportional(typography::SM);
        let font_large = egui::FontId::proportional(typography::MD);

        // Top info box (above profit zone) - background with rounded corners
        let info_y =
            target_y - DESIGN_TOKENS.sizing.position_tool.label_offset_y - DESIGN_TOKENS.spacing.xl;
        let info_rect = Rect::from_min_max(
            Pos2::new(
                entry.x + DESIGN_TOKENS.sizing.position_tool.label_offset_x,
                info_y,
            ),
            Pos2::new(
                entry.x + DESIGN_TOKENS.sizing.position_tool.label_info_width,
                info_y + DESIGN_TOKENS.sizing.position_tool.label_info_height,
            ),
        );

        // Visibility check for info box
        if painter.clip_rect().intersects(info_rect) {
            painter.rect_filled(
                info_rect,
                DESIGN_TOKENS.rounding.md,
                DESIGN_TOKENS
                    .semantic
                    .extended
                    .chart_axis_bg
                    .gamma_multiply(0.9),
            );

            // P&L and Risk/Reward in info box
            let pnl_text = format!(
                "P&L: {:.2} ({:.2}%)  •  Risk/Reward: {:.2}  •  Qty: {}",
                total_pnl,
                (total_pnl / (entry_price * qty) * 100.0),
                risk_reward,
                qty as i32
            );
            painter.text(
                Pos2::new(
                    entry.x + DESIGN_TOKENS.sizing.position_tool.text_offset_x,
                    info_y + DESIGN_TOKENS.spacing.xl,
                ),
                egui::Align2::LEFT_CENTER,
                pnl_text,
                font_large,
                if total_pnl >= 0.0 {
                    profit_color
                } else {
                    loss_color
                },
            );
        }

        // Target label (on right side of line)
        let target_label = format!(
            "Target: {:.2} ({:.2}%) {}",
            target_price_actual, profit_pct, qty as i32
        );
        let target_bg = Rect::from_min_max(
            Pos2::new(
                right + DESIGN_TOKENS.sizing.position_tool.label_offset_x,
                target_y - DESIGN_TOKENS.sizing.position_tool.label_offset_x,
            ),
            Pos2::new(
                right
                    + DESIGN_TOKENS.sizing.position_tool.label_offset_x
                    + DESIGN_TOKENS.sizing.position_tool.label_target_width,
                target_y + DESIGN_TOKENS.sizing.position_tool.label_offset_x,
            ),
        );

        // Visibility check for target label
        if painter.clip_rect().intersects(target_bg) {
            painter.rect_filled(
                target_bg,
                DESIGN_TOKENS.rounding.sm,
                DESIGN_TOKENS
                    .semantic
                    .extended
                    .bullish
                    .gamma_multiply(200_f32 / 255.0),
            );
            painter.text(
                Pos2::new(
                    right + DESIGN_TOKENS.sizing.position_tool.text_offset_x,
                    target_y,
                ),
                egui::Align2::LEFT_CENTER,
                target_label,
                font_small.clone(),
                Color32::WHITE,
            );
        }

        // Stop label (on right side of line)
        let stop_label = format!("Stop: {:.2} ({:.2}%) {}", stop_price, loss_pct, qty as i32);
        let stop_bg = Rect::from_min_max(
            Pos2::new(
                right + DESIGN_TOKENS.sizing.position_tool.label_offset_x,
                stop_y - DESIGN_TOKENS.sizing.position_tool.label_offset_x,
            ),
            Pos2::new(
                right
                    + DESIGN_TOKENS.sizing.position_tool.label_offset_x
                    + DESIGN_TOKENS.sizing.position_tool.label_target_width,
                stop_y + DESIGN_TOKENS.sizing.position_tool.label_offset_x,
            ),
        );

        // Visibility check for stop label
        if painter.clip_rect().intersects(stop_bg) {
            painter.rect_filled(
                stop_bg,
                DESIGN_TOKENS.rounding.sm,
                DESIGN_TOKENS
                    .semantic
                    .extended
                    .bearish
                    .gamma_multiply(200_f32 / 255.0),
            );
            painter.text(
                Pos2::new(
                    right + DESIGN_TOKENS.sizing.position_tool.text_offset_x,
                    stop_y,
                ),
                egui::Align2::LEFT_CENTER,
                stop_label,
                font_small,
                Color32::WHITE,
            );
        }
    }
}