pine-interpreter 0.1.0

Interpreter and runtime for Pine Script.
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
// Output-related types, traits, and implementations

use std::collections::HashMap;

/// Represents a color with RGBA components
#[derive(Clone, Debug, PartialEq)]
pub struct Color {
    pub r: u8, // Red component (0-255)
    pub g: u8, // Green component (0-255)
    pub b: u8, // Blue component (0-255)
    pub t: u8, // Transparency (0-100)
}

impl Color {
    pub fn new(r: u8, g: u8, b: u8, t: u8) -> Self {
        Color { r, g, b, t }
    }
}

/// Represents a label drawable object
#[derive(Clone, Debug)]
pub struct Label {
    pub x: f64,
    pub y: f64,
    pub text: String,
    pub xloc: String,
    pub yloc: String,
    pub color: Option<Color>,
    pub style: String,
    pub textcolor: Option<Color>,
    pub size: String,
    pub textalign: String,
    pub tooltip: Option<String>,
    pub text_font_family: String,
}

/// Represents a box drawable object
/// A `fill(...)` between two plots or hlines.
#[derive(Clone, Debug)]
pub struct FillObject {
    /// Id of the first plot/hline (`None` when it was `na`).
    pub id1: Option<usize>,
    /// Id of the second plot/hline.
    pub id2: Option<usize>,
    pub color: Option<Color>,
    pub title: String,
}

/// Chart-wide settings written by global functions like `bgcolor`/`barcolor`.
#[derive(Clone, Debug, Default)]
pub struct GlobalContext {
    /// Background color (`bgcolor`).
    pub bgcolor: Option<Color>,
    /// Price-bar color (`barcolor`).
    pub barcolor: Option<Color>,
}

/// An `alertcondition(...)` declaration — a named alert with a message.
#[derive(Clone, Debug, Default)]
pub struct AlertCondition {
    pub title: String,
    pub message: String,
}

/// The `indicator(...)` declaration — a script's identity and display settings.
#[derive(Clone, Debug, Default)]
pub struct Indicator {
    pub title: String,
    pub shorttitle: String,
    pub overlay: bool,
    pub format: String,
    pub precision: Option<i64>,
    pub timeframe: String,
}

/// A trend line drawn between two points, `(x1, y1)`–`(x2, y2)`.
#[derive(Clone, Debug)]
pub struct LineObject {
    pub x1: f64,
    pub y1: f64,
    pub x2: f64,
    pub y2: f64,
    pub xloc: String,
    pub extend: String,
    pub color: Option<Color>,
    pub style: String,
    pub width: f64,
}

/// One cell of a [`Table`].
#[derive(Clone, Debug, Default)]
pub struct TableCell {
    pub text: String,
    pub text_color: Option<Color>,
    pub bgcolor: Option<Color>,
    pub text_size: String,
    pub text_halign: String,
    pub text_valign: String,
}

/// A table overlay: a fixed grid of cells anchored to a chart position.
#[derive(Clone, Debug)]
pub struct Table {
    pub position: String,
    pub columns: usize,
    pub rows: usize,
    pub bgcolor: Option<Color>,
    pub cells: HashMap<(usize, usize), TableCell>,
}

#[derive(Clone, Debug)]
pub struct PineBox {
    pub left: f64,
    pub top: f64,
    pub right: f64,
    pub bottom: f64,
    pub border_color: Option<Color>,
    pub border_width: f64,
    pub border_style: String,
    pub extend: String,
    pub xloc: String,
    pub bgcolor: Option<Color>,
    pub text: String,
    pub text_size: f64,
    pub text_color: Option<Color>,
    pub text_halign: String,
    pub text_valign: String,
    pub text_wrap: String,
    pub text_font_family: String,
}

/// Represents a plot output
#[derive(Clone, Debug, Default)]
pub struct Plot {
    pub series: f64,
    pub title: String,
    pub color: Option<Color>,
    pub linewidth: f64,
    pub style: String,
    pub trackprice: bool,
    pub histbase: f64,
    pub offset: f64,
    pub join: bool,
    pub editable: bool,
    pub show_last: Option<f64>,
    pub display: String,
    pub format: Option<String>,
    pub precision: Option<f64>,
    pub force_overlay: bool,
    pub linestyle: String,
}

