nautilus_common/messages/system/trading.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2026 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
16use std::{
17 any::Any,
18 fmt::{Debug, Display},
19};
20
21use indexmap::IndexMap;
22use nautilus_core::{UUID4, UnixNanos};
23use nautilus_model::{enums::TradingState, identifiers::TraderId};
24use serde::{Deserialize, Serialize};
25
26/// Represents an event where trading state has changed at the `RiskEngine`.
27#[repr(C)]
28#[derive(Clone, Debug, PartialEq, Eq, 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 TradingStateChanged {
35 /// The trader ID associated with the event.
36 pub trader_id: TraderId,
37 /// The trading state.
38 pub state: TradingState,
39 /// The risk engine configuration.
40 pub config: IndexMap<String, String>,
41 /// The event ID.
42 pub event_id: UUID4,
43 /// UNIX timestamp (nanoseconds) when the event occurred.
44 pub ts_event: UnixNanos,
45 /// UNIX timestamp (nanoseconds) when the instance was initialized.
46 pub ts_init: UnixNanos,
47}
48
49impl TradingStateChanged {
50 /// Creates a new [`TradingStateChanged`] instance.
51 #[must_use]
52 pub fn new(
53 trader_id: TraderId,
54 state: TradingState,
55 config: IndexMap<String, String>,
56 event_id: UUID4,
57 ts_event: UnixNanos,
58 ts_init: UnixNanos,
59 ) -> Self {
60 Self {
61 trader_id,
62 state,
63 config,
64 event_id,
65 ts_event,
66 ts_init,
67 }
68 }
69
70 pub fn as_any(&self) -> &dyn Any {
71 self
72 }
73}
74
75impl Display for TradingStateChanged {
76 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77 write!(
78 f,
79 "{}(trader_id={}, state={}, event_id={})",
80 stringify!(TradingStateChanged),
81 self.trader_id,
82 self.state,
83 self.event_id,
84 )
85 }
86}