Skip to main content

nautilus_bybit/common/
instruments.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//! Instrument definition diffing and emission for the Bybit adapter.
17
18use ahash::{AHashMap, AHashSet};
19use nautilus_common::{live::sender::EventSender, messages::DataEvent};
20use nautilus_model::{
21    identifiers::InstrumentId,
22    instruments::{Instrument, InstrumentAny},
23};
24
25/// Returns `true` if the economically meaningful fields of two instruments differ.
26///
27/// [`InstrumentAny`]'s `PartialEq` compares only the instrument ID, and definitions carry
28/// per-fetch timestamps that always change - so neither is usable for detecting "real" updates.
29/// This compares the fields a strategy prices and sizes against, ignoring timestamps.
30fn economics_differ(a: &InstrumentAny, b: &InstrumentAny) -> bool {
31    a.margin_init() != b.margin_init()
32        || a.margin_maint() != b.margin_maint()
33        || a.price_precision() != b.price_precision()
34        || a.size_precision() != b.size_precision()
35        || a.price_increment() != b.price_increment()
36        || a.size_increment() != b.size_increment()
37        || a.multiplier() != b.multiplier()
38        || a.lot_size() != b.lot_size()
39        || a.min_quantity() != b.min_quantity()
40        || a.max_quantity() != b.max_quantity()
41        || a.min_notional() != b.min_notional()
42        || a.max_notional() != b.max_notional()
43        || a.min_price() != b.min_price()
44        || a.max_price() != b.max_price()
45}
46
47/// Compares a fresh instrument snapshot against cached definitions, emitting [`DataEvent::Instrument`]
48/// events for new and economically-changed instruments.
49///
50/// The cache is updated to reflect each meaningful change regardless of subscription, while emissions
51/// are gated by `subscriptions`: only subscribed instruments produce events. Pass `None` to emit for
52/// all changes (e.g. a venue-wide subscription).
53pub fn diff_and_emit_instruments(
54    new_instruments: &[InstrumentAny],
55    cached: &mut AHashMap<InstrumentId, InstrumentAny>,
56    subscriptions: Option<&AHashSet<InstrumentId>>,
57    sender: &EventSender<DataEvent>,
58) {
59    let is_subscribed = |id: &InstrumentId| subscriptions.is_none_or(|subs| subs.contains(id));
60
61    for instrument in new_instruments {
62        let id = instrument.id();
63        let changed = cached
64            .get(&id)
65            .is_none_or(|prev| economics_differ(prev, instrument));
66
67        if changed {
68            cached.insert(id, instrument.clone());
69            if is_subscribed(&id)
70                && let Err(e) = sender.send(DataEvent::Instrument(instrument.clone()))
71            {
72                log::error!("Failed to emit instrument event: {e}");
73            }
74        }
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use nautilus_core::UnixNanos;
81    use nautilus_model::{
82        identifiers::{InstrumentId, Symbol},
83        instruments::{CryptoPerpetual, InstrumentAny},
84        types::{Currency, Money, Price, Quantity},
85    };
86    use rstest::rstest;
87    use rust_decimal::Decimal;
88
89    use super::*;
90
91    /// Builds a BTCUSDT linear perp with the given economic fields; everything else is fixed so a
92    /// single varied field is what the diff sees.
93    fn perp(
94        margin_init: Decimal,
95        size_increment: Quantity,
96        min_notional: Option<Money>,
97    ) -> InstrumentAny {
98        InstrumentAny::CryptoPerpetual(
99            CryptoPerpetual::builder()
100                .instrument_id(InstrumentId::from("BTCUSDT-LINEAR.BYBIT"))
101                .raw_symbol(Symbol::from("BTCUSDT-LINEAR"))
102                .base_currency(Currency::BTC())
103                .quote_currency(Currency::USDT())
104                .settlement_currency(Currency::USDT())
105                .is_inverse(false)
106                .price_precision(1)
107                .size_precision(3)
108                .price_increment(Price::from("0.1"))
109                .size_increment(size_increment)
110                .min_quantity(Quantity::from("0.001"))
111                .margin_init(margin_init)
112                .maybe_min_notional(min_notional)
113                .ts_event(UnixNanos::default())
114                .ts_init(UnixNanos::default())
115                .build()
116                .unwrap(),
117        )
118    }
119
120    fn default_perp() -> InstrumentAny {
121        perp(Decimal::ZERO, Quantity::from("0.001"), None)
122    }
123
124    #[rstest]
125    fn test_emits_for_new_instrument() {
126        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
127        let instrument = default_perp();
128        let mut cached = AHashMap::new();
129
130        diff_and_emit_instruments(
131            std::slice::from_ref(&instrument),
132            &mut cached,
133            None,
134            &tx.into(),
135        );
136
137        match rx.try_recv().expect("expected instrument event") {
138            DataEvent::Instrument(emitted) => assert_eq!(emitted.id(), instrument.id()),
139            _ => panic!("expected Instrument event"),
140        }
141        assert!(cached.contains_key(&instrument.id()));
142    }
143
144    #[rstest]
145    fn test_no_emit_when_unchanged() {
146        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
147        let instrument = default_perp();
148        let mut cached = AHashMap::new();
149        cached.insert(instrument.id(), default_perp());
150
151        diff_and_emit_instruments(&[instrument], &mut cached, None, &tx.into());
152
153        assert!(rx.try_recv().is_err());
154    }
155
156    #[rstest]
157    fn test_emits_on_margin_change() {
158        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
159        let id = default_perp().id();
160        let mut cached = AHashMap::new();
161        cached.insert(id, default_perp());
162
163        // Same instrument, higher initial margin.
164        let updated = perp(Decimal::ONE, Quantity::from("0.001"), None);
165        diff_and_emit_instruments(&[updated], &mut cached, None, &tx.into());
166
167        match rx
168            .try_recv()
169            .expect("expected instrument event on margin change")
170        {
171            DataEvent::Instrument(emitted) => assert_eq!(emitted.margin_init(), Decimal::ONE),
172            _ => panic!("expected Instrument event"),
173        }
174    }
175
176    #[rstest]
177    fn test_emits_on_size_increment_and_min_notional_change() {
178        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
179        let id = default_perp().id();
180
181        // size_increment change
182        let mut cached = AHashMap::new();
183        cached.insert(id, default_perp());
184        let bigger_step = perp(Decimal::ZERO, Quantity::from("0.002"), None);
185        diff_and_emit_instruments(&[bigger_step], &mut cached, None, &tx.clone().into());
186        assert!(rx.try_recv().is_ok(), "size_increment change should emit");
187
188        // min_notional change
189        let mut cached = AHashMap::new();
190        cached.insert(id, default_perp());
191        let with_min = perp(
192            Decimal::ZERO,
193            Quantity::from("0.001"),
194            Some(Money::new(5.0, Currency::USDT())),
195        );
196        diff_and_emit_instruments(&[with_min], &mut cached, None, &tx.into());
197        assert!(rx.try_recv().is_ok(), "min_notional change should emit");
198    }
199
200    #[rstest]
201    fn test_subscription_gating_updates_cache_but_only_emits_for_subscribed() {
202        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
203        let id = default_perp().id();
204
205        // Not subscribed: cache still updates, but no event.
206        let empty_subs = AHashSet::new();
207        let mut cached = AHashMap::new();
208        cached.insert(id, default_perp());
209        let updated = perp(Decimal::ONE, Quantity::from("0.001"), None);
210
211        diff_and_emit_instruments(
212            &[updated],
213            &mut cached,
214            Some(&empty_subs),
215            &tx.clone().into(),
216        );
217
218        assert!(rx.try_recv().is_err(), "unsubscribed should not emit");
219        assert_eq!(
220            cached.get(&id).unwrap().margin_init(),
221            Decimal::ONE,
222            "cache should update regardless of subscription"
223        );
224
225        // Subscribed: emits.
226        let mut subs = AHashSet::new();
227        subs.insert(id);
228        let mut cached = AHashMap::new();
229        cached.insert(id, default_perp());
230        let updated = perp(Decimal::ONE, Quantity::from("0.001"), None);
231
232        diff_and_emit_instruments(&[updated], &mut cached, Some(&subs), &tx.into());
233
234        assert!(rx.try_recv().is_ok(), "subscribed should emit");
235    }
236}