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