/// Represents a plotarrow output
#[derive(Clone, Debug)]
pub struct Plotarrow {
    pub series: f64,
    pub title: String,
    pub colorup: Option<Color>,
    pub colordown: Option<Color>,
    pub offset: f64,
    pub minheight: f64,
    pub maxheight: f64,
    pub editable: bool,
    pub show_last: Option<f64>,
    pub display: String,
    pub format: Option<String>,
    pub precision: Option<f64>,
    pub force_overlay: bool,
}

/// Represents a plotbar output
#[derive(Clone, Debug)]
pub struct Plotbar {
    pub open: f64,
    pub high: f64,
    pub low: f64,
    pub close: f64,
    pub title: String,
    pub color: Option<Color>,
    pub editable: bool,
    pub show_last: Option<f64>,
    pub display: String,
    pub format: Option<String>,
    pub precision: Option<f64>,
    pub force_overlay: bool,
}

/// Represents a plotcandle output
#[derive(Clone, Debug)]
pub struct Plotcandle {
    pub open: f64,
    pub high: f64,
    pub low: f64,
    pub close: f64,
    pub title: String,
    pub color: Option<Color>,
    pub wickcolor: Option<Color>,
    pub editable: bool,
    pub show_last: Option<f64>,
    pub bordercolor: Option<Color>,
    pub display: String,
    pub format: Option<String>,
    pub precision: Option<f64>,
    pub force_overlay: bool,
}

/// Represents a plotchar output
#[derive(Clone, Debug)]
pub struct Plotchar {
    pub series: f64,
    pub title: String,
    pub char: String,
    pub location: String,
    pub color: Option<Color>,
    pub offset: f64,
    pub text: String,
    pub textcolor: Option<Color>,
    pub editable: bool,
    pub size: String,
    pub show_last: Option<f64>,
    pub display: String,
    pub format: Option<String>,
    pub precision: Option<f64>,
    pub force_overlay: bool,
}

/// Represents a plotshape output
#[derive(Clone, Debug)]
pub struct Plotshape {
    pub series: f64,
    pub title: String,
    pub style: String,
    pub location: String,
    pub color: Option<Color>,
    pub offset: f64,
    pub text: String,
    pub textcolor: Option<Color>,
    pub editable: bool,
    pub size: String,
    pub show_last: Option<f64>,
    pub display: String,
    pub format: Option<String>,
    pub precision: Option<f64>,
    pub force_overlay: bool,
}

/// Log level
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogLevel {
    Info,
    Warning,
    Error,
}

/// A log entry with level and message
#[derive(Debug, Clone)]
pub struct LogEntry {
    pub level: LogLevel,
    pub message: String,
}

/// The default value of a declared input, preserved with its type so a host can
/// render the right settings widget.
#[derive(Debug, Clone, PartialEq)]
pub enum InputValue {
    Int(i64),
    Float(f64),
    Bool(bool),
    Str(String),
    Color(Color),
}

/// A declared script input (`input.int(...)`, `input.source(...)`, ...).
///
/// Recorded into the output so a host can enumerate a script's configurable
/// settings without executing anything itself. The script still receives the
/// default value at runtime.
#[derive(Debug, Clone)]
pub struct Input {
    /// Which `input.*` function declared it: `"int"`, `"float"`, `"bool"`,
    /// `"string"`, `"source"`, `"color"`, `"session"`, `"time"`.
    pub kind: String,
    /// Display title (`title` argument), empty when none was given.
    pub title: String,
    /// Group the input belongs to (`group` argument), empty when none.
    pub group: String,
    /// Default value returned to the script.
    pub default: InputValue,
}

/// Base trait for all output implementations
///
/// This trait defines the minimal contract that all output types must implement.
/// Extension traits (LogOutput, PlotOutput, etc.) add additional capabilities.
pub trait PineOutput: Default + Clone + std::fmt::Debug + 'static {
    /// Clear all output data for a new iteration
    fn clear(&mut self);
}

/// Extension trait for logging output
pub trait LogOutput: PineOutput {
    /// Add a log entry with the given level and message
    fn add_log(&mut self, level: LogLevel, message: String);
    /// Get all log entries
    fn get_logs(&self) -> &[LogEntry];
}

