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 funding;
25pub mod greeks;
26pub mod order;
27pub mod prices;
28pub mod quote;
29pub mod status;
30pub mod trade;
31
32use indexmap::IndexMap;
33#[cfg(feature = "ffi")]
34use nautilus_core::ffi::cvec::CVec;
35use pyo3::{exceptions::PyValueError, prelude::*, types::PyCapsule};
36
37use crate::data::{
38 Bar, Data, DataType, FundingRateUpdate, IndexPriceUpdate, MarkPriceUpdate, OrderBookDelta,
39 QuoteTick, TradeTick, close::InstrumentClose, is_monotonically_increasing_by_init,
40};
41
42const ERROR_MONOTONICITY: &str = "`data` was not monotonically increasing by the `ts_init` field";
43
44#[pymethods]
45#[cfg_attr(feature = "python", pyo3_stub_gen::derive::gen_stub_pymethods)]
46impl DataType {
47 #[new]
48 #[pyo3(signature = (type_name, metadata=None))]
49 fn py_new(type_name: &str, metadata: Option<IndexMap<String, String>>) -> Self {
50 Self::new(type_name, metadata)
51 }
52
53 #[getter]
54 #[pyo3(name = "type_name")]
55 fn py_type_name(&self) -> &str {
56 self.type_name()
57 }
58
59 #[getter]
60 #[pyo3(name = "metadata")]
61 fn py_metadata(&self) -> Option<IndexMap<String, String>> {
62 self.metadata().cloned()
63 }
64
65 #[getter]
66 #[pyo3(name = "topic")]
67 fn py_topic(&self) -> &str {
68 self.topic()
69 }
70}
71
72#[must_use]
91pub fn data_to_pycapsule(py: Python, data: Data) -> Py<PyAny> {
92 let capsule = PyCapsule::new_with_destructor(py, data, None, |_, _| {})
95 .expect("Error creating `PyCapsule`");
96 capsule.into_any().unbind()
97}
98
99#[cfg(feature = "ffi")]
116#[pyfunction]
117#[allow(unsafe_code)]
118pub fn drop_cvec_pycapsule(capsule: &Bound<'_, PyAny>) {
119 let capsule: &Bound<'_, PyCapsule> = capsule
120 .downcast::<PyCapsule>()
121 .expect("Error on downcast to `&PyCapsule`");
122 let cvec: &CVec = unsafe { &*(capsule.pointer() as *const CVec) };
123 let data: Vec<Data> =
124 unsafe { Vec::from_raw_parts(cvec.ptr.cast::<Data>(), cvec.len, cvec.cap) };
125 drop(data);
126}
127
128#[cfg(not(feature = "ffi"))]
129#[pyfunction]
130pub fn drop_cvec_pycapsule(_capsule: &Bound<'_, PyAny>) {
137 panic!("`ffi` feature is not enabled");
138}
139
140pub fn pyobjects_to_book_deltas(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<OrderBookDelta>> {
146 let deltas: Vec<OrderBookDelta> = data
147 .into_iter()
148 .map(|obj| OrderBookDelta::from_pyobject(&obj))
149 .collect::<PyResult<Vec<OrderBookDelta>>>()?;
150
151 if !is_monotonically_increasing_by_init(&deltas) {
153 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
154 }
155
156 Ok(deltas)
157}
158
159pub fn pyobjects_to_quotes(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<QuoteTick>> {
165 let quotes: Vec<QuoteTick> = data
166 .into_iter()
167 .map(|obj| QuoteTick::from_pyobject(&obj))
168 .collect::<PyResult<Vec<QuoteTick>>>()?;
169
170 if !is_monotonically_increasing_by_init("es) {
172 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
173 }
174
175 Ok(quotes)
176}
177
178pub fn pyobjects_to_trades(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<TradeTick>> {
184 let trades: Vec<TradeTick> = data
185 .into_iter()
186 .map(|obj| TradeTick::from_pyobject(&obj))
187 .collect::<PyResult<Vec<TradeTick>>>()?;
188
189 if !is_monotonically_increasing_by_init(&trades) {
191 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
192 }
193
194 Ok(trades)
195}
196
197pub fn pyobjects_to_bars(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<Bar>> {
203 let bars: Vec<Bar> = data
204 .into_iter()
205 .map(|obj| Bar::from_pyobject(&obj))
206 .collect::<PyResult<Vec<Bar>>>()?;
207
208 if !is_monotonically_increasing_by_init(&bars) {
210 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
211 }
212
213 Ok(bars)
214}
215
216pub fn pyobjects_to_mark_prices(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<MarkPriceUpdate>> {
222 let mark_prices: Vec<MarkPriceUpdate> = data
223 .into_iter()
224 .map(|obj| MarkPriceUpdate::from_pyobject(&obj))
225 .collect::<PyResult<Vec<MarkPriceUpdate>>>()?;
226
227 if !is_monotonically_increasing_by_init(&mark_prices) {
229 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
230 }
231
232 Ok(mark_prices)
233}
234
235pub fn pyobjects_to_index_prices(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<IndexPriceUpdate>> {
241 let index_prices: Vec<IndexPriceUpdate> = data
242 .into_iter()
243 .map(|obj| IndexPriceUpdate::from_pyobject(&obj))
244 .collect::<PyResult<Vec<IndexPriceUpdate>>>()?;
245
246 if !is_monotonically_increasing_by_init(&index_prices) {
248 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
249 }
250
251 Ok(index_prices)
252}
253
254pub fn pyobjects_to_instrument_closes(
260 data: Vec<Bound<'_, PyAny>>,
261) -> PyResult<Vec<InstrumentClose>> {
262 let closes: Vec<InstrumentClose> = data
263 .into_iter()
264 .map(|obj| InstrumentClose::from_pyobject(&obj))
265 .collect::<PyResult<Vec<InstrumentClose>>>()?;
266
267 if !is_monotonically_increasing_by_init(&closes) {
269 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
270 }
271
272 Ok(closes)
273}
274
275pub fn pyobjects_to_funding_rates(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<FundingRateUpdate>> {
281 let funding_rates: Vec<FundingRateUpdate> = data
282 .into_iter()
283 .map(|obj| FundingRateUpdate::from_pyobject(&obj))
284 .collect::<PyResult<Vec<FundingRateUpdate>>>()?;
285
286 if !is_monotonically_increasing_by_init(&funding_rates) {
288 return Err(PyValueError::new_err(ERROR_MONOTONICITY));
289 }
290
291 Ok(funding_rates)
292}