nautilus_model/data/
close.rs
1use 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#[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 pub instrument_id: InstrumentId,
37 pub close_price: Price,
39 pub close_type: InstrumentCloseType,
41 pub ts_event: UnixNanos,
43 pub ts_init: UnixNanos,
45}
46
47impl InstrumentClose {
48 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 #[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#[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}