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]
92pub fn data_to_pycapsule(py: Python, data: Data) -> Py<PyAny> {
93 let capsule = PyCapsule::new_with_destructor(py, data, None, |_, _| {})
96 .expect("Error creating `PyCapsule`");
97 capsule.into_any().unbind()
98}
99
100#[cfg(feature = "ffi")]
117#[pyfunction]
118#[allow(unsafe_code)]
119pub fn drop_cvec_pycapsule(capsule: &Bound<'_, PyAny>) {
120 let capsule: &Bound<'_, PyCapsule> = capsule
121 .cast::<PyCapsule>()
122 .expect("Error on downcast to `&PyCapsule`");
123 let cvec: &CVec = unsafe { &*(capsule.pointer_checked(None).unwrap().as_ptr() as *const CVec) };
124 let data: Vec<Data> =
125 unsafe { Vec::from_raw_parts(cvec.ptr.cast::<Data>(), cvec.len, cvec.cap) };
126 drop(data);
127}
128
129#[cfg(not(feature = "ffi"))]
130#[pyfunction]
131pub fn drop_cvec_pycapsule(_capsule: &Bound<'_, PyAny>) {
138 panic!("`ffi` feature is not enabled");
139}
140
141pub fn pyobjects_to_book_deltas(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<OrderBookDelta>> {
147 let deltas: Vec<OrderBookDelta> = data
148 .into_iter()
149 .map(|obj| OrderBookDelta::from_pyobject(&obj))
150 .collect::<PyResult<Vec<OrderBookDelta>>>()?;
151
152 if !is_monotonically_increasing_by_init(&deltas) {
154 return Err(to_pyvalue_err(ERROR_MONOTONICITY));
155 }
156
157 Ok(deltas)
158}
159
160pub fn pyobjects_to_quotes(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<QuoteTick>> {
166 let quotes: Vec<QuoteTick> = data
167 .into_iter()
168 .map(|obj| QuoteTick::from_pyobject(&obj))
169 .collect::<PyResult<Vec<QuoteTick>>>()?;
170
171 if !is_monotonically_increasing_by_init("es) {
173 return Err(to_pyvalue_err(ERROR_MONOTONICITY));
174 }
175
176 Ok(quotes)
177}
178
179pub fn pyobjects_to_trades(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<TradeTick>> {
185 let trades: Vec<TradeTick> = data
186 .into_iter()
187 .map(|obj| TradeTick::from_pyobject(&obj))
188 .collect::<PyResult<Vec<TradeTick>>>()?;
189
190 if !is_monotonically_increasing_by_init(&trades) {
192 return Err(to_pyvalue_err(ERROR_MONOTONICITY));
193 }
194
195 Ok(trades)
196}
197
198pub fn pyobjects_to_bars(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<Bar>> {
204 let bars: Vec<Bar> = data
205 .into_iter()
206 .map(|obj| Bar::from_pyobject(&obj))
207 .collect::<PyResult<Vec<Bar>>>()?;
208
209 if !is_monotonically_increasing_by_init(&bars) {
211 return Err(to_pyvalue_err(ERROR_MONOTONICITY));
212 }
213
214 Ok(bars)
215}
216
217pub fn pyobjects_to_mark_prices(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<MarkPriceUpdate>> {
223 let mark_prices: Vec<MarkPriceUpdate> = data
224 .into_iter()
225 .map(|obj| MarkPriceUpdate::from_pyobject(&obj))
226 .collect::<PyResult<Vec<MarkPriceUpdate>>>()?;
227
228 if !is_monotonically_increasing_by_init(&mark_prices) {
230 return Err(to_pyvalue_err(ERROR_MONOTONICITY));
231 }
232
233 Ok(mark_prices)
234}
235
236pub fn pyobjects_to_index_prices(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<IndexPriceUpdate>> {
242 let index_prices: Vec<IndexPriceUpdate> = data
243 .into_iter()
244 .map(|obj| IndexPriceUpdate::from_pyobject(&obj))
245 .collect::<PyResult<Vec<IndexPriceUpdate>>>()?;
246
247 if !is_monotonically_increasing_by_init(&index_prices) {
249 return Err(to_pyvalue_err(ERROR_MONOTONICITY));
250 }
251
252 Ok(index_prices)
253}
254
255pub fn pyobjects_to_instrument_closes(
261 data: Vec<Bound<'_, PyAny>>,
262) -> PyResult<Vec<InstrumentClose>> {
263 let closes: Vec<InstrumentClose> = data
264 .into_iter()
265 .map(|obj| InstrumentClose::from_pyobject(&obj))
266 .collect::<PyResult<Vec<InstrumentClose>>>()?;
267
268 if !is_monotonically_increasing_by_init(&closes) {
270 return Err(to_pyvalue_err(ERROR_MONOTONICITY));
271 }
272
273 Ok(closes)
274}
275
276pub fn pyobjects_to_funding_rates(data: Vec<Bound<'_, PyAny>>) -> PyResult<Vec<FundingRateUpdate>> {
282 let funding_rates: Vec<FundingRateUpdate> = data
283 .into_iter()
284 .map(|obj| FundingRateUpdate::from_pyobject(&obj))
285 .collect::<PyResult<Vec<FundingRateUpdate>>>()?;
286
287 if !is_monotonically_increasing_by_init(&funding_rates) {
289 return Err(to_pyvalue_err(ERROR_MONOTONICITY));
290 }
291
292 Ok(funding_rates)
293}