nautilus_model/events/order/
updated.rs1use std::fmt::{Debug, Display};
17
18use derive_builder::Builder;
19use nautilus_core::{UUID4, UnixNanos, serialization::from_bool_as_u8};
20use rust_decimal::Decimal;
21use serde::{Deserialize, Serialize};
22use ustr::Ustr;
23
24use crate::{
25 enums::{
26 ContingencyType, LiquiditySide, OrderSide, OrderType, TimeInForce, TrailingOffsetType,
27 TriggerType,
28 },
29 events::OrderEvent,
30 identifiers::{
31 AccountId, ClientOrderId, ExecAlgorithmId, InstrumentId, OrderListId, PositionId,
32 StrategyId, TradeId, TraderId, VenueOrderId,
33 },
34 types::{Currency, Money, Price, Quantity},
35};
36
37#[repr(C)]
38#[derive(Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, Builder)]
39#[builder(default)]
40#[serde(tag = "type")]
41#[cfg_attr(
42 feature = "python",
43 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
44)]
45pub struct OrderUpdated {
46 pub trader_id: TraderId,
48 pub strategy_id: StrategyId,
50 pub instrument_id: InstrumentId,
52 pub client_order_id: ClientOrderId,
54 pub venue_order_id: Option<VenueOrderId>,
56 pub account_id: Option<AccountId>,
58 pub quantity: Quantity,
60 pub price: Option<Price>,
62 pub trigger_price: Option<Price>,
64 pub protection_price: Option<Price>,
66 pub event_id: UUID4,
68 pub ts_event: UnixNanos,
70 pub ts_init: UnixNanos,
72 #[serde(deserialize_with = "from_bool_as_u8")]
74 pub reconciliation: u8, }
76
77impl OrderUpdated {
78 #[allow(clippy::too_many_arguments)]
80 pub fn new(
81 trader_id: TraderId,
82 strategy_id: StrategyId,
83 instrument_id: InstrumentId,
84 client_order_id: ClientOrderId,
85 quantity: Quantity,
86 event_id: UUID4,
87 ts_event: UnixNanos,
88 ts_init: UnixNanos,
89 reconciliation: bool,
90 venue_order_id: Option<VenueOrderId>,
91 account_id: Option<AccountId>,
92 price: Option<Price>,
93 trigger_price: Option<Price>,
94 protection_price: Option<Price>,
95 ) -> Self {
96 Self {
97 trader_id,
98 strategy_id,
99 instrument_id,
100 client_order_id,
101 quantity,
102 event_id,
103 ts_event,
104 ts_init,
105 reconciliation: u8::from(reconciliation),
106 venue_order_id,
107 account_id,
108 price,
109 trigger_price,
110 protection_price,
111 }
112 }
113}
114
115impl Debug for OrderUpdated {
116 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117 write!(
118 f,
119 "{}(trader_id={}, strategy_id={}, instrument_id={}, client_order_id={}, \
120 venue_order_id={}, account_id={}, quantity={}, price={}, trigger_price={}, protection_price={}, event_id={}, ts_event={}, ts_init={})",
121 stringify!(OrderUpdated),
122 self.trader_id,
123 self.strategy_id,
124 self.instrument_id,
125 self.client_order_id,
126 self.venue_order_id
127 .map_or("None".to_string(), |venue_order_id| format!(
128 "{venue_order_id}"
129 )),
130 self.account_id
131 .map_or("None".to_string(), |account_id| format!("{account_id}")),
132 self.quantity,
133 self.price
134 .map_or("None".to_string(), |price| price.to_formatted_string()),
135 self.trigger_price
136 .map_or("None".to_string(), |trigger_price| trigger_price
137 .to_formatted_string()),
138 self.protection_price
139 .map_or("None".to_string(), |protection_price| protection_price
140 .to_formatted_string()),
141 self.event_id,
142 self.ts_event,
143 self.ts_init
144 )
145 }
146}
147
148impl Display for OrderUpdated {
149 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
150 write!(
151 f,
152 "{}(instrument_id={}, client_order_id={}, venue_order_id={}, account_id={}, quantity={}, price={}, trigger_price={}, protection_price={}, ts_event={})",
153 stringify!(OrderUpdated),
154 self.instrument_id,
155 self.client_order_id,
156 self.venue_order_id
157 .map_or("None".to_string(), |venue_order_id| format!(
158 "{venue_order_id}"
159 )),
160 self.account_id
161 .map_or("None".to_string(), |account_id| format!("{account_id}")),
162 self.quantity.to_formatted_string(),
163 self.price
164 .map_or("None".to_string(), |price| price.to_formatted_string()),
165 self.trigger_price
166 .map_or("None".to_string(), |trigger_price| trigger_price
167 .to_formatted_string()),
168 self.protection_price
169 .map_or("None".to_string(), |protection_price| protection_price
170 .to_formatted_string()),
171 self.ts_event
172 )
173 }
174}
175
176impl OrderEvent for OrderUpdated {
177 fn id(&self) -> UUID4 {
178 self.event_id
179 }
180
181 fn kind(&self) -> &str {
182 stringify!(OrderUpdated)
183 }
184
185 fn order_type(&self) -> Option<OrderType> {
186 None
187 }
188
189 fn order_side(&self) -> Option<OrderSide> {
190 None
191 }
192
193 fn trader_id(&self) -> TraderId {
194 self.trader_id
195 }
196
197 fn strategy_id(&self) -> StrategyId {
198 self.strategy_id
199 }
200
201 fn instrument_id(&self) -> InstrumentId {
202 self.instrument_id
203 }
204
205 fn trade_id(&self) -> Option<TradeId> {
206 None
207 }
208
209 fn currency(&self) -> Option<Currency> {
210 None
211 }
212
213 fn client_order_id(&self) -> ClientOrderId {
214 self.client_order_id
215 }
216
217 fn reason(&self) -> Option<Ustr> {
218 None
219 }
220
221 fn quantity(&self) -> Option<Quantity> {
222 Some(self.quantity)
223 }
224
225 fn time_in_force(&self) -> Option<TimeInForce> {
226 None
227 }
228
229 fn liquidity_side(&self) -> Option<LiquiditySide> {
230 None
231 }
232
233 fn post_only(&self) -> Option<bool> {
234 None
235 }
236
237 fn reduce_only(&self) -> Option<bool> {
238 None
239 }
240
241 fn quote_quantity(&self) -> Option<bool> {
242 None
243 }
244
245 fn reconciliation(&self) -> bool {
246 false
247 }
248
249 fn price(&self) -> Option<Price> {
250 self.price
251 }
252
253 fn last_px(&self) -> Option<Price> {
254 None
255 }
256
257 fn last_qty(&self) -> Option<Quantity> {
258 None
259 }
260
261 fn trigger_price(&self) -> Option<Price> {
262 self.trigger_price
263 }
264
265 fn trigger_type(&self) -> Option<TriggerType> {
266 None
267 }
268
269 fn limit_offset(&self) -> Option<Decimal> {
270 None
271 }
272
273 fn trailing_offset(&self) -> Option<Decimal> {
274 None
275 }
276
277 fn trailing_offset_type(&self) -> Option<TrailingOffsetType> {
278 None
279 }
280
281 fn expire_time(&self) -> Option<UnixNanos> {
282 None
283 }
284
285 fn display_qty(&self) -> Option<Quantity> {
286 None
287 }
288
289 fn emulation_trigger(&self) -> Option<TriggerType> {
290 None
291 }
292
293 fn trigger_instrument_id(&self) -> Option<InstrumentId> {
294 None
295 }
296
297 fn contingency_type(&self) -> Option<ContingencyType> {
298 None
299 }
300
301 fn order_list_id(&self) -> Option<OrderListId> {
302 None
303 }
304
305 fn linked_order_ids(&self) -> Option<Vec<ClientOrderId>> {
306 None
307 }
308
309 fn parent_order_id(&self) -> Option<ClientOrderId> {
310 None
311 }
312
313 fn exec_algorithm_id(&self) -> Option<ExecAlgorithmId> {
314 None
315 }
316
317 fn exec_spawn_id(&self) -> Option<ClientOrderId> {
318 None
319 }
320
321 fn venue_order_id(&self) -> Option<VenueOrderId> {
322 self.venue_order_id
323 }
324
325 fn account_id(&self) -> Option<AccountId> {
326 self.account_id
327 }
328
329 fn position_id(&self) -> Option<PositionId> {
330 None
331 }
332
333 fn commission(&self) -> Option<Money> {
334 None
335 }
336
337 fn ts_event(&self) -> UnixNanos {
338 self.ts_event
339 }
340
341 fn ts_init(&self) -> UnixNanos {
342 self.ts_init
343 }
344}
345
346#[cfg(test)]
347mod tests {
348 use rstest::rstest;
349
350 use crate::events::order::{stubs::*, updated::OrderUpdated};
351
352 #[rstest]
353 fn test_order_updated_display(order_updated: OrderUpdated) {
354 let display = format!("{order_updated}");
355 assert_eq!(
356 display,
357 "OrderUpdated(instrument_id=BTCUSDT.COINBASE, client_order_id=O-19700101-000000-001-001-1, venue_order_id=001, account_id=SIM-001, quantity=100, price=22_000, trigger_price=None, protection_price=None, ts_event=0)"
358 );
359 }
360}