/// Macro to easily implement all output traits by delegating to a base field
///
/// # Example
/// ```ignore
/// impl_output_traits_delegate!(CustomOutput, base);
/// ```
#[macro_export]
macro_rules! impl_output_traits_delegate {
    ($type:ty, $field:ident) => {
        impl $crate::LogOutput for $type {
            fn add_log(&mut self, level: $crate::LogLevel, message: String) {
                self.$field.add_log(level, message)
            }
            fn get_logs(&self) -> &[$crate::LogEntry] {
                self.$field.get_logs()
            }
        }

        impl $crate::PlotOutput for $type {
            fn add_plot(&mut self, plot: $crate::Plot) {
                self.$field.add_plot(plot)
            }
            fn plots(&self) -> &[$crate::Plot] {
                self.$field.plots()
            }
            fn add_plotarrow(&mut self, arrow: $crate::Plotarrow) {
                self.$field.add_plotarrow(arrow)
            }
            fn plotarrows(&self) -> &[$crate::Plotarrow] {
                self.$field.plotarrows()
            }
            fn add_plotbar(&mut self, bar: $crate::Plotbar) {
                self.$field.add_plotbar(bar)
            }
            fn plotbars(&self) -> &[$crate::Plotbar] {
                self.$field.plotbars()
            }
            fn add_plotcandle(&mut self, candle: $crate::Plotcandle) {
                self.$field.add_plotcandle(candle)
            }
            fn plotcandles(&self) -> &[$crate::Plotcandle] {
                self.$field.plotcandles()
            }
            fn add_plotchar(&mut self, char: $crate::Plotchar) {
                self.$field.add_plotchar(char)
            }
            fn plotchars(&self) -> &[$crate::Plotchar] {
                self.$field.plotchars()
            }
            fn add_plotshape(&mut self, shape: $crate::Plotshape) {
                self.$field.add_plotshape(shape)
            }
            fn plotshapes(&self) -> &[$crate::Plotshape] {
                self.$field.plotshapes()
            }
        }

        impl $crate::LabelOutput for $type {
            fn add_label(&mut self, label: $crate::Label) -> usize {
                self.$field.add_label(label)
            }
            fn get_label(&self, id: usize) -> Option<&$crate::Label> {
                self.$field.get_label(id)
            }
            fn get_label_mut(&mut self, id: usize) -> Option<&mut $crate::Label> {
                self.$field.get_label_mut(id)
            }
            fn delete_label(&mut self, id: usize) -> bool {
                self.$field.delete_label(id)
            }
        }

        impl $crate::BoxOutput for $type {
            fn add_box(&mut self, box_obj: $crate::PineBox) -> usize {
                self.$field.add_box(box_obj)
            }
            fn get_box(&self, id: usize) -> Option<&$crate::PineBox> {
                self.$field.get_box(id)
            }
            fn get_box_mut(&mut self, id: usize) -> Option<&mut $crate::PineBox> {
                self.$field.get_box_mut(id)
            }
            fn delete_box(&mut self, id: usize) -> bool {
                self.$field.delete_box(id)
            }
        }

        impl $crate::InputOutput for $type {
            fn add_input(&mut self, input: $crate::Input) {
                self.$field.add_input(input)
            }
            fn inputs(&self) -> &[$crate::Input] {
                self.$field.inputs()
            }
        }

        impl $crate::IndicatorOutput for $type {
            fn set_indicator(&mut self, indicator: $crate::Indicator) {
                self.$field.set_indicator(indicator)
            }
            fn indicator(&self) -> Option<&$crate::Indicator> {
                self.$field.indicator()
            }
        }

        impl $crate::AlertConditionOutput for $type {
            fn add_alertcondition(&mut self, alert: $crate::AlertCondition) {
                self.$field.add_alertcondition(alert)
            }
            fn alertconditions(&self) -> &[$crate::AlertCondition] {
                self.$field.alertconditions()
            }
        }

        impl $crate::FillOutput for $type {
            fn add_fill(&mut self, fill: $crate::FillObject) {
                self.$field.add_fill(fill)
            }
            fn fills(&self) -> &[$crate::FillObject] {
                self.$field.fills()
            }
        }

        impl $crate::GlobalOutput for $type {
            fn set_bgcolor(&mut self, color: Option<$crate::Color>) {
                self.$field.set_bgcolor(color)
            }
            fn set_barcolor(&mut self, color: Option<$crate::Color>) {
                self.$field.set_barcolor(color)
            }
            fn global_context(&self) -> &$crate::GlobalContext {
                self.$field.global_context()
            }
        }

        impl $crate::LineOutput for $type {
            fn add_line(&mut self, line: $crate::LineObject) -> usize {
                self.$field.add_line(line)
            }
            fn get_line(&self, id: usize) -> Option<&$crate::LineObject> {
                self.$field.get_line(id)
            }
            fn get_line_mut(&mut self, id: usize) -> Option<&mut $crate::LineObject> {
                self.$field.get_line_mut(id)
            }
            fn delete_line(&mut self, id: usize) -> bool {
                self.$field.delete_line(id)
            }
        }

        impl $crate::TableOutput for $type {
            fn add_table(&mut self, table: $crate::Table) -> usize {
                self.$field.add_table(table)
            }
            fn get_table(&self, id: usize) -> Option<&$crate::Table> {
                self.$field.get_table(id)
            }
            fn get_table_mut(&mut self, id: usize) -> Option<&mut $crate::Table> {
                self.$field.get_table_mut(id)
            }
            fn delete_table(&mut self, id: usize) -> bool {
                self.$field.delete_table(id)
            }
        }
    };
}

