1use std::{
17 collections::hash_map::DefaultHasher,
18 hash::{Hash, Hasher},
19};
20
21use nautilus_core::{
22 from_pydict,
23 python::{IntoPyObjectNautilusExt, serialization::from_dict_pyo3, to_pyvalue_err},
24};
25use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
26use rust_decimal::Decimal;
27use ustr::Ustr;
28
29use crate::{
30 enums::AssetClass,
31 identifiers::{InstrumentId, Symbol},
32 instruments::BinaryOption,
33 types::{Currency, Money, Price, Quantity},
34};
35
36#[pymethods]
37impl BinaryOption {
38 #[allow(clippy::too_many_arguments)]
39 #[new]
40 #[pyo3(signature = (instrument_id, raw_symbol, asset_class, currency, activation_ns, expiration_ns, price_precision, size_precision, price_increment, size_increment, ts_event, ts_init, outcome=None, description=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, info=None))]
41 fn py_new(
42 instrument_id: InstrumentId,
43 raw_symbol: Symbol,
44 asset_class: AssetClass,
45 currency: Currency,
46 activation_ns: u64,
47 expiration_ns: u64,
48 price_precision: u8,
49 size_precision: u8,
50 price_increment: Price,
51 size_increment: Quantity,
52 ts_event: u64,
53 ts_init: u64,
54 outcome: Option<String>,
55 description: Option<String>,
56 max_quantity: Option<Quantity>,
57 min_quantity: Option<Quantity>,
58 max_notional: Option<Money>,
59 min_notional: Option<Money>,
60 max_price: Option<Price>,
61 min_price: Option<Price>,
62 margin_init: Option<Decimal>,
63 margin_maint: Option<Decimal>,
64 maker_fee: Option<Decimal>,
65 taker_fee: Option<Decimal>,
66 info: Option<Py<PyDict>>,
67 ) -> PyResult<Self> {
68 let info_map = if let Some(info_dict) = info {
70 Python::attach(|py| from_pydict(py, info_dict))?
71 } else {
72 None
73 };
74
75 Self::new_checked(
76 instrument_id,
77 raw_symbol,
78 asset_class,
79 currency,
80 activation_ns.into(),
81 expiration_ns.into(),
82 price_precision,
83 size_precision,
84 price_increment,
85 size_increment,
86 outcome.map(|x| Ustr::from(&x)),
87 description.map(|x| Ustr::from(&x)),
88 max_quantity,
89 min_quantity,
90 max_notional,
91 min_notional,
92 max_price,
93 min_price,
94 margin_init,
95 margin_maint,
96 maker_fee,
97 taker_fee,
98 info_map,
99 ts_event.into(),
100 ts_init.into(),
101 )
102 .map_err(to_pyvalue_err)
103 }
104
105 fn __hash__(&self) -> isize {
106 let mut hasher = DefaultHasher::new();
107 self.hash(&mut hasher);
108 hasher.finish() as isize
109 }
110
111 fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
112 match op {
113 CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
114 CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
115 _ => py.NotImplemented(),
116 }
117 }
118
119 #[getter]
120 fn type_str(&self) -> &str {
121 stringify!(BinaryOption)
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 = "currency")]
144 fn py_currency(&self) -> Currency {
145 self.currency
146 }
147
148 #[getter]
149 #[pyo3(name = "activation_ns")]
150 fn py_activation_ns(&self) -> u64 {
151 self.activation_ns.as_u64()
152 }
153
154 #[getter]
155 #[pyo3(name = "expiration_ns")]
156 fn py_expiration_ns(&self) -> u64 {
157 self.expiration_ns.as_u64()
158 }
159
160 #[getter]
161 #[pyo3(name = "price_precision")]
162 fn py_price_precision(&self) -> u8 {
163 self.price_precision
164 }
165
166 #[getter]
167 #[pyo3(name = "size_precision")]
168 fn py_size_precision(&self) -> u8 {
169 self.size_precision
170 }
171
172 #[getter]
173 #[pyo3(name = "price_increment")]
174 fn py_price_increment(&self) -> Price {
175 self.price_increment
176 }
177
178 #[getter]
179 #[pyo3(name = "size_increment")]
180 fn py_size_increment(&self) -> Quantity {
181 self.size_increment
182 }
183
184 #[getter]
185 #[pyo3(name = "outcome")]
186 fn py_outcome(&self) -> Option<&str> {
187 match self.outcome {
188 Some(outcome) => Some(outcome.as_str()),
189 None => None,
190 }
191 }
192
193 #[getter]
194 #[pyo3(name = "description")]
195 fn py_description(&self) -> Option<&str> {
196 match self.description {
197 Some(description) => Some(description.as_str()),
198 None => None,
199 }
200 }
201
202 #[getter]
203 #[pyo3(name = "max_quantity")]
204 fn py_max_quantity(&self) -> Option<Quantity> {
205 self.max_quantity
206 }
207
208 #[getter]
209 #[pyo3(name = "min_quantity")]
210 fn py_min_quantity(&self) -> Option<Quantity> {
211 self.min_quantity
212 }
213
214 #[getter]
215 #[pyo3(name = "max_notional")]
216 fn py_max_notional(&self) -> Option<Money> {
217 self.max_notional
218 }
219
220 #[getter]
221 #[pyo3(name = "min_notional")]
222 fn py_min_notional(&self) -> Option<Money> {
223 self.min_notional
224 }
225
226 #[getter]
227 #[pyo3(name = "max_price")]
228 fn py_max_price(&self) -> Option<Price> {
229 self.max_price
230 }
231
232 #[getter]
233 #[pyo3(name = "min_price")]
234 fn py_min_price(&self) -> Option<Price> {
235 self.min_price
236 }
237
238 #[getter]
239 #[pyo3(name = "margin_init")]
240 fn py_margin_init(&self) -> Decimal {
241 self.margin_init
242 }
243
244 #[getter]
245 #[pyo3(name = "margin_maint")]
246 fn py_margin_maint(&self) -> Decimal {
247 self.margin_maint
248 }
249
250 #[getter]
251 #[pyo3(name = "maker_fee")]
252 fn py_maker_fee(&self) -> Decimal {
253 self.maker_fee
254 }
255
256 #[getter]
257 #[pyo3(name = "taker_fee")]
258 fn py_taker_fee(&self) -> Decimal {
259 self.taker_fee
260 }
261
262 #[getter]
263 #[pyo3(name = "info")]
264 fn py_info(&self, py: Python<'_>) -> PyResult<Py<PyDict>> {
265 if let Some(ref info_map) = self.info {
267 let py_dict = PyDict::new(py);
268 for (key, value) in info_map {
269 let json_str = serde_json::to_string(value).map_err(to_pyvalue_err)?;
271 let py_value =
272 PyModule::import(py, "json")?.call_method("loads", (json_str,), None)?;
273 py_dict.set_item(key, py_value)?;
274 }
275 Ok(py_dict.unbind())
276 } else {
277 Ok(PyDict::new(py).unbind())
278 }
279 }
280
281 #[getter]
282 #[pyo3(name = "ts_event")]
283 fn py_ts_event(&self) -> u64 {
284 self.ts_event.as_u64()
285 }
286
287 #[getter]
288 #[pyo3(name = "ts_init")]
289 fn py_ts_init(&self) -> u64 {
290 self.ts_init.as_u64()
291 }
292
293 #[staticmethod]
294 #[pyo3(name = "from_dict")]
295 fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
296 from_dict_pyo3(py, values)
297 }
298
299 #[pyo3(name = "to_dict")]
300 fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
301 let dict = PyDict::new(py);
302 dict.set_item("type", stringify!(BinaryOption))?;
303 dict.set_item("id", self.id.to_string())?;
304 dict.set_item("raw_symbol", self.raw_symbol.to_string())?;
305 dict.set_item("asset_class", self.asset_class.to_string())?;
306 dict.set_item("currency", self.currency.code.to_string())?;
307 dict.set_item("activation_ns", self.activation_ns.as_u64())?;
308 dict.set_item("expiration_ns", self.expiration_ns.as_u64())?;
309 dict.set_item("price_precision", self.price_precision)?;
310 dict.set_item("size_precision", self.size_precision)?;
311 dict.set_item("price_increment", self.price_increment.to_string())?;
312 dict.set_item("size_increment", self.size_increment.to_string())?;
313 dict.set_item("margin_init", self.margin_init.to_string())?;
314 dict.set_item("margin_maint", self.margin_maint.to_string())?;
315 dict.set_item("maker_fee", self.maker_fee.to_string())?;
316 dict.set_item("taker_fee", self.taker_fee.to_string())?;
317 dict.set_item("ts_event", self.ts_event.as_u64())?;
318 dict.set_item("ts_init", self.ts_init.as_u64())?;
319 if let Some(ref info_map) = self.info {
321 let info_dict = PyDict::new(py);
322 for (key, value) in info_map {
323 let json_str = serde_json::to_string(value).map_err(to_pyvalue_err)?;
324 let py_value =
325 PyModule::import(py, "json")?.call_method("loads", (json_str,), None)?;
326 info_dict.set_item(key, py_value)?;
327 }
328 dict.set_item("info", info_dict)?;
329 } else {
330 dict.set_item("info", PyDict::new(py))?;
331 }
332 match &self.outcome {
333 Some(value) => dict.set_item("outcome", value.to_string())?,
334 None => dict.set_item("outcome", py.None())?,
335 }
336 match &self.description {
337 Some(value) => dict.set_item("description", value.to_string())?,
338 None => dict.set_item("description", py.None())?,
339 }
340 match self.max_quantity {
341 Some(value) => dict.set_item("max_quantity", value.to_string())?,
342 None => dict.set_item("max_quantity", py.None())?,
343 }
344 match self.min_quantity {
345 Some(value) => dict.set_item("min_quantity", value.to_string())?,
346 None => dict.set_item("min_quantity", py.None())?,
347 }
348 match self.max_notional {
349 Some(value) => dict.set_item("max_notional", value.to_string())?,
350 None => dict.set_item("max_notional", py.None())?,
351 }
352 match self.min_notional {
353 Some(value) => dict.set_item("min_notional", value.to_string())?,
354 None => dict.set_item("min_notional", py.None())?,
355 }
356 match self.max_price {
357 Some(value) => dict.set_item("max_price", value.to_string())?,
358 None => dict.set_item("max_price", py.None())?,
359 }
360 match self.min_price {
361 Some(value) => dict.set_item("min_price", value.to_string())?,
362 None => dict.set_item("min_price", py.None())?,
363 }
364 Ok(dict.into())
365 }
366}
367
368#[cfg(test)]
369mod tests {
370 use pyo3::{prelude::*, types::PyDict};
371 use rstest::rstest;
372
373 use crate::instruments::{BinaryOption, stubs::*};
374
375 #[rstest]
376 fn test_dict_round_trip(binary_option: BinaryOption) {
377 Python::initialize();
378 Python::attach(|py| {
379 let values = binary_option.py_to_dict(py).unwrap();
380 let values: Py<PyDict> = values.extract(py).unwrap();
381 let new_binary_option = BinaryOption::py_from_dict(py, values).unwrap();
382 assert_eq!(binary_option, new_binary_option);
383 });
384 }
385}