Skip to main content

nautilus_coinapi/python/
csv.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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
16//! Python iterators for CoinAPI flat-file streams.
17
18use std::fmt::Debug;
19
20use nautilus_core::python::to_pyvalue_err;
21use nautilus_model::data::{OrderBookDelta, QuoteTick, TradeTick};
22use pyo3::prelude::*;
23
24macro_rules! impl_coinapi_stream_iterator {
25    ($struct_name:ident, $data_type:ty) => {
26        #[pyclass(unsendable)]
27        #[pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.adapters.coinapi")]
28        pub struct $struct_name {
29            pub(crate) stream: Box<dyn Iterator<Item = anyhow::Result<Vec<$data_type>>>>,
30        }
31
32        impl Debug for $struct_name {
33            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34                write!(f, "{} {{ stream: ... }}", stringify!($struct_name))
35            }
36        }
37
38        #[pymethods]
39        #[pyo3_stub_gen::derive::gen_stub_pymethods]
40        impl $struct_name {
41            const fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
42                slf
43            }
44
45            fn __next__(&mut self) -> PyResult<Option<Vec<$data_type>>> {
46                match self.stream.next() {
47                    Some(Ok(chunk)) => Ok(Some(chunk)),
48                    Some(Err(e)) => Err(to_pyvalue_err(e)),
49                    None => Ok(None),
50                }
51            }
52        }
53    };
54}
55
56impl_coinapi_stream_iterator!(CoinApiTradeStreamIterator, TradeTick);
57impl_coinapi_stream_iterator!(CoinApiQuoteStreamIterator, QuoteTick);
58impl_coinapi_stream_iterator!(CoinApiDeltaStreamIterator, OrderBookDelta);
59
60#[cfg(test)]
61mod tests {
62    use std::{collections::HashMap, path::PathBuf};
63
64    use nautilus_core::UnixNanos;
65    use nautilus_model::{
66        data::{Bar, BarType},
67        identifiers::InstrumentId,
68        types::{Price, Quantity},
69    };
70    use pyo3::exceptions::PyValueError;
71    use rstest::rstest;
72
73    use super::*;
74    use crate::csv::{load_bars, stream_quotes};
75
76    #[rstest]
77    fn quote_iterator_preserves_chunks_limit_values_and_eof() {
78        let directory = tempfile::TempDir::new().unwrap();
79        let path = directory.path().join("quotes.csv");
80        std::fs::write(
81            &path,
82            "time_exchange;time_coinapi;ask_px;ask_sx;bid_px;bid_sx\n\
83            2025-09-01T00:00:00.1000000;2025-09-01T00:00:00.2000000;101.25;2.5;100.5;1\n\
84            2025-09-01T00:00:01.3000000;2025-09-01T00:00:01.4000000;101.375;2.0625;100.125;0.125\n\
85            2025-09-01T00:00:02;2025-09-01T00:00:03;102;3;101;2\n",
86        )
87        .unwrap();
88        let instrument_id = InstrumentId::from("BTC-USD.COINBASE");
89        let decoder = stream_quotes(path, instrument_id, 1, Some(3), Some(4), Some(2)).unwrap();
90
91        let mut stream = CoinApiQuoteStreamIterator {
92            stream: Box::new(decoder),
93        };
94
95        let first = stream.__next__().unwrap();
96        let second = stream.__next__().unwrap();
97        let end = stream.__next__().unwrap();
98        let repeated_end = stream.__next__().unwrap();
99
100        assert_eq!(
101            first,
102            Some(vec![QuoteTick::new(
103                instrument_id,
104                Price::from("100.500"),
105                Price::from("101.250"),
106                Quantity::from("1.0000"),
107                Quantity::from("2.5000"),
108                UnixNanos::from(1_756_684_800_100_000_000),
109                UnixNanos::from(1_756_684_800_200_000_000)
110            )])
111        );
112        assert_eq!(
113            second,
114            Some(vec![QuoteTick::new(
115                instrument_id,
116                Price::from("100.125"),
117                Price::from("101.375"),
118                Quantity::from("0.1250"),
119                Quantity::from("2.0625"),
120                UnixNanos::from(1_756_684_801_300_000_000),
121                UnixNanos::from(1_756_684_801_400_000_000)
122            )])
123        );
124        assert_eq!(
125            first
126                .into_iter()
127                .chain(second)
128                .flatten()
129                .map(|quote| (
130                    quote.bid_price.precision,
131                    quote.ask_price.precision,
132                    quote.bid_size.precision,
133                    quote.ask_size.precision
134                ))
135                .collect::<Vec<_>>(),
136            [(3, 3, 4, 4); 2]
137        );
138        assert_eq!(end, None);
139        assert_eq!(repeated_end, None);
140    }
141
142    #[rstest]
143    fn quote_iterator_reports_precision_errors_as_value_error() {
144        Python::initialize();
145        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
146            .join("test_data/csv/quotes_binance_spot_btc_usdt_20250901.csv");
147        let decoder = stream_quotes(
148            path,
149            InstrumentId::from("BTCUSDT.BINANCE"),
150            1,
151            Some(1),
152            None,
153            None,
154        )
155        .unwrap();
156
157        let mut stream = CoinApiQuoteStreamIterator {
158            stream: Box::new(decoder),
159        };
160
161        let error = stream.__next__().unwrap_err();
162
163        Python::attach(|py| {
164            assert!(error.is_instance_of::<PyValueError>(py));
165            assert_eq!(
166                error.value(py).to_string(),
167                "bid_px 108246.35 is finer than instrument precision 1"
168            );
169        });
170    }
171
172    #[rstest]
173    fn ohlcv_decoding_retains_symbol_mapping() {
174        let directory = tempfile::TempDir::new().unwrap();
175        let path = directory.path().join("TP-1MIN.csv");
176        std::fs::write(
177            &path,
178            "symbol_id;time_period_end;px_open;px_high;px_low;px_close;sx_sum\n\
179            99;2025-09-01T00:01:00;1;2;0.5;1.5;4\n\
180            7;2025-09-01T00:02:00;101.25;103.5;99.75;102.5;4.125\n",
181        )
182        .unwrap();
183        let symbols = HashMap::from([(7, (InstrumentId::from("BTC-USD.COINBASE"), 2))]);
184        let bars = load_bars(path, &symbols, None, Some(1)).unwrap();
185
186        let timestamp = UnixNanos::from(1_756_684_920_000_000_000);
187        assert_eq!(
188            bars,
189            [Bar::new(
190                BarType::from("BTC-USD.COINBASE-1-MINUTE-LAST-EXTERNAL"),
191                Price::from("101.25"),
192                Price::from("103.50"),
193                Price::from("99.75"),
194                Price::from("102.50"),
195                Quantity::from("4.125"),
196                timestamp,
197                timestamp
198            )]
199        );
200        assert_eq!(
201            (
202                bars[0].open.precision,
203                bars[0].high.precision,
204                bars[0].low.precision,
205                bars[0].close.precision,
206                bars[0].volume.precision
207            ),
208            (2, 2, 2, 2, 3)
209        );
210    }
211}