nautilus_model/python/instruments/
currency_pair.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use 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 = (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))]
38    fn py_new(
39        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        lot_size: Option<Quantity>,
50        max_quantity: Option<Quantity>,
51        min_quantity: Option<Quantity>,
52        max_notional: Option<Money>,
53        min_notional: Option<Money>,
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    ) -> PyResult<Self> {
61        Self::new_checked(
62            id,
63            raw_symbol,
64            base_currency,
65            quote_currency,
66            price_precision,
67            size_precision,
68            price_increment,
69            size_increment,
70            lot_size,
71            max_quantity,
72            min_quantity,
73            max_notional,
74            min_notional,
75            max_price,
76            min_price,
77            margin_init,
78            margin_maint,
79            maker_fee,
80            taker_fee,
81            ts_event.into(),
82            ts_init.into(),
83        )
84        .map_err(to_pyvalue_err)
85    }
86
87    fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
88        match op {
89            CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
90            CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
91            _ => py.NotImplemented(),
92        }
93    }
94
95    fn __hash__(&self) -> isize {
96        let mut hasher = DefaultHasher::new();
97        self.hash(&mut hasher);
98        hasher.finish() as isize
99    }
100
101    #[getter]
102    fn type_str(&self) -> &str {
103        stringify!(CurrencyPair)
104    }
105
106    #[getter]
107    #[pyo3(name = "id")]
108    fn py_id(&self) -> InstrumentId {
109        self.id
110    }
111
112    #[getter]
113    #[pyo3(name = "raw_symbol")]
114    fn py_raw_symbol(&self) -> Symbol {
115        self.raw_symbol
116    }
117
118    #[getter]
119    #[pyo3(name = "base_currency")]
120    fn py_base_currency(&self) -> Currency {
121        self.base_currency
122    }
123
124    #[getter]
125    #[pyo3(name = "quote_currency")]
126    fn py_quote_currency(&self) -> Currency {
127        self.quote_currency
128    }
129
130    #[getter]
131    #[pyo3(name = "price_precision")]
132    fn py_price_precision(&self) -> u8 {
133        self.price_precision
134    }
135
136    #[getter]
137    #[pyo3(name = "size_precision")]
138    fn py_size_precision(&self) -> u8 {
139        self.size_precision
140    }
141
142    #[getter]
143    #[pyo3(name = "price_increment")]
144    fn py_price_increment(&self) -> Price {
145        self.price_increment
146    }
147
148    #[getter]
149    #[pyo3(name = "size_increment")]
150    fn py_size_increment(&self) -> Quantity {
151        self.size_increment
152    }
153
154    #[getter]
155    #[pyo3(name = "lot_size")]
156    fn py_lot_size(&self) -> Option<Quantity> {
157        self.lot_size
158    }
159
160    #[getter]
161    #[pyo3(name = "max_quantity")]
162    fn py_max_quantity(&self) -> Option<Quantity> {
163        self.max_quantity
164    }
165
166    #[getter]
167    #[pyo3(name = "min_quantity")]
168    fn py_min_quantity(&self) -> Option<Quantity> {
169        self.min_quantity
170    }
171
172    #[getter]
173    #[pyo3(name = "max_notional")]
174    fn py_max_notional(&self) -> Option<Money> {
175        self.max_notional
176    }
177
178    #[getter]
179    #[pyo3(name = "min_notional")]
180    fn py_min_notional(&self) -> Option<Money> {
181        self.min_notional
182    }
183
184    #[getter]
185    #[pyo3(name = "max_price")]
186    fn py_max_price(&self) -> Option<Price> {
187        self.max_price
188    }
189
190    #[getter]
191    #[pyo3(name = "min_price")]
192    fn py_min_price(&self) -> Option<Price> {
193        self.min_price
194    }
195
196    #[getter]
197    #[pyo3(name = "maker_fee")]
198    fn py_maker_fee(&self) -> Decimal {
199        self.maker_fee
200    }
201
202    #[getter]
203    #[pyo3(name = "taker_fee")]
204    fn py_taker_fee(&self) -> Decimal {
205        self.taker_fee
206    }
207
208    #[getter]
209    #[pyo3(name = "margin_maint")]
210    fn py_margin_maint(&self) -> Decimal {
211        self.margin_maint
212    }
213
214    #[getter]
215    #[pyo3(name = "margin_init")]
216    fn py_margin_init(&self) -> Decimal {
217        self.margin_init
218    }
219
220    #[getter]
221    #[pyo3(name = "ts_event")]
222    fn py_ts_event(&self) -> u64 {
223        self.ts_event.as_u64()
224    }
225
226    #[getter]
227    #[pyo3(name = "ts_init")]
228    fn py_ts_init(&self) -> u64 {
229        self.ts_init.as_u64()
230    }
231
232    #[getter]
233    #[pyo3(name = "info")]
234    fn py_info(&self, py: Python<'_>) -> PyResult<PyObject> {
235        Ok(PyDict::new(py).into())
236    }
237
238    #[staticmethod]
239    #[pyo3(name = "from_dict")]
240    fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
241        from_dict_pyo3(py, values)
242    }
243
244    #[pyo3(name = "to_dict")]
245    fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
246        let dict = PyDict::new(py);
247        dict.set_item("type", stringify!(CurrencyPair))?;
248        dict.set_item("id", self.id.to_string())?;
249        dict.set_item("raw_symbol", self.raw_symbol.to_string())?;
250        dict.set_item("base_currency", self.base_currency.code.to_string())?;
251        dict.set_item("quote_currency", self.quote_currency.code.to_string())?;
252        dict.set_item("price_precision", self.price_precision)?;
253        dict.set_item("size_precision", self.size_precision)?;
254        dict.set_item("price_increment", self.price_increment.to_string())?;
255        dict.set_item("size_increment", self.size_increment.to_string())?;
256        dict.set_item("maker_fee", self.maker_fee.to_string())?;
257        dict.set_item("taker_fee", self.taker_fee.to_string())?;
258        dict.set_item("margin_init", self.margin_init.to_string())?;
259        dict.set_item("margin_maint", self.margin_maint.to_string())?;
260        dict.set_item("info", PyDict::new(py))?;
261        dict.set_item("ts_event", self.ts_event.as_u64())?;
262        dict.set_item("ts_init", self.ts_init.as_u64())?;
263        match self.lot_size {
264            Some(value) => dict.set_item("lot_size", value.to_string())?,
265            None => dict.set_item("lot_size", py.None())?,
266        }
267        match self.max_quantity {
268            Some(value) => dict.set_item("max_quantity", value.to_string())?,
269            None => dict.set_item("max_quantity", py.None())?,
270        }
271        match self.min_quantity {
272            Some(value) => dict.set_item("min_quantity", value.to_string())?,
273            None => dict.set_item("min_quantity", py.None())?,
274        }
275        match self.max_notional {
276            Some(value) => dict.set_item("max_notional", value.to_string())?,
277            None => dict.set_item("max_notional", py.None())?,
278        }
279        match self.min_notional {
280            Some(value) => dict.set_item("min_notional", value.to_string())?,
281            None => dict.set_item("min_notional", 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        Ok(dict.into())
292    }
293}