finance_query/models/options/
contract.rs

1use serde::{Deserialize, Serialize};
2use std::ops::Deref;
3
4/// A collection of option contracts with DataFrame support.
5///
6/// This wrapper allows `options.calls.to_dataframe()` syntax while still
7/// acting like a `Vec<OptionContract>` for iteration, indexing, etc.
8#[derive(Debug, Clone, Default, Serialize, Deserialize)]
9#[serde(transparent)]
10pub struct Contracts(pub Vec<OptionContract>);
11
12impl Deref for Contracts {
13    type Target = Vec<OptionContract>;
14
15    fn deref(&self) -> &Self::Target {
16        &self.0
17    }
18}
19
20impl IntoIterator for Contracts {
21    type Item = OptionContract;
22    type IntoIter = std::vec::IntoIter<OptionContract>;
23
24    fn into_iter(self) -> Self::IntoIter {
25        self.0.into_iter()
26    }
27}
28
29impl<'a> IntoIterator for &'a Contracts {
30    type Item = &'a OptionContract;
31    type IntoIter = std::slice::Iter<'a, OptionContract>;
32
33    fn into_iter(self) -> Self::IntoIter {
34        self.0.iter()
35    }
36}
37
38#[cfg(feature = "dataframe")]
39impl Contracts {
40    /// Converts the contracts to a polars DataFrame.
41    pub fn to_dataframe(&self) -> ::polars::prelude::PolarsResult<::polars::prelude::DataFrame> {
42        OptionContract::vec_to_dataframe(&self.0)
43    }
44}
45
46/// An options contract (call or put)
47///
48/// Note: This struct cannot be manually constructed - obtain via `Ticker::options()`.
49#[non_exhaustive]
50#[derive(Debug, Clone, Serialize, Deserialize)]
51#[cfg_attr(feature = "dataframe", derive(crate::ToDataFrame))]
52#[serde(rename_all = "camelCase")]
53pub struct OptionContract {
54    /// Contract symbol (e.g., "AAPL250117C00150000")
55    pub contract_symbol: String,
56
57    /// Strike price
58    pub strike: f64,
59
60    /// Contract currency
61    pub currency: Option<String>,
62
63    /// Last trade price
64    pub last_price: Option<f64>,
65
66    /// Price change
67    pub change: Option<f64>,
68
69    /// Percent change
70    pub percent_change: Option<f64>,
71
72    /// Trading volume
73    pub volume: Option<i64>,
74
75    /// Open interest
76    pub open_interest: Option<i64>,
77
78    /// Bid price
79    pub bid: Option<f64>,
80
81    /// Ask price
82    pub ask: Option<f64>,
83
84    /// Contract size (usually 100)
85    pub contract_size: Option<String>,
86
87    /// Expiration date (Unix timestamp)
88    pub expiration: Option<i64>,
89
90    /// Last trade date (Unix timestamp)
91    pub last_trade_date: Option<i64>,
92
93    /// Implied volatility
94    pub implied_volatility: Option<f64>,
95
96    /// Whether the option is in the money
97    pub in_the_money: Option<bool>,
98}