Skip to main content

nautilus_execution/client/
mod.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
16//! Execution client implementations for trading venue connectivity.
17
18use std::{
19    fmt::Debug,
20    ops::{Deref, DerefMut},
21};
22
23use nautilus_common::messages::execution::{
24    GenerateFillReports, GenerateOrderStatusReport, GenerateOrderStatusReports,
25    GeneratePositionStatusReports,
26};
27use nautilus_core::UnixNanos;
28use nautilus_model::{
29    enums::OmsType,
30    identifiers::{
31        AccountId, ClientId, ClientOrderId, InstrumentId, StrategyId, Venue, VenueOrderId,
32    },
33    instruments::InstrumentAny,
34    reports::{ExecutionMassStatus, FillReport, OrderStatusReport, PositionStatusReport},
35};
36
37pub mod core;
38
39use nautilus_common::clients::ExecutionClient;
40
41/// Wraps an [`ExecutionClient`], managing its lifecycle and providing access to the client.
42pub struct ExecutionClientAdapter {
43    pub(crate) client: Box<dyn ExecutionClient>,
44    pub client_id: ClientId,
45    pub venue: Venue,
46    pub account_id: AccountId,
47    pub oms_type: OmsType,
48}
49
50impl Deref for ExecutionClientAdapter {
51    type Target = Box<dyn ExecutionClient>;
52
53    fn deref(&self) -> &Self::Target {
54        &self.client
55    }
56}
57
58impl DerefMut for ExecutionClientAdapter {
59    fn deref_mut(&mut self) -> &mut Self::Target {
60        &mut self.client
61    }
62}
63
64impl Debug for ExecutionClientAdapter {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        f.debug_struct(stringify!(ExecutionClientAdapter))
67            .field("client_id", &self.client_id)
68            .field("venue", &self.venue)
69            .field("account_id", &self.account_id)
70            .field("oms_type", &self.oms_type)
71            .finish()
72    }
73}
74
75impl ExecutionClientAdapter {
76    /// Creates a new [`ExecutionClientAdapter`] with the given client.
77    #[must_use]
78    pub fn new(client: Box<dyn ExecutionClient>) -> Self {
79        let client_id = client.client_id();
80        let venue = client.venue();
81        let account_id = client.account_id();
82        let oms_type = client.oms_type();
83
84        Self {
85            client,
86            client_id,
87            venue,
88            account_id,
89            oms_type,
90        }
91    }
92
93    /// Connects the execution client to the venue.
94    ///
95    /// # Errors
96    ///
97    /// Returns an error if connection fails.
98    pub async fn connect(&mut self) -> anyhow::Result<()> {
99        self.client.connect().await
100    }
101
102    /// Disconnects the execution client from the venue.
103    ///
104    /// # Errors
105    ///
106    /// Returns an error if disconnection fails.
107    pub async fn disconnect(&mut self) -> anyhow::Result<()> {
108        self.client.disconnect().await
109    }
110
111    /// Generates a single order status report.
112    ///
113    /// # Errors
114    ///
115    /// Returns an error if report generation fails.
116    pub async fn generate_order_status_report(
117        &self,
118        cmd: &GenerateOrderStatusReport,
119    ) -> anyhow::Result<Option<OrderStatusReport>> {
120        self.client.generate_order_status_report(cmd).await
121    }
122
123    /// Generates multiple order status reports.
124    ///
125    /// # Errors
126    ///
127    /// Returns an error if report generation fails.
128    pub async fn generate_order_status_reports(
129        &self,
130        cmd: &GenerateOrderStatusReports,
131    ) -> anyhow::Result<Vec<OrderStatusReport>> {
132        self.client.generate_order_status_reports(cmd).await
133    }
134
135    /// Generates fill reports based on execution results.
136    ///
137    /// # Errors
138    ///
139    /// Returns an error if fill report generation fails.
140    pub async fn generate_fill_reports(
141        &self,
142        cmd: GenerateFillReports,
143    ) -> anyhow::Result<Vec<FillReport>> {
144        self.client.generate_fill_reports(cmd).await
145    }
146
147    /// Generates position status reports.
148    ///
149    /// # Errors
150    ///
151    /// Returns an error if generation fails.
152    pub async fn generate_position_status_reports(
153        &self,
154        cmd: &GeneratePositionStatusReports,
155    ) -> anyhow::Result<Vec<PositionStatusReport>> {
156        self.client.generate_position_status_reports(cmd).await
157    }
158
159    /// Generates mass status for executions.
160    ///
161    /// # Errors
162    ///
163    /// Returns an error if status generation fails.
164    pub async fn generate_mass_status(
165        &self,
166        lookback_mins: Option<u64>,
167    ) -> anyhow::Result<Option<ExecutionMassStatus>> {
168        self.client.generate_mass_status(lookback_mins).await
169    }
170
171    /// Forwards an instrument update to the underlying execution client.
172    pub fn on_instrument(&mut self, instrument: InstrumentAny) {
173        self.client.on_instrument(instrument);
174    }
175
176    /// Registers an external order for tracking by the execution client.
177    ///
178    /// This is called after reconciliation creates an external order, allowing the
179    /// execution client to track it for subsequent events (e.g., cancellations).
180    pub fn register_external_order(
181        &self,
182        client_order_id: ClientOrderId,
183        venue_order_id: VenueOrderId,
184        instrument_id: InstrumentId,
185        strategy_id: StrategyId,
186        ts_init: UnixNanos,
187    ) {
188        self.client.register_external_order(
189            client_order_id,
190            venue_order_id,
191            instrument_id,
192            strategy_id,
193            ts_init,
194        );
195    }
196}