deribit_http/model/
ticker.rs

1/******************************************************************************
2   Author: Joaquín Béjar García
3   Email: jb@taunais.com
4   Date: 15/9/25
5******************************************************************************/
6use crate::model::instrument::InstrumentKind;
7use crate::model::other::Greeks;
8use pretty_simple_display::{DebugPretty, DisplaySimple};
9use serde::{Deserialize, Serialize};
10use serde_with::skip_serializing_none;
11
12/// Ticker stats sub-structure
13#[skip_serializing_none]
14#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
15pub struct TickerStats {
16    /// Trading volume
17    pub volume: f64,
18    /// Trading volume in USD
19    pub volume_usd: Option<f64>,
20    /// Price change from previous period
21    pub price_change: Option<f64>,
22    /// Highest price in the period
23    pub high: Option<f64>,
24    /// Lowest price in the period
25    pub low: Option<f64>,
26}
27
28/// Ticker data structure with corrected field types
29#[skip_serializing_none]
30#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
31pub struct TickerData {
32    /// Name of the instrument
33    pub instrument_name: String,
34    /// Last traded price
35    pub last_price: Option<f64>,
36    /// Current mark price
37    pub mark_price: f64,
38    /// Best bid price available
39    pub best_bid_price: Option<f64>,
40    /// Best ask price available
41    pub best_ask_price: Option<f64>,
42    /// Amount available at best bid price
43    pub best_bid_amount: f64,
44    /// Amount available at best ask price
45    pub best_ask_amount: f64,
46    /// Trading volume in base currency
47    pub volume: Option<f64>,
48    /// Trading volume in USD
49    pub volume_usd: Option<f64>,
50    /// Open interest for the instrument
51    pub open_interest: Option<f64>,
52    /// Highest price in 24h period
53    pub high: Option<f64>,
54    /// Lowest price in 24h period
55    pub low: Option<f64>,
56    /// Absolute price change in 24h
57    pub price_change: Option<f64>,
58    /// Percentage price change in 24h
59    pub price_change_percentage: Option<f64>,
60    /// Implied volatility at best bid
61    pub bid_iv: Option<f64>,
62    /// Implied volatility at best ask
63    pub ask_iv: Option<f64>,
64    /// Mark implied volatility
65    pub mark_iv: Option<f64>,
66    /// Timestamp of the ticker data
67    pub timestamp: u64,
68    /// Current state of the instrument
69    pub state: String,
70    /// Settlement price (for expired instruments)
71    pub settlement_price: Option<f64>,
72    /// Additional ticker statistics
73    pub stats: TickerStats,
74    /// Greeks for options (delta, gamma, vega, theta, rho)
75    pub greeks: Option<Greeks>,
76    /// Index price
77    pub index_price: Option<f64>,
78    /// Minimum price
79    pub min_price: Option<f64>,
80    /// Maximum price
81    pub max_price: Option<f64>,
82    /// Interest rate
83    pub interest_rate: Option<f64>,
84    /// Underlying price
85    pub underlying_price: Option<f64>,
86    /// Underlying index
87    pub underlying_index: Option<String>,
88    /// Estimated delivery price
89    pub estimated_delivery_price: Option<f64>,
90}
91
92/// Ticker information
93#[skip_serializing_none]
94#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
95pub struct Ticker {
96    /// Instrument name
97    pub instrument_name: String,
98    /// Timestamp of the ticker data
99    pub timestamp: i64,
100    /// Best bid price
101    pub best_bid_price: Option<f64>,
102    /// Best bid amount
103    pub best_bid_amount: Option<f64>,
104    /// Best ask price
105    pub best_ask_price: Option<f64>,
106    /// Best ask amount
107    pub best_ask_amount: Option<f64>,
108    /// Last trade price
109    pub last_price: Option<f64>,
110    /// Mark price
111    pub mark_price: Option<f64>,
112    /// Index price
113    pub index_price: Option<f64>,
114    /// Open interest
115    pub open_interest: f64,
116    /// 24h volume
117    pub volume_24h: f64,
118    /// 24h volume in USD
119    pub volume_usd_24h: f64,
120    /// 24h price change
121    pub price_change_24h: f64,
122    /// High price in 24h
123    pub high_24h: Option<f64>,
124    /// Low price in 24h
125    pub low_24h: Option<f64>,
126    /// Underlying price (for derivatives)
127    pub underlying_price: Option<f64>,
128    /// Underlying index
129    pub underlying_index: Option<String>,
130    /// Instrument kind
131    pub instrument_kind: Option<InstrumentKind>,
132    /// Current funding rate (for perpetuals)
133    pub current_funding: Option<f64>,
134    /// Funding 8h rate
135    pub funding_8h: Option<f64>,
136    /// Implied volatility (for options)
137    pub iv: Option<f64>,
138    /// Greeks (for options)
139    pub greeks: Option<Greeks>,
140    /// Interest rate
141    pub interest_rate: Option<f64>,
142}
143
144impl Ticker {
145    /// Calculate bid-ask spread
146    pub fn spread(&self) -> Option<f64> {
147        match (self.best_ask_price, self.best_bid_price) {
148            (Some(ask), Some(bid)) => Some(ask - bid),
149            _ => None,
150        }
151    }
152
153    /// Calculate mid price
154    pub fn mid_price(&self) -> Option<f64> {
155        match (self.best_ask_price, self.best_bid_price) {
156            (Some(ask), Some(bid)) => Some((ask + bid) / 2.0),
157            _ => None,
158        }
159    }
160
161    /// Calculate spread percentage
162    pub fn spread_percentage(&self) -> Option<f64> {
163        match (self.spread(), self.mid_price()) {
164            (Some(spread), Some(mid)) if mid != 0.0 => Some((spread / mid) * 100.0),
165            _ => None,
166        }
167    }
168
169    /// Check if there's a valid bid-ask spread
170    pub fn has_valid_spread(&self) -> bool {
171        self.best_bid_price.is_some() && self.best_ask_price.is_some()
172    }
173}