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