nautilus-backtest 0.62.0

Core backtesting machinery for the Nautilus trading engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Provides a `BacktestExecutionClient` implementation for backtesting.

use std::{cell::RefCell, fmt::Debug, rc::Rc};

use async_trait::async_trait;
use nautilus_common::{
    cache::Cache,
    clients::ExecutionClient,
    clock::Clock,
    factories::OrderEventFactory,
    messages::execution::{
        BatchCancelOrders, BatchModifyOrders, CancelAllOrders, CancelOrder, ModifyOrder,
        QueryAccount, QueryOrder, SubmitOrder, SubmitOrderList, TradingCommand,
    },
    msgbus::{self, MessagingSwitchboard},
};
use nautilus_core::{Params, UnixNanos, WeakCell};
use nautilus_execution::client::core::ExecutionClientCore;
use nautilus_model::{
    accounts::AccountAny,
    enums::OmsType,
    events::OrderEventAny,
    identifiers::{AccountId, ClientId, ClientOrderId, TraderId, Venue},
    orders::OrderAny,
    types::{AccountBalance, MarginBalance},
};

use crate::exchange::SimulatedExchange;

/// Execution client implementation for backtesting trading operations.
///
/// The `BacktestExecutionClient` provides an execution client interface for
/// backtesting environments, handling order management and trade execution
/// through simulated exchanges. It processes trading commands and coordinates
/// with the simulation infrastructure to provide realistic execution behavior.
#[derive(Clone)]
pub struct BacktestExecutionClient {
    core: ExecutionClientCore,
    factory: OrderEventFactory,
    cache: Rc<RefCell<Cache>>,
    clock: Rc<RefCell<dyn Clock>>,
    exchange: WeakCell<SimulatedExchange>,
    /// Buffered order events for deferred processing.
    ///
    /// Events like `OrderSubmitted` cannot be sent synchronously through
    /// the msgbus during `submit_order` because the exec engine holds a
    /// borrow via its `execute` handler. Instead, events are buffered here
    /// and drained by the engine after the execute borrow is released.
    queued_events: Rc<RefCell<Vec<OrderEventAny>>>,
    routing: bool,
    _frozen_account: bool,
}

impl Debug for BacktestExecutionClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct(stringify!(BacktestExecutionClient))
            .field("client_id", &self.core.client_id)
            .field("routing", &self.routing)
            .finish_non_exhaustive()
    }
}

impl BacktestExecutionClient {
    /// Creates a new [`BacktestExecutionClient`] instance.
    #[must_use]
    pub fn new(
        trader_id: TraderId,
        account_id: AccountId,
        exchange: &Rc<RefCell<SimulatedExchange>>,
        cache: Rc<RefCell<Cache>>,
        clock: Rc<RefCell<dyn Clock>>,
        routing: Option<bool>,
        frozen_account: Option<bool>,
    ) -> Self {
        let routing = routing.unwrap_or(false);
        let frozen_account = frozen_account.unwrap_or(false);
        let exchange_id = exchange.borrow().id;
        let account_type = exchange.borrow().account_type;
        let base_currency = exchange.borrow().base_currency;

        let core = ExecutionClientCore::new(
            trader_id,
            ClientId::from(exchange_id.as_str()),
            Venue::from(exchange_id.as_str()),
            exchange.borrow().oms_type,
            account_id,
            account_type,
            base_currency,
            cache.clone(),
        );

        let factory = OrderEventFactory::new(trader_id, account_id, account_type, base_currency);

        Self {
            core,
            factory,
            exchange: WeakCell::from(Rc::downgrade(exchange)),
            cache,
            clock,
            queued_events: Rc::new(RefCell::new(Vec::new())),
            routing,
            _frozen_account: frozen_account,
        }
    }

    fn get_order(&self, client_order_id: ClientOrderId) -> anyhow::Result<OrderAny> {
        Ok(self.cache.borrow().try_order_owned(&client_order_id)?)
    }

    /// Drain buffered order events, sending each to the exec engine.
    pub fn drain_queued_events(&self) {
        let events: Vec<OrderEventAny> = self.queued_events.borrow_mut().drain(..).collect();
        let endpoint = MessagingSwitchboard::exec_engine_process();
        for event in events {
            msgbus::send_order_event(endpoint, event);
        }
    }

    pub(crate) fn order_event_handler(&self) -> Rc<dyn Fn(OrderEventAny)> {
        let queued_events = Rc::clone(&self.queued_events);
        Rc::new(move |event| queued_events.borrow_mut().push(event))
    }
}

