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