nautilus_model/data/
close.rs1use std::{collections::HashMap, fmt::Display, hash::Hash};
19
20use indexmap::IndexMap;
21use nautilus_core::{UnixNanos, serialization::Serializable};
22use serde::{Deserialize, Serialize};
23
24use super::{ARROW_ENUM_DICTIONARY, ARROW_TIMESTAMP_NANOSECOND, HasTsInit};
25use crate::{
26 enums::InstrumentCloseType,
27 identifiers::InstrumentId,
28 types::{Price, fixed::FIXED_DECIMAL},
29};
30
31#[repr(C)]
33#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
34#[serde(tag = "type")]
35#[cfg_attr(
36 feature = "python",
37 pyo3::pyclass(module = "nautilus_trader.model", from_py_object)
38)]
39#[cfg_attr(
40 feature = "python",
41 pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
42)]
43pub struct InstrumentClose {
44 pub instrument_id: InstrumentId,
46 pub close_price: Price,
48 pub close_type: InstrumentCloseType,
50 pub ts_event: UnixNanos,
52 pub ts_init: UnixNanos,
54}
55
56impl InstrumentClose {
57 #[must_use]
59 pub fn new(
60 instrument_id: InstrumentId,
61 close_price: Price,
62 close_type: InstrumentCloseType,
63 ts_event: UnixNanos,
64 ts_init: UnixNanos,
65 ) -> Self {
66 Self {
67 instrument_id,
68 close_price,
69 close_type,
70 ts_event,
71 ts_init,
72 }
73 }
74
75 #[must_use]
77 pub fn get_metadata(
78 instrument_id: &InstrumentId,
79 price_precision: u8,
80 ) -> HashMap<String, String> {
81 let mut metadata = HashMap::new();
82 metadata.insert("instrument_id".to_string(), instrument_id.to_string());
83 metadata.insert("price_precision".to_string(), price_precision.to_string());
84 metadata
85 }
86
87 #[must_use]
89 pub fn get_fields() -> IndexMap<String, String> {
90 let mut metadata = IndexMap::new();
91 metadata.insert("close_price".to_string(), FIXED_DECIMAL.to_string());
92 metadata.insert("close_type".to_string(), ARROW_ENUM_DICTIONARY.to_string());
93 metadata.insert(
94 "ts_event".to_string(),
95 ARROW_TIMESTAMP_NANOSECOND.to_string(),
96 );
97 metadata.insert(
98 "ts_init".to_string(),
99 ARROW_TIMESTAMP_NANOSECOND.to_string(),
100 );
101 metadata
102 }
103}
104
105impl Display for InstrumentClose {
106 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
107 write!(
108 f,
109 "{},{},{},{}",
110 self.instrument_id, self.close_price, self.close_type, self.ts_event
111 )
112 }
113}
114
115impl Serializable for InstrumentClose {}
116
117impl HasTsInit for InstrumentClose {
118 fn ts_init(&self) -> UnixNanos {
119 self.ts_init
120 }
121}
122
123#[cfg(test)]
124mod tests {
125 use nautilus_core::serialization::msgpack::{FromMsgPack, ToMsgPack};
126 use rstest::rstest;
127
128 use super::*;
129 use crate::{identifiers::InstrumentId, types::Price};
130
131 #[rstest]
132 fn test_new() {
133 let instrument_id = InstrumentId::from("AAPL.XNAS");
134 let close_price = Price::from("150.20");
135 let close_type = InstrumentCloseType::EndOfSession;
136 let ts_event = UnixNanos::from(1);
137 let ts_init = UnixNanos::from(2);
138
139 let instrument_close =
140 InstrumentClose::new(instrument_id, close_price, close_type, ts_event, ts_init);
141
142 assert_eq!(instrument_close.instrument_id, instrument_id);
143 assert_eq!(instrument_close.close_price, close_price);
144 assert_eq!(instrument_close.close_type, close_type);
145 assert_eq!(instrument_close.ts_event, ts_event);
146 assert_eq!(instrument_close.ts_init, ts_init);
147 }
148
149 #[rstest]
150 fn test_to_string() {
151 let instrument_id = InstrumentId::from("AAPL.XNAS");
152 let close_price = Price::from("150.20");
153 let close_type = InstrumentCloseType::EndOfSession;
154 let ts_event = UnixNanos::from(1);
155 let ts_init = UnixNanos::from(2);
156
157 let instrument_close =
158 InstrumentClose::new(instrument_id, close_price, close_type, ts_event, ts_init);
159
160 assert_eq!(
161 format!("{instrument_close}"),
162 "AAPL.XNAS,150.20,END_OF_SESSION,1"
163 );
164 }
165
166 #[rstest]
167 fn test_json_serialization() {
168 let instrument_id = InstrumentId::from("AAPL.XNAS");
169 let close_price = Price::from("150.20");
170 let close_type = InstrumentCloseType::EndOfSession;
171 let ts_event = UnixNanos::from(1);
172 let ts_init = UnixNanos::from(2);
173
174 let instrument_close =
175 InstrumentClose::new(instrument_id, close_price, close_type, ts_event, ts_init);
176
177 let serialized = instrument_close.to_json_bytes().unwrap();
178 let deserialized = InstrumentClose::from_json_bytes(serialized.as_ref()).unwrap();
179
180 assert_eq!(deserialized, instrument_close);
181 }
182
183 #[rstest]
184 fn test_msgpack_serialization() {
185 let instrument_id = InstrumentId::from("AAPL.XNAS");
186 let close_price = Price::from("150.20");
187 let close_type = InstrumentCloseType::EndOfSession;
188 let ts_event = UnixNanos::from(1);
189 let ts_init = UnixNanos::from(2);
190
191 let instrument_close =
192 InstrumentClose::new(instrument_id, close_price, close_type, ts_event, ts_init);
193
194 let serialized = instrument_close.to_msgpack_bytes().unwrap();
195 let deserialized = InstrumentClose::from_msgpack_bytes(serialized.as_ref()).unwrap();
196
197 assert_eq!(deserialized, instrument_close);
198 }
199}