nautilus_model/python/data/
mod.rs1pub mod bar;
19pub mod bet;
20pub mod close;
21pub mod delta;
22pub mod deltas;
23pub mod depth;
24pub mod greeks;
25pub mod order;
26pub mod prices;
27pub mod quote;
28pub mod status;
29pub mod trade;
30
31use indexmap::IndexMap;
32#[cfg(feature = "ffi")]
33use nautilus_core::ffi::cvec::CVec;
34use pyo3::{exceptions::PyValueError, prelude::*, types::PyCapsule};
35
36use crate::data::{
37 Bar, Data, DataType, IndexPriceUpdate, MarkPriceUpdate, OrderBookDelta, QuoteTick, TradeTick,
38 close::InstrumentClose, is_monotonically_increasing_by_init,
39};
40
41const ERROR_MONOTONICITY: &str = "`data` was not monotonically increasing by the `ts_init` field";
42
43#[pymethods]
44impl DataType {
45 #[new]
46 #[pyo3(signature = (type_name, metadata=None))]
47 fn py_new(type_name: &str, metadata: Option<IndexMap<String, String>>) -> Self {
48 Self::new(type_name, metadata)
49 }
50
51 #[getter]
52 #[pyo3(name = "type_name")]
53 fn py_type_name(&self) -> &str {
54 self.type_name()
55 }
56
57 #[getter]
58 #[pyo3(name = "metadata")]
59 fn py_metadata(&self) -> Option<IndexMap<String, String>> {
60 self.metadata().cloned()
61 }
62
63 #[getter]
64 #[pyo3(name = "topic")]
65 fn py_topic(&self) -> &str {
66 self.topic()
67 }
68}
69
70#[must_use]
89pub fn data_to_pycapsule(py: Python, data: Data) -> PyObject {
90 let capsule = PyCapsule::new(py, data, None).expect("Error creating `PyCapsule`");
91 capsule.into_any().unbind()
92}
93
94#[pyfunction]
112#[cfg(feature = "ffi")]
113pub fn drop_cvec_pycapsule(capsule: &Bound<'_, PyAny>) {
114 let capsule: &Bound<'_, PyCapsule> = capsule
115 .downcast::<PyCapsule>()
116 .expect("Error on downcast to `&PyCapsule`");
117 let cvec: &CVec = unsafe { &*(capsule.pointer() as *const CVec) };
118 let data: Vec<Data> =
119 unsafe { Vec::from_raw_parts(cvec.ptr.cast::<Data>(), cvec.len, cvec.cap) };
120 drop(data);
121}
122
123#[pyfunction]
124#[cfg(not(feature = "ffi"))]
125pub fn drop_cvec_pycapsule(_capsule: &Bound<'_, PyAny>) {
126 panic!("`ffi` feature is not enabled");
127}
128
129pub fn pyobjects_to_book_deltas(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<OrderBookDelta>> {
131 let deltas: Vec<OrderBookDelta> = data
132 .into_iter()
133 .map(|obj| OrderBookDelta::from_pyobject(&obj))
134 .collect::<PyResult<Vec<OrderBookDelta>>>()?;
135
136 if !is_monotonically_increasing_by_init(&deltas) {
138 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
139 }
140
141 Ok(deltas)
142}
143
144pub fn pyobjects_to_quotes(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<QuoteTick>> {
146 let quotes: Vec<QuoteTick> = data
147 .into_iter()
148 .map(|obj| QuoteTick::from_pyobject(&obj))
149 .collect::<PyResult<Vec<QuoteTick>>>()?;
150
151 if !is_monotonically_increasing_by_init("es) {
153 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
154 }
155
156 Ok(quotes)
157}
158
159pub fn pyobjects_to_trades(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<TradeTick>> {
161 let trades: Vec<TradeTick> = data
162 .into_iter()
163 .map(|obj| TradeTick::from_pyobject(&obj))
164 .collect::<PyResult<Vec<TradeTick>>>()?;
165
166 if !is_monotonically_increasing_by_init(&trades) {
168 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
169 }
170
171 Ok(trades)
172}
173
174pub fn pyobjects_to_bars(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<Bar>> {
176 let bars: Vec<Bar> = data
177 .into_iter()
178 .map(|obj| Bar::from_pyobject(&obj))
179 .collect::<PyResult<Vec<Bar>>>()?;
180
181 if !is_monotonically_increasing_by_init(&bars) {
183 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
184 }
185
186 Ok(bars)
187}
188
189pub fn pyobjects_to_mark_prices(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<MarkPriceUpdate>> {
191 let mark_prices: Vec<MarkPriceUpdate> = data
192 .into_iter()
193 .map(|obj| MarkPriceUpdate::from_pyobject(&obj))
194 .collect::<PyResult<Vec<MarkPriceUpdate>>>()?;
195
196 if !is_monotonically_increasing_by_init(&mark_prices) {
198 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
199 }
200
201 Ok(mark_prices)
202}
203
204pub fn pyobjects_to_index_prices(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<IndexPriceUpdate>> {
206 let index_prices: Vec<IndexPriceUpdate> = data
207 .into_iter()
208 .map(|obj| IndexPriceUpdate::from_pyobject(&obj))
209 .collect::<PyResult<Vec<IndexPriceUpdate>>>()?;
210
211 if !is_monotonically_increasing_by_init(&index_prices) {
213 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
214 }
215
216 Ok(index_prices)
217}
218
219pub fn pyobjects_to_instrument_closes(
221 data: Vec<Bound<'_, PyAny>>,
222) -> PyResult<Vec<InstrumentClose>> {
223 let closes: Vec<InstrumentClose> = data
224 .into_iter()
225 .map(|obj| InstrumentClose::from_pyobject(&obj))
226 .collect::<PyResult<Vec<InstrumentClose>>>()?;
227
228 if !is_monotonically_increasing_by_init(&closes) {
230 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
231 }
232
233 Ok(closes)
234}