nautilus_model/python/data/
mod.rs1pub mod bar;
19pub mod bet;
20pub mod delta;
21pub mod deltas;
22pub mod depth;
23pub mod greeks;
24pub mod order;
25pub mod quote;
26pub mod status;
27pub mod trade;
28
29use indexmap::IndexMap;
30#[cfg(feature = "ffi")]
31use nautilus_core::ffi::cvec::CVec;
32use pyo3::{exceptions::PyValueError, prelude::*, types::PyCapsule};
33
34use crate::data::{
35 is_monotonically_increasing_by_init, Bar, Data, DataType, OrderBookDelta, QuoteTick, TradeTick,
36};
37
38const ERROR_MONOTONICITY: &str = "`data` was not monotonically increasing by the `ts_init` field";
39
40#[pymethods]
41impl DataType {
42 #[new]
43 #[pyo3(signature = (type_name, metadata=None))]
44 fn py_new(type_name: &str, metadata: Option<IndexMap<String, String>>) -> Self {
45 Self::new(type_name, metadata)
46 }
47
48 #[getter]
49 #[pyo3(name = "type_name")]
50 fn py_type_name(&self) -> &str {
51 self.type_name()
52 }
53
54 #[getter]
55 #[pyo3(name = "metadata")]
56 fn py_metadata(&self) -> Option<IndexMap<String, String>> {
57 self.metadata().cloned()
58 }
59
60 #[getter]
61 #[pyo3(name = "topic")]
62 fn py_topic(&self) -> &str {
63 self.topic()
64 }
65}
66
67#[must_use]
86pub fn data_to_pycapsule(py: Python, data: Data) -> PyObject {
87 let capsule = PyCapsule::new(py, data, None).expect("Error creating `PyCapsule`");
88 capsule.into_any().unbind()
89}
90
91#[pyfunction]
109#[cfg(feature = "ffi")]
110pub fn drop_cvec_pycapsule(capsule: &Bound<'_, PyAny>) {
111 let capsule: &Bound<'_, PyCapsule> = capsule
112 .downcast::<PyCapsule>()
113 .expect("Error on downcast to `&PyCapsule`");
114 let cvec: &CVec = unsafe { &*(capsule.pointer() as *const CVec) };
115 let data: Vec<Data> =
116 unsafe { Vec::from_raw_parts(cvec.ptr.cast::<Data>(), cvec.len, cvec.cap) };
117 drop(data);
118}
119
120#[pyfunction]
121#[cfg(not(feature = "ffi"))]
122pub fn drop_cvec_pycapsule(_capsule: &Bound<'_, PyAny>) {
123 panic!("`ffi` feature is not enabled");
124}
125
126pub fn pyobjects_to_order_book_deltas(
128 data: Vec<Bound<'_, PyAny>>,
129) -> PyResult<Vec<OrderBookDelta>> {
130 let deltas: Vec<OrderBookDelta> = data
131 .into_iter()
132 .map(|obj| OrderBookDelta::from_pyobject(&obj))
133 .collect::<PyResult<Vec<OrderBookDelta>>>()?;
134
135 if !is_monotonically_increasing_by_init(&deltas) {
137 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
138 }
139
140 Ok(deltas)
141}
142
143pub fn pyobjects_to_quote_ticks(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<QuoteTick>> {
145 let ticks: Vec<QuoteTick> = data
146 .into_iter()
147 .map(|obj| QuoteTick::from_pyobject(&obj))
148 .collect::<PyResult<Vec<QuoteTick>>>()?;
149
150 if !is_monotonically_increasing_by_init(&ticks) {
152 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
153 }
154
155 Ok(ticks)
156}
157
158pub fn pyobjects_to_trade_ticks(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<TradeTick>> {
160 let ticks: Vec<TradeTick> = data
161 .into_iter()
162 .map(|obj| TradeTick::from_pyobject(&obj))
163 .collect::<PyResult<Vec<TradeTick>>>()?;
164
165 if !is_monotonically_increasing_by_init(&ticks) {
167 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
168 }
169
170 Ok(ticks)
171}
172
173pub fn pyobjects_to_bars(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<Bar>> {
175 let bars: Vec<Bar> = data
176 .into_iter()
177 .map(|obj| Bar::from_pyobject(&obj))
178 .collect::<PyResult<Vec<Bar>>>()?;
179
180 if !is_monotonically_increasing_by_init(&bars) {
182 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
183 }
184
185 Ok(bars)
186}