deribit_http/model/
ticker.rs1use 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#[skip_serializing_none]
14#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
15pub struct TickerStats {
16 pub volume: f64,
18 pub volume_usd: Option<f64>,
20 pub price_change: Option<f64>,
22 pub high: Option<f64>,
24 pub low: Option<f64>,
26}
27
28#[skip_serializing_none]
30#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
31pub struct TickerData {
32 pub instrument_name: String,
34 pub last_price: Option<f64>,
36 pub mark_price: f64,
38 pub best_bid_price: Option<f64>,
40 pub best_ask_price: Option<f64>,
42 pub best_bid_amount: f64,
44 pub best_ask_amount: f64,
46 pub volume: Option<f64>,
48 pub volume_usd: Option<f64>,
50 pub open_interest: Option<f64>,
52 pub high: Option<f64>,
54 pub low: Option<f64>,
56 pub price_change: Option<f64>,
58 pub price_change_percentage: Option<f64>,
60 pub bid_iv: Option<f64>,
62 pub ask_iv: Option<f64>,
64 pub mark_iv: Option<f64>,
66 pub timestamp: u64,
68 pub state: String,
70 pub settlement_price: Option<f64>,
72 pub stats: TickerStats,
74 pub greeks: Option<Greeks>,
76 pub index_price: Option<f64>,
78 pub min_price: Option<f64>,
80 pub max_price: Option<f64>,
82 pub interest_rate: Option<f64>,
84 pub underlying_price: Option<f64>,
86 pub underlying_index: Option<String>,
88 pub estimated_delivery_price: Option<f64>,
90}
91
92#[skip_serializing_none]
94#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
95pub struct Ticker {
96 pub instrument_name: String,
98 pub timestamp: i64,
100 pub best_bid_price: Option<f64>,
102 pub best_bid_amount: Option<f64>,
104 pub best_ask_price: Option<f64>,
106 pub best_ask_amount: Option<f64>,
108 pub last_price: Option<f64>,
110 pub mark_price: Option<f64>,
112 pub index_price: Option<f64>,
114 pub open_interest: f64,
116 pub volume_24h: f64,
118 pub volume_usd_24h: f64,
120 pub price_change_24h: f64,
122 pub high_24h: Option<f64>,
124 pub low_24h: Option<f64>,
126 pub underlying_price: Option<f64>,
128 pub underlying_index: Option<String>,
130 pub instrument_kind: Option<InstrumentKind>,
132 pub current_funding: Option<f64>,
134 pub funding_8h: Option<f64>,
136 pub iv: Option<f64>,
138 pub greeks: Option<Greeks>,
140 pub interest_rate: Option<f64>,
142}
143
144impl Ticker {
145 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 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 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 pub fn has_valid_spread(&self) -> bool {
171 self.best_bid_price.is_some() && self.best_ask_price.is_some()
172 }
173}