Skip to main content

bybit/models/
options_ticker.rs

1use crate::prelude::*;
2
3/// Ticker data for options contracts.
4///
5/// Contains market data for options contracts, including Greeks (delta, gamma, vega, theta),
6/// implied volatility, and other options-specific metrics.
7#[derive(Serialize, Deserialize, Clone, Debug)]
8pub struct OptionsTicker {
9    /// Symbol name.
10    pub symbol: String,
11
12    /// Best bid price.
13    #[serde(rename = "bid1Price")]
14    pub bid1_price: String,
15
16    /// Best bid size.
17    #[serde(rename = "bid1Size")]
18    pub bid1_size: String,
19
20    /// Best bid implied volatility.
21    #[serde(rename = "bid1Iv")]
22    pub bid1_iv: String,
23
24    /// Best ask price.
25    #[serde(rename = "ask1Price")]
26    pub ask1_price: String,
27
28    /// Best ask size.
29    #[serde(rename = "ask1Size")]
30    pub ask1_size: String,
31
32    /// Best ask implied volatility.
33    #[serde(rename = "ask1Iv")]
34    pub ask1_iv: String,
35
36    /// Last traded price.
37    #[serde(rename = "lastPrice")]
38    pub last_price: String,
39
40    /// The highest price in the last 24 hours.
41    #[serde(rename = "highPrice24h")]
42    pub high_price_24h: String,
43
44    /// The lowest price in the last 24 hours.
45    #[serde(rename = "lowPrice24h")]
46    pub low_price_24h: String,
47
48    /// Mark price.
49    #[serde(rename = "markPrice")]
50    pub mark_price: String,
51
52    /// Index price.
53    #[serde(rename = "indexPrice")]
54    pub index_price: String,
55
56    /// Mark price implied volatility.
57    #[serde(rename = "markIv")]
58    pub mark_iv: String,
59
60    /// Underlying asset price.
61    #[serde(rename = "underlyingPrice")]
62    pub underlying_price: String,
63
64    /// Open interest size.
65    #[serde(rename = "openInterest")]
66    pub open_interest: String,
67
68    /// Turnover for 24h.
69    #[serde(rename = "turnover24h")]
70    pub turnover_24h: String,
71
72    /// Volume for 24h.
73    #[serde(rename = "volume24h")]
74    pub volume_24h: String,
75
76    /// Total volume.
77    #[serde(rename = "totalVolume")]
78    pub total_volume: String,
79
80    /// Total turnover.
81    #[serde(rename = "totalTurnover")]
82    pub total_turnover: String,
83
84    /// Delta - rate of change of option price with respect to underlying price.
85    pub delta: String,
86
87    /// Gamma - rate of change of delta with respect to underlying price.
88    pub gamma: String,
89
90    /// Vega - rate of change of option price with respect to implied volatility.
91    pub vega: String,
92
93    /// Theta - rate of change of option price with respect to time.
94    pub theta: String,
95
96    /// Predicted delivery price. It has a value 30 mins before delivery.
97    #[serde(rename = "predictedDeliveryPrice")]
98    pub predicted_delivery_price: String,
99
100    /// The change in the last 24 hours.
101    #[serde(rename = "change24h")]
102    pub change_24h: String,
103}
104
105impl OptionsTicker {
106    /// Creates a new OptionsTicker with default values.
107    pub fn new(symbol: String) -> Self {
108        Self {
109            symbol,
110            bid1_price: String::new(),
111            bid1_size: String::new(),
112            bid1_iv: String::new(),
113            ask1_price: String::new(),
114            ask1_size: String::new(),
115            ask1_iv: String::new(),
116            last_price: String::new(),
117            high_price_24h: String::new(),
118            low_price_24h: String::new(),
119            mark_price: String::new(),
120            index_price: String::new(),
121            mark_iv: String::new(),
122            underlying_price: String::new(),
123            open_interest: String::new(),
124            turnover_24h: String::new(),
125            volume_24h: String::new(),
126            total_volume: String::new(),
127            total_turnover: String::new(),
128            delta: String::new(),
129            gamma: String::new(),
130            vega: String::new(),
131            theta: String::new(),
132            predicted_delivery_price: String::new(),
133            change_24h: String::new(),
134        }
135    }
136
137    /// Returns the best bid price as a floating-point number, if parseable.
138    pub fn bid1_price_f64(&self) -> Option<f64> {
139        self.bid1_price.parse().ok()
140    }
141
142    /// Returns the best ask price as a floating-point number, if parseable.
143    pub fn ask1_price_f64(&self) -> Option<f64> {
144        self.ask1_price.parse().ok()
145    }
146
147    /// Returns the last price as a floating-point number, if parseable.
148    pub fn last_price_f64(&self) -> Option<f64> {
149        self.last_price.parse().ok()
150    }
151
152    /// Returns the mark price as a floating-point number, if parseable.
153    pub fn mark_price_f64(&self) -> Option<f64> {
154        self.mark_price.parse().ok()
155    }
156
157    /// Returns the index price as a floating-point number, if parseable.
158    pub fn index_price_f64(&self) -> Option<f64> {
159        self.index_price.parse().ok()
160    }
161
162    /// Returns the underlying price as a floating-point number, if parseable.
163    pub fn underlying_price_f64(&self) -> Option<f64> {
164        self.underlying_price.parse().ok()
165    }
166
167    /// Returns the bid-ask spread.
168    pub fn spread(&self) -> Option<f64> {
169        let bid = self.bid1_price_f64()?;
170        let ask = self.ask1_price_f64()?;
171        Some(ask - bid)
172    }
173
174    /// Returns the mid price (average of bid and ask).
175    pub fn mid_price(&self) -> Option<f64> {
176        let bid = self.bid1_price_f64()?;
177        let ask = self.ask1_price_f64()?;
178        Some((bid + ask) / 2.0)
179    }
180
181    /// Returns the 24-hour change as a floating-point number, if parseable.
182    pub fn change_24h_f64(&self) -> Option<f64> {
183        self.change_24h.parse().ok()
184    }
185
186    /// Returns the 24-hour high price as a floating-point number, if parseable.
187    pub fn high_price_24h_f64(&self) -> Option<f64> {
188        self.high_price_24h.parse().ok()
189    }
190
191    /// Returns the 24-hour low price as a floating-point number, if parseable.
192    pub fn low_price_24h_f64(&self) -> Option<f64> {
193        self.low_price_24h.parse().ok()
194    }
195
196    /// Returns the open interest as a floating-point number, if parseable.
197    pub fn open_interest_f64(&self) -> Option<f64> {
198        self.open_interest.parse().ok()
199    }
200
201    /// Returns the 24-hour volume as a floating-point number, if parseable.
202    pub fn volume_24h_f64(&self) -> Option<f64> {
203        self.volume_24h.parse().ok()
204    }
205
206    /// Returns the 24-hour turnover as a floating-point number, if parseable.
207    pub fn turnover_24h_f64(&self) -> Option<f64> {
208        self.turnover_24h.parse().ok()
209    }
210
211    /// Returns the total volume as a floating-point number, if parseable.
212    pub fn total_volume_f64(&self) -> Option<f64> {
213        self.total_volume.parse().ok()
214    }
215
216    /// Returns the total turnover as a floating-point number, if parseable.
217    pub fn total_turnover_f64(&self) -> Option<f64> {
218        self.total_turnover.parse().ok()
219    }
220
221    /// Returns the delta as a floating-point number, if parseable.
222    pub fn delta_f64(&self) -> Option<f64> {
223        self.delta.parse().ok()
224    }
225
226    /// Returns the gamma as a floating-point number, if parseable.
227    pub fn gamma_f64(&self) -> Option<f64> {
228        self.gamma.parse().ok()
229    }
230
231    /// Returns the vega as a floating-point number, if parseable.
232    pub fn vega_f64(&self) -> Option<f64> {
233        self.vega.parse().ok()
234    }
235
236    /// Returns the theta as a floating-point number, if parseable.
237    pub fn theta_f64(&self) -> Option<f64> {
238        self.theta.parse().ok()
239    }
240
241    /// Returns the bid implied volatility as a floating-point number, if parseable.
242    pub fn bid1_iv_f64(&self) -> Option<f64> {
243        self.bid1_iv.parse().ok()
244    }
245
246    /// Returns the ask implied volatility as a floating-point number, if parseable.
247    pub fn ask1_iv_f64(&self) -> Option<f64> {
248        self.ask1_iv.parse().ok()
249    }
250
251    /// Returns the mark implied volatility as a floating-point number, if parseable.
252    pub fn mark_iv_f64(&self) -> Option<f64> {
253        self.mark_iv.parse().ok()
254    }
255
256    /// Returns the predicted delivery price as a floating-point number, if parseable.
257    pub fn predicted_delivery_price_f64(&self) -> Option<f64> {
258        self.predicted_delivery_price.parse().ok()
259    }
260
261    /// Returns the bid size as a floating-point number, if parseable.
262    pub fn bid1_size_f64(&self) -> Option<f64> {
263        self.bid1_size.parse().ok()
264    }
265
266    /// Returns the ask size as a floating-point number, if parseable.
267    pub fn ask1_size_f64(&self) -> Option<f64> {
268        self.ask1_size.parse().ok()
269    }
270
271    /// Returns the bid-ask size ratio.
272    pub fn bid_ask_size_ratio(&self) -> Option<f64> {
273        let bid_size = self.bid1_size_f64()?;
274        let ask_size = self.ask1_size_f64()?;
275        if ask_size == 0.0 {
276            return None;
277        }
278        Some(bid_size / ask_size)
279    }
280
281    /// Returns the total size (bid + ask).
282    pub fn total_size(&self) -> Option<f64> {
283        let bid_size = self.bid1_size_f64()?;
284        let ask_size = self.ask1_size_f64()?;
285        Some(bid_size + ask_size)
286    }
287
288    /// Returns the implied volatility spread (ask IV - bid IV).
289    pub fn iv_spread(&self) -> Option<f64> {
290        let bid_iv = self.bid1_iv_f64()?;
291        let ask_iv = self.ask1_iv_f64()?;
292        Some(ask_iv - bid_iv)
293    }
294
295    /// Returns the mid implied volatility (average of bid and ask IV).
296    pub fn mid_iv(&self) -> Option<f64> {
297        let bid_iv = self.bid1_iv_f64()?;
298        let ask_iv = self.ask1_iv_f64()?;
299        Some((bid_iv + ask_iv) / 2.0)
300    }
301}