Skip to main content

nautilus_backtest/defi/
engine.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//! DeFi data input for the backtest engine.
17
18use nautilus_model::{data::Data, defi::DefiData, identifiers::ClientId};
19
20use crate::engine::BacktestEngine;
21
22impl BacktestEngine {
23    /// Adds DeFi data to the engine for replay during the backtest run.
24    ///
25    /// The `client_id` registers a backtest data client for DeFi startup
26    /// subscriptions and pool snapshot requests. When `client_id` is `None`, a
27    /// default `BACKTEST` client is registered.
28    ///
29    /// # Errors
30    ///
31    /// Returns an error if `data` is empty.
32    pub fn add_defi_data(
33        &mut self,
34        data: Vec<DefiData>,
35        client_id: Option<ClientId>,
36        sort: bool,
37    ) -> anyhow::Result<()> {
38        self.add_defi_data_iterator(data, client_id, sort)
39    }
40
41    /// Adds DeFi data from an iterator for replay during the backtest run.
42    ///
43    /// # Errors
44    ///
45    /// Returns an error if `data` is empty.
46    pub fn add_defi_data_iterator<I>(
47        &mut self,
48        data: I,
49        client_id: Option<ClientId>,
50        sort: bool,
51    ) -> anyhow::Result<()>
52    where
53        I: IntoIterator<Item = DefiData>,
54    {
55        let data: Vec<Data> = data
56            .into_iter()
57            .map(|defi| Data::Defi(Box::new(defi)))
58            .collect();
59        self.add_data(data, client_id, false, sort)
60    }
61
62    pub(crate) fn add_defi_data_client_if_not_exists(&mut self, client_id: Option<ClientId>) {
63        let client_id = client_id.unwrap_or_else(|| ClientId::from("BACKTEST"));
64        self.add_data_client_if_not_exists(client_id);
65    }
66}