|
| 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 | +import pandas as pd |
| 17 | + |
| 18 | +from nautilus_trader.backtest.engine import BacktestEngine |
| 19 | +from nautilus_trader.backtest.engine import BacktestEngineConfig |
| 20 | +from nautilus_trader.common.actor import Actor |
| 21 | +from nautilus_trader.config import LoggingConfig |
| 22 | +from nautilus_trader.model.currencies import USD |
| 23 | +from nautilus_trader.model.enums import AccountType |
| 24 | +from nautilus_trader.model.enums import OmsType |
| 25 | +from nautilus_trader.model.identifiers import Venue |
| 26 | +from nautilus_trader.model.objects import Money |
| 27 | +from nautilus_trader.test_kit.providers import TestInstrumentProvider |
| 28 | +from nautilus_trader.test_kit.stubs.data import TestDataStubs |
| 29 | + |
| 30 | + |
| 31 | +class TimerActor(Actor): |
| 32 | + def __init__(self, instrument_id): |
| 33 | + super().__init__() |
| 34 | + self.instrument_id = instrument_id |
| 35 | + self.timer_fired_count = 0 |
| 36 | + self.last_timer_time = 0 |
| 37 | + self.received_data = [] |
| 38 | + |
| 39 | + def on_start(self): |
| 40 | + self.subscribe_quote_ticks(self.instrument_id) |
| 41 | + |
| 42 | + def timer_callback(self, event): |
| 43 | + self.timer_fired_count += 1 |
| 44 | + self.last_timer_time = self.clock.timestamp_ns() |
| 45 | + |
| 46 | + def on_quote_tick(self, tick): |
| 47 | + self.received_data.append(tick) |
| 48 | + |
| 49 | + |
| 50 | +class TestBacktestEngineTimers: |
| 51 | + def setup_method(self): |
| 52 | + self.engine = BacktestEngine( |
| 53 | + BacktestEngineConfig(logging=LoggingConfig(bypass_logging=True)), |
| 54 | + ) |
| 55 | + self.engine.add_venue( |
| 56 | + venue=Venue("SIM"), |
| 57 | + oms_type=OmsType.HEDGING, |
| 58 | + account_type=AccountType.MARGIN, |
| 59 | + base_currency=USD, |
| 60 | + starting_balances=[Money(1000000, USD)], |
| 61 | + ) |
| 62 | + self.instrument = TestInstrumentProvider.default_fx_ccy("EUR/USD") |
| 63 | + self.engine.add_instrument(self.instrument) |
| 64 | + |
| 65 | + def test_timer_execution_no_data(self): |
| 66 | + # Test that timers fire correctly even when no data is provided |
| 67 | + actor = TimerActor(self.instrument.id) |
| 68 | + self.engine.add_actor(actor) |
| 69 | + |
| 70 | + start_time = pd.Timestamp("2024-01-01", tz="UTC") |
| 71 | + end_time = start_time + pd.Timedelta(seconds=10) |
| 72 | + timer_time = start_time + pd.Timedelta(seconds=5) |
| 73 | + |
| 74 | + actor.clock.set_time_alert("test_timer", timer_time, actor.timer_callback) |
| 75 | + |
| 76 | + self.engine.run(start=start_time, end=end_time) |
| 77 | + |
| 78 | + assert actor.timer_fired_count == 1 |
| 79 | + assert actor.last_timer_time == timer_time.value |
| 80 | + |
| 81 | + def test_on_the_fly_data_loading_from_timer(self): |
| 82 | + # Test that data added via add_data_iterator in a timer callback is processed correctly. |
| 83 | + # This mirrors the real-world pattern where subscriptions use generators for on-the-fly loading. |
| 84 | + actor = TimerActor(self.instrument.id) |
| 85 | + self.engine.add_actor(actor) |
| 86 | + |
| 87 | + start_time = pd.Timestamp("2024-01-01", tz="UTC") |
| 88 | + end_time = start_time + pd.Timedelta(seconds=10) |
| 89 | + timer_time = start_time + pd.Timedelta(seconds=5) |
| 90 | + data_time = start_time + pd.Timedelta(seconds=6) |
| 91 | + |
| 92 | + def data_generator(): |
| 93 | + # Generator that yields data when called from timer callback |
| 94 | + # This simulates how _handle_subscribe uses generators for on-the-fly loading |
| 95 | + yield [TestDataStubs.quote_tick(self.instrument, ts_init=data_time.value)] |
| 96 | + |
| 97 | + def timer_callback_with_data(event): |
| 98 | + actor.timer_callback(event) |
| 99 | + # Load data on the fly using add_data_iterator (like _handle_subscribe does) |
| 100 | + self.engine.add_data_iterator("on_the_fly_data", data_generator()) |
| 101 | + |
| 102 | + actor.clock.set_time_alert("test_timer", timer_time, timer_callback_with_data) |
| 103 | + |
| 104 | + self.engine.run(start=start_time, end=end_time) |
| 105 | + |
| 106 | + assert actor.timer_fired_count == 1 |
| 107 | + assert len(actor.received_data) == 1 |
| 108 | + assert actor.received_data[0].ts_init == data_time.value |
| 109 | + |
| 110 | + def test_multiple_timers_same_timestamp(self): |
| 111 | + # Test that multiple timers at the exact same timestamp all fire |
| 112 | + actor = TimerActor(self.instrument.id) |
| 113 | + self.engine.add_actor(actor) |
| 114 | + |
| 115 | + start_time = pd.Timestamp("2024-01-01", tz="UTC") |
| 116 | + end_time = start_time + pd.Timedelta(seconds=10) |
| 117 | + timer_time = start_time + pd.Timedelta(seconds=5) |
| 118 | + |
| 119 | + actor.clock.set_time_alert("timer1", timer_time, actor.timer_callback) |
| 120 | + actor.clock.set_time_alert("timer2", timer_time, actor.timer_callback) |
| 121 | + actor.clock.set_time_alert("timer3", timer_time, actor.timer_callback) |
| 122 | + |
| 123 | + self.engine.run(start=start_time, end=end_time) |
| 124 | + |
| 125 | + assert actor.timer_fired_count == 3 |
| 126 | + assert actor.last_timer_time == timer_time.value |
| 127 | + |
| 128 | + def test_chained_timers(self): |
| 129 | + # Test that a timer scheduling another timer works |
| 130 | + actor = TimerActor(self.instrument.id) |
| 131 | + self.engine.add_actor(actor) |
| 132 | + |
| 133 | + start_time = pd.Timestamp("2024-01-01", tz="UTC") |
| 134 | + end_time = start_time + pd.Timedelta(seconds=10) |
| 135 | + timer1_time = start_time + pd.Timedelta(seconds=5) |
| 136 | + timer2_time = start_time + pd.Timedelta(seconds=6) |
| 137 | + |
| 138 | + def timer1_callback(event): |
| 139 | + actor.timer_callback(event) |
| 140 | + actor.clock.set_time_alert("timer2", timer2_time, actor.timer_callback) |
| 141 | + |
| 142 | + actor.clock.set_time_alert("timer1", timer1_time, timer1_callback) |
| 143 | + |
| 144 | + self.engine.run(start=start_time, end=end_time) |
| 145 | + |
| 146 | + assert actor.timer_fired_count == 2 |
| 147 | + assert actor.last_timer_time == timer2_time.value |
| 148 | + |
| 149 | + def test_chained_timers_same_timestamp(self): |
| 150 | + # Test that a timer scheduling another timer for the SAME timestamp works |
| 151 | + actor = TimerActor(self.instrument.id) |
| 152 | + self.engine.add_actor(actor) |
| 153 | + |
| 154 | + start_time = pd.Timestamp("2024-01-01", tz="UTC") |
| 155 | + end_time = start_time + pd.Timedelta(seconds=10) |
| 156 | + timer_time = start_time + pd.Timedelta(seconds=5) |
| 157 | + |
| 158 | + def timer1_callback(event): |
| 159 | + actor.timer_callback(event) |
| 160 | + # Schedule another one for the same time |
| 161 | + actor.clock.set_time_alert("timer2", timer_time, actor.timer_callback) |
| 162 | + |
| 163 | + actor.clock.set_time_alert("timer1", timer_time, timer1_callback) |
| 164 | + |
| 165 | + self.engine.run(start=start_time, end=end_time) |
| 166 | + |
| 167 | + assert actor.timer_fired_count == 2 |
| 168 | + assert actor.last_timer_time == timer_time.value |
| 169 | + |
| 170 | + def test_timers_alphabetical_order_same_timestamp(self): |
| 171 | + # Test that multiple timers at the same timestamp fire regardless of name order |
| 172 | + actor = TimerActor(self.instrument.id) |
| 173 | + self.engine.add_actor(actor) |
| 174 | + |
| 175 | + start_time = pd.Timestamp("2024-01-01", tz="UTC") |
| 176 | + end_time = start_time + pd.Timedelta(seconds=10) |
| 177 | + timer_time = start_time + pd.Timedelta(seconds=5) |
| 178 | + |
| 179 | + fired_order = [] |
| 180 | + |
| 181 | + def callback_z(event): |
| 182 | + fired_order.append("z") |
| 183 | + |
| 184 | + def callback_a(event): |
| 185 | + fired_order.append("a") |
| 186 | + |
| 187 | + def callback_m(event): |
| 188 | + fired_order.append("m") |
| 189 | + |
| 190 | + actor.clock.set_time_alert("z_timer", timer_time, callback_z) |
| 191 | + actor.clock.set_time_alert("a_timer", timer_time, callback_a) |
| 192 | + actor.clock.set_time_alert("m_timer", timer_time, callback_m) |
| 193 | + |
| 194 | + self.engine.run(start=start_time, end=end_time) |
| 195 | + |
| 196 | + # They should all fire. In current implementation, order might be alphabetical |
| 197 | + # due to Rust's BTreeMap/ordered keys, but the key thing is they ALL fire. |
| 198 | + assert len(fired_order) == 3 |
| 199 | + assert fired_order == ["a", "m", "z"] |
| 200 | + |
| 201 | + def test_timers_and_data_interwoven(self): |
| 202 | + # Test that timers and data are processed in the correct interleaved order |
| 203 | + start_time = pd.Timestamp("2024-01-01", tz="UTC") |
| 204 | + end_time = start_time + pd.Timedelta(seconds=10) |
| 205 | + |
| 206 | + # Timeline: |
| 207 | + # T+2: Data1 |
| 208 | + # T+4: Timer1 |
| 209 | + # T+6: Data2 |
| 210 | + # T+8: Timer2 |
| 211 | + |
| 212 | + events = [] |
| 213 | + |
| 214 | + def timer_callback(event): |
| 215 | + events.append(f"timer_{event.ts_event}") |
| 216 | + |
| 217 | + class InterwovenActor(TimerActor): |
| 218 | + def on_quote_tick(self, tick): |
| 219 | + events.append(f"data_{tick.ts_init}") |
| 220 | + |
| 221 | + actor = InterwovenActor(self.instrument.id) |
| 222 | + self.engine.add_actor(actor) |
| 223 | + |
| 224 | + t2 = (start_time + pd.Timedelta(seconds=2)).value |
| 225 | + t4 = (start_time + pd.Timedelta(seconds=4)).value |
| 226 | + t6 = (start_time + pd.Timedelta(seconds=6)).value |
| 227 | + t8 = (start_time + pd.Timedelta(seconds=8)).value |
| 228 | + |
| 229 | + self.engine.add_data( |
| 230 | + [ |
| 231 | + TestDataStubs.quote_tick(self.instrument, ts_init=t2), |
| 232 | + TestDataStubs.quote_tick(self.instrument, ts_init=t6), |
| 233 | + ], |
| 234 | + ) |
| 235 | + actor.clock.set_time_alert("timer1", pd.Timestamp(t4, unit="ns", tz="UTC"), timer_callback) |
| 236 | + actor.clock.set_time_alert("timer2", pd.Timestamp(t8, unit="ns", tz="UTC"), timer_callback) |
| 237 | + |
| 238 | + self.engine.run(start=start_time, end=end_time) |
| 239 | + |
| 240 | + expected = [f"data_{t2}", f"timer_{t4}", f"data_{t6}", f"timer_{t8}"] |
| 241 | + assert events == expected |
0 commit comments