Skip to main content

nautilus_data/engine/
bar.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
16use std::fmt::Debug;
17
18use anyhow::Context;
19use jiff::tz::TimeZone;
20use nautilus_common::{
21    messages::data::{SubscribeBars, SubscribeCommand},
22    msgbus::{self, MStr, Topic, TypedHandler},
23};
24use nautilus_core::{UUID4, datetime::get_timezone};
25use nautilus_model::data::{Bar, BarType, QuoteTick, TradeTick};
26
27use super::{
28    AggregationSource, BAR_AGGREGATOR_PRIORITY, BarAggregation, BarAggregator, BarBarHandler,
29    BarQuoteHandler, BarTradeHandler, DataCommand, DataEngine, DataResponse, Instrument,
30    InstrumentAny, PriceType, Rc, RefCell, RenkoBarAggregator, RequestBarAggregation,
31    RequestCommand, SubscribeQuotes, SubscribeTrades, TickBarAggregator,
32    TickImbalanceBarAggregator, TickRunsBarAggregator, TimeBarAggregator, UnsubscribeBars,
33    UnsubscribeCommand, UnsubscribeQuotes, UnsubscribeTrades, ValueBarAggregator,
34    ValueImbalanceBarAggregator, ValueRunsBarAggregator, VolumeBarAggregator,
35    VolumeImbalanceBarAggregator, VolumeRunsBarAggregator, handlers::WarmupBuffer,
36    log_error_on_cache_insert, process_engine_bar, request_bar_aggregation_from_params,
37    request_params, requests::time_zone_from_params, switchboard,
38};
39
40impl DataEngine {
41    pub(super) fn prepare_request_bar_aggregators_from_state(
42        &mut self,
43        request_id: UUID4,
44        state: &RequestBarAggregation,
45    ) -> anyhow::Result<()> {
46        if !self.can_start_request_bar_aggregators(request_id, state) {
47            anyhow::bail!(
48                "Cannot request aggregated bars: one of the aggregators in `bar_types` is already running"
49            );
50        }
51
52        self.request_bar_aggregations
53            .insert(request_id, state.clone());
54
55        if let Err(e) = self.init_request_bar_aggregators(request_id, state) {
56            self.cleanup_request_bar_aggregators(&request_id);
57            return Err(e);
58        }
59
60        Ok(())
61    }
62
63    pub(super) fn prepare_request_bar_aggregators(
64        &mut self,
65        req: &RequestCommand,
66    ) -> anyhow::Result<()> {
67        let request_id = *req.request_id();
68
69        let Some(state) = request_bar_aggregation_from_params(request_params(req))? else {
70            return Ok(());
71        };
72
73        self.prepare_request_bar_aggregators_from_state(request_id, &state)
74    }
75
76    fn can_start_request_bar_aggregators(
77        &self,
78        request_id: UUID4,
79        state: &RequestBarAggregation,
80    ) -> bool {
81        let aggregator_request_id = state.aggregator_request_id(request_id);
82        state.bar_types.iter().all(|bar_type| {
83            let key = bar_aggregator_key(*bar_type, aggregator_request_id);
84            self.bar_aggregators
85                .get(&key)
86                .is_none_or(|aggregator| !aggregator.borrow().is_running())
87        })
88    }
89
90    fn init_request_bar_aggregators(
91        &mut self,
92        request_id: UUID4,
93        state: &RequestBarAggregation,
94    ) -> anyhow::Result<()> {
95        let aggregator_request_id = state.aggregator_request_id(request_id);
96
97        for bar_type in &state.bar_types {
98            self.create_bar_aggregator_for_key(
99                *bar_type,
100                aggregator_request_id,
101                state.skip_first_non_full_bar,
102                state.time_zone.clone(),
103            )?;
104            self.setup_bar_aggregator(*bar_type, true, aggregator_request_id)?;
105
106            let key = bar_aggregator_key(*bar_type, aggregator_request_id);
107            if let Some(aggregator) = self.bar_aggregators.get(&key) {
108                if state.disable_build_with_no_updates {
109                    aggregator.borrow_mut().set_build_with_no_updates(false);
110                }
111
112                aggregator.borrow_mut().set_is_running(true);
113            }
114        }
115
116        self.set_request_bar_aggregator_chain_handlers(request_id, state);
117
118        Ok(())
119    }
120
121    fn set_request_bar_aggregator_chain_handlers(
122        &self,
123        request_id: UUID4,
124        state: &RequestBarAggregation,
125    ) {
126        let aggregator_request_id = state.aggregator_request_id(request_id);
127
128        for bar_type in &state.bar_types {
129            let key = bar_aggregator_key(*bar_type, aggregator_request_id);
130
131            let Some(aggregator) = self.bar_aggregators.get(&key).cloned() else {
132                continue;
133            };
134
135            let downstream: Vec<_> = state
136                .bar_types
137                .iter()
138                .filter(|candidate| {
139                    candidate.is_composite()
140                        && candidate.composite().standard() == bar_type.standard()
141                })
142                .filter_map(|candidate| {
143                    let key = bar_aggregator_key(*candidate, aggregator_request_id);
144                    self.bar_aggregators.get(&key).cloned()
145                })
146                .collect();
147
148            let cache = Rc::clone(&self.cache);
149
150            let handler: Box<dyn FnMut(Bar)> = Box::new(move |bar: Bar| {
151                // Request-generated bars are delivered only through the cache.
152                if let Err(e) = cache.as_ref().borrow_mut().add_bar_historical(bar) {
153                    log_error_on_cache_insert(&e);
154                }
155
156                for aggregator in &downstream {
157                    aggregator.borrow_mut().handle_bar(bar);
158                }
159            });
160
161            aggregator.borrow_mut().set_historical_mode(true, handler);
162        }
163    }
164
165    pub(super) fn cleanup_request_bar_aggregators(&mut self, request_id: &UUID4) -> bool {
166        let Some(state) = self.request_bar_aggregations.remove(request_id) else {
167            return false;
168        };
169
170        let aggregator_request_id = state.aggregator_request_id(*request_id);
171
172        // Finish each source before the aggregators that depend on it, so the bars from its
173        // replayed live data reach dependents still in historical mode
174        let bar_types = sources_before_dependents(&state.bar_types);
175
176        for bar_type in bar_types {
177            let key = bar_aggregator_key(bar_type, aggregator_request_id);
178            let has_live_handlers =
179                state.update_subscriptions && self.bar_aggregator_handlers.contains_key(&key);
180
181            self.finish_historical_bar_aggregator(&key, has_live_handlers);
182
183            let keep_running = if has_live_handlers {
184                match self.setup_bar_aggregator(bar_type, false, aggregator_request_id) {
185                    Ok(()) => true,
186                    Err(e) => {
187                        log::error!(
188                            "Error starting live request bar aggregator for {bar_type}: {e}"
189                        );
190                        false
191                    }
192                }
193            } else {
194                false
195            };
196
197            if let Some(aggregator) = self.bar_aggregators.get(&key) {
198                aggregator.borrow_mut().set_is_running(keep_running);
199            }
200
201            let stop_after_request = self.bar_aggregators_pending_stop.contains(&key)
202                && !self.has_pending_shared_bar_aggregation(bar_type);
203
204            if (!state.update_subscriptions || stop_after_request)
205                && let Err(e) = self.stop_bar_aggregator(bar_type, aggregator_request_id)
206            {
207                log::error!("Error stopping request bar aggregator for {bar_type}: {e}");
208            }
209        }
210
211        true
212    }
213
214    fn finish_historical_bar_aggregator(
215        &mut self,
216        key: &BarAggregatorKey,
217        has_live_handlers: bool,
218    ) {
219        // Replay buffered updates before closing their last historical interval
220        if has_live_handlers
221            && let Some(warmup) = self.bar_aggregator_warmups.remove(key)
222            && let Some(aggregator) = self.bar_aggregators.get(key)
223        {
224            warmup.release(aggregator.borrow_mut().as_mut());
225        }
226
227        if let Some(aggregator) = self.bar_aggregators.get(key) {
228            let mut aggregator = aggregator.borrow_mut();
229
230            if let Some(aggregator) = aggregator.as_any_mut().downcast_mut::<TimeBarAggregator>() {
231                aggregator.flush_pending_historical_bar();
232            }
233        }
234    }
235
236    pub(super) fn process_request_bar_aggregation_response(
237        &mut self,
238        resp: &DataResponse,
239        completed: bool,
240    ) {
241        let correlation_id = *resp.correlation_id();
242
243        let Some(state) = self.request_bar_aggregations.get(&correlation_id).cloned() else {
244            return;
245        };
246
247        match resp {
248            DataResponse::Quotes(r) => {
249                for quote in r.data.iter() {
250                    self.update_request_bar_aggregators_from_quote(&state, correlation_id, *quote);
251                }
252            }
253            DataResponse::Trades(r) => {
254                for trade in r.data.iter() {
255                    self.update_request_bar_aggregators_from_trade(&state, correlation_id, *trade);
256                }
257            }
258            DataResponse::Bars(r) => {
259                for bar in r.data.iter() {
260                    self.update_request_bar_aggregators_from_bar(&state, correlation_id, *bar);
261                }
262            }
263            _ => {}
264        }
265
266        if completed {
267            self.cleanup_request_bar_aggregators(&correlation_id);
268        }
269    }
270
271    pub(super) fn update_request_bar_aggregators_from_quote(
272        &self,
273        state: &RequestBarAggregation,
274        request_id: UUID4,
275        quote: QuoteTick,
276    ) {
277        let aggregator_request_id = state.aggregator_request_id(request_id);
278
279        for bar_type in &state.bar_types {
280            if bar_type.is_composite()
281                || bar_type.instrument_id() != quote.instrument_id
282                || bar_type.spec().price_type == PriceType::Last
283            {
284                continue;
285            }
286
287            self.update_request_bar_aggregator(*bar_type, aggregator_request_id, |aggregator| {
288                aggregator.handle_quote(quote);
289            });
290        }
291    }
292
293    pub(super) fn update_request_bar_aggregators_from_trade(
294        &self,
295        state: &RequestBarAggregation,
296        request_id: UUID4,
297        trade: TradeTick,
298    ) {
299        let aggregator_request_id = state.aggregator_request_id(request_id);
300
301        for bar_type in &state.bar_types {
302            if bar_type.is_composite()
303                || bar_type.instrument_id() != trade.instrument_id
304                || bar_type.spec().price_type != PriceType::Last
305            {
306                continue;
307            }
308
309            self.update_request_bar_aggregator(*bar_type, aggregator_request_id, |aggregator| {
310                aggregator.handle_trade(trade);
311            });
312        }
313    }
314
315    pub(super) fn update_request_bar_aggregators_from_bar(
316        &self,
317        state: &RequestBarAggregation,
318        request_id: UUID4,
319        bar: Bar,
320    ) {
321        let aggregator_request_id = state.aggregator_request_id(request_id);
322
323        for bar_type in &state.bar_types {
324            if !bar_type.is_composite()
325                || bar_type.composite().standard() != bar.bar_type.standard()
326            {
327                continue;
328            }
329
330            self.update_request_bar_aggregator(*bar_type, aggregator_request_id, |aggregator| {
331                aggregator.handle_bar(bar);
332            });
333        }
334    }
335
336    pub(super) fn update_request_bar_aggregator<F>(
337        &self,
338        bar_type: BarType,
339        request_id: Option<UUID4>,
340        update: F,
341    ) where
342        F: FnOnce(&mut dyn BarAggregator),
343    {
344        let key = bar_aggregator_key(bar_type, request_id);
345
346        let Some(aggregator) = self.bar_aggregators.get(&key) else {
347            log::error!("Cannot update request bar aggregator: no aggregator found for {bar_type}");
348            return;
349        };
350
351        update(aggregator.borrow_mut().as_mut());
352    }
353
354    pub(super) fn subscribe_bars(&mut self, cmd: &SubscribeBars) -> anyhow::Result<()> {
355        let bar_type = cmd
356            .single_bar_type()
357            .expect("native engine receives one bar type");
358
359        match bar_type.aggregation_source() {
360            AggregationSource::Internal => self.start_bar_aggregation(cmd)?,
361            AggregationSource::External => {
362                if bar_type.instrument_id().is_synthetic() {
363                    anyhow::bail!(
364                        "Cannot subscribe for externally aggregated synthetic instrument bar data"
365                    );
366                }
367            }
368        }
369
370        Ok(())
371    }
372
373    pub(super) fn unsubscribe_bars(&mut self, cmd: &UnsubscribeBars) {
374        let bar_type = cmd
375            .single_bar_type()
376            .expect("native engine receives one bar type");
377
378        // Don't remove aggregator if other exact-topic subscribers still exist
379        let topic = switchboard::get_bars_topic(bar_type.standard());
380        if msgbus::exact_subscriber_count_bars(topic) > 0 {
381            return;
382        }
383
384        let retained = self
385            .subscriptions_bar_aggregation
386            .get(&bar_type.standard())
387            .map(|subscription| subscription.command.clone());
388
389        let command = retained.map_or_else(
390            || cmd.clone(),
391            |subscribe| {
392                UnsubscribeBars::new(
393                    subscribe.bar_types,
394                    subscribe.client_id,
395                    subscribe.venue,
396                    cmd.command_id,
397                    cmd.ts_init,
398                    Some(subscribe.command_id),
399                    subscribe.params,
400                )
401            },
402        );
403
404        let key = bar_aggregator_key(bar_type, None);
405
406        // History retains this instance after its live-source ownership was released
407        if self.bar_aggregators_pending_stop.contains(&key) {
408            return;
409        }
410
411        if self.bar_aggregators.contains_key(&key) {
412            let result = if self.has_pending_shared_bar_aggregation(bar_type) {
413                // Release live data without canceling an accepted historical aggregation
414                self.unsubscribe_bar_aggregator_handlers(&key);
415                self.bar_aggregators_pending_stop.insert(key);
416                Ok(())
417            } else {
418                self.stop_bar_aggregator(bar_type, None)
419            };
420
421            match result {
422                Ok(()) => {
423                    self.subscriptions_bar_aggregation
424                        .remove(&bar_type.standard());
425                    self.unsubscribe_bar_aggregator(&command);
426                }
427                Err(e) => log::error!("Error stopping bar aggregator for {bar_type}: {e}"),
428            }
429        }
430
431        // After stopping a composite, release its source through `unsubscribe_bars`, which frees
432        // the client feed recorded in the source's retained command.
433        if command
434            .single_bar_type()
435            .expect("retained aggregation has one bar type")
436            .is_composite()
437        {
438            let source_type = command
439                .single_bar_type()
440                .expect("retained aggregation has one bar type")
441                .composite();
442
443            if self
444                .bar_aggregators
445                .contains_key(&bar_aggregator_key(source_type, None))
446            {
447                self.unsubscribe_bars(&UnsubscribeBars::new(
448                    source_type,
449                    command.client_id,
450                    command.venue,
451                    UUID4::new(),
452                    command.ts_init,
453                    Some(command.command_id),
454                    command.params.clone(),
455                ));
456            }
457        }
458    }
459
460    fn has_pending_shared_bar_aggregation(&self, bar_type: BarType) -> bool {
461        self.request_bar_aggregations.values().any(|state| {
462            state.update_subscriptions
463                && state
464                    .bar_types
465                    .iter()
466                    .any(|requested| requested.standard() == bar_type.standard())
467        })
468    }
469
470    fn create_bar_aggregator(
471        &self,
472        instrument: &InstrumentAny,
473        bar_type: BarType,
474        skip_first_non_full_bar: Option<bool>,
475        time_zone: TimeZone,
476    ) -> Box<dyn BarAggregator> {
477        let cache = Rc::clone(&self.cache);
478        let validate_sequence = self.config.validate_data_sequence;
479
480        let handler = move |bar: Bar| {
481            process_engine_bar(&cache, validate_sequence, true, bar);
482        };
483
484        let clock = Rc::clone(&self.clock);
485        let config = self.config.clone();
486
487        let price_precision = instrument.price_precision();
488        let size_precision = instrument.size_precision();
489
490        if bar_type.spec().is_time_aggregated() {
491            let time_bars_origin_offset = config
492                .time_bars_origin_offset
493                .get(&bar_type.spec().aggregation)
494                .map(|duration| jiff::SignedDuration::try_from(*duration).unwrap_or_default());
495
496            Box::new(TimeBarAggregator::new(
497                bar_type,
498                price_precision,
499                size_precision,
500                clock,
501                handler,
502                config.time_bars_build_with_no_updates,
503                config.time_bars_timestamp_on_close,
504                config.time_bars_interval_type,
505                time_bars_origin_offset,
506                time_zone,
507                config.time_bars_build_delay,
508                skip_first_non_full_bar.unwrap_or(config.time_bars_skip_first_non_full_bar),
509            ))
510        } else {
511            match bar_type.spec().aggregation {
512                BarAggregation::Tick => Box::new(TickBarAggregator::new(
513                    bar_type,
514                    price_precision,
515                    size_precision,
516                    handler,
517                )) as Box<dyn BarAggregator>,
518                BarAggregation::TickImbalance => Box::new(TickImbalanceBarAggregator::new(
519                    bar_type,
520                    price_precision,
521                    size_precision,
522                    handler,
523                )) as Box<dyn BarAggregator>,
524                BarAggregation::TickRuns => Box::new(TickRunsBarAggregator::new(
525                    bar_type,
526                    price_precision,
527                    size_precision,
528                    handler,
529                )) as Box<dyn BarAggregator>,
530                BarAggregation::Volume => Box::new(VolumeBarAggregator::new(
531                    bar_type,
532                    price_precision,
533                    size_precision,
534                    handler,
535                )) as Box<dyn BarAggregator>,
536                BarAggregation::VolumeImbalance => Box::new(VolumeImbalanceBarAggregator::new(
537                    bar_type,
538                    price_precision,
539                    size_precision,
540                    handler,
541                )) as Box<dyn BarAggregator>,
542                BarAggregation::VolumeRuns => Box::new(VolumeRunsBarAggregator::new(
543                    bar_type,
544                    price_precision,
545                    size_precision,
546                    handler,
547                )) as Box<dyn BarAggregator>,
548                BarAggregation::Value => Box::new(ValueBarAggregator::new(
549                    bar_type,
550                    price_precision,
551                    size_precision,
552                    handler,
553                )) as Box<dyn BarAggregator>,
554                BarAggregation::ValueImbalance => Box::new(ValueImbalanceBarAggregator::new(
555                    bar_type,
556                    price_precision,
557                    size_precision,
558                    handler,
559                )) as Box<dyn BarAggregator>,
560                BarAggregation::ValueRuns => Box::new(ValueRunsBarAggregator::new(
561                    bar_type,
562                    price_precision,
563                    size_precision,
564                    handler,
565                )) as Box<dyn BarAggregator>,
566                BarAggregation::Renko => Box::new(RenkoBarAggregator::new(
567                    bar_type,
568                    price_precision,
569                    size_precision,
570                    instrument.price_increment(),
571                    handler,
572                )) as Box<dyn BarAggregator>,
573                other => unreachable!(
574                    "Unsupported internal bar aggregation dispatch for {other:?}; update `create_bar_aggregator`"
575                ),
576            }
577        }
578    }
579
580    pub(super) fn create_bar_aggregator_for_key(
581        &mut self,
582        bar_type: BarType,
583        request_id: Option<UUID4>,
584        skip_first_non_full_bar: Option<bool>,
585        time_zone: Option<TimeZone>,
586    ) -> anyhow::Result<()> {
587        let key = bar_aggregator_key(bar_type, request_id);
588
589        let time_zone = match time_zone {
590            Some(zone) => zone,
591            None => get_timezone(self.config.time_bars_time_zone.as_deref().unwrap_or("UTC"))
592                .context("invalid `time_bars_time_zone` configuration")?,
593        };
594
595        if let Some(aggregator) = self.bar_aggregators.get(&key) {
596            let aggregator = aggregator.borrow();
597            if let Some(aggregator) = aggregator.as_any().downcast_ref::<TimeBarAggregator>() {
598                anyhow::ensure!(
599                    aggregator.time_zone() == &time_zone,
600                    "Cannot reuse bar aggregator {bar_type} with a different time zone"
601                );
602            }
603
604            return Ok(());
605        }
606
607        let instrument = {
608            let cache = self.cache.borrow();
609            cache
610                .instrument(&bar_type.instrument_id())
611                .ok_or_else(|| {
612                    anyhow::anyhow!(
613                        "Cannot start bar aggregation: no instrument found for {}",
614                        bar_type.instrument_id(),
615                    )
616                })?
617                .clone()
618        };
619
620        let aggregator =
621            self.create_bar_aggregator(&instrument, bar_type, skip_first_non_full_bar, time_zone);
622        debug_assert_eq!(
623            aggregator.bar_type(),
624            key.0,
625            "aggregator bar type must match its standardized key"
626        );
627        self.bar_aggregators
628            .insert(key, Rc::new(RefCell::new(aggregator)));
629
630        Ok(())
631    }
632
633    fn start_bar_aggregation(&mut self, cmd: &SubscribeBars) -> anyhow::Result<()> {
634        let bar_type = cmd
635            .single_bar_type()
636            .expect("native engine receives one bar type");
637        let time_zone = time_zone_from_params(cmd.params.as_ref())?;
638        let skip_first_non_full_bar = cmd
639            .params
640            .as_ref()
641            .and_then(|params| params.get_bool("skip_first_non_full_bar"));
642        self.create_bar_aggregator_for_key(
643            bar_type,
644            None,
645            skip_first_non_full_bar,
646            time_zone.clone(),
647        )?;
648        let key = bar_aggregator_key(bar_type, None);
649
650        if self
651            .bar_aggregators
652            .get(&key)
653            .is_some_and(|aggregator| aggregator.borrow().is_running())
654            && self.bar_aggregator_handlers.contains_key(&key)
655        {
656            if let Some(source_command) = self
657                .subscriptions_bar_aggregation
658                .get(&bar_type.standard())
659                .and_then(|subscription| subscription.source.clone())
660            {
661                self.execute(DataCommand::Subscribe(source_command));
662            }
663
664            log::debug!("Reusing running bar aggregator for {bar_type}");
665            return Ok(());
666        }
667
668        self.start_bar_aggregator(bar_type, None, skip_first_non_full_bar, time_zone)?;
669        self.bar_aggregators_pending_stop.remove(&key);
670        let source = self.subscribe_bar_aggregator(cmd);
671        self.subscriptions_bar_aggregation.insert(
672            bar_type.standard(),
673            BarAggregationSubscription {
674                command: cmd.clone(),
675                source,
676            },
677        );
678
679        Ok(())
680    }
681
682    fn start_bar_aggregator(
683        &mut self,
684        bar_type: BarType,
685        request_id: Option<UUID4>,
686        skip_first_non_full_bar: Option<bool>,
687        time_zone: Option<TimeZone>,
688    ) -> anyhow::Result<()> {
689        let key = bar_aggregator_key(bar_type, request_id);
690        let bar_type_std = bar_type.standard();
691
692        self.create_bar_aggregator_for_key(
693            bar_type,
694            request_id,
695            skip_first_non_full_bar,
696            time_zone,
697        )?;
698        let aggregator = self
699            .bar_aggregators
700            .get(&key)
701            .map(Rc::clone)
702            .ok_or_else(|| anyhow::anyhow!("Cannot start bar aggregation for {bar_type}"))?;
703        let defer_subscription_activation = request_id.is_none()
704            && aggregator.borrow().is_running()
705            && !self.bar_aggregator_handlers.contains_key(&key);
706
707        if !self.bar_aggregator_handlers.contains_key(&key) {
708            // Subscribe to underlying data topics
709            let mut subscriptions = Vec::new();
710
711            // A deferred aggregator is still replaying its request in historical mode, so live data
712            // is held back and replayed once the request completes
713            let defer = defer_subscription_activation;
714            let warmup;
715
716            if bar_type.is_composite() {
717                let topic = switchboard::get_bars_topic(bar_type.composite());
718                let buffer = defer.then(WarmupBuffer::new);
719                let handler = TypedHandler::new(
720                    BarBarHandler::new(&aggregator, bar_type_std).with_warmup(buffer.clone()),
721                );
722                warmup = buffer.map(BarAggregatorWarmup::Bar);
723                msgbus::subscribe_bars(topic.into(), handler.clone(), None);
724                subscriptions.push(BarAggregatorSubscription::Bar { topic, handler });
725            } else if bar_type.spec().price_type == PriceType::Last {
726                let topic = switchboard::get_trades_topic(bar_type.instrument_id());
727                let buffer = defer.then(WarmupBuffer::new);
728                let handler = TypedHandler::new(
729                    BarTradeHandler::new(&aggregator, bar_type_std).with_warmup(buffer.clone()),
730                );
731                warmup = buffer.map(BarAggregatorWarmup::Trade);
732                msgbus::subscribe_trades(
733                    topic.into(),
734                    handler.clone(),
735                    Some(BAR_AGGREGATOR_PRIORITY),
736                );
737                subscriptions.push(BarAggregatorSubscription::Trade { topic, handler });
738            } else {
739                // Warn if imbalance/runs aggregation is wired to quotes (needs aggressor_side from trades)
740                if matches!(
741                    bar_type.spec().aggregation,
742                    BarAggregation::TickImbalance
743                        | BarAggregation::VolumeImbalance
744                        | BarAggregation::ValueImbalance
745                        | BarAggregation::TickRuns
746                        | BarAggregation::VolumeRuns
747                        | BarAggregation::ValueRuns
748                ) {
749                    log::warn!(
750                        "Bar type {bar_type} uses imbalance/runs aggregation which requires trade \
751                         data with `aggressor_side`, but `price_type` is not LAST so it will receive \
752                         quote data: bars will not emit correctly",
753                    );
754                }
755
756                let topic = switchboard::get_quotes_topic(bar_type.instrument_id());
757                let buffer = defer.then(WarmupBuffer::new);
758                let handler = TypedHandler::new(
759                    BarQuoteHandler::new(&aggregator, bar_type_std).with_warmup(buffer.clone()),
760                );
761                warmup = buffer.map(BarAggregatorWarmup::Quote);
762                msgbus::subscribe_quotes(
763                    topic.into(),
764                    handler.clone(),
765                    Some(BAR_AGGREGATOR_PRIORITY),
766                );
767                subscriptions.push(BarAggregatorSubscription::Quote { topic, handler });
768            }
769
770            self.bar_aggregator_handlers.insert(key, subscriptions);
771
772            if let Some(warmup) = warmup {
773                self.bar_aggregator_warmups.insert(key, warmup);
774            }
775        }
776
777        if defer_subscription_activation {
778            return Ok(());
779        }
780
781        self.setup_bar_aggregator(bar_type, false, request_id)?;
782
783        aggregator.borrow_mut().set_is_running(true);
784
785        Ok(())
786    }
787
788    fn subscribe_bar_aggregator(&mut self, cmd: &SubscribeBars) -> Option<SubscribeCommand> {
789        let subscribe = self.bar_aggregator_source_command(cmd)?;
790        self.execute(DataCommand::Subscribe(subscribe.clone()));
791        Some(subscribe)
792    }
793
794    fn bar_aggregator_source_command(&self, cmd: &SubscribeBars) -> Option<SubscribeCommand> {
795        let bar_type = cmd
796            .single_bar_type()
797            .expect("native engine receives one bar type");
798        let key = bar_aggregator_key(bar_type, None);
799        if !self.bar_aggregators.contains_key(&key) {
800            log::error!("Cannot subscribe bar aggregator: no aggregator found for {bar_type}");
801            return None;
802        }
803
804        if bar_type.is_composite() {
805            let composite_bar_type = bar_type.composite();
806            if composite_bar_type.is_externally_aggregated() {
807                let subscribe = SubscribeBars::new(
808                    composite_bar_type,
809                    cmd.client_id,
810                    cmd.venue,
811                    UUID4::new(),
812                    cmd.ts_init,
813                    Some(cmd.command_id),
814                    cmd.params.clone(),
815                );
816                return Some(SubscribeCommand::Bars(subscribe));
817            }
818        } else if bar_type.spec().price_type == PriceType::Last {
819            let subscribe = SubscribeTrades::new(
820                bar_type.instrument_id(),
821                cmd.client_id,
822                cmd.venue,
823                UUID4::new(),
824                cmd.ts_init,
825                Some(cmd.command_id),
826                cmd.params.clone(),
827            );
828            return Some(SubscribeCommand::Trades(subscribe));
829        } else {
830            let subscribe = SubscribeQuotes::new(
831                bar_type.instrument_id(),
832                cmd.client_id,
833                cmd.venue,
834                UUID4::new(),
835                cmd.ts_init,
836                Some(cmd.command_id),
837                cmd.params.clone(),
838            );
839            return Some(SubscribeCommand::Quotes(subscribe));
840        }
841
842        None
843    }
844
845    /// Sets up a bar aggregator.
846    ///
847    /// This method handles historical mode, message bus subscriptions, and time bar aggregator setup.
848    pub(super) fn setup_bar_aggregator(
849        &self,
850        bar_type: BarType,
851        historical: bool,
852        request_id: Option<UUID4>,
853    ) -> anyhow::Result<()> {
854        let key = bar_aggregator_key(bar_type, request_id);
855
856        let aggregator = self.bar_aggregators.get(&key).ok_or_else(|| {
857            anyhow::anyhow!("Cannot setup bar aggregator: no aggregator found for {bar_type}")
858        })?;
859
860        // Set historical mode and handler
861        let cache = Rc::clone(&self.cache);
862        let validate_sequence = self.config.validate_data_sequence;
863        let publish = !historical;
864
865        let handler: Box<dyn FnMut(Bar)> = Box::new(move |bar: Bar| {
866            process_engine_bar(&cache, validate_sequence, publish, bar);
867        });
868
869        aggregator
870            .borrow_mut()
871            .set_historical_mode(historical, handler);
872
873        // For TimeBarAggregator, set clock and start timer
874        if bar_type.spec().is_time_aggregated() {
875            use nautilus_common::clock::VirtualClock;
876
877            if historical {
878                // Each aggregator gets its own independent clock
879                let test_clock = Rc::new(RefCell::new(VirtualClock::new()));
880                aggregator.borrow_mut().set_clock(test_clock);
881                // Set weak reference for historical mode (start_timer called later from preprocess_historical_events)
882                // Store weak reference so start_timer can use it when called later
883                let aggregator_weak = Rc::downgrade(aggregator);
884                aggregator.borrow_mut().set_aggregator_weak(aggregator_weak);
885            } else {
886                aggregator.borrow_mut().set_clock(Rc::clone(&self.clock));
887                aggregator
888                    .borrow_mut()
889                    .start_timer(Some(Rc::clone(aggregator)));
890            }
891        }
892
893        if !historical && request_id.is_none() {
894            self.connect_time_bar_sources(bar_type);
895        }
896
897        Ok(())
898    }
899
900    fn connect_time_bar_sources(&self, bar_type: BarType) {
901        let bar_types = self
902            .subscriptions_bar_aggregation
903            .values()
904            .map(|subscription| {
905                subscription
906                    .command
907                    .single_bar_type()
908                    .expect("retained aggregation has one bar type")
909            })
910            .chain(std::iter::once(bar_type));
911
912        for bar_type in bar_types {
913            if !bar_type.is_composite()
914                || !bar_type.spec().is_time_aggregated()
915                || !bar_type.composite().is_internally_aggregated()
916                || !bar_type.composite().spec().is_time_aggregated()
917            {
918                continue;
919            }
920
921            let Some(source) = self
922                .bar_aggregators
923                .get(&bar_aggregator_key(bar_type.composite(), None))
924            else {
925                continue;
926            };
927
928            let Some(aggregator) = self
929                .bar_aggregators
930                .get(&bar_aggregator_key(bar_type, None))
931            else {
932                continue;
933            };
934
935            if let Some(aggregator) = aggregator
936                .borrow_mut()
937                .as_any_mut()
938                .downcast_mut::<TimeBarAggregator>()
939            {
940                aggregator.set_timer_source(Rc::downgrade(source));
941            }
942        }
943    }
944
945    fn unsubscribe_bar_aggregator(&mut self, cmd: &UnsubscribeBars) {
946        let bar_type = cmd
947            .single_bar_type()
948            .expect("native engine receives one bar type");
949
950        if bar_type.is_composite() {
951            let composite_bar_type = bar_type.composite();
952            if composite_bar_type.is_externally_aggregated() {
953                let unsubscribe = UnsubscribeBars::new(
954                    composite_bar_type,
955                    cmd.client_id,
956                    cmd.venue,
957                    UUID4::new(),
958                    cmd.ts_init,
959                    Some(cmd.command_id),
960                    cmd.params.clone(),
961                );
962                self.execute(DataCommand::Unsubscribe(UnsubscribeCommand::Bars(
963                    unsubscribe,
964                )));
965            }
966        } else if bar_type.spec().price_type == PriceType::Last {
967            let unsubscribe = UnsubscribeTrades::new(
968                bar_type.instrument_id(),
969                cmd.client_id,
970                cmd.venue,
971                UUID4::new(),
972                cmd.ts_init,
973                Some(cmd.command_id),
974                cmd.params.clone(),
975            );
976            self.execute(DataCommand::Unsubscribe(UnsubscribeCommand::Trades(
977                unsubscribe,
978            )));
979        } else {
980            let unsubscribe = UnsubscribeQuotes::new(
981                bar_type.instrument_id(),
982                cmd.client_id,
983                cmd.venue,
984                UUID4::new(),
985                cmd.ts_init,
986                Some(cmd.command_id),
987                cmd.params.clone(),
988            );
989            self.execute(DataCommand::Unsubscribe(UnsubscribeCommand::Quotes(
990                unsubscribe,
991            )));
992        }
993    }
994
995    pub(super) fn stop_bar_aggregator(
996        &mut self,
997        bar_type: BarType,
998        request_id: Option<UUID4>,
999    ) -> anyhow::Result<()> {
1000        let key = bar_aggregator_key(bar_type, request_id);
1001
1002        let aggregator = self.bar_aggregators.shift_remove(&key).ok_or_else(|| {
1003            anyhow::anyhow!("Cannot stop bar aggregator: no aggregator to stop for {bar_type}")
1004        })?;
1005
1006        aggregator.borrow_mut().stop();
1007        self.bar_aggregators_pending_stop.remove(&key);
1008        self.unsubscribe_bar_aggregator_handlers(&key);
1009
1010        Ok(())
1011    }
1012
1013    fn unsubscribe_bar_aggregator_handlers(&mut self, key: &BarAggregatorKey) {
1014        self.bar_aggregator_warmups.remove(key);
1015
1016        // Unsubscribe any registered message handlers
1017        if let Some(subs) = self.bar_aggregator_handlers.remove(key) {
1018            for sub in subs {
1019                match sub {
1020                    BarAggregatorSubscription::Bar { topic, handler } => {
1021                        msgbus::unsubscribe_bars(topic.into(), &handler);
1022                    }
1023                    BarAggregatorSubscription::Trade { topic, handler } => {
1024                        msgbus::unsubscribe_trades(topic.into(), &handler);
1025                    }
1026                    BarAggregatorSubscription::Quote { topic, handler } => {
1027                        msgbus::unsubscribe_quotes(topic.into(), &handler);
1028                    }
1029                }
1030            }
1031        }
1032    }
1033}
1034
1035#[derive(Clone, Debug)]
1036pub(super) struct BarAggregationSubscription {
1037    pub(super) command: SubscribeBars,
1038    pub(super) source: Option<SubscribeCommand>,
1039}
1040
1041/// Identifies a bar aggregator instance.
1042///
1043/// Live subscriptions key on `(bar_type.standard(), None)`. Request-scoped
1044/// aggregators carrying a `request_id` key on `(bar_type.standard(), Some(id))`
1045/// so they can run alongside a live aggregator on the same bar type.
1046pub(crate) type BarAggregatorKey = (BarType, Option<UUID4>);
1047
1048#[inline]
1049pub(crate) fn bar_aggregator_key(bar_type: BarType, request_id: Option<UUID4>) -> BarAggregatorKey {
1050    (bar_type.standard(), request_id)
1051}
1052
1053/// Returns `bar_types` ordered so that every composite follows the bar type it aggregates from.
1054///
1055/// Bar types at the same depth of the chain keep their original order.
1056fn sources_before_dependents(bar_types: &[BarType]) -> Vec<BarType> {
1057    // Depth is bounded by the number of bar types so a cyclic chain cannot loop
1058    let depth = |bar_type: &BarType| {
1059        let mut depth = 0;
1060        let mut current = *bar_type;
1061
1062        while current.is_composite() && depth < bar_types.len() {
1063            let Some(source) = bar_types
1064                .iter()
1065                .find(|candidate| candidate.standard() == current.composite().standard())
1066            else {
1067                break;
1068            };
1069            current = *source;
1070            depth += 1;
1071        }
1072
1073        depth
1074    };
1075
1076    let mut ordered = bar_types.to_vec();
1077    ordered.sort_by_cached_key(depth);
1078    ordered
1079}
1080
1081/// Live data held back for a bar aggregator that is still replaying a historical request.
1082#[derive(Debug)]
1083pub(super) enum BarAggregatorWarmup {
1084    Bar(WarmupBuffer<Bar>),
1085    Trade(WarmupBuffer<TradeTick>),
1086    Quote(WarmupBuffer<QuoteTick>),
1087}
1088
1089impl BarAggregatorWarmup {
1090    /// Ends the warm-up and feeds the held data to `aggregator` in arrival order.
1091    pub(super) fn release(&self, aggregator: &mut dyn BarAggregator) {
1092        match self {
1093            Self::Bar(buffer) => buffer
1094                .release()
1095                .into_iter()
1096                .for_each(|bar| aggregator.handle_bar(bar)),
1097            Self::Trade(buffer) => buffer
1098                .release()
1099                .into_iter()
1100                .for_each(|trade| aggregator.handle_trade(trade)),
1101            Self::Quote(buffer) => buffer
1102                .release()
1103                .into_iter()
1104                .for_each(|quote| aggregator.handle_quote(quote)),
1105        }
1106    }
1107}
1108
1109/// Typed subscription for bar aggregator handlers.
1110///
1111/// Stores the topic and handler for each data type so we can properly
1112/// unsubscribe from the typed routers.
1113#[derive(Clone)]
1114pub enum BarAggregatorSubscription {
1115    Bar {
1116        topic: MStr<Topic>,
1117        handler: TypedHandler<Bar>,
1118    },
1119    Trade {
1120        topic: MStr<Topic>,
1121        handler: TypedHandler<TradeTick>,
1122    },
1123    Quote {
1124        topic: MStr<Topic>,
1125        handler: TypedHandler<QuoteTick>,
1126    },
1127}
1128
1129impl Debug for BarAggregatorSubscription {
1130    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1131        match self {
1132            Self::Bar { topic, handler } => f
1133                .debug_struct(stringify!(Bar))
1134                .field("topic", topic)
1135                .field("handler_id", &handler.id())
1136                .finish(),
1137            Self::Trade { topic, handler } => f
1138                .debug_struct(stringify!(Trade))
1139                .field("topic", topic)
1140                .field("handler_id", &handler.id())
1141                .finish(),
1142            Self::Quote { topic, handler } => f
1143                .debug_struct(stringify!(Quote))
1144                .field("topic", topic)
1145                .field("handler_id", &handler.id())
1146                .finish(),
1147        }
1148    }
1149}
1150
1151#[cfg(test)]
1152mod tests {
1153    use std::{cell::RefCell, rc::Rc};
1154
1155    use indexmap::IndexMap;
1156    use jiff::Timestamp;
1157    use nautilus_common::{
1158        cache::Cache,
1159        clock::{Clock, VirtualClock},
1160        messages::data::SubscribeBars,
1161        msgbus::MessageBus,
1162    };
1163    use nautilus_core::{UUID4, UnixNanos, datetime::get_timezone};
1164    use nautilus_model::{
1165        data::{Bar, BarType},
1166        identifiers::TraderId,
1167        instruments::{InstrumentAny, stubs::audusd_sim},
1168        types::{Price, Quantity},
1169    };
1170    use rstest::rstest;
1171
1172    use super::{
1173        bar_aggregator_key, request_bar_aggregation_from_params, sources_before_dependents,
1174        time_zone_from_params,
1175    };
1176    use crate::engine::{DataEngine, config::DataEngineConfig};
1177
1178    #[rstest]
1179    #[case::utc_default(None, None, "2026-11-02T00:00:00Z")]
1180    #[case::config(Some("America/New_York"), None, "2026-11-02T05:00:00Z")]
1181    #[case::utc_override(Some("America/New_York"), Some("UTC"), "2026-11-02T00:00:00Z")]
1182    fn test_calendar_zone_precedence_and_reuse(
1183        #[case] config_zone: Option<&str>,
1184        #[case] override_zone: Option<&str>,
1185        #[case] expected: &str,
1186    ) {
1187        let _msgbus = MessageBus::new(TraderId::new("TESTER-001"), UUID4::new(), None, None)
1188            .register_message_bus();
1189        let clock = Rc::new(RefCell::new(VirtualClock::new()));
1190        clock.borrow_mut().set_time(UnixNanos::from(
1191            "2026-11-01T12:00:00Z".parse::<Timestamp>().unwrap(),
1192        ));
1193        let cache = Rc::new(RefCell::new(Cache::default()));
1194        cache
1195            .borrow_mut()
1196            .add_instrument(InstrumentAny::CurrencyPair(audusd_sim()))
1197            .unwrap();
1198        let config = DataEngineConfig::builder()
1199            .maybe_time_bars_time_zone(config_zone.map(str::to_owned))
1200            .time_bars_skip_first_non_full_bar(true)
1201            .build();
1202        let mut engine = DataEngine::new(
1203            Rc::clone(&clock) as Rc<RefCell<dyn Clock>>,
1204            Rc::clone(&cache),
1205            Some(config),
1206        );
1207        let bar_type = BarType::from("AUD/USD.SIM-1-DAY-LAST-INTERNAL");
1208        let mut params = IndexMap::from([(
1209            "skip_first_non_full_bar".to_owned(),
1210            serde_json::json!(false),
1211        )]);
1212
1213        if let Some(zone) = override_zone {
1214            params.insert("time_zone".to_owned(), serde_json::json!(zone));
1215        }
1216
1217        let cmd = SubscribeBars::new(
1218            bar_type,
1219            None,
1220            Some(bar_type.instrument_id().venue),
1221            UUID4::new(),
1222            UnixNanos::default(),
1223            None,
1224            Some(nautilus_core::Params::new(params)),
1225        );
1226        engine.subscribe_bars(&cmd).unwrap();
1227        engine.subscribe_bars(&cmd).unwrap();
1228        let mut params = IndexMap::clone(cmd.params.as_ref().unwrap());
1229        params.insert("time_zone".to_owned(), serde_json::json!("Asia/Tokyo"));
1230
1231        let cmd = SubscribeBars {
1232            params: Some(nautilus_core::Params::new(params)),
1233            ..cmd
1234        };
1235
1236        let e = engine.subscribe_bars(&cmd).unwrap_err();
1237
1238        // Request-local instances can use another zone without replacing the live instance
1239        engine
1240            .create_bar_aggregator_for_key(
1241                bar_type,
1242                Some(UUID4::new()),
1243                None,
1244                Some(get_timezone("Asia/Tokyo").unwrap()),
1245            )
1246            .unwrap();
1247        assert_eq!(
1248            e.to_string(),
1249            format!("Cannot reuse bar aggregator {bar_type} with a different time zone")
1250        );
1251        assert_eq!(
1252            clock.borrow().next_time_ns(&format!("TIME_BAR_{bar_type}")),
1253            Some(UnixNanos::from(expected.parse::<Timestamp>().unwrap()))
1254        );
1255        let now = clock.borrow().timestamp_ns();
1256        engine
1257            .bar_aggregators
1258            .get(&bar_aggregator_key(bar_type, None))
1259            .unwrap()
1260            .borrow_mut()
1261            .update(Price::from("0.65000"), Quantity::from(2), now);
1262        let close = UnixNanos::from(expected.parse::<Timestamp>().unwrap());
1263        let events = clock.borrow_mut().advance_time(close, true);
1264        let handlers = clock.borrow().match_handlers(events);
1265        for handler in handlers {
1266            handler.callback.call(handler.event);
1267        }
1268
1269        assert_eq!(
1270            cache.borrow().bar(&bar_type).copied(),
1271            Some(Bar::new(
1272                bar_type,
1273                Price::from("0.65000"),
1274                Price::from("0.65000"),
1275                Price::from("0.65000"),
1276                Price::from("0.65000"),
1277                Quantity::from(2),
1278                close,
1279                close
1280            ))
1281        );
1282    }
1283
1284    #[rstest]
1285    #[case::wrong_type(serde_json::json!(17), "`time_zone` parameter must be a string")]
1286    #[case::unknown_zone(serde_json::json!("Not/A_Zone"), "invalid `time_zone` parameter \"Not/A_Zone\"")]
1287    fn test_calendar_zone_parameter_rejects_invalid_value(
1288        #[case] value: serde_json::Value,
1289        #[case] expected: &str,
1290    ) {
1291        let params = nautilus_core::Params::new([("time_zone".to_owned(), value)].into());
1292        assert_eq!(
1293            time_zone_from_params(Some(&params))
1294                .unwrap_err()
1295                .to_string(),
1296            expected
1297        );
1298    }
1299
1300    #[rstest]
1301    fn test_calendar_request_zone_checks_shared_instance() {
1302        let clock = Rc::new(RefCell::new(VirtualClock::new()));
1303        let cache = Rc::new(RefCell::new(Cache::default()));
1304        cache
1305            .borrow_mut()
1306            .add_instrument(InstrumentAny::CurrencyPair(audusd_sim()))
1307            .unwrap();
1308        let mut engine = DataEngine::new(clock, cache, None);
1309        let bar_type = BarType::from("AUD/USD.SIM-1-MONTH-LAST-INTERNAL");
1310        engine
1311            .create_bar_aggregator_for_key(bar_type, None, None, None)
1312            .unwrap();
1313        let params: nautilus_core::Params = serde_json::from_value(serde_json::json!({
1314            "bar_types": [bar_type.to_string()], "update_subscriptions": true,
1315            "time_zone": "America/New_York"
1316        }))
1317        .unwrap();
1318        let state = request_bar_aggregation_from_params(Some(&params))
1319            .unwrap()
1320            .unwrap();
1321        let id = UUID4::new();
1322        let error = engine
1323            .prepare_request_bar_aggregators_from_state(id, &state)
1324            .unwrap_err();
1325        let mut params = IndexMap::clone(&params);
1326        params.insert("update_subscriptions".to_owned(), serde_json::json!(false));
1327        let params = nautilus_core::Params::new(params);
1328        let state = request_bar_aggregation_from_params(Some(&params))
1329            .unwrap()
1330            .unwrap();
1331        engine
1332            .prepare_request_bar_aggregators_from_state(id, &state)
1333            .unwrap();
1334        let instance = engine
1335            .bar_aggregators
1336            .get(&bar_aggregator_key(bar_type, Some(id)))
1337            .unwrap()
1338            .borrow();
1339        let request_zone = instance
1340            .as_any()
1341            .downcast_ref::<crate::aggregation::TimeBarAggregator>()
1342            .unwrap()
1343            .time_zone()
1344            .clone();
1345        let live = engine
1346            .bar_aggregators
1347            .get(&bar_aggregator_key(bar_type, None))
1348            .unwrap()
1349            .borrow();
1350        let live_zone = live
1351            .as_any()
1352            .downcast_ref::<crate::aggregation::TimeBarAggregator>()
1353            .unwrap()
1354            .time_zone()
1355            .clone();
1356        assert_eq!(
1357            error.to_string(),
1358            format!("Cannot reuse bar aggregator {bar_type} with a different time zone")
1359        );
1360        assert_eq!(request_zone, get_timezone("America/New_York").unwrap());
1361        assert_eq!(live_zone, jiff::tz::TimeZone::UTC);
1362        assert_eq!(engine.bar_aggregators.len(), 2);
1363    }
1364
1365    #[rstest]
1366    #[case::source_first(&["1-MINUTE-MID-INTERNAL", "5-MINUTE-MID-INTERNAL@1-MINUTE-INTERNAL"])]
1367    #[case::composite_first(&["5-MINUTE-MID-INTERNAL@1-MINUTE-INTERNAL", "1-MINUTE-MID-INTERNAL"])]
1368    fn test_sources_before_dependents_orders_two_level_chain(#[case] raw: &[&str]) {
1369        let bar_types = to_bar_types(raw);
1370
1371        let ordered = sources_before_dependents(&bar_types);
1372
1373        assert_eq!(
1374            ordered,
1375            to_bar_types(&[
1376                "1-MINUTE-MID-INTERNAL",
1377                "5-MINUTE-MID-INTERNAL@1-MINUTE-INTERNAL",
1378            ])
1379        );
1380    }
1381
1382    #[rstest]
1383    fn test_sources_before_dependents_orders_three_level_chain_and_keeps_unrelated_order() {
1384        let bar_types = to_bar_types(&[
1385            "15-MINUTE-MID-INTERNAL@5-MINUTE-INTERNAL",
1386            "100-TICK-LAST-INTERNAL",
1387            "5-MINUTE-MID-INTERNAL@1-MINUTE-INTERNAL",
1388            "1-MINUTE-MID-INTERNAL",
1389            "50-TICK-LAST-INTERNAL",
1390        ]);
1391
1392        let ordered = sources_before_dependents(&bar_types);
1393
1394        assert_eq!(
1395            ordered,
1396            to_bar_types(&[
1397                "100-TICK-LAST-INTERNAL",
1398                "1-MINUTE-MID-INTERNAL",
1399                "50-TICK-LAST-INTERNAL",
1400                "5-MINUTE-MID-INTERNAL@1-MINUTE-INTERNAL",
1401                "15-MINUTE-MID-INTERNAL@5-MINUTE-INTERNAL",
1402            ])
1403        );
1404    }
1405
1406    fn to_bar_types(raw: &[&str]) -> Vec<BarType> {
1407        raw.iter()
1408            .map(|bar_type| BarType::from(format!("AUD/USD.SIM-{bar_type}").as_str()))
1409            .collect()
1410    }
1411}