nautilus_model/data/
close.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 `InstrumentClose` data type representing an instrument close at a venue.
17
18use std::{collections::HashMap, fmt::Display, hash::Hash};
19
20use nautilus_core::{UnixNanos, serialization::Serializable};
21use serde::{Deserialize, Serialize};
22
23use super::GetTsInit;
24use crate::{enums::InstrumentCloseType, identifiers::InstrumentId, types::Price};
25
26/// Represents an instrument close at a venue.
27#[repr(C)]
28#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
29#[serde(tag = "type")]
30#[cfg_attr(
31    feature = "python",
32    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
33)]
34pub struct InstrumentClose {
35    /// The instrument ID.
36    pub instrument_id: InstrumentId,
37    /// The closing price for the instrument.
38    pub close_price: Price,
39    /// The type of closing price.
40    pub close_type: InstrumentCloseType,
41    /// UNIX timestamp (nanoseconds) when the close price event occurred.
42    pub ts_event: UnixNanos,
43    /// UNIX timestamp (nanoseconds) when the struct was initialized.
44    pub ts_init: UnixNanos,
45}
46
47impl InstrumentClose {
48    /// Creates a new [`InstrumentClose`] instance.
49    pub fn new(
50        instrument_id: InstrumentId,
51        close_price: Price,
52        close_type: InstrumentCloseType,
53        ts_event: UnixNanos,
54        ts_init: UnixNanos,
55    ) -> Self {
56        Self {
57            instrument_id,
58            close_price,
59            close_type,
60            ts_event,
61            ts_init,
62        }
63    }
64
65    /// Returns the metadata for the type, for use with serialization formats.
66    #[must_use]
67    pub fn get_metadata(instrument_id: &InstrumentId) -> HashMap<String, String> {
68        let mut metadata = HashMap::new();
69        metadata.insert("instrument_id".to_string(), instrument_id.to_string());
70        metadata
71    }
72}
73
74impl Display for InstrumentClose {
75    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76        write!(
77            f,
78            "{},{},{},{}",
79            self.instrument_id, self.close_price, self.close_type, self.ts_event
80        )
81    }
82}
83
84impl Serializable for InstrumentClose {}
85
86impl GetTsInit for InstrumentClose {
87    fn ts_init(&self) -> UnixNanos {
88        self.ts_init
89    }
90}
91
92////////////////////////////////////////////////////////////////////////////////
93// Tests
94////////////////////////////////////////////////////////////////////////////////
95#[cfg(test)]
96mod tests {
97    use rstest::rstest;
98
99    use super::*;
100    use crate::{identifiers::InstrumentId, types::Price};
101
102    #[rstest]
103    fn test_new() {
104        let instrument_id = InstrumentId::from("AAPL.XNAS");
105        let close_price = Price::from("150.20");
106        let close_type = InstrumentCloseType::EndOfSession;
107        let ts_event = UnixNanos::from(1);
108        let ts_init = UnixNanos::from(2);
109
110        let instrument_close =
111            InstrumentClose::new(instrument_id, close_price, close_type, ts_event, ts_init);
112
113        assert_eq!(instrument_close.instrument_id, instrument_id);
114        assert_eq!(instrument_close.close_price, close_price);
115        assert_eq!(instrument_close.close_type, close_type);
116        assert_eq!(instrument_close.ts_event, ts_event);
117        assert_eq!(instrument_close.ts_init, ts_init);
118    }
119
120    #[rstest]
121    fn test_to_string() {
122        let instrument_id = InstrumentId::from("AAPL.XNAS");
123        let close_price = Price::from("150.20");
124        let close_type = InstrumentCloseType::EndOfSession;
125        let ts_event = UnixNanos::from(1);
126        let ts_init = UnixNanos::from(2);
127
128        let instrument_close =
129            InstrumentClose::new(instrument_id, close_price, close_type, ts_event, ts_init);
130
131        assert_eq!(
132            format!("{instrument_close}"),
133            "AAPL.XNAS,150.20,END_OF_SESSION,1"
134        );
135    }
136
137    #[rstest]
138    fn test_json_serialization() {
139        let instrument_id = InstrumentId::from("AAPL.XNAS");
140        let close_price = Price::from("150.20");
141        let close_type = InstrumentCloseType::EndOfSession;
142        let ts_event = UnixNanos::from(1);
143        let ts_init = UnixNanos::from(2);
144
145        let instrument_close =
146            InstrumentClose::new(instrument_id, close_price, close_type, ts_event, ts_init);
147
148        let serialized = instrument_close.as_json_bytes().unwrap();
149        let deserialized = InstrumentClose::from_json_bytes(serialized.as_ref()).unwrap();
150
151        assert_eq!(deserialized, instrument_close);
152    }
153
154    #[rstest]
155    fn test_msgpack_serialization() {
156        let instrument_id = InstrumentId::from("AAPL.XNAS");
157        let close_price = Price::from("150.20");
158        let close_type = InstrumentCloseType::EndOfSession;
159        let ts_event = UnixNanos::from(1);
160        let ts_init = UnixNanos::from(2);
161
162        let instrument_close =
163            InstrumentClose::new(instrument_id, close_price, close_type, ts_event, ts_init);
164
165        let serialized = instrument_close.as_msgpack_bytes().unwrap();
166        let deserialized = InstrumentClose::from_msgpack_bytes(serialized.as_ref()).unwrap();
167
168        assert_eq!(deserialized, instrument_close);
169    }
170}