Skip to main content

egui_charts/model/
signal.rs

1//! What a host's analytics found, in the form the chart is to show it.
2
3use chrono::{DateTime, Utc};
4
5/// One finding of the host's analytics: a marker, a level or a zone, for the chart to draw in the
6/// theme's colours for its direction and category.
7#[derive(Clone, Debug, PartialEq)]
8pub struct Signal {
9    /// Identifies the signal within its subscription: an upsert with the same id replaces it.
10    pub id: SignalId,
11    /// Where the signal sits on the chart.
12    pub shape: SignalShape,
13    /// The group the user can hide signals by, such as "Large trades" or "Pivots".
14    pub category: String,
15    /// Short text drawn next to the signal.
16    pub label: Option<String>,
17    /// Text shown while the pointer is over the signal.
18    pub tooltip: Option<String>,
19}
20
21/// Identifies a signal. The host chooses it.
22#[derive(Clone, Debug, PartialEq, Eq, Hash)]
23pub struct SignalId(
24    /// The host's own identifier, unique within a subscription.
25    pub String,
26);
27
28/// Where a signal sits on the chart, anchored by time so it keeps its place through zoom,
29/// interval changes and history loads. Prices are finite and in the symbol's price units.
30#[derive(Clone, Debug, PartialEq)]
31#[non_exhaustive]
32pub enum SignalShape {
33    /// A mark on the bar containing `time`, at `price` or, without one, beside the bar.
34    Marker {
35        /// UTC time at which the marker is anchored.
36        time: DateTime<Utc>,
37        /// Marker price; `None` uses the direction's default placement.
38        price: Option<f64>,
39        /// Direction used for marker placement and color.
40        direction: Direction,
41    },
42    /// A horizontal line at `price`, starting at `from` or, without one, across the whole chart.
43    Level {
44        /// Price of the horizontal level.
45        price: f64,
46        /// Start time, or `None` for a level across the whole chart.
47        from: Option<DateTime<Utc>>,
48    },
49    /// A directional zone at one price, drawn as a line from `from` to `to`, or onward
50    /// without `to`. Even a zero-duration zone remains visible and can be hovered.
51    PriceZone {
52        /// Start time of the zone.
53        from: DateTime<Utc>,
54        /// End time, at or after `from`, or `None` for an open zone.
55        to: Option<DateTime<Utc>>,
56        /// Price of the resting level.
57        price: f64,
58        /// Side colour of the zone.
59        direction: Direction,
60    },
61    /// The prices from `low` to `high` between `from` and `to`, or from `from` onward without
62    /// `to`. `low` is at most `high`.
63    Zone {
64        /// Start time of the zone.
65        from: DateTime<Utc>,
66        /// End time, or `None` for an open-ended zone.
67        to: Option<DateTime<Utc>>,
68        /// Lower boundary of the zone.
69        low: f64,
70        /// Upper boundary of the zone.
71        high: f64,
72    },
73}
74
75/// Whether a signal is bullish, bearish or neither, which picks its colour.
76#[derive(Clone, Copy, Debug, PartialEq, Eq)]
77pub enum Direction {
78    /// Bullish: drawn in the theme's up colour.
79    Up,
80    /// Bearish: drawn in the theme's down colour.
81    Down,
82    /// Neither bullish nor bearish: drawn in a neutral colour.
83    Neutral,
84}