nautilus_model/python/instruments/
futures_spread.rs1use std::{
17 collections::hash_map::DefaultHasher,
18 hash::{Hash, Hasher},
19};
20
21use nautilus_core::python::{
22 IntoPyObjectNautilusExt, serialization::from_dict_pyo3, to_pyvalue_err,
23};
24use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
25use rust_decimal::Decimal;
26use ustr::Ustr;
27
28use crate::{
29 enums::AssetClass,
30 identifiers::{InstrumentId, Symbol},
31 instruments::FuturesSpread,
32 types::{Currency, Price, Quantity},
33};
34
35#[pymethods]
36impl FuturesSpread {
37 #[allow(clippy::too_many_arguments)]
38 #[new]
39 #[pyo3(signature = (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))]
40 fn py_new(
41 id: InstrumentId,
42 raw_symbol: Symbol,
43 asset_class: AssetClass,
44 underlying: String,
45 strategy_type: String,
46 activation_ns: u64,
47 expiration_ns: u64,
48 currency: Currency,
49 price_precision: u8,
50 price_increment: Price,
51 multiplier: Quantity,
52 lot_size: Quantity,
53 ts_event: u64,
54 ts_init: u64,
55 max_quantity: Option<Quantity>,
56 min_quantity: Option<Quantity>,
57 max_price: Option<Price>,
58 min_price: Option<Price>,
59 margin_init: Option<Decimal>,
60 margin_maint: Option<Decimal>,
61 maker_fee: Option<Decimal>,
62 taker_fee: Option<Decimal>,
63 exchange: Option<String>,
64 ) -> PyResult<Self> {
65 Self::new_checked(
66 id,
67 raw_symbol,
68 asset_class,
69 exchange.map(|x| Ustr::from(&x)),
70 underlying.into(),
71 strategy_type.into(),
72 activation_ns.into(),
73 expiration_ns.into(),
74 currency,
75 price_precision,
76 price_increment,
77 multiplier,
78 lot_size,
79 max_quantity,
80 min_quantity,
81 max_price,
82 min_price,
83 margin_init,
84 margin_maint,
85 maker_fee,
86 taker_fee,
87 ts_event.into(),
88 ts_init.into(),
89 )
90 .map_err(to_pyvalue_err)
91 }
92
93 fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
94 match op {
95 CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
96 CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
97 _ => py.NotImplemented(),
98 }
99 }
100
101 fn __hash__(&self) -> isize {
102 let mut hasher = DefaultHasher::new();
103 self.hash(&mut hasher);
104 hasher.finish() as isize
105 }
106
107 #[getter]
108 fn type_str(&self) -> &str {
109 stringify!(FuturesSpread)
110 }
111
112 #[getter]
113 #[pyo3(name = "id")]
114 fn py_id(&self) -> InstrumentId {
115 self.id
116 }
117
118 #[getter]
119 #[pyo3(name = "raw_symbol")]
120 fn py_raw_symbol(&self) -> Symbol {
121 self.raw_symbol
122 }
123
124 #[getter]
125 #[pyo3(name = "asset_class")]
126 fn py_asset_class(&self) -> AssetClass {
127 self.asset_class
128 }
129
130 #[getter]
131 #[pyo3(name = "exchange")]
132 fn py_exchange(&self) -> Option<String> {
133 self.exchange.map(|e| e.to_string())
134 }
135
136 #[getter]
137 #[pyo3(name = "underlying")]
138 fn py_underlying(&self) -> &str {
139 self.underlying.as_str()
140 }
141
142 #[getter]
143 #[pyo3(name = "strategy_type")]
144 fn py_strategy_type(&self) -> &str {
145 self.strategy_type.as_str()
146 }
147
148 #[getter]
149 #[pyo3(name = "activation_ns")]
150 fn py_activation_ns(&self) -> u64 {
151 self.activation_ns.as_u64()
152 }
153
154 #[getter]
155 #[pyo3(name = "expiration_ns")]
156 fn py_expiration_ns(&self) -> u64 {
157 self.expiration_ns.as_u64()
158 }
159
160 #[getter]
161 #[pyo3(name = "currency")]
162 fn py_currency(&self) -> Currency {
163 self.currency
164 }
165
166 #[getter]
167 #[pyo3(name = "price_precision")]
168 fn py_price_precision(&self) -> u8 {
169 self.price_precision
170 }
171
172 #[getter]
173 #[pyo3(name = "price_increment")]
174 fn py_price_increment(&self) -> Price {
175 self.price_increment
176 }
177
178 #[getter]
179 #[pyo3(name = "size_increment")]
180 fn py_size_increment(&self) -> Quantity {
181 self.size_increment
182 }
183
184 #[getter]
185 #[pyo3(name = "size_precision")]
186 fn py_size_precision(&self) -> u8 {
187 self.size_precision
188 }
189
190 #[getter]
191 #[pyo3(name = "multiplier")]
192 fn py_multiplier(&self) -> Quantity {
193 self.multiplier
194 }
195
196 #[getter]
197 #[pyo3(name = "lot_size")]
198 fn py_lot_size(&self) -> Quantity {
199 self.lot_size
200 }
201
202 #[getter]
203 #[pyo3(name = "max_quantity")]
204 fn py_max_quantity(&self) -> Option<Quantity> {
205 self.max_quantity
206 }
207
208 #[getter]
209 #[pyo3(name = "min_quantity")]
210 fn py_min_quantity(&self) -> Option<Quantity> {
211 self.min_quantity
212 }
213
214 #[getter]
215 #[pyo3(name = "max_price")]
216 fn py_max_price(&self) -> Option<Price> {
217 self.max_price
218 }
219
220 #[getter]
221 #[pyo3(name = "min_price")]
222 fn py_min_price(&self) -> Option<Price> {
223 self.min_price
224 }
225
226 #[getter]
227 #[pyo3(name = "margin_init")]
228 fn py_margin_init(&self) -> Decimal {
229 self.margin_init
230 }
231
232 #[getter]
233 #[pyo3(name = "margin_maint")]
234 fn py_margin_maint(&self) -> Decimal {
235 self.margin_maint
236 }
237
238 #[getter]
239 #[pyo3(name = "info")]
240 fn py_info(&self, py: Python<'_>) -> PyResult<PyObject> {
241 Ok(PyDict::new(py).into())
242 }
243
244 #[getter]
245 #[pyo3(name = "ts_event")]
246 fn py_ts_event(&self) -> u64 {
247 self.ts_event.as_u64()
248 }
249
250 #[getter]
251 #[pyo3(name = "ts_init")]
252 fn py_ts_init(&self) -> u64 {
253 self.ts_init.as_u64()
254 }
255
256 #[staticmethod]
257 #[pyo3(name = "from_dict")]
258 fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
259 from_dict_pyo3(py, values)
260 }
261
262 #[pyo3(name = "to_dict")]
263 fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
264 let dict = PyDict::new(py);
265 dict.set_item("type", stringify!(FuturesSpread))?;
266 dict.set_item("id", self.id.to_string())?;
267 dict.set_item("raw_symbol", self.raw_symbol.to_string())?;
268 dict.set_item("asset_class", self.asset_class.to_string())?;
269 dict.set_item("underlying", self.underlying.to_string())?;
270 dict.set_item("strategy_type", self.strategy_type.to_string())?;
271 dict.set_item("activation_ns", self.activation_ns.as_u64())?;
272 dict.set_item("expiration_ns", self.expiration_ns.as_u64())?;
273 dict.set_item("currency", self.currency.code.to_string())?;
274 dict.set_item("price_precision", self.price_precision)?;
275 dict.set_item("price_increment", self.price_increment.to_string())?;
276 dict.set_item("size_increment", self.size_increment.to_string())?;
277 dict.set_item("size_precision", self.size_precision)?;
278 dict.set_item("multiplier", self.multiplier.to_string())?;
279 dict.set_item("lot_size", self.lot_size.to_string())?;
280 dict.set_item("margin_init", self.margin_init.to_string())?;
281 dict.set_item("margin_maint", self.margin_maint.to_string())?;
282 dict.set_item("maker_fee", self.maker_fee.to_string())?;
283 dict.set_item("taker_fee", self.taker_fee.to_string())?;
284 dict.set_item("info", PyDict::new(py))?;
285 dict.set_item("ts_event", self.ts_event.as_u64())?;
286 dict.set_item("ts_init", self.ts_init.as_u64())?;
287 match self.max_quantity {
288 Some(value) => dict.set_item("max_quantity", value.to_string())?,
289 None => dict.set_item("max_quantity", py.None())?,
290 }
291 match self.min_quantity {
292 Some(value) => dict.set_item("min_quantity", value.to_string())?,
293 None => dict.set_item("min_quantity", py.None())?,
294 }
295 match self.max_price {
296 Some(value) => dict.set_item("max_price", value.to_string())?,
297 None => dict.set_item("max_price", py.None())?,
298 }
299 match self.min_price {
300 Some(value) => dict.set_item("min_price", value.to_string())?,
301 None => dict.set_item("min_price", py.None())?,
302 }
303 match self.exchange {
304 Some(value) => dict.set_item("exchange", value.to_string())?,
305 None => dict.set_item("exchange", py.None())?,
306 }
307 Ok(dict.into())
308 }
309}