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