Skip to main content

nautilus_execution/client/
core.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//! Base execution client functionality.
17
18use std::sync::atomic::{AtomicBool, Ordering};
19
20use nautilus_common::cache::{Cache, CacheView};
21use nautilus_model::{
22    enums::{AccountType, OmsType},
23    identifiers::{AccountId, ClientId, ClientOrderId, TraderId, Venue},
24    orders::{OrderAny, OrderList},
25    types::Currency,
26};
27
28/// Base implementation for execution clients providing identity and connection state.
29///
30/// This struct provides the foundation for all execution clients, holding
31/// client identity, connection state, and read-only cache access. Execution
32/// clients use this as a base and extend it with venue-specific implementations.
33///
34/// For event generation, use `OrderEventFactory` from `nautilus_common::factories`.
35/// For live adapters, use `ExecutionEventEmitter` which combines event generation
36/// with async dispatch. For backtest/sandbox, use `OrderEventFactory` directly
37/// and dispatch via `msgbus::send_order_event()`.
38#[derive(Debug)]
39pub struct ExecutionClientCore {
40    pub trader_id: TraderId,
41    pub client_id: ClientId,
42    pub venue: Venue,
43    pub oms_type: OmsType,
44    pub account_id: AccountId,
45    pub account_type: AccountType,
46    pub base_currency: Option<Currency>,
47    connected: AtomicBool,
48    started: AtomicBool,
49    instruments_initialized: AtomicBool,
50    cache: CacheView,
51}
52
53impl Clone for ExecutionClientCore {
54    fn clone(&self) -> Self {
55        Self {
56            trader_id: self.trader_id,
57            client_id: self.client_id,
58            venue: self.venue,
59            oms_type: self.oms_type,
60            account_id: self.account_id,
61            account_type: self.account_type,
62            base_currency: self.base_currency,
63            connected: AtomicBool::new(self.connected.load(Ordering::Acquire)),
64            started: AtomicBool::new(self.started.load(Ordering::Acquire)),
65            instruments_initialized: AtomicBool::new(
66                self.instruments_initialized.load(Ordering::Acquire),
67            ),
68            cache: self.cache.clone(),
69        }
70    }
71}
72
73impl ExecutionClientCore {
74    /// Creates a new [`ExecutionClientCore`] instance.
75    #[expect(clippy::too_many_arguments)]
76    #[must_use]
77    pub fn new(
78        trader_id: TraderId,
79        client_id: ClientId,
80        venue: Venue,
81        oms_type: OmsType,
82        account_id: AccountId,
83        account_type: AccountType,
84        base_currency: Option<Currency>,
85        cache: impl Into<CacheView>,
86    ) -> Self {
87        Self {
88            trader_id,
89            client_id,
90            venue,
91            oms_type,
92            account_id,
93            account_type,
94            base_currency,
95            connected: AtomicBool::new(false),
96            started: AtomicBool::new(false),
97            instruments_initialized: AtomicBool::new(false),
98            cache: cache.into(),
99        }
100    }
101
102    /// Returns a read-only borrow of the cache.
103    pub fn cache(&self) -> std::cell::Ref<'_, Cache> {
104        self.cache.borrow()
105    }
106
107    /// Returns the order for the given `client_order_id` from the cache.
108    ///
109    /// # Errors
110    ///
111    /// Returns an error if the order is not found in the cache.
112    pub fn get_order(&self, client_order_id: &ClientOrderId) -> anyhow::Result<OrderAny> {
113        self.cache
114            .borrow()
115            .order(client_order_id)
116            .map(|o| o.clone())
117            .ok_or_else(|| anyhow::anyhow!("Order not found in cache: {client_order_id}"))
118    }
119
120    /// Returns all orders for the given order list from the cache.
121    ///
122    /// # Errors
123    ///
124    /// Returns an error if any order is not found in the cache.
125    pub fn get_orders_for_list(&self, order_list: &OrderList) -> anyhow::Result<Vec<OrderAny>> {
126        order_list
127            .client_order_ids
128            .iter()
129            .map(|id| self.get_order(id))
130            .collect()
131    }
132
133    /// Returns `true` if the client is connected.
134    #[must_use]
135    pub fn is_connected(&self) -> bool {
136        self.connected.load(Ordering::Acquire)
137    }
138
139    /// Returns `true` if the client is disconnected.
140    #[must_use]
141    pub fn is_disconnected(&self) -> bool {
142        !self.is_connected()
143    }
144
145    /// Sets the client as connected.
146    pub fn set_connected(&self) {
147        self.connected.store(true, Ordering::Release);
148    }
149
150    /// Sets the client as disconnected.
151    pub fn set_disconnected(&self) {
152        self.connected.store(false, Ordering::Release);
153    }
154
155    /// Returns `true` if the client has been started.
156    #[must_use]
157    pub fn is_started(&self) -> bool {
158        self.started.load(Ordering::Acquire)
159    }
160
161    /// Returns `true` if the client has not been started.
162    #[must_use]
163    pub fn is_stopped(&self) -> bool {
164        !self.is_started()
165    }
166
167    /// Sets the client as started.
168    pub fn set_started(&self) {
169        self.started.store(true, Ordering::Release);
170    }
171
172    /// Sets the client as stopped.
173    pub fn set_stopped(&self) {
174        self.started.store(false, Ordering::Release);
175    }
176
177    /// Returns `true` if instruments have been initialized.
178    #[must_use]
179    pub fn instruments_initialized(&self) -> bool {
180        self.instruments_initialized.load(Ordering::Acquire)
181    }
182
183    /// Sets instruments as initialized.
184    pub fn set_instruments_initialized(&self) {
185        self.instruments_initialized.store(true, Ordering::Release);
186    }
187
188    /// Sets the account identifier for the execution client.
189    pub const fn set_account_id(&mut self, account_id: AccountId) {
190        self.account_id = account_id;
191    }
192}