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 nautilus_core::python::to_pyvalue_err;
36use pyo3::{prelude::*, types::PyCapsule};
37
38use crate::data::{
39 Bar, Data, DataType, FundingRateUpdate, IndexPriceUpdate, MarkPriceUpdate, OrderBookDelta,
40 QuoteTick, TradeTick, close::InstrumentClose, is_monotonically_increasing_by_init,
41};
42
43const ERROR_MONOTONICITY: &str = "`data` was not monotonically increasing by the `ts_init` field";
44
45#[pymethods]
46#[cfg_attr(feature = "python", pyo3_stub_gen::derive::gen_stub_pymethods)]
47impl DataType {
48 #[new]
49 #[pyo3(signature = (type_name, metadata=None))]
50 fn py_new(type_name: &str, metadata: Option<IndexMap<String, String>>) -> Self {
51 Self::new(type_name, metadata)
52 }
53
54 #[getter]
55 #[pyo3(name = "type_name")]
56 fn py_type_name(&self) -> &str {
57 self.type_name()
58 }
59
60 #[getter]
61 #[pyo3(name = "metadata")]
62 fn py_metadata(&self) -> Option<IndexMap<String, String>> {
63 self.metadata().cloned()
64 }
65
66 #[getter]
67 #[pyo3(name = "topic")]
68 fn py_topic(&self) -> &str {
69 self.topic()
70 }
71}
72
73#[must_use]
84pub fn data_to_pycapsule(py: Python, data: Data) -> Py<PyAny> {
85 let capsule = PyCapsule::new_with_destructor(py, data, None, |_, _| {})
88 .expect("Error creating `PyCapsule`");
89 capsule.into_any().unbind()
90}
91
92#[cfg(feature = "ffi")]
106#[pyfunction]
107#[allow(unsafe_code)]
108pub fn drop_cvec_pycapsule(capsule: &Bound<'_, PyAny>) {
109 let capsule: &Bound<'_, PyCapsule> = capsule
110 .cast::<PyCapsule>()
111 .expect("Error on downcast to `&PyCapsule`");
112 let cvec: &CVec = unsafe { &*(capsule.pointer_checked(None).unwrap().as_ptr() as *const CVec) };
113 let data: Vec<Data> =
114 unsafe { Vec::from_raw_parts(cvec.ptr.cast::<Data>(), cvec.len, cvec.cap) };
115 drop(data);
116}
117
118#[cfg(not(feature = "ffi"))]
119#[pyfunction]
120pub fn drop_cvec_pycapsule(_capsule: &Bound<'_, PyAny>) {
127 panic!("`ffi` feature is not enabled");
128}
129
130pub fn pyobjects_to_book_deltas(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<OrderBookDelta>> {
136 let deltas: Vec<OrderBookDelta> = data
137 .into_iter()
138 .map(|obj| OrderBookDelta::from_pyobject(&obj))
139 .collect::<PyResult<Vec<OrderBookDelta>>>()?;
140
141 if !is_monotonically_increasing_by_init(&deltas) {
143 return Err(to_pyvalue_err(ERROR_MONOTONICITY));
144 }
145
146 Ok(deltas)
147}
148
149pub fn pyobjects_to_quotes(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<QuoteTick>> {
155 let quotes: Vec<QuoteTick> = data
156 .into_iter()
157 .map(|obj| QuoteTick::from_pyobject(&obj))
158 .collect::<PyResult<Vec<QuoteTick>>>()?;
159
160 if !is_monotonically_increasing_by_init("es) {
162 return Err(to_pyvalue_err(ERROR_MONOTONICITY));
163 }
164
165 Ok(quotes)
166}
167
168pub fn pyobjects_to_trades(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<TradeTick>> {
174 let trades: Vec<TradeTick> = data
175 .into_iter()
176 .map(|obj| TradeTick::from_pyobject(&obj))
177 .collect::<PyResult<Vec<TradeTick>>>()?;
178
179 if !is_monotonically_increasing_by_init(&trades) {
181 return Err(to_pyvalue_err(ERROR_MONOTONICITY));
182 }
183
184 Ok(trades)
185}
186
187pub fn pyobjects_to_bars(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<Bar>> {
193 let bars: Vec<Bar> = data
194 .into_iter()
195 .map(|obj| Bar::from_pyobject(&obj))
196 .collect::<PyResult<Vec<Bar>>>()?;
197
198 if !is_monotonically_increasing_by_init(&bars) {
200 return Err(to_pyvalue_err(ERROR_MONOTONICITY));
201 }
202
203 Ok(bars)
204}
205
206pub fn pyobjects_to_mark_prices(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<MarkPriceUpdate>> {
212 let mark_prices: Vec<MarkPriceUpdate> = data
213 .into_iter()
214 .map(|obj| MarkPriceUpdate::from_pyobject(&obj))
215 .collect::<PyResult<Vec<MarkPriceUpdate>>>()?;
216
217 if !is_monotonically_increasing_by_init(&mark_prices) {
219 return Err(to_pyvalue_err(ERROR_MONOTONICITY));
220 }
221
222 Ok(mark_prices)
223}
224
225pub fn pyobjects_to_index_prices(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<IndexPriceUpdate>> {
231 let index_prices: Vec<IndexPriceUpdate> = data
232 .into_iter()
233 .map(|obj| IndexPriceUpdate::from_pyobject(&obj))
234 .collect::<PyResult<Vec<IndexPriceUpdate>>>()?;
235
236 if !is_monotonically_increasing_by_init(&index_prices) {
238 return Err(to_pyvalue_err(ERROR_MONOTONICITY));
239 }
240
241 Ok(index_prices)
242}
243
244pub fn pyobjects_to_instrument_closes(
250 data: Vec<Bound<'_, PyAny>>,
251) -> PyResult<Vec<InstrumentClose>> {
252 let closes: Vec<InstrumentClose> = data
253 .into_iter()
254 .map(|obj| InstrumentClose::from_pyobject(&obj))
255 .collect::<PyResult<Vec<InstrumentClose>>>()?;
256
257 if !is_monotonically_increasing_by_init(&closes) {
259 return Err(to_pyvalue_err(ERROR_MONOTONICITY));
260 }
261
262 Ok(closes)
263}
264
265pub fn pyobjects_to_funding_rates(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<FundingRateUpdate>> {
271 let funding_rates: Vec<FundingRateUpdate> = data
272 .into_iter()
273 .map(|obj| FundingRateUpdate::from_pyobject(&obj))
274 .collect::<PyResult<Vec<FundingRateUpdate>>>()?;
275
276 if !is_monotonically_increasing_by_init(&funding_rates) {
278 return Err(to_pyvalue_err(ERROR_MONOTONICITY));
279 }
280
281 Ok(funding_rates)
282}