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