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