nautilus_backtest/
accumulator.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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
16use nautilus_common::{clock::TestClock, timer::TimeEventHandlerV2};
17use nautilus_core::UnixNanos;
18
19/// Provides a means of accumulating and draining time event handlers.
20#[derive(Debug)]
21pub struct TimeEventAccumulator {
22    event_handlers: Vec<TimeEventHandlerV2>,
23}
24
25impl TimeEventAccumulator {
26    /// Creates a new [`TimeEventAccumulator`] instance.
27    #[must_use]
28    pub const fn new() -> Self {
29        Self {
30            event_handlers: Vec::new(),
31        }
32    }
33
34    /// Advance the given clock to the `to_time_ns`.
35    pub fn advance_clock(&mut self, clock: &mut TestClock, to_time_ns: UnixNanos, set_time: bool) {
36        let events = clock.advance_time(to_time_ns, set_time);
37        let handlers = clock.match_handlers(events);
38        self.event_handlers.extend(handlers);
39    }
40
41    /// Drain the accumulated time event handlers in sorted order (by the events `ts_event`).
42    pub fn drain(&mut self) -> Vec<TimeEventHandlerV2> {
43        // stable sort is not necessary since there is no relation between
44        // events of the same clock. Only time based ordering is needed.
45        self.event_handlers
46            .sort_unstable_by_key(|v| v.event.ts_event);
47        self.event_handlers.drain(..).collect()
48    }
49}
50
51impl Default for TimeEventAccumulator {
52    /// Creates a new default [`TimeEventAccumulator`] instance.
53    fn default() -> Self {
54        Self::new()
55    }
56}
57
58////////////////////////////////////////////////////////////////////////////////
59// Tests
60////////////////////////////////////////////////////////////////////////////////
61#[cfg(all(test, feature = "python"))]
62mod tests {
63    use nautilus_common::timer::{TimeEvent, TimeEventCallback};
64    use nautilus_core::UUID4;
65    use pyo3::{Py, Python, prelude::*, types::PyList};
66    use rstest::*;
67    use ustr::Ustr;
68
69    use super::*;
70
71    #[rstest]
72    fn test_accumulator_drain_sorted() {
73        pyo3::prepare_freethreaded_python();
74
75        Python::with_gil(|py| {
76            let py_list = PyList::empty(py);
77            let py_append = Py::from(py_list.getattr("append").unwrap());
78
79            let mut accumulator = TimeEventAccumulator::new();
80
81            let time_event1 = TimeEvent::new(
82                Ustr::from("TEST_EVENT_1"),
83                UUID4::new(),
84                100.into(),
85                100.into(),
86            );
87            let time_event2 = TimeEvent::new(
88                Ustr::from("TEST_EVENT_2"),
89                UUID4::new(),
90                300.into(),
91                300.into(),
92            );
93            let time_event3 = TimeEvent::new(
94                Ustr::from("TEST_EVENT_3"),
95                UUID4::new(),
96                200.into(),
97                200.into(),
98            );
99
100            // Note: as_ptr returns a borrowed pointer. It is valid as long
101            // as the object is in scope. In this case `callback_ptr` is valid
102            // as long as `py_append` is in scope.
103            let callback = TimeEventCallback::from(py_append.into_any());
104
105            let handler1 = TimeEventHandlerV2::new(time_event1.clone(), callback.clone());
106            let handler2 = TimeEventHandlerV2::new(time_event2.clone(), callback.clone());
107            let handler3 = TimeEventHandlerV2::new(time_event3.clone(), callback);
108
109            accumulator.event_handlers.push(handler1);
110            accumulator.event_handlers.push(handler2);
111            accumulator.event_handlers.push(handler3);
112
113            let drained_handlers = accumulator.drain();
114
115            assert_eq!(drained_handlers.len(), 3);
116            assert_eq!(drained_handlers[0].event.ts_event, time_event1.ts_event);
117            assert_eq!(drained_handlers[1].event.ts_event, time_event3.ts_event);
118            assert_eq!(drained_handlers[2].event.ts_event, time_event2.ts_event);
119        });
120    }
121}