egui_charts/drawings/domain/
vwap.rs1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
8#[serde(try_from = "VwapBandFields")]
9pub struct VwapBand {
10 pub enabled: bool,
12 multiplier: f64,
13}
14
15#[derive(Deserialize)]
17struct VwapBandFields {
18 enabled: bool,
19 multiplier: f64,
20}
21
22impl TryFrom<VwapBandFields> for VwapBand {
23 type Error = &'static str;
24
25 fn try_from(fields: VwapBandFields) -> Result<Self, Self::Error> {
26 Self::new(fields.enabled, fields.multiplier)
27 .ok_or("anchored VWAP band multiplier out of bounds")
28 }
29}
30
31impl VwapBand {
32 pub fn new(enabled: bool, multiplier: f64) -> Option<Self> {
35 (multiplier > 0.0 && multiplier <= 10.0).then_some(Self {
36 enabled,
37 multiplier,
38 })
39 }
40
41 pub fn multiplier(&self) -> f64 {
43 self.multiplier
44 }
45}
46
47#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
49#[non_exhaustive]
50pub struct VwapConfig {
51 pub bands: [VwapBand; 3],
53}
54
55impl Default for VwapConfig {
56 fn default() -> Self {
58 let band = |enabled, multiplier| VwapBand {
59 enabled,
60 multiplier,
61 };
62 Self {
63 bands: [band(true, 1.0), band(false, 2.0), band(false, 3.0)],
64 }
65 }
66}