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