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_model::{
28    enums::OmsType,
29    identifiers::{AccountId, ClientId, Venue},
30    reports::{ExecutionMassStatus, FillReport, OrderStatusReport, PositionStatusReport},
31};
32
33pub mod base;
34
35use nautilus_common::clients::ExecutionClient;
36
37/// Wraps an [`ExecutionClient`], managing its lifecycle and providing access to the client.
38pub struct ExecutionClientAdapter {
39    pub(crate) client: Box<dyn ExecutionClient>,
40    pub client_id: ClientId,
41    pub venue: Venue,
42    pub account_id: AccountId,
43    pub oms_type: OmsType,
44}
45
46impl Deref for ExecutionClientAdapter {
47    type Target = Box<dyn ExecutionClient>;
48
49    fn deref(&self) -> &Self::Target {
50        &self.client
51    }
52}
53
54impl DerefMut for ExecutionClientAdapter {
55    fn deref_mut(&mut self) -> &mut Self::Target {
56        &mut self.client
57    }
58}
59
60impl Debug for ExecutionClientAdapter {
61    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62        f.debug_struct(stringify!(ExecutionClientAdapter))
63            .field("client_id", &self.client_id)
64            .field("venue", &self.venue)
65            .field("account_id", &self.account_id)
66            .field("oms_type", &self.oms_type)
67            .finish()
68    }
69}
70
71impl ExecutionClientAdapter {
72    /// Creates a new [`ExecutionClientAdapter`] with the given client.
73    #[must_use]
74    pub fn new(client: Box<dyn ExecutionClient>) -> Self {
75        let client_id = client.client_id();
76        let venue = client.venue();
77        let account_id = client.account_id();
78        let oms_type = client.oms_type();
79
80        Self {
81            client,
82            client_id,
83            venue,
84            account_id,
85            oms_type,
86        }
87    }
88
89    /// Connects the execution client to the venue.
90    ///
91    /// # Errors
92    ///
93    /// Returns an error if connection fails.
94    pub async fn connect(&mut self) -> anyhow::Result<()> {
95        self.client.connect().await
96    }
97
98    /// Disconnects the execution client from the venue.
99    ///
100    /// # Errors
101    ///
102    /// Returns an error if disconnection fails.
103    pub async fn disconnect(&mut self) -> anyhow::Result<()> {
104        self.client.disconnect().await
105    }
106
107    /// Generates a single order status report.
108    ///
109    /// # Errors
110    ///
111    /// Returns an error if report generation fails.
112    pub async fn generate_order_status_report(
113        &self,
114        cmd: &GenerateOrderStatusReport,
115    ) -> anyhow::Result<Option<OrderStatusReport>> {
116        self.client.generate_order_status_report(cmd).await
117    }
118
119    /// Generates multiple order status reports.
120    ///
121    /// # Errors
122    ///
123    /// Returns an error if report generation fails.
124    pub async fn generate_order_status_reports(
125        &self,
126        cmd: &GenerateOrderStatusReports,
127    ) -> anyhow::Result<Vec<OrderStatusReport>> {
128        self.client.generate_order_status_reports(cmd).await
129    }
130
131    /// Generates fill reports based on execution results.
132    ///
133    /// # Errors
134    ///
135    /// Returns an error if fill report generation fails.
136    pub async fn generate_fill_reports(
137        &self,
138        cmd: GenerateFillReports,
139    ) -> anyhow::Result<Vec<FillReport>> {
140        self.client.generate_fill_reports(cmd).await
141    }
142
143    /// Generates position status reports.
144    ///
145    /// # Errors
146    ///
147    /// Returns an error if generation fails.
148    pub async fn generate_position_status_reports(
149        &self,
150        cmd: &GeneratePositionStatusReports,
151    ) -> anyhow::Result<Vec<PositionStatusReport>> {
152        self.client.generate_position_status_reports(cmd).await
153    }
154
155    /// Generates mass status for executions.
156    ///
157    /// # Errors
158    ///
159    /// Returns an error if status generation fails.
160    pub async fn generate_mass_status(
161        &self,
162        lookback_mins: Option<u64>,
163    ) -> anyhow::Result<Option<ExecutionMassStatus>> {
164        self.client.generate_mass_status(lookback_mins).await
165    }
166}