nautilus_model/python/instruments/
betting.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;
26use ustr::Ustr;
27
28use crate::{
29    enums::{AssetClass, InstrumentClass},
30    identifiers::{InstrumentId, Symbol},
31    instruments::BettingInstrument,
32    types::{Currency, Money, Price, Quantity},
33};
34
35#[pymethods]
36impl BettingInstrument {
37    #[allow(clippy::too_many_arguments)]
38    #[new]
39    #[pyo3(signature = (id, raw_symbol, event_type_id, event_type_name, competition_id, competition_name, event_id, event_name, event_country_code, event_open_date, betting_type, market_id, market_name, market_type, market_start_time, selection_id, selection_name, selection_handicap, currency, price_precision, size_precision, price_increment, size_increment, ts_event, ts_init, 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))]
40    fn py_new(
41        id: InstrumentId,
42        raw_symbol: Symbol,
43        event_type_id: u64,
44        event_type_name: String,
45        competition_id: u64,
46        competition_name: String,
47        event_id: u64,
48        event_name: String,
49        event_country_code: String,
50        event_open_date: u64,
51        betting_type: String,
52        market_id: String,
53        market_name: String,
54        market_type: String,
55        market_start_time: u64,
56        selection_id: u64,
57        selection_name: String,
58        selection_handicap: f64,
59        currency: Currency,
60        price_precision: u8,
61        size_precision: u8,
62        price_increment: Price,
63        size_increment: Quantity,
64        ts_event: u64,
65        ts_init: u64,
66        max_quantity: Option<Quantity>,
67        min_quantity: Option<Quantity>,
68        max_notional: Option<Money>,
69        min_notional: Option<Money>,
70        max_price: Option<Price>,
71        min_price: Option<Price>,
72        margin_init: Option<Decimal>,
73        margin_maint: Option<Decimal>,
74        maker_fee: Option<Decimal>,
75        taker_fee: Option<Decimal>,
76    ) -> PyResult<Self> {
77        Self::new_checked(
78            id,
79            raw_symbol,
80            event_type_id,
81            Ustr::from(&event_type_name),
82            competition_id,
83            Ustr::from(&competition_name),
84            event_id,
85            Ustr::from(&event_name),
86            Ustr::from(&event_country_code),
87            event_open_date.into(),
88            Ustr::from(&betting_type),
89            Ustr::from(&market_id),
90            Ustr::from(&market_name),
91            Ustr::from(&market_type),
92            market_start_time.into(),
93            selection_id,
94            Ustr::from(&selection_name),
95            selection_handicap,
96            currency,
97            price_precision,
98            size_precision,
99            price_increment,
100            size_increment,
101            max_quantity,
102            min_quantity,
103            max_notional,
104            min_notional,
105            max_price,
106            min_price,
107            margin_init,
108            margin_maint,
109            maker_fee,
110            taker_fee,
111            ts_event.into(),
112            ts_init.into(),
113        )
114        .map_err(to_pyvalue_err)
115    }
116
117    fn __hash__(&self) -> isize {
118        let mut hasher = DefaultHasher::new();
119        self.hash(&mut hasher);
120        hasher.finish() as isize
121    }
122
123    fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
124        match op {
125            CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
126            CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
127            _ => py.NotImplemented(),
128        }
129    }
130
131    #[getter]
132    fn type_str(&self) -> &str {
133        stringify!(BettingInstrument)
134    }
135
136    #[getter]
137    #[pyo3(name = "id")]
138    fn py_id(&self) -> InstrumentId {
139        self.id
140    }
141
142    #[getter]
143    #[pyo3(name = "raw_symbol")]
144    fn py_raw_symbol(&self) -> Symbol {
145        self.raw_symbol
146    }
147
148    #[getter]
149    #[pyo3(name = "asset_class")]
150    fn py_asset_class(&self) -> AssetClass {
151        AssetClass::Alternative
152    }
153
154    #[getter]
155    #[pyo3(name = "instrument_class")]
156    fn py_instrument_class(&self) -> InstrumentClass {
157        InstrumentClass::SportsBetting
158    }
159
160    #[getter]
161    #[pyo3(name = "event_type_id")]
162    fn py_event_type_id(&self) -> u64 {
163        self.event_type_id
164    }
165
166    #[getter]
167    #[pyo3(name = "event_type_name")]
168    fn py_event_type_name(&self) -> &str {
169        self.event_type_name.as_str()
170    }
171
172    #[getter]
173    #[pyo3(name = "competition_id")]
174    fn py_competition_id(&self) -> u64 {
175        self.competition_id
176    }
177
178    #[getter]
179    #[pyo3(name = "competition_name")]
180    fn py_competition_name(&self) -> &str {
181        self.competition_name.as_str()
182    }
183
184    #[getter]
185    #[pyo3(name = "event_id")]
186    fn py_event_id(&self) -> u64 {
187        self.event_id
188    }
189
190    #[getter]
191    #[pyo3(name = "event_name")]
192    fn py_event_name(&self) -> &str {
193        self.event_name.as_str()
194    }
195
196    #[getter]
197    #[pyo3(name = "event_country_code")]
198    fn py_event_country_code(&self) -> &str {
199        self.event_country_code.as_str()
200    }
201
202    #[getter]
203    #[pyo3(name = "event_open_date")]
204    fn py_event_open_date(&self) -> u64 {
205        self.event_open_date.as_u64()
206    }
207
208    #[getter]
209    #[pyo3(name = "betting_type")]
210    fn py_betting_type(&self) -> &str {
211        self.betting_type.as_str()
212    }
213
214    #[getter]
215    #[pyo3(name = "market_id")]
216    fn py_market_id(&self) -> &str {
217        self.market_id.as_str()
218    }
219
220    #[getter]
221    #[pyo3(name = "market_name")]
222    fn py_market_name(&self) -> &str {
223        self.market_name.as_str()
224    }
225
226    #[getter]
227    #[pyo3(name = "market_type")]
228    fn py_market_type(&self) -> &str {
229        self.market_type.as_str()
230    }
231
232    #[getter]
233    #[pyo3(name = "market_start_time")]
234    fn py_market_start_time(&self) -> u64 {
235        self.market_start_time.as_u64()
236    }
237
238    #[getter]
239    #[pyo3(name = "selection_id")]
240    fn py_selection_id(&self) -> u64 {
241        self.selection_id
242    }
243
244    #[getter]
245    #[pyo3(name = "selection_name")]
246    fn py_selection_name(&self) -> &str {
247        self.selection_name.as_str()
248    }
249
250    #[getter]
251    #[pyo3(name = "selection_name")]
252    fn py_selection_handicap(&self) -> f64 {
253        self.selection_handicap
254    }
255
256    #[getter]
257    #[pyo3(name = "currency")]
258    fn py_currency(&self) -> Currency {
259        self.currency
260    }
261
262    #[getter]
263    #[pyo3(name = "price_precision")]
264    fn py_price_precision(&self) -> u8 {
265        self.price_precision
266    }
267
268    #[getter]
269    #[pyo3(name = "size_precision")]
270    fn py_size_precision(&self) -> u8 {
271        self.size_precision
272    }
273
274    #[getter]
275    #[pyo3(name = "price_increment")]
276    fn py_price_increment(&self) -> Price {
277        self.price_increment
278    }
279
280    #[getter]
281    #[pyo3(name = "size_increment")]
282    fn py_size_increment(&self) -> Quantity {
283        self.size_increment
284    }
285
286    #[getter]
287    #[pyo3(name = "max_quantity")]
288    fn py_max_quantity(&self) -> Option<Quantity> {
289        self.max_quantity
290    }
291
292    #[getter]
293    #[pyo3(name = "min_quantity")]
294    fn py_min_quantity(&self) -> Option<Quantity> {
295        self.min_quantity
296    }
297
298    #[getter]
299    #[pyo3(name = "max_notional")]
300    fn py_max_notional(&self) -> Option<Money> {
301        self.max_notional
302    }
303
304    #[getter]
305    #[pyo3(name = "min_notional")]
306    fn py_min_notional(&self) -> Option<Money> {
307        self.min_notional
308    }
309
310    #[getter]
311    #[pyo3(name = "max_price")]
312    fn py_max_price(&self) -> Option<Price> {
313        self.max_price
314    }
315
316    #[getter]
317    #[pyo3(name = "min_price")]
318    fn py_min_price(&self) -> Option<Price> {
319        self.min_price
320    }
321
322    #[getter]
323    #[pyo3(name = "maker_fee")]
324    fn py_maker_fee(&self) -> Decimal {
325        self.maker_fee
326    }
327
328    #[getter]
329    #[pyo3(name = "taker_fee")]
330    fn py_taker_fee(&self) -> Decimal {
331        self.taker_fee
332    }
333
334    #[getter]
335    #[pyo3(name = "info")]
336    fn py_info(&self, py: Python<'_>) -> PyResult<PyObject> {
337        Ok(PyDict::new(py).into())
338    }
339
340    #[getter]
341    #[pyo3(name = "ts_event")]
342    fn py_ts_event(&self) -> u64 {
343        self.ts_event.as_u64()
344    }
345
346    #[getter]
347    #[pyo3(name = "ts_init")]
348    fn py_ts_init(&self) -> u64 {
349        self.ts_init.as_u64()
350    }
351
352    #[staticmethod]
353    #[pyo3(name = "from_dict")]
354    fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
355        from_dict_pyo3(py, values)
356    }
357
358    #[pyo3(name = "to_dict")]
359    fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
360        let dict = PyDict::new(py);
361        dict.set_item("type", stringify!(BettingInstrument))?;
362        dict.set_item("id", self.id.to_string())?;
363        dict.set_item("raw_symbol", self.raw_symbol.to_string())?;
364        dict.set_item("event_type_id", self.event_type_id)?;
365        dict.set_item("event_type_name", self.event_type_name.to_string())?;
366        dict.set_item("competition_id", self.competition_id)?;
367        dict.set_item("competition_name", self.competition_name.to_string())?;
368        dict.set_item("event_id", self.event_id)?;
369        dict.set_item("event_name", self.event_name.to_string())?;
370        dict.set_item("event_country_code", self.event_country_code.to_string())?;
371        dict.set_item("event_open_date", self.event_open_date.as_u64())?;
372        dict.set_item("betting_type", self.betting_type.to_string())?;
373        dict.set_item("market_id", self.market_id.to_string())?;
374        dict.set_item("market_name", self.market_name.to_string())?;
375        dict.set_item("market_type", self.market_type.to_string())?;
376        dict.set_item("market_start_time", self.market_start_time.as_u64())?;
377        dict.set_item("selection_id", self.selection_id)?;
378        dict.set_item("selection_name", self.selection_name.to_string())?;
379        dict.set_item("selection_handicap", self.selection_handicap)?;
380        dict.set_item("currency", self.currency.code.to_string())?;
381        dict.set_item("price_precision", self.price_precision)?;
382        dict.set_item("size_precision", self.size_precision)?;
383        dict.set_item("price_increment", self.price_increment.to_string())?;
384        dict.set_item("size_increment", self.size_increment.to_string())?;
385        dict.set_item("margin_init", self.margin_init.to_string())?;
386        dict.set_item("margin_maint", self.margin_maint.to_string())?;
387        dict.set_item("maker_fee", self.maker_fee.to_string())?;
388        dict.set_item("taker_fee", self.taker_fee.to_string())?;
389        dict.set_item("ts_event", self.ts_event.as_u64())?;
390        dict.set_item("ts_init", self.ts_init.as_u64())?;
391        dict.set_item("info", PyDict::new(py))?;
392        match self.max_quantity {
393            Some(value) => dict.set_item("max_quantity", value.to_string())?,
394            None => dict.set_item("max_quantity", py.None())?,
395        }
396        match self.min_quantity {
397            Some(value) => dict.set_item("min_quantity", value.to_string())?,
398            None => dict.set_item("min_quantity", py.None())?,
399        }
400        match self.max_notional {
401            Some(value) => dict.set_item("max_notional", value.to_string())?,
402            None => dict.set_item("max_notional", py.None())?,
403        }
404        match self.min_notional {
405            Some(value) => dict.set_item("min_notional", value.to_string())?,
406            None => dict.set_item("min_notional", py.None())?,
407        }
408        match self.max_price {
409            Some(value) => dict.set_item("max_price", value.to_string())?,
410            None => dict.set_item("max_price", py.None())?,
411        }
412        match self.min_price {
413            Some(value) => dict.set_item("min_price", value.to_string())?,
414            None => dict.set_item("min_price", py.None())?,
415        }
416        Ok(dict.into())
417    }
418}
419
420////////////////////////////////////////////////////////////////////////////////
421// Tests
422////////////////////////////////////////////////////////////////////////////////
423#[cfg(test)]
424mod tests {
425    use pyo3::{prelude::*, prepare_freethreaded_python, types::PyDict};
426    use rstest::rstest;
427
428    use crate::instruments::{BettingInstrument, stubs::*};
429
430    #[rstest]
431    fn test_dict_round_trip(betting: BettingInstrument) {
432        prepare_freethreaded_python();
433        Python::with_gil(|py| {
434            let values = betting.py_to_dict(py).unwrap();
435            let values: Py<PyDict> = values.extract(py).unwrap();
436            let new_betting = BettingInstrument::py_from_dict(py, values).unwrap();
437            assert_eq!(betting, new_betting);
438        })
439    }
440}