Skip to main content

egui_charts/model/
price_source.rs

1//! Price source selection for chart series.
2//!
3//! Defines which price value to use for line-based chart calculations.
4
5/// Price source for series calculations
6#[derive(
7    Clone, Copy, Debug, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize,
8)]
9pub enum PriceSource {
10    /// Opening price
11    Open,
12    /// Highest price
13    High,
14    /// Lowest price
15    Low,
16    /// Closing price (most common)
17    #[default]
18    Close,
19    /// (High + Low) / 2
20    HL2,
21    /// (High + Low + Close) / 3
22    HLC3,
23    /// (Open + High + Low + Close) / 4
24    OHLC4,
25}
26
27impl PriceSource {
28    /// Display label for the price source
29    pub fn label(&self) -> &'static str {
30        match self {
31            Self::Open => "Open",
32            Self::High => "High",
33            Self::Low => "Low",
34            Self::Close => "Close",
35            Self::HL2 => "HL/2",
36            Self::HLC3 => "HLC/3",
37            Self::OHLC4 => "OHLC/4",
38        }
39    }
40
41    /// All available price sources
42    pub fn all() -> [Self; 7] {
43        [
44            Self::Open,
45            Self::High,
46            Self::Low,
47            Self::Close,
48            Self::HL2,
49            Self::HLC3,
50            Self::OHLC4,
51        ]
52    }
53
54    /// Compute the price value from OHLC data
55    pub fn compute(&self, open: f64, high: f64, low: f64, close: f64) -> f64 {
56        match self {
57            Self::Open => open,
58            Self::High => high,
59            Self::Low => low,
60            Self::Close => close,
61            Self::HL2 => (high + low) / 2.0,
62            Self::HLC3 => (high + low + close) / 3.0,
63            Self::OHLC4 => (open + high + low + close) / 4.0,
64        }
65    }
66}