#[async_trait(?Send)]
impl ExecutionClient for BacktestExecutionClient {
    fn is_connected(&self) -> bool {
        self.core.is_connected()
    }

    fn client_id(&self) -> ClientId {
        self.core.client_id
    }

    fn account_id(&self) -> AccountId {
        self.core.account_id
    }

    fn venue(&self) -> Venue {
        self.core.venue
    }

    fn oms_type(&self) -> OmsType {
        self.core.oms_type
    }

    fn get_account(&self) -> Option<AccountAny> {
        self.cache.borrow().account_owned(&self.core.account_id)
    }

    fn generate_account_state(
        &self,
        balances: Vec<AccountBalance>,
        margins: Vec<MarginBalance>,
        reported: bool,
        ts_event: UnixNanos,
        info: Option<Params>,
    ) -> anyhow::Result<()> {
        let ts_init = self.clock.borrow().timestamp_ns();
        let state = self
            .factory
            .generate_account_state(balances, margins, reported, ts_event, ts_init, info);
        let endpoint = MessagingSwitchboard::portfolio_update_account();
        msgbus::send_account_state(endpoint, &state);
        Ok(())
    }

    fn start(&mut self) -> anyhow::Result<()> {
        self.core.set_connected();
        log::info!("Backtest execution client started");
        Ok(())
    }

    fn stop(&mut self) -> anyhow::Result<()> {
        self.core.set_disconnected();
        log::info!("Backtest execution client stopped");
        Ok(())
    }

    fn submit_order(&self, cmd: SubmitOrder) -> anyhow::Result<()> {
        // Buffer the OrderSubmitted event for deferred processing to avoid
        // RefCell re-entrancy (exec_engine holds a borrow during execute)
        let order = self.get_order(cmd.client_order_id)?;
        let ts_init = self.clock.borrow().timestamp_ns();
        let event = self.factory.generate_order_submitted(&order, ts_init);
        self.queued_events.borrow_mut().push(event);

        if let Some(exchange) = self.exchange.upgrade() {
            exchange.borrow_mut().send(TradingCommand::SubmitOrder(cmd));
        } else {
            log::error!("submit_order: SimulatedExchange has been dropped");
        }
        Ok(())
    }

    fn submit_order_list(&self, cmd: SubmitOrderList) -> anyhow::Result<()> {
        let ts_init = self.clock.borrow().timestamp_ns();

        let orders: Vec<OrderAny> = self
            .cache
            .borrow()
            .orders_for_ids(&cmd.order_list.client_order_ids, &cmd);

        // Buffer events for deferred processing
        let mut queued = self.queued_events.borrow_mut();

        for order in &orders {
            let event = self.factory.generate_order_submitted(order, ts_init);
            queued.push(event);
        }
        drop(queued);

        if let Some(exchange) = self.exchange.upgrade() {
            exchange
                .borrow_mut()
                .send(TradingCommand::SubmitOrderList(cmd));
        } else {
            log::error!("submit_order_list: SimulatedExchange has been dropped");
        }
        Ok(())
    }

    fn modify_order(&self, cmd: ModifyOrder) -> anyhow::Result<()> {
        if let Some(exchange) = self.exchange.upgrade() {
            exchange.borrow_mut().send(TradingCommand::ModifyOrder(cmd));
        } else {
            log::error!("modify_order: SimulatedExchange has been dropped");
        }
        Ok(())
    }

    fn batch_modify_orders(&self, cmd: BatchModifyOrders) -> anyhow::Result<()> {
        if let Some(exchange) = self.exchange.upgrade() {
            exchange
                .borrow_mut()
                .send(TradingCommand::ModifyOrders(cmd));
        } else {
            log::error!("batch_modify_orders: SimulatedExchange has been dropped");
        }
        Ok(())
    }

    fn cancel_order(&self, cmd: CancelOrder) -> anyhow::Result<()> {
        if let Some(exchange) = self.exchange.upgrade() {
            exchange.borrow_mut().send(TradingCommand::CancelOrder(cmd));
        } else {
            log::error!("cancel_order: SimulatedExchange has been dropped");
        }
        Ok(())
    }

    fn cancel_all_orders(&self, cmd: CancelAllOrders) -> anyhow::Result<()> {
        if let Some(exchange) = self.exchange.upgrade() {
            exchange
                .borrow_mut()
                .send(TradingCommand::CancelAllOrders(cmd));
        } else {
            log::error!("cancel_all_orders: SimulatedExchange has been dropped");
        }
        Ok(())
    }

