use pulse_macros::pwp; use crate::PulseWire; #[derive(Debug, Clone)] pub struct Symbol(pub String); #[derive(Debug, Clone, Copy)] pub struct USD(pub f64); #[pwp] #[derive(Copy, serde::Deserialize, serde::Serialize)] pub enum TimeFrame { #[serde(rename = "1m")] M1, #[serde(rename = "3m")] M3, #[serde(rename = "5m")] M5, #[serde(rename = "15m")] M15, #[serde(rename = "30m")] M30, #[serde(rename = "1h")] H1, #[serde(rename = "2h")] H2, #[serde(rename = "4h")] H4, #[serde(rename = "8h")] H8, #[serde(rename = "12h")] H12, #[serde(rename = "1d")] D1, #[serde(rename = "3d")] D3, #[serde(rename = "1w")] W1, #[serde(rename = "1M")] Month1, } #[pwp] pub enum Direction { Buy, Sell, } #[pwp] pub enum Volatility { Low, Medium, High, } impl std::fmt::Display for Symbol { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "\x1b[35m{}\x1b[0m", self.0) } } impl std::fmt::Display for USD { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { if self.0 > 0.0 { write!(f, "\x1b[32m${}\x1b[0m", format_f64(self.0)) } else { write!(f, "\x1b[31m${}\x1b[0m", format_f64(self.0)) } } } impl PulseWire for Symbol { fn from_com(com: &mut Vec) -> Self { Self(String::from_com(com)) } fn to_com(&self) -> Vec { self.0.to_com() } } impl PulseWire for USD { fn from_com(com: &mut Vec) -> Self { Self(f64::from_com(com)) } fn to_com(&self) -> Vec { self.0.to_com() } } pub fn format_f64(value: f64) -> String { let abs = value.abs(); let (divisor, suffix) = if abs >= 1_000_000_000.0 { (1_000_000_000.0, "B") } else if abs >= 1_000_000.0 { (1_000_000.0, "M") } else if abs >= 1_000.0 { (1_000.0, "K") } else { (1.0, "") }; if divisor != 1.0 { let formatted = value / divisor; // Remove unnecessary trailing zeros let s = format!("{:.2}", formatted) .trim_end_matches('0') .trim_end_matches('.') .to_string(); return format!("{}{}", s, suffix); } let val = format!("{:.3}", value); let parts: Vec<&str> = val.split('.').collect(); let int = parts[0].to_string(); let negative = int.starts_with('-'); let start = if negative { 1 } else { 0 }; let mut result = String::new(); for (i, c) in int[start..].chars().rev().enumerate() { if i > 0 && i % 3 == 0 { result.push(','); } result.push(c); } let mut formatted: String = result.chars().rev().collect(); if negative { formatted.insert(0, '-'); } if parts.len() > 1 { formatted.push('.'); formatted.push_str(parts[1]); } formatted } impl TimeFrame { pub fn as_str(&self) -> &'static str { match self { Self::M1 => "1m", Self::M3 => "3m", Self::M5 => "5m", Self::M15 => "15m", Self::M30 => "30m", Self::H1 => "1h", Self::H2 => "2h", Self::H4 => "4h", Self::H8 => "8h", Self::H12 => "12h", Self::D1 => "1d", Self::D3 => "3d", Self::W1 => "1w", Self::Month1 => "1M", } } } impl std::fmt::Display for Direction { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Buy => write!(f, "BUY"), Self::Sell => write!(f, "SELL"), } } } impl std::fmt::Display for Volatility { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Low => write!(f, "Low"), Self::Medium => write!(f, "Medium"), Self::High => write!(f, "High"), } } }