#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ChartPoint {
pub bar_idx: f32,
pub price: f64,
}
impl ChartPoint {
#[inline]
pub fn new(bar_idx: f32, price: f64) -> Self {
Self { bar_idx, price }
}
#[inline]
pub fn zero() -> Self {
Self {
bar_idx: 0.0,
price: 0.0,
}
}
#[inline]
pub fn shift_bar_idx(&mut self, delta: f32) {
self.bar_idx += delta;
}
#[inline]
pub fn with_shifted_bar_idx(self, delta: f32) -> Self {
Self {
bar_idx: self.bar_idx + delta,
price: self.price,
}
}
pub fn lerp(a: ChartPoint, b: ChartPoint, t: f32) -> ChartPoint {
ChartPoint {
bar_idx: a.bar_idx + (b.bar_idx - a.bar_idx) * t,
price: a.price + (b.price - a.price) * t as f64,
}
}
}
impl Default for ChartPoint {
fn default() -> Self {
Self::zero()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let point = ChartPoint::new(100.0, 150.50);
assert_eq!(point.bar_idx, 100.0);
assert_eq!(point.price, 150.50);
}
#[test]
fn test_shift_bar_idx() {
let mut point = ChartPoint::new(100.0, 150.50);
point.shift_bar_idx(50.0);
assert_eq!(point.bar_idx, 150.0);
assert_eq!(point.price, 150.50);
}
#[test]
fn test_lerp() {
let a = ChartPoint::new(0.0, 100.0);
let b = ChartPoint::new(10.0, 200.0);
let mid = ChartPoint::lerp(a, b, 0.5);
assert_eq!(mid.bar_idx, 5.0);
assert_eq!(mid.price, 150.0);
}
}