Skip to main content

nautilus_common/messages/execution/
modify.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::fmt::Display;
17
18use derive_builder::Builder;
19use nautilus_core::{Params, UUID4, UnixNanos};
20use nautilus_model::{
21    identifiers::{ClientId, ClientOrderId, InstrumentId, StrategyId, TraderId, VenueOrderId},
22    types::{Price, Quantity},
23};
24use serde::{Deserialize, Serialize};
25
26#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Builder)]
27#[serde(tag = "type")]
28pub struct ModifyOrder {
29    pub trader_id: TraderId,
30    pub client_id: Option<ClientId>,
31    pub strategy_id: StrategyId,
32    pub instrument_id: InstrumentId,
33    pub client_order_id: ClientOrderId,
34    pub venue_order_id: Option<VenueOrderId>,
35    pub quantity: Option<Quantity>,
36    pub price: Option<Price>,
37    pub trigger_price: Option<Price>,
38    pub command_id: UUID4,
39    pub ts_init: UnixNanos,
40    pub params: Option<Params>,
41    #[builder(default)]
42    pub correlation_id: Option<UUID4>,
43    #[builder(default)]
44    #[serde(default, skip_serializing_if = "Option::is_none")]
45    pub causation_id: Option<UUID4>,
46}
47
48impl ModifyOrder {
49    /// Creates a new [`ModifyOrder`] instance.
50    #[expect(clippy::too_many_arguments)]
51    #[must_use]
52    pub fn new(
53        trader_id: TraderId,
54        client_id: Option<ClientId>,
55        strategy_id: StrategyId,
56        instrument_id: InstrumentId,
57        client_order_id: ClientOrderId,
58        venue_order_id: Option<VenueOrderId>,
59        quantity: Option<Quantity>,
60        price: Option<Price>,
61        trigger_price: Option<Price>,
62        command_id: UUID4,
63        ts_init: UnixNanos,
64        params: Option<Params>,
65        correlation_id: Option<UUID4>,
66    ) -> Self {
67        Self {
68            trader_id,
69            client_id,
70            strategy_id,
71            instrument_id,
72            client_order_id,
73            venue_order_id,
74            quantity,
75            price,
76            trigger_price,
77            command_id,
78            ts_init,
79            params,
80            correlation_id,
81            causation_id: None,
82        }
83    }
84}
85
86impl Display for ModifyOrder {
87    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88        write!(
89            f,
90            "ModifyOrder(instrument_id={}, client_order_id={}, venue_order_id={:?}, quantity={}, price={}, trigger_price={})",
91            self.instrument_id,
92            self.client_order_id,
93            self.venue_order_id,
94            self.quantity
95                .map_or("None".to_string(), |quantity| format!("{quantity}")),
96            self.price
97                .map_or("None".to_string(), |price| format!("{price}")),
98            self.trigger_price
99                .map_or("None".to_string(), |trigger_price| format!(
100                    "{trigger_price}"
101                )),
102        )
103    }
104}
105
106#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Builder)]
107#[serde(tag = "type")]
108pub struct BatchModifyOrders {
109    pub trader_id: TraderId,
110    pub client_id: Option<ClientId>,
111    pub strategy_id: StrategyId,
112    pub instrument_id: InstrumentId,
113    pub modifies: Vec<ModifyOrder>,
114    pub command_id: UUID4,
115    pub ts_init: UnixNanos,
116    pub params: Option<Params>,
117    #[builder(default)]
118    pub correlation_id: Option<UUID4>,
119    #[builder(default)]
120    #[serde(default, skip_serializing_if = "Option::is_none")]
121    pub causation_id: Option<UUID4>,
122}
123
124impl BatchModifyOrders {
125    /// Creates a new [`BatchModifyOrders`] instance.
126    #[expect(clippy::too_many_arguments)]
127    #[must_use]
128    pub fn new(
129        trader_id: TraderId,
130        client_id: Option<ClientId>,
131        strategy_id: StrategyId,
132        instrument_id: InstrumentId,
133        modifies: Vec<ModifyOrder>,
134        command_id: UUID4,
135        ts_init: UnixNanos,
136        params: Option<Params>,
137        correlation_id: Option<UUID4>,
138    ) -> Self {
139        Self {
140            trader_id,
141            client_id,
142            strategy_id,
143            instrument_id,
144            modifies,
145            command_id,
146            ts_init,
147            params,
148            correlation_id,
149            causation_id: None,
150        }
151    }
152}
153
154impl Display for BatchModifyOrders {
155    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
156        write!(
157            f,
158            "BatchModifyOrders(instrument_id={}, modifies={})",
159            self.instrument_id,
160            self.modifies.len(),
161        )
162    }
163}
164
165#[cfg(test)]
166mod tests {}