nautilus_model/data/
status.rs

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//! An `InstrumentStatus` data type representing a change in an instrument market status.
17
18use std::{
19    collections::HashMap,
20    fmt::{Display, Formatter},
21    hash::Hash,
22};
23
24use derive_builder::Builder;
25use nautilus_core::{serialization::Serializable, UnixNanos};
26use serde::{Deserialize, Serialize};
27use ustr::Ustr;
28
29use super::GetTsInit;
30use crate::{enums::MarketStatusAction, identifiers::InstrumentId};
31
32/// Represents an event that indicates a change in an instrument market status.
33#[repr(C)]
34#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Builder)]
35#[serde(tag = "type")]
36#[cfg_attr(
37    feature = "python",
38    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
39)]
40pub struct InstrumentStatus {
41    /// The instrument ID for the status change.
42    pub instrument_id: InstrumentId,
43    /// The instrument market status action.
44    pub action: MarketStatusAction,
45    /// UNIX timestamp (nanoseconds) when the status event occurred.
46    pub ts_event: UnixNanos,
47    /// UNIX timestamp (nanoseconds) when the struct was initialized.
48    pub ts_init: UnixNanos,
49    /// Additional details about the cause of the status change.
50    pub reason: Option<Ustr>,
51    /// Further information about the status change (if provided).
52    pub trading_event: Option<Ustr>,
53    /// The state of trading in the instrument.
54    pub is_trading: Option<bool>,
55    /// The state of quoting in the instrument.
56    pub is_quoting: Option<bool>,
57    /// The state of short sell restrictions for the instrument (if applicable).
58    pub is_short_sell_restricted: Option<bool>,
59}
60
61impl InstrumentStatus {
62    /// Creates a new [`InstrumentStatus`] instance.
63    #[allow(clippy::too_many_arguments)]
64    pub fn new(
65        instrument_id: InstrumentId,
66        action: MarketStatusAction,
67        ts_event: UnixNanos,
68        ts_init: UnixNanos,
69        reason: Option<Ustr>,
70        trading_event: Option<Ustr>,
71        is_trading: Option<bool>,
72        is_quoting: Option<bool>,
73        is_short_sell_restricted: Option<bool>,
74    ) -> Self {
75        Self {
76            instrument_id,
77            action,
78            ts_event,
79            ts_init,
80            reason,
81            trading_event,
82            is_trading,
83            is_quoting,
84            is_short_sell_restricted,
85        }
86    }
87
88    /// Returns the metadata for the type, for use with serialization formats.
89    #[must_use]
90    pub fn get_metadata(instrument_id: &InstrumentId) -> HashMap<String, String> {
91        let mut metadata = HashMap::new();
92        metadata.insert("instrument_id".to_string(), instrument_id.to_string());
93        metadata
94    }
95}
96
97// TODO: Revisit this
98impl Display for InstrumentStatus {
99    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
100        write!(
101            f,
102            "{},{},{},{}",
103            self.instrument_id, self.action, self.ts_event, self.ts_init,
104        )
105    }
106}
107
108impl Serializable for InstrumentStatus {}
109
110impl GetTsInit for InstrumentStatus {
111    fn ts_init(&self) -> UnixNanos {
112        self.ts_init
113    }
114}
115
116////////////////////////////////////////////////////////////////////////////////
117// Tests
118////////////////////////////////////////////////////////////////////////////////
119#[cfg(test)]
120mod tests {
121    use nautilus_core::serialization::Serializable;
122    use rstest::rstest;
123
124    use super::*;
125    use crate::data::stubs::stub_instrument_status;
126
127    #[rstest]
128    fn test_to_string(stub_instrument_status: InstrumentStatus) {
129        assert_eq!(stub_instrument_status.to_string(), "MSFT.XNAS,TRADING,1,2");
130    }
131
132    #[rstest]
133    fn test_json_serialization(stub_instrument_status: InstrumentStatus) {
134        let serialized = stub_instrument_status.as_json_bytes().unwrap();
135        let deserialized = InstrumentStatus::from_json_bytes(serialized.as_ref()).unwrap();
136        assert_eq!(deserialized, stub_instrument_status);
137    }
138
139    #[rstest]
140    fn test_msgpack_serialization(stub_instrument_status: InstrumentStatus) {
141        let serialized = stub_instrument_status.as_msgpack_bytes().unwrap();
142        let deserialized = InstrumentStatus::from_msgpack_bytes(serialized.as_ref()).unwrap();
143        assert_eq!(deserialized, stub_instrument_status);
144    }
145}