nautilus_binance/common/symbol.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//! Binance symbol conversion utilities.
17
18use nautilus_model::identifiers::InstrumentId;
19use ustr::Ustr;
20
21use super::{consts::BINANCE_VENUE, enums::BinanceProductType};
22
23/// Converts a Binance symbol to a Nautilus instrument ID.
24///
25/// For USD-M futures, appends "-PERP" suffix to match Nautilus symbology.
26/// For COIN-M futures, keeps the symbol as-is (uses "_PERP" format).
27///
28/// # Examples
29///
30/// - ("BTCUSDT", UsdM) → "BTCUSDT-PERP.BINANCE"
31/// - ("ETHUSD_PERP", CoinM) → "ETHUSD_PERP.BINANCE"
32#[must_use]
33pub fn format_instrument_id(symbol: &Ustr, product_type: BinanceProductType) -> InstrumentId {
34 let nautilus_symbol = match product_type {
35 BinanceProductType::UsdM => {
36 // USD-M symbols don't have _PERP suffix from Binance, we add -PERP
37 format!("{symbol}-PERP")
38 }
39 BinanceProductType::CoinM => {
40 // COIN-M symbols already have _PERP suffix from Binance
41 symbol.to_string()
42 }
43 _ => symbol.to_string(),
44 };
45 InstrumentId::new(nautilus_symbol.into(), *BINANCE_VENUE)
46}
47
48/// Converts a Nautilus instrument ID to a Binance-compatible symbol.
49///
50/// This function strips common suffixes like "-PERP" that Nautilus uses for
51/// internal symbology but Binance doesn't recognize.
52///
53/// # Examples
54///
55/// - "BTCUSDT-PERP" → "BTCUSDT"
56/// - "ETHUSD_PERP" → "ETHUSD_PERP" (COIN-M format, kept as-is)
57/// - "BTCUSDT" → "BTCUSDT"
58#[must_use]
59pub fn format_binance_symbol(instrument_id: &InstrumentId) -> String {
60 let symbol = instrument_id.symbol.as_str();
61
62 if symbol.ends_with("-PERP") {
63 symbol.trim_end_matches("-PERP").to_string()
64 } else {
65 symbol.to_string()
66 }
67}
68
69/// Converts a Nautilus instrument ID to a lowercase Binance WebSocket stream symbol.
70///
71/// This is used for constructing WebSocket stream names which require lowercase symbols.
72#[must_use]
73pub fn format_binance_stream_symbol(instrument_id: &InstrumentId) -> String {
74 format_binance_symbol(instrument_id).to_lowercase()
75}
76
77#[cfg(test)]
78mod tests {
79 use rstest::rstest;
80
81 use super::*;
82
83 #[rstest]
84 #[case("BTCUSDT-PERP.BINANCE", "BTCUSDT")]
85 #[case("ETHUSDT-PERP.BINANCE", "ETHUSDT")]
86 #[case("BTCUSD_PERP.BINANCE", "BTCUSD_PERP")]
87 #[case("BTCUSDT.BINANCE", "BTCUSDT")]
88 #[case("ETHBTC.BINANCE", "ETHBTC")]
89 fn test_format_binance_symbol(#[case] input: &str, #[case] expected: &str) {
90 let instrument_id = InstrumentId::from(input);
91 assert_eq!(format_binance_symbol(&instrument_id), expected);
92 }
93
94 #[rstest]
95 #[case("BTCUSDT-PERP.BINANCE", "btcusdt")]
96 #[case("ETHUSDT-PERP.BINANCE", "ethusdt")]
97 #[case("BTCUSD_PERP.BINANCE", "btcusd_perp")]
98 fn test_format_binance_stream_symbol(#[case] input: &str, #[case] expected: &str) {
99 let instrument_id = InstrumentId::from(input);
100 assert_eq!(format_binance_stream_symbol(&instrument_id), expected);
101 }
102}