Skip to content

Commit e83a4c7

Browse files
committed
Add tardis_normalize_symbol_str function for Python
1 parent a06d66a commit e83a4c7

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

crates/adapters/tardis/src/enums.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use strum::{AsRefStr, Display, EnumIter, EnumString, FromRepr};
1919
use ustr::Ustr;
2020

2121
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Display, FromRepr, EnumString)]
22+
#[strum(ascii_case_insensitive)]
23+
#[strum(serialize_all = "lowercase")]
2224
#[serde(rename_all = "lowercase")]
2325
/// The instrument type for the symbol.
2426
pub enum InstrumentType {

crates/adapters/tardis/src/python/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,29 @@ pub mod enums;
2121
pub mod http;
2222
pub mod machine;
2323

24+
use nautilus_core::python::to_pyvalue_err;
25+
use std::str::FromStr;
26+
2427
use pyo3::prelude::*;
28+
use ustr::Ustr;
29+
30+
use super::enums::{Exchange, InstrumentType};
31+
use crate::parse::normalize_symbol_str;
32+
33+
#[pyfunction(name = "tardis_normalize_symbol_str")]
34+
#[pyo3(signature = (symbol, exchange, instrument_type, is_inverse=None))]
35+
pub fn py_tardis_normalize_symbol_str(
36+
symbol: String,
37+
exchange: String,
38+
instrument_type: String,
39+
is_inverse: Option<bool>,
40+
) -> PyResult<String> {
41+
let symbol = Ustr::from(&symbol);
42+
let exchange = Exchange::from_str(&exchange).map_err(to_pyvalue_err)?;
43+
let instrument_type = InstrumentType::from_str(&instrument_type).map_err(to_pyvalue_err)?;
44+
45+
Ok(normalize_symbol_str(symbol, &exchange, &instrument_type, is_inverse).to_string())
46+
}
2547

2648
/// Loaded as nautilus_pyo3.tardis
2749
///
@@ -55,6 +77,7 @@ pub fn tardis(_: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
5577
)?)?;
5678
m.add_function(wrap_pyfunction!(csv::py_load_tardis_quotes, m)?)?;
5779
m.add_function(wrap_pyfunction!(csv::py_load_tardis_trades, m)?)?;
80+
m.add_function(wrap_pyfunction!(py_tardis_normalize_symbol_str, m)?)?;
5881

5982
Ok(())
6083
}

nautilus_trader/core/nautilus_pyo3.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -5008,7 +5008,8 @@ class DatabentoLiveClient:
50085008

50095009
# Tardis
50105010

5011-
def tardis_exchange_from_venue_str(venue_str: str) -> list[str]: ...
5011+
def tardis_normalize_symbol_str(symbol: str, exchange: str, instrument_type: str, is_inverse: bool | None = None) -> str: ...
5012+
def tardis_exchange_from_venue_str(value: str) -> list[str]: ...
50125013
def bar_spec_to_tardis_trade_bar_string(bar_spec: BarSpecification) -> str: ...
50135014

50145015
def load_tardis_deltas(filepath: str, price_precision: int | None = None, size_precision: int | None = None, instrument_id: InstrumentId | None = None, limit: int | None = None) -> list[OrderBookDelta]: ... # noqa
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
16+
from nautilus_trader.core import nautilus_pyo3
17+
18+
19+
def test_normalize_symbol_str() -> None:
20+
# Arrange, Act
21+
result = nautilus_pyo3.tardis_normalize_symbol_str(
22+
symbol="BTCUSDT",
23+
exchange="binance-futures",
24+
instrument_type="perpetual",
25+
is_inverse=False,
26+
)
27+
28+
# Assert
29+
assert result == "BTCUSDT-PERP"

0 commit comments

Comments
 (0)