Skip to main content

cripsy_binary_api/domain/
payout.rs

1//! Payout, comisiones y strike list digital.
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6pub struct Payout {
7    pub symbol: String,
8    pub expiration_seconds: u64,
9    /// Porcentaje de ganancia 0-100 (p. ej. 82.0 = 82%).
10    pub profit_percent: f64,
11    /// true = digital spot; false = binaria/turbo clásica.
12    pub is_spot: bool,
13    pub strike: Option<f64>,
14    /// El bróker reporta el activo cerrado (ask nulo ⇒ sin payout).
15    pub closed: bool,
16}
17
18#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
19pub struct StrikeQuote {
20    pub strike: f64,
21    /// `instrument_id` call, p. ej. `doEURUSD201907191250PT1MC11350481SPT`.
22    pub call_instrument_id: Option<String>,
23    pub put_instrument_id: Option<String>,
24    pub call_profit_percent: Option<f64>,
25    pub put_profit_percent: Option<f64>,
26}
27
28#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
29pub struct StrikeList {
30    pub symbol: String,
31    pub duration_minutes: u64,
32    pub quotes: Vec<StrikeQuote>,
33    /// Epoch segundos de la captura (hora del servidor).
34    pub captured_at: i64,
35}
36
37impl StrikeList {
38    /// Quote más cercano a `price`; None si la lista está vacía.
39    pub fn nearest(&self, price: f64) -> Option<&StrikeQuote> {
40        self.quotes.iter().min_by(|a, b| {
41            (a.strike - price)
42                .abs()
43                .partial_cmp(&(b.strike - price).abs())
44                .unwrap_or(std::cmp::Ordering::Equal)
45        })
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    fn quote(strike: f64) -> StrikeQuote {
54        StrikeQuote {
55            strike,
56            call_instrument_id: None,
57            put_instrument_id: None,
58            call_profit_percent: None,
59            put_profit_percent: None,
60        }
61    }
62
63    #[test]
64    fn nearest_encuentra_el_mas_cercano() {
65        let list = StrikeList {
66            symbol: "EURUSD".into(),
67            duration_minutes: 1,
68            quotes: vec![quote(1.10), quote(1.25), quote(1.30)],
69            captured_at: 0,
70        };
71        let q = list.nearest(1.24).unwrap();
72        assert_eq!(q.strike, 1.25);
73    }
74
75    #[test]
76    fn nearest_vacio_es_none() {
77        let list = StrikeList {
78            symbol: "EURUSD".into(),
79            duration_minutes: 1,
80            quotes: vec![],
81            captured_at: 0,
82        };
83        assert!(list.nearest(1.0).is_none());
84    }
85}