/// Extension trait for plot-related output
pub trait PlotOutput: PineOutput {
    /// Add a plot output
    fn add_plot(&mut self, plot: Plot);
    /// Get all plot outputs
    fn plots(&self) -> &[Plot];

    /// Add a plotarrow output
    fn add_plotarrow(&mut self, arrow: Plotarrow);
    /// Get all plotarrow outputs
    fn plotarrows(&self) -> &[Plotarrow];

    /// Add a plotbar output
    fn add_plotbar(&mut self, bar: Plotbar);
    /// Get all plotbar outputs
    fn plotbars(&self) -> &[Plotbar];

    /// Add a plotcandle output
    fn add_plotcandle(&mut self, candle: Plotcandle);
    /// Get all plotcandle outputs
    fn plotcandles(&self) -> &[Plotcandle];

    /// Add a plotchar output
    fn add_plotchar(&mut self, char: Plotchar);
    /// Get all plotchar outputs
    fn plotchars(&self) -> &[Plotchar];

    /// Add a plotshape output
    fn add_plotshape(&mut self, shape: Plotshape);
    /// Get all plotshape outputs
    fn plotshapes(&self) -> &[Plotshape];
}

/// Extension trait for label output
pub trait LabelOutput: PineOutput {
    /// Add a label and return its ID
    fn add_label(&mut self, label: Label) -> usize;
    /// Get a reference to a label by ID
    fn get_label(&self, id: usize) -> Option<&Label>;
    /// Get a mutable reference to a label by ID
    fn get_label_mut(&mut self, id: usize) -> Option<&mut Label>;
    /// Delete a label by ID and return true if it existed
    fn delete_label(&mut self, id: usize) -> bool;
}

/// Extension trait for box output
pub trait BoxOutput: PineOutput {
    /// Add a box and return its ID
    fn add_box(&mut self, box_obj: PineBox) -> usize;
    /// Get a reference to a box by ID
    fn get_box(&self, id: usize) -> Option<&PineBox>;
    /// Get a mutable reference to a box by ID
    fn get_box_mut(&mut self, id: usize) -> Option<&mut PineBox>;
    /// Delete a box by ID and return true if it existed
    fn delete_box(&mut self, id: usize) -> bool;
}

/// Extension trait for table output
pub trait TableOutput: PineOutput {
    /// Add a table and return its ID
    fn add_table(&mut self, table: Table) -> usize;
    /// Get a reference to a table by ID
    fn get_table(&self, id: usize) -> Option<&Table>;
    /// Get a mutable reference to a table by ID
    fn get_table_mut(&mut self, id: usize) -> Option<&mut Table>;
    /// Delete a table by ID and return true if it existed
    fn delete_table(&mut self, id: usize) -> bool;
}

/// Extension trait for line output
pub trait LineOutput: PineOutput {
    /// Add a line and return its ID
    fn add_line(&mut self, line: LineObject) -> usize;
    /// Get a reference to a line by ID
    fn get_line(&self, id: usize) -> Option<&LineObject>;
    /// Get a mutable reference to a line by ID
    fn get_line_mut(&mut self, id: usize) -> Option<&mut LineObject>;
    /// Delete a line by ID and return true if it existed
    fn delete_line(&mut self, id: usize) -> bool;
}

/// Extension trait for recording `fill(...)` areas.
pub trait FillOutput: PineOutput {
    fn add_fill(&mut self, fill: FillObject);
    fn fills(&self) -> &[FillObject];
}

