nautilus_tardis/python/
csv.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::path::PathBuf;
17
18use nautilus_core::{ffi::cvec::CVec, python::to_pyvalue_err};
19use nautilus_model::{
20    data::{Data, OrderBookDelta, OrderBookDepth10, QuoteTick, TradeTick},
21    identifiers::InstrumentId,
22};
23use pyo3::{prelude::*, types::PyCapsule};
24
25use crate::csv::{
26    load_deltas, load_depth10_from_snapshot25, load_depth10_from_snapshot5, load_quote_ticks,
27    load_trade_ticks,
28};
29
30#[pyfunction(name = "load_tardis_deltas")]
31#[pyo3(signature = (filepath, price_precision, size_precision, instrument_id=None, limit=None))]
32pub fn py_load_tardis_deltas(
33    filepath: PathBuf,
34    price_precision: u8,
35    size_precision: u8,
36    instrument_id: Option<InstrumentId>,
37    limit: Option<usize>,
38) -> PyResult<Vec<OrderBookDelta>> {
39    load_deltas(
40        filepath,
41        price_precision,
42        size_precision,
43        instrument_id,
44        limit,
45    )
46    .map_err(to_pyvalue_err)
47}
48
49#[pyfunction(name = "load_tardis_deltas_as_pycapsule")]
50#[pyo3(signature = (filepath, price_precision, size_precision, instrument_id=None, limit=None))]
51pub fn py_load_tardis_deltas_as_pycapsule(
52    py: Python,
53    filepath: PathBuf,
54    price_precision: u8,
55    size_precision: u8,
56    instrument_id: Option<InstrumentId>,
57    limit: Option<usize>,
58) -> PyResult<PyObject> {
59    let deltas = load_deltas(
60        filepath,
61        price_precision,
62        size_precision,
63        instrument_id,
64        limit,
65    )
66    .map_err(to_pyvalue_err)?;
67    let deltas: Vec<Data> = deltas.into_iter().map(Data::Delta).collect();
68
69    let cvec: CVec = deltas.into();
70    let capsule = PyCapsule::new::<CVec>(py, cvec, None)?;
71    Ok(capsule.into_py(py))
72}
73
74#[pyfunction(name = "load_tardis_depth10_from_snapshot5")]
75#[pyo3(signature = (filepath, price_precision, size_precision, instrument_id=None, limit=None))]
76pub fn py_load_tardis_depth10_from_snapshot5(
77    filepath: PathBuf,
78    price_precision: u8,
79    size_precision: u8,
80    instrument_id: Option<InstrumentId>,
81    limit: Option<usize>,
82) -> PyResult<Vec<OrderBookDepth10>> {
83    load_depth10_from_snapshot5(
84        filepath,
85        price_precision,
86        size_precision,
87        instrument_id,
88        limit,
89    )
90    .map_err(to_pyvalue_err)
91}
92
93#[pyfunction(name = "load_tardis_depth10_from_snapshot5_as_pycapsule")]
94#[pyo3(signature = (filepath, price_precision, size_precision, instrument_id=None, limit=None))]
95pub fn py_load_tardis_depth10_from_snapshot5_as_pycapsule(
96    py: Python,
97    filepath: PathBuf,
98    price_precision: u8,
99    size_precision: u8,
100    instrument_id: Option<InstrumentId>,
101    limit: Option<usize>,
102) -> PyResult<PyObject> {
103    let depths = load_depth10_from_snapshot5(
104        filepath,
105        price_precision,
106        size_precision,
107        instrument_id,
108        limit,
109    )
110    .map_err(to_pyvalue_err)?;
111    let depths: Vec<Data> = depths.into_iter().map(Data::Depth10).collect();
112
113    let cvec: CVec = depths.into();
114    let capsule = PyCapsule::new::<CVec>(py, cvec, None)?;
115    Ok(capsule.into_py(py))
116}
117
118#[pyfunction(name = "load_tardis_depth10_from_snapshot25")]
119#[pyo3(signature = (filepath, price_precision, size_precision, instrument_id=None, limit=None))]
120pub fn py_load_tardis_depth10_from_snapshot25(
121    filepath: PathBuf,
122    price_precision: u8,
123    size_precision: u8,
124    instrument_id: Option<InstrumentId>,
125    limit: Option<usize>,
126) -> PyResult<Vec<OrderBookDepth10>> {
127    load_depth10_from_snapshot25(
128        filepath,
129        price_precision,
130        size_precision,
131        instrument_id,
132        limit,
133    )
134    .map_err(to_pyvalue_err)
135}
136
137#[pyfunction(name = "load_tardis_depth10_from_snapshot25_as_pycapsule")]
138#[pyo3(signature = (filepath, price_precision, size_precision, instrument_id=None, limit=None))]
139pub fn py_load_tardis_depth10_from_snapshot25_as_pycapsule(
140    py: Python,
141    filepath: PathBuf,
142    price_precision: u8,
143    size_precision: u8,
144    instrument_id: Option<InstrumentId>,
145    limit: Option<usize>,
146) -> PyResult<PyObject> {
147    let depths = load_depth10_from_snapshot25(
148        filepath,
149        price_precision,
150        size_precision,
151        instrument_id,
152        limit,
153    )
154    .map_err(to_pyvalue_err)?;
155    let depths: Vec<Data> = depths.into_iter().map(Data::Depth10).collect();
156
157    let cvec: CVec = depths.into();
158    let capsule = PyCapsule::new::<CVec>(py, cvec, None)?;
159    Ok(capsule.into_py(py))
160}
161
162#[pyfunction(name = "load_tardis_quotes")]
163#[pyo3(signature = (filepath, price_precision, size_precision, instrument_id=None, limit=None))]
164pub fn py_load_tardis_quotes(
165    filepath: PathBuf,
166    price_precision: u8,
167    size_precision: u8,
168    instrument_id: Option<InstrumentId>,
169    limit: Option<usize>,
170) -> PyResult<Vec<QuoteTick>> {
171    load_quote_ticks(
172        filepath,
173        price_precision,
174        size_precision,
175        instrument_id,
176        limit,
177    )
178    .map_err(to_pyvalue_err)
179}
180
181#[pyfunction(name = "load_tardis_quotes_as_pycapsule")]
182#[pyo3(signature = (filepath, price_precision, size_precision, instrument_id=None, limit=None))]
183pub fn py_load_tardis_quotes_as_pycapsule(
184    py: Python,
185    filepath: PathBuf,
186    price_precision: u8,
187    size_precision: u8,
188    instrument_id: Option<InstrumentId>,
189    limit: Option<usize>,
190) -> PyResult<PyObject> {
191    let quotes = load_quote_ticks(
192        filepath,
193        price_precision,
194        size_precision,
195        instrument_id,
196        limit,
197    )
198    .map_err(to_pyvalue_err)?;
199    let quotes: Vec<Data> = quotes.into_iter().map(Data::Quote).collect();
200
201    let cvec: CVec = quotes.into();
202    let capsule = PyCapsule::new::<CVec>(py, cvec, None)?;
203    Ok(capsule.into_py(py))
204}
205
206#[pyfunction(name = "load_tardis_trades")]
207#[pyo3(signature = (filepath, price_precision, size_precision, instrument_id=None, limit=None))]
208pub fn py_load_tardis_trades(
209    filepath: PathBuf,
210    price_precision: u8,
211    size_precision: u8,
212    instrument_id: Option<InstrumentId>,
213    limit: Option<usize>,
214) -> PyResult<Vec<TradeTick>> {
215    load_trade_ticks(
216        filepath,
217        price_precision,
218        size_precision,
219        instrument_id,
220        limit,
221    )
222    .map_err(to_pyvalue_err)
223}
224
225#[pyfunction(name = "load_tardis_trades_as_pycapsule")]
226#[pyo3(signature = (filepath, price_precision, size_precision, instrument_id=None, limit=None))]
227pub fn py_load_tardis_trades_as_pycapsule(
228    py: Python,
229    filepath: PathBuf,
230    price_precision: u8,
231    size_precision: u8,
232    instrument_id: Option<InstrumentId>,
233    limit: Option<usize>,
234) -> PyResult<PyObject> {
235    let trades = load_trade_ticks(
236        filepath,
237        price_precision,
238        size_precision,
239        instrument_id,
240        limit,
241    )
242    .map_err(to_pyvalue_err)?;
243    let trades: Vec<Data> = trades.into_iter().map(Data::Trade).collect();
244
245    let cvec: CVec = trades.into();
246    let capsule = PyCapsule::new::<CVec>(py, cvec, None)?;
247    Ok(capsule.into_py(py))
248}