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