use crate::model::Bar;
use crate::studies::{Indicator, IndicatorValue};
use crate::tokens::DESIGN_TOKENS;
use egui::Color32;
#[derive(Clone)]
pub struct VWMA {
period: usize,
values: Vec<IndicatorValue>,
color: Color32,
visible: bool,
}
impl VWMA {
pub fn new(period: usize) -> Self {
Self {
period,
values: Vec::new(),
color: DESIGN_TOKENS.semantic.extended.success, visible: true,
}
}
pub fn with_color(mut self, color: Color32) -> Self {
self.color = color;
self
}
}
impl Default for VWMA {
fn default() -> Self {
Self::new(20)
}
}
impl Indicator for VWMA {
fn name(&self) -> &str {
"VWMA"
}
fn desc(&self) -> &str {
"Volume Weighted Moving Avg - MA weighted by volume"
}
fn calculate(&mut self, data: &[Bar]) {
self.values.clear();
if data.len() < self.period {
return;
}
for i in 0..data.len() {
if i < self.period - 1 {
self.values.push(IndicatorValue::None);
} else {
let window = &data[i + 1 - self.period..=i];
let price_volume_sum: f64 = window.iter().map(|bar| bar.close * bar.volume).sum();
let volume_sum: f64 = window.iter().map(|bar| bar.volume).sum();
let vwma = if volume_sum > 0.0 {
price_volume_sum / volume_sum
} else {
window.iter().map(|bar| bar.close).sum::<f64>() / self.period as f64
};
self.values.push(IndicatorValue::Single(vwma));
}
}
}
fn values(&self) -> &[IndicatorValue] {
&self.values
}
fn colors(&self) -> Vec<Color32> {
vec![self.color]
}
fn set_colors(&mut self, colors: Vec<Color32>) {
if !colors.is_empty() {
self.color = colors[0];
}
}
fn is_overlay(&self) -> bool {
true }
fn is_visible(&self) -> bool {
self.visible
}
fn set_visible(&mut self, visible: bool) {
self.visible = visible;
}
fn clone_box(&self) -> Box<dyn Indicator> {
Box::new(self.clone())
}
fn line_names(&self) -> Vec<String> {
vec![format!("VWMA({})", self.period)]
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::Utc;
fn make_bar(close: f64, volume: f64) -> Bar {
Bar {
time: Utc::now(),
open: close,
high: close,
low: close,
close,
volume,
}
}
#[test]
fn test_vwma_calculation() {
let mut vwma = VWMA::new(3);
let data = vec![
make_bar(100.0, 1000.0),
make_bar(102.0, 2000.0),
make_bar(101.0, 1500.0),
make_bar(103.0, 1000.0),
];
vwma.calculate(&data);
assert_eq!(vwma.values.len(), 4);
assert!(matches!(vwma.values[0], IndicatorValue::None));
assert!(matches!(vwma.values[1], IndicatorValue::None));
if let IndicatorValue::Single(v) = vwma.values[2] {
assert!((v - 101.22).abs() < 0.01);
}
}
}