nautilus_execution/messages/
modify.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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::{cell::RefCell, fmt::Display, rc::Rc};
17
18use derive_builder::Builder;
19use nautilus_core::{UUID4, UnixNanos};
20use nautilus_model::{
21    identifiers::{ClientId, ClientOrderId, InstrumentId, StrategyId, TraderId, VenueOrderId},
22    orders::OrderAny,
23    types::{Price, Quantity},
24};
25use serde::{Deserialize, Serialize};
26
27use crate::order_emulator::emulator::OrderEmulator;
28
29#[derive(Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Builder)]
30#[builder(default)]
31#[serde(tag = "type")]
32pub struct ModifyOrder {
33    pub trader_id: TraderId,
34    pub client_id: ClientId,
35    pub strategy_id: StrategyId,
36    pub instrument_id: InstrumentId,
37    pub client_order_id: ClientOrderId,
38    pub venue_order_id: VenueOrderId,
39    pub quantity: Option<Quantity>,
40    pub price: Option<Price>,
41    pub trigger_price: Option<Price>,
42    pub command_id: UUID4,
43    pub ts_init: UnixNanos,
44}
45
46impl ModifyOrder {
47    /// Creates a new [`ModifyOrder`] instance.
48    #[allow(clippy::too_many_arguments)]
49    pub const fn new(
50        trader_id: TraderId,
51        client_id: ClientId,
52        strategy_id: StrategyId,
53        instrument_id: InstrumentId,
54        client_order_id: ClientOrderId,
55        venue_order_id: VenueOrderId,
56        quantity: Option<Quantity>,
57        price: Option<Price>,
58        trigger_price: Option<Price>,
59        command_id: UUID4,
60        ts_init: UnixNanos,
61    ) -> anyhow::Result<Self> {
62        Ok(Self {
63            trader_id,
64            client_id,
65            strategy_id,
66            instrument_id,
67            client_order_id,
68            venue_order_id,
69            quantity,
70            price,
71            trigger_price,
72            command_id,
73            ts_init,
74        })
75    }
76}
77
78impl Display for ModifyOrder {
79    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80        write!(
81            f,
82            "ModifyOrder(instrument_id={}, client_order_id={}, venue_order_id={}, quantity={}, price={}, trigger_price={})",
83            self.instrument_id,
84            self.client_order_id,
85            self.venue_order_id,
86            self.quantity
87                .map_or("None".to_string(), |quantity| format!("{quantity}")),
88            self.price
89                .map_or("None".to_string(), |price| format!("{price}")),
90            self.trigger_price
91                .map_or("None".to_string(), |trigger_price| format!(
92                    "{trigger_price}"
93                )),
94        )
95    }
96}
97
98pub trait ModifyOrderHandler {
99    fn handle_modify_order(&self, order: &mut OrderAny, quantity: Quantity);
100}
101
102pub enum ModifyOrderHandlerAny {
103    OrderEmulator(Rc<RefCell<OrderEmulator>>),
104}
105
106impl ModifyOrderHandler for ModifyOrderHandlerAny {
107    fn handle_modify_order(&self, order: &mut OrderAny, quantity: Quantity) {
108        match self {
109            Self::OrderEmulator(order_emulator) => {
110                order_emulator.borrow_mut().update_order(order, quantity);
111            }
112        }
113    }
114}
115
116////////////////////////////////////////////////////////////////////////////////
117// Tests
118////////////////////////////////////////////////////////////////////////////////
119#[cfg(test)]
120mod tests {}