deribit_http/model/
option.rs

1/******************************************************************************
2   Author: Joaquín Béjar García
3   Email: jb@taunais.com
4   Date: 20/9/25
5******************************************************************************/
6use crate::HttpError;
7use crate::prelude::OptionType;
8use chrono::{DateTime, Utc};
9use pretty_simple_display::{DebugPretty, DisplaySimple};
10use serde::{Deserialize, Serialize};
11use serde_json::Value;
12
13/// Option contract information structure
14///
15/// Contains all the essential information about an option contract including
16/// the underlying symbol, option type, strike price, and expiration date.
17#[derive(DebugPretty, DisplaySimple, Clone, PartialEq, Serialize, Deserialize)]
18pub struct OptionInfo {
19    /// The underlying asset symbol (e.g., "BTC", "ETH")
20    pub symbol: String,
21    /// The type of option (Call or Put)
22    pub option_type: OptionType,
23    /// The strike price of the option contract
24    pub strike_price: f64,
25    /// The expiration date in DDMMMYY format (e.g., "28NOV25")
26    pub expiration_date: String,
27}
28
29impl OptionInfo {
30    /// Parses an option string in the format: "SYMBOL-DDMMMYY-STRIKE-TYPE"
31    /// Example: "BTC-28NOV25-108000-P"
32    pub fn parse_from_string(option_string: &str) -> Result<Self, HttpError> {
33        let parts: Vec<&str> = option_string.split('-').collect();
34
35        // Validate we have exactly 4 parts
36        if parts.len() != 4 {
37            return Err(HttpError::ParseError("InvalidFormat".to_string()));
38        }
39
40        let symbol = parts[0].to_string();
41        let expiration_date = parts[1].to_string();
42
43        // Parse strike price
44        let strike_price = parts[2]
45            .parse::<f64>()
46            .map_err(|_| HttpError::ParseError("InvalidStrikePrice".to_string()))?;
47
48        // Parse option type
49        let option_type = match parts[3].to_uppercase().as_str() {
50            "C" => OptionType::Call,
51            "P" => OptionType::Put,
52            _ => return Err(HttpError::ParseError("InvalidOptionType".to_string())),
53        };
54
55        // Basic validation for expiration date format (DDMMMYY)
56        if expiration_date.len() != 7 {
57            return Err(HttpError::ParseError("InvalidExpirationDate".to_string()));
58        };
59
60        Ok(OptionInfo {
61            symbol,
62            option_type,
63            strike_price,
64            expiration_date,
65        })
66    }
67}
68
69/// Spread information for bid/ask prices
70#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
71pub struct Spread {
72    /// Best bid price
73    pub bid: Option<f64>,
74    /// Best ask price
75    pub ask: Option<f64>,
76    /// Mid price (average of bid and ask)
77    pub mid: Option<f64>,
78}
79
80/// Basic Greeks values for option pricing
81#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
82pub struct BasicGreeks {
83    /// Delta value for call option
84    pub delta_call: Option<f64>,
85    /// Delta value for put option
86    pub delta_put: Option<f64>,
87    /// Gamma value (rate of change of delta)
88    pub gamma: Option<f64>,
89}
90
91/// Comprehensive option data structure containing all relevant pricing and risk information
92#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
93pub struct BasicOptionData {
94    /// Strike price of the option
95    pub strike_price: f64,
96    /// Best bid price for call option
97    pub call_bid: Option<f64>,
98    /// Best ask price for call option
99    pub call_ask: Option<f64>,
100    /// Best bid price for put option
101    pub put_bid: Option<f64>,
102    /// Best ask price for put option
103    pub put_ask: Option<f64>,
104    /// Implied volatility for call and put options (call_iv, put_iv)
105    pub implied_volatility: (Option<f64>, Option<f64>),
106    /// Delta value for call option
107    pub delta_call: Option<f64>,
108    /// Delta value for put option
109    pub delta_put: Option<f64>,
110    /// Gamma value (rate of change of delta)
111    pub gamma: Option<f64>,
112    /// Total trading volume
113    pub volume: f64,
114    /// Total open interest
115    pub open_interest: f64,
116    /// Option expiration date
117    pub expiration_date: Option<DateTime<Utc>>,
118    /// Current price of the underlying asset
119    pub underlying_price: Option<f64>,
120    /// Risk-free interest rate
121    pub risk_free_rate: f64,
122    /// Additional fields as JSON value
123    pub extra_fields: Option<Value>,
124}