/// Extension trait for chart-wide globals (`bgcolor`, `barcolor`).
pub trait GlobalOutput: PineOutput {
    fn set_bgcolor(&mut self, color: Option<Color>);
    fn set_barcolor(&mut self, color: Option<Color>);
    /// The accumulated chart-wide settings.
    fn global_context(&self) -> &GlobalContext;
}

/// Extension trait for recording declared alert conditions.
pub trait AlertConditionOutput: PineOutput {
    /// Record a declared alert condition.
    fn add_alertcondition(&mut self, alert: AlertCondition);
    /// Every alert condition declared so far, in declaration order.
    fn alertconditions(&self) -> &[AlertCondition];
}

/// Extension trait for the script's `indicator(...)` declaration.
pub trait IndicatorOutput: PineOutput {
    /// Record the indicator declaration (a script has at most one).
    fn set_indicator(&mut self, indicator: Indicator);
    /// The declaration, if the script declared one.
    fn indicator(&self) -> Option<&Indicator>;
}

/// Extension trait for recording declared inputs
pub trait InputOutput: PineOutput {
    /// Record a declared input.
    fn add_input(&mut self, input: Input);
    /// Every input declared so far, in declaration order.
    fn inputs(&self) -> &[Input];
}

/// Default implementation of PineOutput that supports all features
#[derive(Default, Clone, Debug)]
pub struct DefaultPineOutput {
    /// Label storage for drawable objects
    labels: HashMap<usize, Label>,
    /// Next label ID
    next_label_id: usize,
    /// Box storage for drawable objects
    boxes: HashMap<usize, PineBox>,
    /// Next box ID
    next_box_id: usize,
    /// Line storage for drawable objects
    lines: HashMap<usize, LineObject>,
    /// Next line ID
    next_line_id: usize,
    /// Table storage for drawable objects
    tables: HashMap<usize, Table>,
    /// Next table ID
    next_table_id: usize,
    /// Plot outputs
    plots: Vec<Plot>,
    /// Plotarrow outputs
    plotarrows: Vec<Plotarrow>,
    /// Plotbar outputs
    plotbars: Vec<Plotbar>,
    /// Plotcandle outputs
    plotcandles: Vec<Plotcandle>,
    /// Plotchar outputs
    plotchars: Vec<Plotchar>,
    /// Plotshape outputs
    plotshapes: Vec<Plotshape>,
    /// Log entries
    logs: Vec<LogEntry>,
    /// Declared inputs
    inputs: Vec<Input>,
    /// The `indicator(...)` declaration, if any.
    indicator: Option<Indicator>,
    /// Chart-wide settings (`bgcolor`, `barcolor`).
    globals: GlobalContext,
    /// Declared alert conditions.
    alertconditions: Vec<AlertCondition>,
    /// `fill(...)` areas.
    fills: Vec<FillObject>,
}

impl PineOutput for DefaultPineOutput {
    fn clear(&mut self) {
        self.labels.clear();
        self.boxes.clear();
        self.plots.clear();
        self.plotarrows.clear();
        self.plotbars.clear();
        self.plotcandles.clear();
        self.plotchars.clear();
        self.plotshapes.clear();
        self.logs.clear();
        self.inputs.clear();
        self.indicator = None;
        self.globals = GlobalContext::default();
        self.alertconditions.clear();
        self.fills.clear();
        self.lines.clear();
        self.tables.clear();
        // Reset ID counters
        self.next_label_id = 0;
        self.next_box_id = 0;
        self.next_line_id = 0;
        self.next_table_id = 0;
    }
}

impl LogOutput for DefaultPineOutput {
    fn add_log(&mut self, level: LogLevel, message: String) {
        self.logs.push(LogEntry { level, message });
    }

    fn get_logs(&self) -> &[LogEntry] {
        &self.logs
    }
}

impl PlotOutput for DefaultPineOutput {
    fn add_plot(&mut self, plot: Plot) {
        self.plots.push(plot);
    }

    fn plots(&self) -> &[Plot] {
        &self.plots
    }

    fn add_plotarrow(&mut self, arrow: Plotarrow) {
        self.plotarrows.push(arrow);
    }

    fn plotarrows(&self) -> &[Plotarrow] {
        &self.plotarrows
    }

    fn add_plotbar(&mut self, bar: Plotbar) {
        self.plotbars.push(bar);
    }

