Skip to main content

cripsy_binary_api/domain/
candle.rs

1//! Velas: históricas y streaming.
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
6pub struct Candle {
7    pub from: i64,
8    pub to: i64,
9    pub open: f64,
10    pub close: f64,
11    pub high: f64,
12    pub low: f64,
13    pub volume: Option<f64>,
14}
15
16impl Candle {
17    /// ¿La vela es consistente (high >= low, precios positivos)?
18    pub fn is_consistent(&self) -> bool {
19        self.high >= self.low && self.open >= 0.0 && self.close >= 0.0 && self.from <= self.to
20    }
21}
22
23#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
24pub struct CandleRequest {
25    pub symbol: String,
26    /// Segundos por vela (1, 5, 60, 300, ...).
27    pub interval: u64,
28    pub count: usize,
29    pub end_time: Option<i64>,
30}
31
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
33pub struct CandleSubscribe {
34    pub symbol: String,
35    pub interval: u64,
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn vela_consistente() {
44        let ok = Candle {
45            from: 1,
46            to: 60,
47            open: 1.0,
48            close: 1.2,
49            high: 1.3,
50            low: 0.9,
51            volume: Some(5.0),
52        };
53        let mala = Candle { low: 2.0, ..ok };
54        assert!(ok.is_consistent());
55        assert!(!mala.is_consistent());
56    }
57
58    #[test]
59    fn request_serializa() {
60        let r = CandleRequest {
61            symbol: "EURUSD".into(),
62            interval: 60,
63            count: 100,
64            end_time: None,
65        };
66        let json = serde_json::to_string(&r).unwrap();
67        assert!(json.contains("\"symbol\":\"EURUSD\""));
68        assert!(json.contains("\"interval\":60"));
69    }
70}