nautilus_tardis/python/
http.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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 nautilus_core::{
17    UnixNanos,
18    python::{IntoPyObjectPoseiExt, enums::parse_enum, to_pyruntime_err},
19};
20use nautilus_model::python::instruments::instrument_any_to_pyobject;
21use pyo3::prelude::*;
22
23use crate::{
24    enums::Exchange,
25    http::{TardisHttpClient, query::InstrumentFilterBuilder},
26};
27
28#[pymethods]
29impl TardisHttpClient {
30    #[new]
31    #[pyo3(signature = (api_key=None, base_url=None, timeout_secs=None, normalize_symbols=true))]
32    fn py_new(
33        api_key: Option<&str>,
34        base_url: Option<&str>,
35        timeout_secs: Option<u64>,
36        normalize_symbols: bool,
37    ) -> PyResult<Self> {
38        Self::new(api_key, base_url, timeout_secs, normalize_symbols).map_err(to_pyruntime_err)
39    }
40
41    #[allow(clippy::too_many_arguments)]
42    #[pyo3(name = "instruments")]
43    #[pyo3(signature = (exchange, symbol=None, base_currency=None, quote_currency=None, instrument_type=None, contract_type=None, active=None, start=None, end=None, available_offset=None, effective=None, ts_init=None))]
44    fn py_instruments<'py>(
45        &self,
46        exchange: String,
47        symbol: Option<String>,
48        base_currency: Option<Vec<String>>,
49        quote_currency: Option<Vec<String>>,
50        instrument_type: Option<Vec<String>>,
51        contract_type: Option<Vec<String>>,
52        active: Option<bool>,
53        start: Option<u64>,
54        end: Option<u64>,
55        available_offset: Option<u64>,
56        effective: Option<u64>,
57        ts_init: Option<u64>,
58        py: Python<'py>,
59    ) -> PyResult<Bound<'py, PyAny>> {
60        let exchange: Exchange = parse_enum(&exchange, stringify!(exchange))?;
61
62        let filter = InstrumentFilterBuilder::default()
63            .base_currency(base_currency)
64            .quote_currency(quote_currency)
65            .instrument_type(instrument_type)
66            .contract_type(contract_type)
67            .active(active)
68            // NOTE: The Tardis instruments metadata API does not function correctly when using
69            // the `availableSince` and `availableTo` params.
70            // .available_since(start.map(|x| DateTime::from_timestamp_nanos(x as i64)))
71            // .available_to(end.map(|x| DateTime::from_timestamp_nanos(x as i64)))
72            .build()
73            .unwrap(); // SAFETY: Safe since all fields are Option
74
75        let self_clone = self.clone();
76
77        pyo3_async_runtimes::tokio::future_into_py(py, async move {
78            let instruments = self_clone
79                .instruments(
80                    exchange,
81                    symbol.as_deref(),
82                    Some(&filter),
83                    start.map(UnixNanos::from),
84                    end.map(UnixNanos::from),
85                    available_offset.map(UnixNanos::from),
86                    effective.map(UnixNanos::from),
87                    ts_init.map(UnixNanos::from),
88                )
89                .await
90                .map_err(to_pyruntime_err)?;
91
92            Python::with_gil(|py| {
93                let mut py_instruments = Vec::new();
94                for inst in instruments {
95                    py_instruments.push(instrument_any_to_pyobject(py, inst)?);
96                }
97                Ok(py_instruments.into_py_any_unwrap(py))
98            })
99        })
100    }
101}