nautilus_common/clients/data.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//! Data client trait definition.
17
18use async_trait::async_trait;
19use nautilus_model::identifiers::{ClientId, Venue};
20
21use super::log_not_implemented;
22use crate::messages::data::{
23 RequestBars, RequestBookDepth, RequestBookSnapshot, RequestCustomData, RequestFundingRates,
24 RequestInstrument, RequestInstruments, RequestQuotes, RequestTrades, SubscribeBars,
25 SubscribeBookDeltas, SubscribeBookDepth10, SubscribeCustomData, SubscribeFundingRates,
26 SubscribeIndexPrices, SubscribeInstrument, SubscribeInstrumentClose, SubscribeInstrumentStatus,
27 SubscribeInstruments, SubscribeMarkPrices, SubscribeQuotes, SubscribeTrades, UnsubscribeBars,
28 UnsubscribeBookDeltas, UnsubscribeBookDepth10, UnsubscribeCustomData, UnsubscribeFundingRates,
29 UnsubscribeIndexPrices, UnsubscribeInstrument, UnsubscribeInstrumentClose,
30 UnsubscribeInstrumentStatus, UnsubscribeInstruments, UnsubscribeMarkPrices, UnsubscribeQuotes,
31 UnsubscribeTrades,
32};
33#[cfg(feature = "defi")]
34use crate::messages::defi::{
35 RequestPoolSnapshot, SubscribeBlocks, SubscribePool, SubscribePoolFeeCollects,
36 SubscribePoolFlashEvents, SubscribePoolLiquidityUpdates, SubscribePoolSwaps, UnsubscribeBlocks,
37 UnsubscribePool, UnsubscribePoolFeeCollects, UnsubscribePoolFlashEvents,
38 UnsubscribePoolLiquidityUpdates, UnsubscribePoolSwaps,
39};
40
41/// Defines the interface for a data client, managing connections, subscriptions, and requests.
42///
43/// # Thread Safety
44///
45/// Client instances are not intended to be sent across threads. The `?Send` bound
46/// allows implementations to hold non-Send state for any Python interop.
47#[async_trait(?Send)]
48pub trait DataClient {
49 /// Returns the unique identifier for this data client.
50 fn client_id(&self) -> ClientId;
51
52 /// Returns the optional venue this client is associated with.
53 fn venue(&self) -> Option<Venue>;
54
55 /// Starts the data client.
56 ///
57 /// # Errors
58 ///
59 /// Returns an error if the operation fails.
60 fn start(&mut self) -> anyhow::Result<()>;
61
62 /// Stops the data client.
63 ///
64 /// # Errors
65 ///
66 /// Returns an error if the operation fails.
67 fn stop(&mut self) -> anyhow::Result<()>;
68
69 /// Resets the data client to its initial state.
70 ///
71 /// # Errors
72 ///
73 /// Returns an error if the operation fails.
74 fn reset(&mut self) -> anyhow::Result<()>;
75
76 /// Disposes of client resources and cleans up.
77 ///
78 /// # Errors
79 ///
80 /// Returns an error if the operation fails.
81 fn dispose(&mut self) -> anyhow::Result<()>;
82
83 /// Returns `true` if the client is currently connected.
84 fn is_connected(&self) -> bool;
85
86 /// Returns `true` if the client is currently disconnected.
87 fn is_disconnected(&self) -> bool;
88
89 /// Connects the client to the data provider.
90 ///
91 /// For live clients, this triggers the actual connection to external APIs.
92 /// For backtest clients, this is a no-op.
93 ///
94 /// # Errors
95 ///
96 /// Returns an error if the connection fails.
97 async fn connect(&mut self) -> anyhow::Result<()> {
98 Ok(())
99 }
100
101 /// Disconnects the client from the data provider.
102 ///
103 /// For live clients, this closes connections to external APIs.
104 /// For backtest clients, this is a no-op.
105 ///
106 /// # Errors
107 ///
108 /// Returns an error if the disconnection fails.
109 async fn disconnect(&mut self) -> anyhow::Result<()> {
110 Ok(())
111 }
112
113 /// Subscribes to custom data types according to the command.
114 ///
115 /// # Errors
116 ///
117 /// Returns an error if the subscribe operation fails.
118 fn subscribe(&mut self, cmd: &SubscribeCustomData) -> anyhow::Result<()> {
119 log_not_implemented(&cmd);
120 Ok(())
121 }
122
123 /// Subscribes to instruments list for the specified venue.
124 ///
125 /// # Errors
126 ///
127 /// Returns an error if the subscribe operation fails.
128 fn subscribe_instruments(&mut self, cmd: &SubscribeInstruments) -> anyhow::Result<()> {
129 log_not_implemented(&cmd);
130 Ok(())
131 }
132
133 /// Subscribes to data for a single instrument.
134 ///
135 /// # Errors
136 ///
137 /// Returns an error if the subscribe operation fails.
138 fn subscribe_instrument(&mut self, cmd: &SubscribeInstrument) -> anyhow::Result<()> {
139 log_not_implemented(&cmd);
140 Ok(())
141 }
142
143 /// Subscribes to order book delta updates for the specified instrument.
144 ///
145 /// # Errors
146 ///
147 /// Returns an error if the subscribe operation fails.
148 fn subscribe_book_deltas(&mut self, cmd: &SubscribeBookDeltas) -> anyhow::Result<()> {
149 log_not_implemented(&cmd);
150 Ok(())
151 }
152
153 /// Subscribes to top 10 order book depth updates for the specified instrument.
154 ///
155 /// # Errors
156 ///
157 /// Returns an error if the subscribe operation fails.
158 fn subscribe_book_depth10(&mut self, cmd: &SubscribeBookDepth10) -> anyhow::Result<()> {
159 log_not_implemented(&cmd);
160 Ok(())
161 }
162
163 /// Subscribes to quote updates for the specified instrument.
164 ///
165 /// # Errors
166 ///
167 /// Returns an error if the subscribe operation fails.
168 fn subscribe_quotes(&mut self, cmd: &SubscribeQuotes) -> anyhow::Result<()> {
169 log_not_implemented(&cmd);
170 Ok(())
171 }
172
173 /// Subscribes to trade updates for the specified instrument.
174 ///
175 /// # Errors
176 ///
177 /// Returns an error if the subscribe operation fails.
178 fn subscribe_trades(&mut self, cmd: &SubscribeTrades) -> anyhow::Result<()> {
179 log_not_implemented(&cmd);
180 Ok(())
181 }
182
183 /// Subscribes to mark price updates for the specified instrument.
184 ///
185 /// # Errors
186 ///
187 /// Returns an error if the subscribe operation fails.
188 fn subscribe_mark_prices(&mut self, cmd: &SubscribeMarkPrices) -> anyhow::Result<()> {
189 log_not_implemented(&cmd);
190 Ok(())
191 }
192
193 /// Subscribes to index price updates for the specified instrument.
194 ///
195 /// # Errors
196 ///
197 /// Returns an error if the subscribe operation fails.
198 fn subscribe_index_prices(&mut self, cmd: &SubscribeIndexPrices) -> anyhow::Result<()> {
199 log_not_implemented(&cmd);
200 Ok(())
201 }
202
203 /// Subscribes to funding rate updates for the specified instrument.
204 ///
205 /// # Errors
206 ///
207 /// Returns an error if the subscribe operation fails.
208 fn subscribe_funding_rates(&mut self, cmd: &SubscribeFundingRates) -> anyhow::Result<()> {
209 log_not_implemented(&cmd);
210 Ok(())
211 }
212
213 /// Subscribes to bar updates of the specified bar type.
214 ///
215 /// # Errors
216 ///
217 /// Returns an error if the subscribe operation fails.
218 fn subscribe_bars(&mut self, cmd: &SubscribeBars) -> anyhow::Result<()> {
219 log_not_implemented(&cmd);
220 Ok(())
221 }
222
223 /// Subscribes to status updates for the specified instrument.
224 ///
225 /// # Errors
226 ///
227 /// Returns an error if the subscribe operation fails.
228 fn subscribe_instrument_status(
229 &mut self,
230 cmd: &SubscribeInstrumentStatus,
231 ) -> anyhow::Result<()> {
232 log_not_implemented(&cmd);
233 Ok(())
234 }
235
236 /// Subscribes to instrument close events for the specified instrument.
237 ///
238 /// # Errors
239 ///
240 /// Returns an error if the subscription operation fails.
241 fn subscribe_instrument_close(&mut self, cmd: &SubscribeInstrumentClose) -> anyhow::Result<()> {
242 log_not_implemented(&cmd);
243 Ok(())
244 }
245
246 #[cfg(feature = "defi")]
247 /// Subscribes to blocks for a specified blockchain.
248 ///
249 /// # Errors
250 ///
251 /// Returns an error if the subscription operation fails.
252 fn subscribe_blocks(&mut self, cmd: &SubscribeBlocks) -> anyhow::Result<()> {
253 log_not_implemented(&cmd);
254 Ok(())
255 }
256
257 #[cfg(feature = "defi")]
258 /// Subscribes to pool definition updates for a specified AMM pool.
259 ///
260 /// # Errors
261 ///
262 /// Returns an error if the subscription operation fails.
263 fn subscribe_pool(&mut self, cmd: &SubscribePool) -> anyhow::Result<()> {
264 log_not_implemented(&cmd);
265 Ok(())
266 }
267
268 #[cfg(feature = "defi")]
269 /// Subscribes to pool swaps for a specified AMM pool.
270 ///
271 /// # Errors
272 ///
273 /// Returns an error if the subscription operation fails.
274 fn subscribe_pool_swaps(&mut self, cmd: &SubscribePoolSwaps) -> anyhow::Result<()> {
275 log_not_implemented(&cmd);
276 Ok(())
277 }
278
279 #[cfg(feature = "defi")]
280 /// Subscribes to pool liquidity updates for a specified AMM pool.
281 ///
282 /// # Errors
283 ///
284 /// Returns an error if the subscription operation fails.
285 fn subscribe_pool_liquidity_updates(
286 &mut self,
287 cmd: &SubscribePoolLiquidityUpdates,
288 ) -> anyhow::Result<()> {
289 log_not_implemented(&cmd);
290 Ok(())
291 }
292
293 #[cfg(feature = "defi")]
294 /// Subscribes to pool fee collects for a specified AMM pool.
295 ///
296 /// # Errors
297 ///
298 /// Returns an error if the subscription operation fails.
299 fn subscribe_pool_fee_collects(
300 &mut self,
301 cmd: &SubscribePoolFeeCollects,
302 ) -> anyhow::Result<()> {
303 log_not_implemented(&cmd);
304 Ok(())
305 }
306
307 #[cfg(feature = "defi")]
308 /// Subscribes to pool flash loan events for a specified AMM pool.
309 ///
310 /// # Errors
311 ///
312 /// Returns an error if the subscription operation fails.
313 fn subscribe_pool_flash_events(
314 &mut self,
315 cmd: &SubscribePoolFlashEvents,
316 ) -> anyhow::Result<()> {
317 log_not_implemented(&cmd);
318 Ok(())
319 }
320
321 /// Unsubscribes from custom data types according to the command.
322 ///
323 /// # Errors
324 ///
325 /// Returns an error if the unsubscribe operation fails.
326 fn unsubscribe(&mut self, cmd: &UnsubscribeCustomData) -> anyhow::Result<()> {
327 log_not_implemented(&cmd);
328 Ok(())
329 }
330
331 /// Unsubscribes from instruments list for the specified venue.
332 ///
333 /// # Errors
334 ///
335 /// Returns an error if the unsubscribe operation fails.
336 fn unsubscribe_instruments(&mut self, cmd: &UnsubscribeInstruments) -> anyhow::Result<()> {
337 log_not_implemented(&cmd);
338 Ok(())
339 }
340
341 /// Unsubscribes from data for the specified instrument.
342 ///
343 /// # Errors
344 ///
345 /// Returns an error if the unsubscribe operation fails.
346 fn unsubscribe_instrument(&mut self, cmd: &UnsubscribeInstrument) -> anyhow::Result<()> {
347 log_not_implemented(&cmd);
348 Ok(())
349 }
350
351 /// Unsubscribes from order book delta updates for the specified instrument.
352 ///
353 /// # Errors
354 ///
355 /// Returns an error if the unsubscribe operation fails.
356 fn unsubscribe_book_deltas(&mut self, cmd: &UnsubscribeBookDeltas) -> anyhow::Result<()> {
357 log_not_implemented(&cmd);
358 Ok(())
359 }
360
361 /// Unsubscribes from top 10 order book depth updates for the specified instrument.
362 ///
363 /// # Errors
364 ///
365 /// Returns an error if the unsubscribe operation fails.
366 fn unsubscribe_book_depth10(&mut self, cmd: &UnsubscribeBookDepth10) -> anyhow::Result<()> {
367 log_not_implemented(&cmd);
368 Ok(())
369 }
370
371 /// Unsubscribes from quote updates for the specified instrument.
372 ///
373 /// # Errors
374 ///
375 /// Returns an error if the unsubscribe operation fails.
376 fn unsubscribe_quotes(&mut self, cmd: &UnsubscribeQuotes) -> anyhow::Result<()> {
377 log_not_implemented(&cmd);
378 Ok(())
379 }
380
381 /// Unsubscribes from trade updates for the specified instrument.
382 ///
383 /// # Errors
384 ///
385 /// Returns an error if the unsubscribe operation fails.
386 fn unsubscribe_trades(&mut self, cmd: &UnsubscribeTrades) -> anyhow::Result<()> {
387 log_not_implemented(&cmd);
388 Ok(())
389 }
390
391 /// Unsubscribes from mark price updates for the specified instrument.
392 ///
393 /// # Errors
394 ///
395 /// Returns an error if the unsubscribe operation fails.
396 fn unsubscribe_mark_prices(&mut self, cmd: &UnsubscribeMarkPrices) -> anyhow::Result<()> {
397 log_not_implemented(&cmd);
398 Ok(())
399 }
400
401 /// Unsubscribes from index price updates for the specified instrument.
402 ///
403 /// # Errors
404 ///
405 /// Returns an error if the unsubscribe operation fails.
406 fn unsubscribe_index_prices(&mut self, cmd: &UnsubscribeIndexPrices) -> anyhow::Result<()> {
407 log_not_implemented(&cmd);
408 Ok(())
409 }
410
411 /// Unsubscribes from funding rate updates for the specified instrument.
412 ///
413 /// # Errors
414 ///
415 /// Returns an error if the unsubscribe operation fails.
416 fn unsubscribe_funding_rates(&mut self, cmd: &UnsubscribeFundingRates) -> anyhow::Result<()> {
417 log_not_implemented(&cmd);
418 Ok(())
419 }
420
421 /// Unsubscribes from bar updates of the specified bar type.
422 ///
423 /// # Errors
424 ///
425 /// Returns an error if the unsubscribe operation fails.
426 fn unsubscribe_bars(&mut self, cmd: &UnsubscribeBars) -> anyhow::Result<()> {
427 log_not_implemented(&cmd);
428 Ok(())
429 }
430
431 /// Unsubscribes from instrument status updates for the specified instrument.
432 ///
433 /// # Errors
434 ///
435 /// Returns an error if the unsubscribe operation fails.
436 fn unsubscribe_instrument_status(
437 &mut self,
438 cmd: &UnsubscribeInstrumentStatus,
439 ) -> anyhow::Result<()> {
440 log_not_implemented(&cmd);
441 Ok(())
442 }
443
444 /// Unsubscribes from instrument close events for the specified instrument.
445 ///
446 /// # Errors
447 ///
448 /// Returns an error if the unsubscribe operation fails.
449 fn unsubscribe_instrument_close(
450 &mut self,
451 cmd: &UnsubscribeInstrumentClose,
452 ) -> anyhow::Result<()> {
453 log_not_implemented(&cmd);
454 Ok(())
455 }
456
457 #[cfg(feature = "defi")]
458 /// Unsubscribes from blocks for a specified blockchain.
459 ///
460 /// # Errors
461 ///
462 /// Returns an error if the subscription operation fails.
463 fn unsubscribe_blocks(&mut self, cmd: &UnsubscribeBlocks) -> anyhow::Result<()> {
464 log_not_implemented(&cmd);
465 Ok(())
466 }
467
468 #[cfg(feature = "defi")]
469 /// Unsubscribes from pool definition updates for a specified AMM pool.
470 ///
471 /// # Errors
472 ///
473 /// Returns an error if the subscription operation fails.
474 fn unsubscribe_pool(&mut self, cmd: &UnsubscribePool) -> anyhow::Result<()> {
475 log_not_implemented(&cmd);
476 Ok(())
477 }
478
479 #[cfg(feature = "defi")]
480 /// Unsubscribes from swaps for a specified AMM pool.
481 ///
482 /// # Errors
483 ///
484 /// Returns an error if the subscription operation fails.
485 fn unsubscribe_pool_swaps(&mut self, cmd: &UnsubscribePoolSwaps) -> anyhow::Result<()> {
486 log_not_implemented(&cmd);
487 Ok(())
488 }
489
490 #[cfg(feature = "defi")]
491 /// Unsubscribes from pool liquidity updates for a specified AMM pool.
492 ///
493 /// # Errors
494 ///
495 /// Returns an error if the subscription operation fails.
496 fn unsubscribe_pool_liquidity_updates(
497 &mut self,
498 cmd: &UnsubscribePoolLiquidityUpdates,
499 ) -> anyhow::Result<()> {
500 log_not_implemented(&cmd);
501 Ok(())
502 }
503
504 #[cfg(feature = "defi")]
505 /// Unsubscribes from pool fee collects for a specified AMM pool.
506 ///
507 /// # Errors
508 ///
509 /// Returns an error if the subscription operation fails.
510 fn unsubscribe_pool_fee_collects(
511 &mut self,
512 cmd: &UnsubscribePoolFeeCollects,
513 ) -> anyhow::Result<()> {
514 log_not_implemented(&cmd);
515 Ok(())
516 }
517
518 #[cfg(feature = "defi")]
519 /// Unsubscribes from pool flash loan events for a specified AMM pool.
520 ///
521 /// # Errors
522 ///
523 /// Returns an error if the subscription operation fails.
524 fn unsubscribe_pool_flash_events(
525 &mut self,
526 cmd: &UnsubscribePoolFlashEvents,
527 ) -> anyhow::Result<()> {
528 log_not_implemented(&cmd);
529 Ok(())
530 }
531
532 /// Sends a custom data request to the provider.
533 ///
534 /// # Errors
535 ///
536 /// Returns an error if the data request fails.
537 fn request_data(&self, request: RequestCustomData) -> anyhow::Result<()> {
538 log_not_implemented(&request);
539 Ok(())
540 }
541
542 /// Requests a list of instruments from the provider for a given venue.
543 ///
544 /// # Errors
545 ///
546 /// Returns an error if the instruments request fails.
547 fn request_instruments(&self, request: RequestInstruments) -> anyhow::Result<()> {
548 log_not_implemented(&request);
549 Ok(())
550 }
551
552 /// Requests detailed data for a single instrument.
553 ///
554 /// # Errors
555 ///
556 /// Returns an error if the instrument request fails.
557 fn request_instrument(&self, request: RequestInstrument) -> anyhow::Result<()> {
558 log_not_implemented(&request);
559 Ok(())
560 }
561
562 /// Requests a snapshot of the order book for a specified instrument.
563 ///
564 /// # Errors
565 ///
566 /// Returns an error if the book snapshot request fails.
567 fn request_book_snapshot(&self, request: RequestBookSnapshot) -> anyhow::Result<()> {
568 log_not_implemented(&request);
569 Ok(())
570 }
571
572 /// Requests historical or streaming quote data for a specified instrument.
573 ///
574 /// # Errors
575 ///
576 /// Returns an error if the quotes request fails.
577 fn request_quotes(&self, request: RequestQuotes) -> anyhow::Result<()> {
578 log_not_implemented(&request);
579 Ok(())
580 }
581
582 /// Requests historical or streaming trade data for a specified instrument.
583 ///
584 /// # Errors
585 ///
586 /// Returns an error if the trades request fails.
587 fn request_trades(&self, request: RequestTrades) -> anyhow::Result<()> {
588 log_not_implemented(&request);
589 Ok(())
590 }
591
592 /// Requests historical or streaming funding rate data for a specified instrument.
593 ///
594 /// # Errors
595 ///
596 /// Returns an error if the trades request fails.
597 fn request_funding_rates(&self, request: RequestFundingRates) -> anyhow::Result<()> {
598 log_not_implemented(&request);
599 Ok(())
600 }
601
602 /// Requests historical or streaming bar data for a specified instrument and bar type.
603 ///
604 /// # Errors
605 ///
606 /// Returns an error if the bars request fails.
607 fn request_bars(&self, request: RequestBars) -> anyhow::Result<()> {
608 log_not_implemented(&request);
609 Ok(())
610 }
611
612 /// Requests historical order book depth data for a specified instrument.
613 ///
614 /// # Errors
615 ///
616 /// Returns an error if the order book depths request fails.
617 fn request_book_depth(&self, request: RequestBookDepth) -> anyhow::Result<()> {
618 log_not_implemented(&request);
619 Ok(())
620 }
621
622 #[cfg(feature = "defi")]
623 /// Requests a snapshot of a specific AMM pool.
624 ///
625 /// # Errors
626 ///
627 /// Returns an error if the pool snapshot request fails.
628 fn request_pool_snapshot(&self, request: RequestPoolSnapshot) -> anyhow::Result<()> {
629 log_not_implemented(&request);
630 Ok(())
631 }
632}