nautilus_execution/client/mod.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3// https://poseitrader.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 nautilus_common::messages::execution::{
17 BatchCancelOrders, CancelAllOrders, CancelOrder, GenerateFillReports,
18 GenerateOrderStatusReport, GeneratePositionReports, ModifyOrder, QueryOrder, SubmitOrder,
19 SubmitOrderList,
20};
21use nautilus_core::UnixNanos;
22use nautilus_model::{
23 accounts::AccountAny,
24 enums::OmsType,
25 identifiers::{AccountId, ClientId, Venue},
26 reports::{ExecutionMassStatus, FillReport, OrderStatusReport, PositionStatusReport},
27 types::{AccountBalance, MarginBalance},
28};
29
30pub mod base;
31
32pub trait ExecutionClient {
33 fn is_connected(&self) -> bool;
34 fn client_id(&self) -> ClientId;
35 fn account_id(&self) -> AccountId;
36 fn venue(&self) -> Venue;
37 fn oms_type(&self) -> OmsType;
38 fn get_account(&self) -> Option<AccountAny>;
39
40 /// Generates and publishes the account state event.
41 ///
42 /// # Errors
43 ///
44 /// Returns an error if generating the account state fails.
45 fn generate_account_state(
46 &self,
47 balances: Vec<AccountBalance>,
48 margins: Vec<MarginBalance>,
49 reported: bool,
50 ts_event: UnixNanos,
51 ) -> anyhow::Result<()>;
52
53 /// Starts the execution client.
54 ///
55 /// # Errors
56 ///
57 /// Returns an error if the client fails to start.
58 fn start(&mut self) -> anyhow::Result<()>;
59
60 /// Stops the execution client.
61 ///
62 /// # Errors
63 ///
64 /// Returns an error if the client fails to stop.
65 fn stop(&mut self) -> anyhow::Result<()>;
66
67 /// Submits a single order command to the execution venue.
68 ///
69 /// # Errors
70 ///
71 /// Returns an error if submission fails.
72 fn submit_order(&self, cmd: &SubmitOrder) -> anyhow::Result<()>;
73
74 /// Submits a list of orders to the execution venue.
75 ///
76 /// # Errors
77 ///
78 /// Returns an error if submission fails.
79 fn submit_order_list(&self, cmd: &SubmitOrderList) -> anyhow::Result<()>;
80
81 /// Modifies an existing order.
82 ///
83 /// # Errors
84 ///
85 /// Returns an error if modification fails.
86 fn modify_order(&self, cmd: &ModifyOrder) -> anyhow::Result<()>;
87
88 /// Cancels a specific order.
89 ///
90 /// # Errors
91 ///
92 /// Returns an error if cancellation fails.
93 fn cancel_order(&self, cmd: &CancelOrder) -> anyhow::Result<()>;
94
95 /// Cancels all orders.
96 ///
97 /// # Errors
98 ///
99 /// Returns an error if cancellation fails.
100 fn cancel_all_orders(&self, cmd: &CancelAllOrders) -> anyhow::Result<()>;
101
102 /// Cancels a batch of orders.
103 ///
104 /// # Errors
105 ///
106 /// Returns an error if batch cancellation fails.
107 fn batch_cancel_orders(&self, cmd: &BatchCancelOrders) -> anyhow::Result<()>;
108
109 /// Queries the status of an order.
110 ///
111 /// # Errors
112 ///
113 /// Returns an error if the query fails.
114 fn query_order(&self, cmd: &QueryOrder) -> anyhow::Result<()>;
115}
116
117pub trait LiveExecutionClient: ExecutionClient {
118 /// Establishes a connection for live execution.
119 ///
120 /// # Errors
121 ///
122 /// Returns an error if connection fails.
123 fn connect(&self) -> anyhow::Result<()>;
124
125 /// Disconnects the live execution client.
126 ///
127 /// # Errors
128 ///
129 /// Returns an error if disconnection fails.
130 fn disconnect(&self) -> anyhow::Result<()>;
131
132 /// Generates a single order status report.
133 ///
134 /// # Errors
135 ///
136 /// Returns an error if report generation fails.
137 fn generate_order_status_report(
138 &self,
139 cmd: &GenerateOrderStatusReport,
140 ) -> anyhow::Result<Option<OrderStatusReport>>;
141
142 /// Generates multiple order status reports.
143 ///
144 /// # Errors
145 ///
146 /// Returns an error if report generation fails.
147 fn generate_order_status_reports(
148 &self,
149 cmd: &GenerateOrderStatusReport,
150 ) -> anyhow::Result<Vec<OrderStatusReport>>;
151
152 /// Generates fill reports based on execution results.
153 ///
154 /// # Errors
155 ///
156 /// Returns an error if fill report generation fails.
157 fn generate_fill_reports(&self, report: GenerateFillReports)
158 -> anyhow::Result<Vec<FillReport>>;
159
160 /// Generates position status reports.
161 ///
162 /// # Errors
163 ///
164 /// Returns an error if generation fails.
165 fn generate_position_status_reports(
166 &self,
167 cmd: &GeneratePositionReports,
168 ) -> anyhow::Result<Vec<PositionStatusReport>>;
169
170 /// Generates mass status for executions.
171 ///
172 /// # Errors
173 ///
174 /// Returns an error if status generation fails.
175 fn generate_mass_status(
176 &self,
177 lookback_mins: Option<u64>,
178 ) -> anyhow::Result<Option<ExecutionMassStatus>>;
179}