nautilus_execution/client/
core.rs1use std::{
19 cell::RefCell,
20 rc::Rc,
21 sync::atomic::{AtomicBool, Ordering},
22};
23
24use nautilus_common::cache::Cache;
25use nautilus_model::{
26 enums::{AccountType, OmsType},
27 identifiers::{AccountId, ClientId, ClientOrderId, TraderId, Venue},
28 orders::{OrderAny, OrderList},
29 types::Currency,
30};
31
32#[derive(Debug)]
43pub struct ExecutionClientCore {
44 pub trader_id: TraderId,
45 pub client_id: ClientId,
46 pub venue: Venue,
47 pub oms_type: OmsType,
48 pub account_id: AccountId,
49 pub account_type: AccountType,
50 pub base_currency: Option<Currency>,
51 connected: AtomicBool,
52 started: AtomicBool,
53 instruments_initialized: AtomicBool,
54 cache: Rc<RefCell<Cache>>,
55}
56
57impl Clone for ExecutionClientCore {
58 fn clone(&self) -> Self {
59 Self {
60 trader_id: self.trader_id,
61 client_id: self.client_id,
62 venue: self.venue,
63 oms_type: self.oms_type,
64 account_id: self.account_id,
65 account_type: self.account_type,
66 base_currency: self.base_currency,
67 connected: AtomicBool::new(self.connected.load(Ordering::Acquire)),
68 started: AtomicBool::new(self.started.load(Ordering::Acquire)),
69 instruments_initialized: AtomicBool::new(
70 self.instruments_initialized.load(Ordering::Acquire),
71 ),
72 cache: self.cache.clone(),
73 }
74 }
75}
76
77impl ExecutionClientCore {
78 #[allow(clippy::too_many_arguments)]
80 #[must_use]
81 pub fn new(
82 trader_id: TraderId,
83 client_id: ClientId,
84 venue: Venue,
85 oms_type: OmsType,
86 account_id: AccountId,
87 account_type: AccountType,
88 base_currency: Option<Currency>,
89 cache: Rc<RefCell<Cache>>,
90 ) -> Self {
91 Self {
92 trader_id,
93 client_id,
94 venue,
95 oms_type,
96 account_id,
97 account_type,
98 base_currency,
99 connected: AtomicBool::new(false),
100 started: AtomicBool::new(false),
101 instruments_initialized: AtomicBool::new(false),
102 cache,
103 }
104 }
105
106 pub fn cache(&self) -> std::cell::Ref<'_, Cache> {
108 self.cache.borrow()
109 }
110
111 pub fn get_order(&self, client_order_id: &ClientOrderId) -> anyhow::Result<OrderAny> {
117 self.cache
118 .borrow()
119 .order(client_order_id)
120 .cloned()
121 .ok_or_else(|| anyhow::anyhow!("Order not found in cache: {client_order_id}"))
122 }
123
124 pub fn get_orders_for_list(&self, order_list: &OrderList) -> anyhow::Result<Vec<OrderAny>> {
130 order_list
131 .client_order_ids
132 .iter()
133 .map(|id| self.get_order(id))
134 .collect()
135 }
136
137 #[must_use]
139 pub fn is_connected(&self) -> bool {
140 self.connected.load(Ordering::Acquire)
141 }
142
143 #[must_use]
145 pub fn is_disconnected(&self) -> bool {
146 !self.is_connected()
147 }
148
149 pub fn set_connected(&self) {
151 self.connected.store(true, Ordering::Release);
152 }
153
154 pub fn set_disconnected(&self) {
156 self.connected.store(false, Ordering::Release);
157 }
158
159 #[must_use]
161 pub fn is_started(&self) -> bool {
162 self.started.load(Ordering::Acquire)
163 }
164
165 #[must_use]
167 pub fn is_stopped(&self) -> bool {
168 !self.is_started()
169 }
170
171 pub fn set_started(&self) {
173 self.started.store(true, Ordering::Release);
174 }
175
176 pub fn set_stopped(&self) {
178 self.started.store(false, Ordering::Release);
179 }
180
181 #[must_use]
183 pub fn instruments_initialized(&self) -> bool {
184 self.instruments_initialized.load(Ordering::Acquire)
185 }
186
187 pub fn set_instruments_initialized(&self) {
189 self.instruments_initialized.store(true, Ordering::Release);
190 }
191
192 pub const fn set_account_id(&mut self, account_id: AccountId) {
194 self.account_id = account_id;
195 }
196}