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