    fn batch_cancel_orders(&self, cmd: BatchCancelOrders) -> anyhow::Result<()> {
        if let Some(exchange) = self.exchange.upgrade() {
            exchange
                .borrow_mut()
                .send(TradingCommand::CancelOrders(cmd));
        } else {
            log::error!("batch_cancel_orders: SimulatedExchange has been dropped");
        }
        Ok(())
    }

    fn query_account(&self, cmd: QueryAccount) -> anyhow::Result<()> {
        log::warn!("Backtest execution client does not support account queries: {cmd}");
        Ok(())
    }

    fn query_order(&self, cmd: QueryOrder) -> anyhow::Result<()> {
        log::warn!("Backtest execution client does not support order queries: {cmd}");
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use nautilus_common::{clock::TestClock, messages::execution::QueryOrder};
    use nautilus_core::UUID4;
    use nautilus_execution::models::latency::StaticLatencyModel;
    use nautilus_model::{
        enums::{AccountType, BookType, OmsType},
        identifiers::{InstrumentId, StrategyId},
        stubs::TestDefault,
        types::{Currency, Money},
    };
    use rstest::rstest;

    use super::*;
    use crate::config::SimulatedVenueConfig;

    fn setup_client_with_latency() -> (BacktestExecutionClient, Rc<RefCell<SimulatedExchange>>) {
        let cache = Rc::new(RefCell::new(Cache::default()));
        let clock: Rc<RefCell<dyn Clock>> = Rc::new(RefCell::new(TestClock::new()));
        let latency_model = StaticLatencyModel::new(
            UnixNanos::default(),
            UnixNanos::default(),
            UnixNanos::default(),
            UnixNanos::default(),
        );
        let config = SimulatedVenueConfig::builder()
            .venue(Venue::new("SIM"))
            .oms_type(OmsType::Netting)
            .account_type(AccountType::Margin)
            .book_type(BookType::L2_MBP)
            .starting_balances(vec![Money::new(1_000.0, Currency::USD())])
            .latency_model(Box::new(latency_model))
            .build()
            .unwrap();
        let exchange = Rc::new(RefCell::new(
            SimulatedExchange::new(config, cache.clone(), clock.clone()).unwrap(),
        ));
        let client = BacktestExecutionClient::new(
            TraderId::test_default(),
            AccountId::test_default(),
            &exchange,
            cache,
            clock,
            None,
            None,
        );

        (client, exchange)
    }

    fn query_order() -> QueryOrder {
        QueryOrder::new(
            TraderId::test_default(),
            None,
            StrategyId::test_default(),
            InstrumentId::from("AUD/USD.SIM"),
            ClientOrderId::from("O-001"),
            None,
            UUID4::new(),
            UnixNanos::default(),
            None,
            None,
        )
    }

    fn query_account() -> QueryAccount {
        QueryAccount::new(
            TraderId::test_default(),
            None,
            AccountId::test_default(),
            UUID4::new(),
            UnixNanos::default(),
            None,
            None,
        )
    }

    #[rstest]
    fn test_new_holds_weak_reference_to_source_exchange() {
        let (client, exchange) = setup_client_with_latency();

        // The client must not co-own the exchange, otherwise the exchange owning the
        // client closes an unbreakable cycle.
        assert_eq!(Rc::strong_count(&exchange), 1);

        let upgraded: Rc<RefCell<SimulatedExchange>> = client
            .exchange
            .upgrade()
            .expect("exchange outlives the client here")
            .into();

        assert!(Rc::ptr_eq(&upgraded, &exchange));
    }

    #[rstest]
    fn test_query_order_is_not_forwarded_to_exchange() {
        let (client, exchange) = setup_client_with_latency();

        // Hold an immutable exchange borrow across the call: if the client
        // forwards, send()'s `exchange.borrow_mut()` panics here. This makes the
        // test bite on a client-only revert rather than being masked by the
        // exchange-side query guard.
        let exchange_ref = exchange.borrow();
        let result = client.query_order(query_order());

        assert!(result.is_ok());
        assert_eq!(exchange_ref.max_inflight_command_ts(), None);
    }

    #[rstest]
    fn test_query_account_is_not_forwarded_to_exchange() {
        let (client, exchange) = setup_client_with_latency();

        // See test_query_order_is_not_forwarded_to_exchange: the held borrow
        // makes a forwarding attempt panic before the exchange guard can mask it.
        let exchange_ref = exchange.borrow();
        let result = client.query_account(query_account());

        assert!(result.is_ok());
        assert_eq!(exchange_ref.max_inflight_command_ts(), None);
    }
}