1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/******************************************************************************
Author: Joaquín Béjar García
Email: jb@taunais.com
Date: 15/9/25
******************************************************************************/
use crate::model::instrument::Instrument;
use crate::model::ticker::TickerData;
use crate::model::{BasicGreeks, BasicOptionData, OptionType, Spread};
use chrono::{DateTime, TimeZone, Utc};
use pretty_simple_display::{DebugPretty, DisplaySimple};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_with::skip_serializing_none;
/// Delivery price data
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct DeliveryPriceData {
/// Date of the delivery price
pub date: String,
/// Delivery price value
pub delivery_price: f64,
}
/// Greeks sub-structure for options
#[skip_serializing_none]
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct Greeks {
/// Delta value
#[serde(skip_serializing_if = "Option::is_none")]
pub delta: Option<f64>,
/// Gamma value
#[serde(skip_serializing_if = "Option::is_none")]
pub gamma: Option<f64>,
/// Vega value
#[serde(skip_serializing_if = "Option::is_none")]
pub vega: Option<f64>,
/// Theta value
#[serde(skip_serializing_if = "Option::is_none")]
pub theta: Option<f64>,
/// Rho value
#[serde(skip_serializing_if = "Option::is_none")]
pub rho: Option<f64>,
}
/// Combined option instrument data with ticker information
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct OptionInstrument {
/// The instrument details
pub instrument: Instrument,
/// Real-time ticker data for the option
pub ticker: TickerData,
}
/// A pair of option instruments representing both call and put options for the same underlying asset
///
/// This structure groups together the call and put options for a specific underlying asset,
/// allowing for easy access to both sides of an option strategy. Both options are optional,
/// meaning you can have just a call, just a put, or both.
///
#[skip_serializing_none]
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct OptionInstrumentPair {
/// Call option instrument data, if available
pub call: Option<OptionInstrument>,
/// Put option instrument data, if available
pub put: Option<OptionInstrument>,
}
impl OptionInstrumentPair {
/// Returns the expiration date and time of the option instrument
///
/// # Returns
///
/// * `Some(DateTime<Utc>)` - The expiration timestamp if available
/// * `None` - If no instrument is available or expiration timestamp is not set
pub fn expiration(&self) -> Option<DateTime<Utc>> {
let expiration_timestamp = match self.instrument() {
Some(i) => i.expiration_timestamp,
None => return None,
};
if let Some(expiration_timestamp) = expiration_timestamp {
Utc.timestamp_millis_opt(expiration_timestamp).single()
} else {
None
}
}
/// Returns the first available instrument from either the call or put option
///
/// # Returns
///
/// * `Some(Instrument)` - The instrument data from call option if available, otherwise from put option
/// * `None` - If neither call nor put options are available
pub fn instrument(&self) -> Option<Instrument> {
self.call
.as_ref()
.map(|i| i.instrument.clone())
.or_else(|| self.put.as_ref().map(|i| i.instrument.clone()))
}
/// Returns the first available ticker data from either the call or put option
///
/// # Returns
///
/// * `Some(TickerData)` - The ticker data from call option if available, otherwise from put option
/// * `None` - If neither call nor put options are available
pub fn ticker(&self) -> Option<TickerData> {
self.call
.as_ref()
.map(|i| i.ticker.clone())
.or_else(|| self.put.as_ref().map(|i| i.ticker.clone()))
}
/// Calculates the total trading volume across both call and put options
///
/// # Returns
///
/// The sum of volumes from both call and put options. Returns 0.0 if no options are available.
pub fn volume(&self) -> f64 {
let mut volume: f64 = 0.0;
if let Some(call) = &self.call {
volume += call.ticker.stats.volume
}
if let Some(put) = &self.put {
volume += put.ticker.stats.volume
}
volume
}
/// Calculates the total open interest across both call and put options
///
/// # Returns
///
/// The sum of open interest from both call and put options. Returns 0.0 if no options are available.
pub fn open_interest(&self) -> f64 {
let mut open_interest: f64 = 0.0;
if let Some(call) = &self.call {
open_interest += call.ticker.open_interest.unwrap_or(0.0)
}
if let Some(put) = &self.put {
open_interest += put.ticker.open_interest.unwrap_or(0.0)
}
open_interest
}
/// Calculates the total interest rate across both call and put options
///
/// # Returns
///
/// The sum of interest rates from both call and put options. Returns 0.0 if no options are available.
pub fn interest_rate(&self) -> f64 {
let mut interest_rate: f64 = 0.0;
if let Some(call) = &self.call {
interest_rate += call.ticker.interest_rate.unwrap_or(0.0)
}
if let Some(put) = &self.put {
interest_rate += put.ticker.interest_rate.unwrap_or(0.0)
}
interest_rate
}
/// Serializes the option instrument pair to a JSON value
///
/// # Returns
///
/// * `Some(Value)` - The serialized JSON representation of this option pair
/// * `None` - If serialization fails
pub fn value(&self) -> Option<Value> {
serde_json::to_value(self).ok()
}
/// Calculates the bid-ask spread for the call option
///
/// # Returns
///
/// A `Spread` struct containing bid, ask, and mid prices for the call option.
/// Returns empty spread (all None values) if no call option is available.
pub fn call_spread(&self) -> Spread {
if let Some(call) = &self.call {
let bid = call.ticker.best_bid_price;
let ask = call.ticker.best_ask_price;
let mid = match (bid, ask) {
(Some(b), Some(a)) => Some((b + a) / 2.0),
(Some(b), None) => Some(b),
(None, Some(a)) => Some(a),
(None, None) => None,
};
Spread { bid, ask, mid }
} else {
Spread {
bid: None,
ask: None,
mid: None,
}
}
}
/// Calculates the bid-ask spread for the put option
///
/// # Returns
///
/// A `Spread` struct containing bid, ask, and mid prices for the put option.
/// Returns empty spread (all None values) if no put option is available.
pub fn put_spread(&self) -> Spread {
if let Some(put) = &self.put {
let bid = put.ticker.best_bid_price;
let ask = put.ticker.best_ask_price;
let mid = match (bid, ask) {
(Some(b), Some(a)) => Some((b + a) / 2.0),
(Some(b), None) => Some(b),
(None, Some(a)) => Some(a),
(None, None) => None,
};
Spread { bid, ask, mid }
} else {
Spread {
bid: None,
ask: None,
mid: None,
}
}
}
/// Returns the implied volatility for both call and put options
///
/// # Returns
///
/// A tuple containing `(call_iv, put_iv)` where each element is the implied volatility
/// for the respective option, or `None` if not available.
pub fn iv(&self) -> (Option<f64>, Option<f64>) {
let call_iv = self.call.as_ref().and_then(|c| c.ticker.mark_iv);
let put_iv = self.put.as_ref().and_then(|p| p.ticker.mark_iv);
(call_iv, put_iv)
}
/// Calculates and returns the basic Greeks for both call and put options
///
/// # Returns
///
/// A `BasicGreeks` struct containing delta values for call and put options,
/// and gamma value (taken from either option if available).
pub fn greeks(&self) -> BasicGreeks {
let delta_call = self
.call
.as_ref()
.and_then(|c| c.ticker.greeks.as_ref().and_then(|g| g.delta));
let delta_put = self
.put
.as_ref()
.and_then(|p| p.ticker.greeks.as_ref().and_then(|g| g.delta));
let gamma = self
.call
.as_ref()
.and_then(|c| c.ticker.greeks.as_ref().and_then(|g| g.gamma))
.or_else(|| {
self.put
.as_ref()
.and_then(|p| p.ticker.greeks.as_ref().and_then(|g| g.gamma))
});
BasicGreeks {
delta_call,
delta_put,
gamma,
}
}
/// Extracts and consolidates all relevant option data into a structured format
///
/// # Returns
///
/// A `BasicOptionData` struct containing comprehensive option information including
/// strike price, bid/ask prices, implied volatility, Greeks, volume, open interest,
/// expiration date, underlying price, risk-free rate, and additional fields.
pub fn data(&self) -> BasicOptionData {
let strike_price: f64 = match self.instrument() {
Some(i) => i.strike.unwrap_or(0.0),
None => 0.0,
};
let call_spread = self.call_spread();
let call_bid: Option<f64> = call_spread.bid;
let call_ask: Option<f64> = call_spread.ask;
let put_spread = self.put_spread();
let put_bid: Option<f64> = put_spread.bid;
let put_ask: Option<f64> = put_spread.ask;
let implied_volatility = self.iv();
let greeks = self.greeks();
let delta_call: Option<f64> = greeks.delta_call;
let delta_put: Option<f64> = greeks.delta_put;
let gamma: Option<f64> = greeks.gamma;
let volume = self.volume();
let open_interest: f64 = self.open_interest();
let expiration_date: Option<DateTime<Utc>> = self.expiration();
let underlying_price: Option<f64> = self.ticker().and_then(|t| t.underlying_price);
let risk_free_rate: f64 = self.interest_rate();
let extra_fields: Option<Value> = self.value();
BasicOptionData {
strike_price,
call_bid,
call_ask,
put_bid,
put_ask,
implied_volatility,
delta_call,
delta_put,
gamma,
volume,
open_interest,
expiration_date,
underlying_price,
risk_free_rate,
extra_fields,
}
}
}
/// Parsed option instrument with ticker data
#[skip_serializing_none]
#[derive(DebugPretty, DisplaySimple, Clone, Serialize)]
pub struct ParsedOptionWithTicker {
/// The instrument name (e.g., "BTC-25DEC21-50000-C")
pub instrument_name: String,
/// Strike price of the option
pub strike: f64,
/// Type of option (Call or Put)
pub option_type: OptionType,
/// Expiry date string
pub expiry: String,
/// Associated ticker data
pub ticker: TickerData,
}
/// Sort direction options
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum SortDirection {
/// Ascending sort order
#[default]
Asc,
/// Descending sort order
Desc,
/// Default sort order (platform-specific)
Default,
}
impl std::fmt::Display for SortDirection {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SortDirection::Asc => write!(f, "asc"),
SortDirection::Desc => write!(f, "desc"),
SortDirection::Default => write!(f, "default"),
}
}
}