nautilus_model/python/instruments/
option_contract.rs1use std::{
17 collections::hash_map::DefaultHasher,
18 hash::{Hash, Hasher},
19};
20
21use nautilus_core::{
22 from_pydict,
23 python::{IntoPyObjectNautilusExt, to_pyvalue_err},
24};
25use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
26use rust_decimal::Decimal;
27use ustr::Ustr;
28
29use crate::{
30 enums::{AssetClass, OptionKind},
31 identifiers::{InstrumentId, Symbol},
32 instruments::OptionContract,
33 types::{Currency, Price, Quantity},
34};
35
36#[pymethods]
37#[pyo3_stub_gen::derive::gen_stub_pymethods]
38impl OptionContract {
39 #[expect(clippy::too_many_arguments)]
41 #[new]
42 #[pyo3(signature = (instrument_id, raw_symbol, asset_class, underlying, option_kind, strike_price, currency, activation_ns, expiration_ns, price_precision, price_increment, multiplier, lot_size, ts_event, ts_init, max_quantity=None, min_quantity=None, max_price=None, min_price=None, margin_init=None, margin_maint=None, exchange=None, tick_scheme=None, info=None))]
43 fn py_new(
44 instrument_id: InstrumentId,
45 raw_symbol: Symbol,
46 asset_class: AssetClass,
47 underlying: String,
48 option_kind: OptionKind,
49 strike_price: Price,
50 currency: Currency,
51 activation_ns: u64,
52 expiration_ns: u64,
53 price_precision: u8,
54 price_increment: Price,
55 multiplier: Quantity,
56 lot_size: Quantity,
57 ts_event: u64,
58 ts_init: u64,
59 max_quantity: Option<Quantity>,
60 min_quantity: Option<Quantity>,
61 max_price: Option<Price>,
62 min_price: Option<Price>,
63 margin_init: Option<Decimal>,
64 margin_maint: Option<Decimal>,
65 exchange: Option<String>,
66 tick_scheme: Option<String>,
67 info: Option<Py<PyDict>>,
68 ) -> PyResult<Self> {
69 let info_map = if let Some(info_dict) = info {
71 Python::attach(|py| from_pydict(py, &info_dict))?
72 } else {
73 None
74 };
75
76 Self::builder()
77 .instrument_id(instrument_id)
78 .raw_symbol(raw_symbol)
79 .asset_class(asset_class)
80 .maybe_exchange(exchange.map(|x| Ustr::from(&x)))
81 .underlying(underlying.into())
82 .option_kind(option_kind)
83 .strike_price(strike_price)
84 .currency(currency)
85 .activation_ns(activation_ns.into())
86 .expiration_ns(expiration_ns.into())
87 .price_precision(price_precision)
88 .price_increment(price_increment)
89 .multiplier(multiplier)
90 .lot_size(lot_size)
91 .maybe_max_quantity(max_quantity)
92 .maybe_min_quantity(min_quantity)
93 .maybe_max_price(max_price)
94 .maybe_min_price(min_price)
95 .maybe_margin_init(margin_init)
96 .maybe_margin_maint(margin_maint)
97 .maybe_tick_scheme(tick_scheme.map(|name| ustr::Ustr::from(name.as_str())))
98 .maybe_info(info_map)
99 .ts_event(ts_event.into())
100 .ts_init(ts_init.into())
101 .build()
102 .map_err(to_pyvalue_err)
103 }
104
105 fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
106 match op {
107 CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
108 CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
109 _ => py.NotImplemented(),
110 }
111 }
112
113 fn __hash__(&self) -> isize {
114 let mut hasher = DefaultHasher::new();
115 self.hash(&mut hasher);
116 hasher.finish() as isize
117 }
118
119 #[getter]
120 fn type_name(&self) -> &'static str {
121 stringify!(OptionContract)
122 }
123
124 #[getter]
125 #[pyo3(name = "id")]
126 fn py_id(&self) -> InstrumentId {
127 self.id
128 }
129
130 #[getter]
131 #[pyo3(name = "raw_symbol")]
132 fn py_raw_symbol(&self) -> Symbol {
133 self.raw_symbol
134 }
135
136 #[getter]
137 #[pyo3(name = "asset_class")]
138 fn py_asset_class(&self) -> AssetClass {
139 self.asset_class
140 }
141
142 #[getter]
143 #[pyo3(name = "exchange")]
144 fn py_exchange(&self) -> Option<String> {
145 self.exchange.map(|e| e.to_string())
146 }
147
148 #[getter]
149 #[pyo3(name = "underlying")]
150 fn py_underlying(&self) -> &str {
151 self.underlying.as_str()
152 }
153
154 #[getter]
155 #[pyo3(name = "option_kind")]
156 fn py_option_kind(&self) -> OptionKind {
157 self.option_kind
158 }
159
160 #[getter]
161 #[pyo3(name = "activation_ns")]
162 fn py_activation_ns(&self) -> u64 {
163 self.activation_ns.as_u64()
164 }
165
166 #[getter]
167 #[pyo3(name = "expiration_ns")]
168 fn py_expiration_ns(&self) -> u64 {
169 self.expiration_ns.as_u64()
170 }
171
172 #[getter]
173 #[pyo3(name = "strike_price")]
174 fn py_strike_price(&self) -> Price {
175 self.strike_price
176 }
177
178 #[getter]
179 #[pyo3(name = "currency")]
180 fn py_currency(&self) -> Currency {
181 self.currency
182 }
183
184 #[getter]
185 #[pyo3(name = "price_precision")]
186 fn py_price_precision(&self) -> u8 {
187 self.price_precision
188 }
189
190 #[getter]
191 #[pyo3(name = "price_increment")]
192 fn py_price_increment(&self) -> Price {
193 self.price_increment
194 }
195
196 #[getter]
197 #[pyo3(name = "size_increment")]
198 fn py_size_increment(&self) -> Quantity {
199 self.size_increment
200 }
201
202 #[getter]
203 #[pyo3(name = "size_precision")]
204 fn py_size_precision(&self) -> u8 {
205 self.size_precision
206 }
207
208 #[getter]
209 #[pyo3(name = "multiplier")]
210 fn py_multiplier(&self) -> Quantity {
211 self.multiplier
212 }
213
214 #[getter]
215 #[pyo3(name = "lot_size")]
216 fn py_lot_size(&self) -> Quantity {
217 self.lot_size
218 }
219
220 #[getter]
221 #[pyo3(name = "max_quantity")]
222 fn py_max_quantity(&self) -> Option<Quantity> {
223 self.max_quantity
224 }
225
226 #[getter]
227 #[pyo3(name = "min_quantity")]
228 fn py_min_quantity(&self) -> Option<Quantity> {
229 self.min_quantity
230 }
231
232 #[getter]
233 #[pyo3(name = "max_price")]
234 fn py_max_price(&self) -> Option<Price> {
235 self.max_price
236 }
237
238 #[getter]
239 #[pyo3(name = "min_price")]
240 fn py_min_price(&self) -> Option<Price> {
241 self.min_price
242 }
243
244 #[getter]
245 #[pyo3(name = "margin_init")]
246 fn py_margin_init(&self) -> Decimal {
247 self.margin_init
248 }
249
250 #[getter]
251 #[pyo3(name = "margin_maint")]
252 fn py_margin_maint(&self) -> Decimal {
253 self.margin_maint
254 }
255
256 #[getter]
257 #[pyo3(name = "info")]
258 fn py_info(&self, py: Python<'_>) -> PyResult<Py<PyDict>> {
259 if let Some(ref info_map) = self.info {
261 let py_dict = PyDict::new(py);
262
263 for (key, value) in info_map {
264 let json_str = serde_json::to_string(value).map_err(to_pyvalue_err)?;
266 let py_value =
267 PyModule::import(py, "json")?.call_method("loads", (json_str,), None)?;
268 py_dict.set_item(key, py_value)?;
269 }
270 Ok(py_dict.unbind())
271 } else {
272 Ok(PyDict::new(py).unbind())
273 }
274 }
275
276 #[getter]
277 #[pyo3(name = "ts_event")]
278 fn py_ts_event(&self) -> u64 {
279 self.ts_event.as_u64()
280 }
281
282 #[getter]
283 #[pyo3(name = "ts_init")]
284 fn py_ts_init(&self) -> u64 {
285 self.ts_init.as_u64()
286 }
287
288 #[staticmethod]
289 #[pyo3(name = "from_dict")]
290 fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
291 crate::python::instruments::from_dict_instrument_pyo3(py, values)
292 }
293
294 #[pyo3(name = "to_dict")]
295 fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
296 let dict = PyDict::new(py);
297 dict.set_item("type", stringify!(OptionContract))?;
298 dict.set_item("id", self.id.to_string())?;
299 dict.set_item("raw_symbol", self.raw_symbol.to_string())?;
300 dict.set_item("asset_class", self.asset_class.to_string())?;
301 dict.set_item("underlying", self.underlying.to_string())?;
302 dict.set_item("option_kind", self.option_kind.to_string())?;
303 dict.set_item("activation_ns", self.activation_ns.as_u64())?;
304 dict.set_item("expiration_ns", self.expiration_ns.as_u64())?;
305 dict.set_item("strike_price", self.strike_price.to_string())?;
306 dict.set_item("currency", self.currency.code.to_string())?;
307 dict.set_item("price_precision", self.price_precision)?;
308 dict.set_item("price_increment", self.price_increment.to_string())?;
309 dict.set_item("size_increment", self.size_increment.to_string())?;
310 dict.set_item("size_precision", self.size_precision)?;
311 dict.set_item("multiplier", self.multiplier.to_string())?;
312 dict.set_item("lot_size", self.lot_size.to_string())?;
313 dict.set_item("margin_init", self.margin_init.to_string())?;
314 dict.set_item("margin_maint", self.margin_maint.to_string())?;
315 if let Some(ref info_map) = self.info {
317 let info_dict = PyDict::new(py);
318
319 for (key, value) in info_map {
320 let json_str = serde_json::to_string(value).map_err(to_pyvalue_err)?;
321 let py_value =
322 PyModule::import(py, "json")?.call_method("loads", (json_str,), None)?;
323 info_dict.set_item(key, py_value)?;
324 }
325 dict.set_item("info", info_dict)?;
326 } else {
327 dict.set_item("info", PyDict::new(py))?;
328 }
329 dict.set_item("ts_event", self.ts_event.as_u64())?;
330 dict.set_item("ts_init", self.ts_init.as_u64())?;
331 match self.max_quantity {
332 Some(value) => dict.set_item("max_quantity", value.to_string())?,
333 None => dict.set_item("max_quantity", py.None())?,
334 }
335
336 match self.min_quantity {
337 Some(value) => dict.set_item("min_quantity", value.to_string())?,
338 None => dict.set_item("min_quantity", py.None())?,
339 }
340
341 match self.max_price {
342 Some(value) => dict.set_item("max_price", value.to_string())?,
343 None => dict.set_item("max_price", py.None())?,
344 }
345
346 match self.min_price {
347 Some(value) => dict.set_item("min_price", value.to_string())?,
348 None => dict.set_item("min_price", py.None())?,
349 }
350
351 match self.exchange {
352 Some(value) => dict.set_item("exchange", value.to_string())?,
353 None => dict.set_item("exchange", py.None())?,
354 }
355 dict.set_item(
356 "tick_scheme",
357 crate::python::instruments::tick_scheme_to_py(self),
358 )?;
359 Ok(dict.into())
360 }
361}