    fn plotbars(&self) -> &[Plotbar] {
        &self.plotbars
    }

    fn add_plotcandle(&mut self, candle: Plotcandle) {
        self.plotcandles.push(candle);
    }

    fn plotcandles(&self) -> &[Plotcandle] {
        &self.plotcandles
    }

    fn add_plotchar(&mut self, char: Plotchar) {
        self.plotchars.push(char);
    }

    fn plotchars(&self) -> &[Plotchar] {
        &self.plotchars
    }

    fn add_plotshape(&mut self, shape: Plotshape) {
        self.plotshapes.push(shape);
    }

    fn plotshapes(&self) -> &[Plotshape] {
        &self.plotshapes
    }
}

impl LabelOutput for DefaultPineOutput {
    fn add_label(&mut self, label: Label) -> usize {
        let id = self.next_label_id;
        self.next_label_id += 1;
        self.labels.insert(id, label);
        id
    }

    fn get_label(&self, id: usize) -> Option<&Label> {
        self.labels.get(&id)
    }

    fn get_label_mut(&mut self, id: usize) -> Option<&mut Label> {
        self.labels.get_mut(&id)
    }

    fn delete_label(&mut self, id: usize) -> bool {
        self.labels.remove(&id).is_some()
    }
}

impl BoxOutput for DefaultPineOutput {
    fn add_box(&mut self, box_obj: PineBox) -> usize {
        let id = self.next_box_id;
        self.next_box_id += 1;
        self.boxes.insert(id, box_obj);
        id
    }

    fn get_box(&self, id: usize) -> Option<&PineBox> {
        self.boxes.get(&id)
    }

    fn get_box_mut(&mut self, id: usize) -> Option<&mut PineBox> {
        self.boxes.get_mut(&id)
    }

    fn delete_box(&mut self, id: usize) -> bool {
        self.boxes.remove(&id).is_some()
    }
}

impl LineOutput for DefaultPineOutput {
    fn add_line(&mut self, line: LineObject) -> usize {
        let id = self.next_line_id;
        self.next_line_id += 1;
        self.lines.insert(id, line);
        id
    }

    fn get_line(&self, id: usize) -> Option<&LineObject> {
        self.lines.get(&id)
    }

    fn get_line_mut(&mut self, id: usize) -> Option<&mut LineObject> {
        self.lines.get_mut(&id)
    }

    fn delete_line(&mut self, id: usize) -> bool {
        self.lines.remove(&id).is_some()
    }
}

impl TableOutput for DefaultPineOutput {
    fn add_table(&mut self, table: Table) -> usize {
        let id = self.next_table_id;
        self.next_table_id += 1;
        self.tables.insert(id, table);
        id
    }

    fn get_table(&self, id: usize) -> Option<&Table> {
        self.tables.get(&id)
    }

    fn get_table_mut(&mut self, id: usize) -> Option<&mut Table> {
        self.tables.get_mut(&id)
    }

    fn delete_table(&mut self, id: usize) -> bool {
        self.tables.remove(&id).is_some()
    }
}

impl InputOutput for DefaultPineOutput {
    fn add_input(&mut self, input: Input) {
        self.inputs.push(input);
    }

    fn inputs(&self) -> &[Input] {
        &self.inputs
    }
}

impl IndicatorOutput for DefaultPineOutput {
    fn set_indicator(&mut self, indicator: Indicator) {
        self.indicator = Some(indicator);
    }

    fn indicator(&self) -> Option<&Indicator> {
        self.indicator.as_ref()
    }
}

impl AlertConditionOutput for DefaultPineOutput {
    fn add_alertcondition(&mut self, alert: AlertCondition) {
        self.alertconditions.push(alert);
    }

    fn alertconditions(&self) -> &[AlertCondition] {
        &self.alertconditions
    }
}

impl FillOutput for DefaultPineOutput {
    fn add_fill(&mut self, fill: FillObject) {
        self.fills.push(fill);
    }

    fn fills(&self) -> &[FillObject] {
        &self.fills
    }
}

impl GlobalOutput for DefaultPineOutput {
    fn set_bgcolor(&mut self, color: Option<Color>) {
        self.globals.bgcolor = color;
    }

    fn set_barcolor(&mut self, color: Option<Color>) {
        self.globals.barcolor = color;
    }

    fn global_context(&self) -> &GlobalContext {
        &self.globals
    }
}