Skip to main content

nautilus_backtest/
data_client.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//! Provides a `BacktestDataClient` implementation for backtesting.
17
18use std::{cell::RefCell, rc::Rc};
19
20use nautilus_common::{
21    cache::Cache,
22    clients::DataClient,
23    messages::data::{
24        RequestBars, RequestBookSnapshot, RequestCustomData, RequestInstrument, RequestInstruments,
25        RequestQuotes, RequestTrades, SubscribeBars, SubscribeBookDeltas, SubscribeBookDepth10,
26        SubscribeCustomData, SubscribeIndexPrices, SubscribeInstrument, SubscribeInstrumentClose,
27        SubscribeInstrumentStatus, SubscribeInstruments, SubscribeMarkPrices, SubscribeQuotes,
28        SubscribeTrades, UnsubscribeBars, UnsubscribeBookDeltas, UnsubscribeBookDepth10,
29        UnsubscribeCustomData, UnsubscribeIndexPrices, UnsubscribeInstrument,
30        UnsubscribeInstrumentClose, UnsubscribeInstrumentStatus, UnsubscribeInstruments,
31        UnsubscribeMarkPrices, UnsubscribeQuotes, UnsubscribeTrades,
32    },
33};
34use nautilus_model::identifiers::{ClientId, Venue};
35
36/// Data client implementation for backtesting market data operations.
37///
38/// The `BacktestDataClient` provides a data client interface specifically designed
39/// for backtesting environments. It handles market data subscriptions and requests
40/// during backtesting, coordinating with the backtesting engine to provide
41/// historical data replay functionality.
42#[derive(Debug)]
43pub struct BacktestDataClient {
44    pub client_id: ClientId,
45    pub venue: Venue,
46    _cache: Rc<RefCell<Cache>>,
47}
48
49impl BacktestDataClient {
50    pub const fn new(client_id: ClientId, venue: Venue, cache: Rc<RefCell<Cache>>) -> Self {
51        Self {
52            client_id,
53            venue,
54            _cache: cache,
55        }
56    }
57}
58
59#[async_trait::async_trait(?Send)]
60impl DataClient for BacktestDataClient {
61    fn client_id(&self) -> ClientId {
62        self.client_id
63    }
64
65    fn venue(&self) -> Option<Venue> {
66        Some(self.venue)
67    }
68
69    fn start(&mut self) -> anyhow::Result<()> {
70        Ok(())
71    }
72
73    fn stop(&mut self) -> anyhow::Result<()> {
74        Ok(())
75    }
76
77    fn reset(&mut self) -> anyhow::Result<()> {
78        Ok(())
79    }
80
81    fn dispose(&mut self) -> anyhow::Result<()> {
82        Ok(())
83    }
84
85    fn is_connected(&self) -> bool {
86        true
87    }
88
89    fn is_disconnected(&self) -> bool {
90        false
91    }
92
93    fn subscribe(&mut self, _cmd: &SubscribeCustomData) -> anyhow::Result<()> {
94        Ok(())
95    }
96
97    fn subscribe_instruments(&mut self, _cmd: &SubscribeInstruments) -> anyhow::Result<()> {
98        Ok(())
99    }
100
101    fn subscribe_instrument(&mut self, _cmd: &SubscribeInstrument) -> anyhow::Result<()> {
102        Ok(())
103    }
104
105    fn subscribe_book_deltas(&mut self, _cmd: &SubscribeBookDeltas) -> anyhow::Result<()> {
106        Ok(())
107    }
108
109    fn subscribe_book_depth10(&mut self, _cmd: &SubscribeBookDepth10) -> anyhow::Result<()> {
110        Ok(())
111    }
112
113    fn subscribe_quotes(&mut self, _cmd: &SubscribeQuotes) -> anyhow::Result<()> {
114        Ok(())
115    }
116
117    fn subscribe_trades(&mut self, _cmd: &SubscribeTrades) -> anyhow::Result<()> {
118        Ok(())
119    }
120
121    fn subscribe_bars(&mut self, _cmd: &SubscribeBars) -> anyhow::Result<()> {
122        Ok(())
123    }
124
125    fn subscribe_mark_prices(&mut self, _cmd: &SubscribeMarkPrices) -> anyhow::Result<()> {
126        Ok(())
127    }
128
129    fn subscribe_index_prices(&mut self, _cmd: &SubscribeIndexPrices) -> anyhow::Result<()> {
130        Ok(())
131    }
132
133    fn subscribe_instrument_status(
134        &mut self,
135        _cmd: &SubscribeInstrumentStatus,
136    ) -> anyhow::Result<()> {
137        Ok(())
138    }
139
140    fn subscribe_instrument_close(
141        &mut self,
142        _cmd: &SubscribeInstrumentClose,
143    ) -> anyhow::Result<()> {
144        Ok(())
145    }
146
147    fn unsubscribe(&mut self, _cmd: &UnsubscribeCustomData) -> anyhow::Result<()> {
148        Ok(())
149    }
150
151    fn unsubscribe_instruments(&mut self, _cmd: &UnsubscribeInstruments) -> anyhow::Result<()> {
152        Ok(())
153    }
154
155    fn unsubscribe_instrument(&mut self, _cmd: &UnsubscribeInstrument) -> anyhow::Result<()> {
156        Ok(())
157    }
158
159    fn unsubscribe_book_deltas(&mut self, _cmd: &UnsubscribeBookDeltas) -> anyhow::Result<()> {
160        Ok(())
161    }
162
163    fn unsubscribe_book_depth10(&mut self, _cmd: &UnsubscribeBookDepth10) -> anyhow::Result<()> {
164        Ok(())
165    }
166
167    fn unsubscribe_quotes(&mut self, _cmd: &UnsubscribeQuotes) -> anyhow::Result<()> {
168        Ok(())
169    }
170
171    fn unsubscribe_trades(&mut self, _cmd: &UnsubscribeTrades) -> anyhow::Result<()> {
172        Ok(())
173    }
174
175    fn unsubscribe_bars(&mut self, _cmd: &UnsubscribeBars) -> anyhow::Result<()> {
176        Ok(())
177    }
178
179    fn unsubscribe_mark_prices(&mut self, _cmd: &UnsubscribeMarkPrices) -> anyhow::Result<()> {
180        Ok(())
181    }
182
183    fn unsubscribe_index_prices(&mut self, _cmd: &UnsubscribeIndexPrices) -> anyhow::Result<()> {
184        Ok(())
185    }
186
187    fn unsubscribe_instrument_status(
188        &mut self,
189        _cmd: &UnsubscribeInstrumentStatus,
190    ) -> anyhow::Result<()> {
191        Ok(())
192    }
193
194    fn unsubscribe_instrument_close(
195        &mut self,
196        _cmd: &UnsubscribeInstrumentClose,
197    ) -> anyhow::Result<()> {
198        Ok(())
199    }
200
201    fn request_data(&self, _request: RequestCustomData) -> anyhow::Result<()> {
202        // No-op in backtest: data is replayed by the engine
203        Ok(())
204    }
205
206    fn request_instruments(&self, _request: RequestInstruments) -> anyhow::Result<()> {
207        // No-op in backtest: instruments are pre-loaded
208        Ok(())
209    }
210
211    fn request_instrument(&self, _request: RequestInstrument) -> anyhow::Result<()> {
212        // No-op in backtest: instruments are pre-loaded
213        Ok(())
214    }
215
216    fn request_book_snapshot(&self, _request: RequestBookSnapshot) -> anyhow::Result<()> {
217        // No-op in backtest
218        Ok(())
219    }
220
221    fn request_quotes(&self, _request: RequestQuotes) -> anyhow::Result<()> {
222        // No-op in backtest: quotes are replayed by the engine
223        Ok(())
224    }
225
226    fn request_trades(&self, _request: RequestTrades) -> anyhow::Result<()> {
227        // No-op in backtest: trades are replayed by the engine
228        Ok(())
229    }
230
231    fn request_bars(&self, _request: RequestBars) -> anyhow::Result<()> {
232        // No-op in backtest: bars are replayed by the engine
233        Ok(())
234    }
235}