nautilus_data/engine/
handlers.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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::{any::Any, cell::RefCell, rc::Rc};
17
18use nautilus_common::msgbus::handler::MessageHandler;
19use nautilus_model::data::{Bar, BarType, QuoteTick, TradeTick};
20use ustr::Ustr;
21
22use crate::aggregation::BarAggregator;
23
24/// Message handler for processing quote ticks through bar aggregators.
25///
26/// This handler receives quote tick messages and forwards them to the underlying
27/// bar aggregator for processing. It's used as part of the data engine's message
28/// routing infrastructure to build bars from incoming quote data.
29#[derive(Debug)]
30pub struct BarQuoteHandler {
31    aggregator: Rc<RefCell<Box<dyn BarAggregator>>>,
32    bar_type: BarType,
33}
34
35impl BarQuoteHandler {
36    pub(crate) fn new(aggregator: Rc<RefCell<Box<dyn BarAggregator>>>, bar_type: BarType) -> Self {
37        Self {
38            aggregator,
39            bar_type,
40        }
41    }
42}
43
44impl MessageHandler for BarQuoteHandler {
45    fn id(&self) -> Ustr {
46        Ustr::from(&format!("BarQuoteHandler|{}", self.bar_type))
47    }
48
49    fn handle(&self, msg: &dyn Any) {
50        if let Some(quote) = msg.downcast_ref::<QuoteTick>() {
51            self.aggregator.borrow_mut().handle_quote(*quote);
52        }
53    }
54
55    fn as_any(&self) -> &dyn Any {
56        self
57    }
58}
59
60/// Message handler for processing trade ticks through bar aggregators.
61///
62/// This handler receives trade tick messages and forwards them to the underlying
63/// bar aggregator for processing. It's used as part of the data engine's message
64/// routing infrastructure to build bars from incoming trade data.
65#[derive(Debug)]
66pub struct BarTradeHandler {
67    aggregator: Rc<RefCell<Box<dyn BarAggregator>>>,
68    bar_type: BarType,
69}
70
71impl BarTradeHandler {
72    pub(crate) fn new(aggregator: Rc<RefCell<Box<dyn BarAggregator>>>, bar_type: BarType) -> Self {
73        Self {
74            aggregator,
75            bar_type,
76        }
77    }
78}
79
80impl MessageHandler for BarTradeHandler {
81    fn id(&self) -> Ustr {
82        Ustr::from(&format!("BarTradeHandler|{}", self.bar_type))
83    }
84
85    fn handle(&self, msg: &dyn Any) {
86        if let Some(trade) = msg.downcast_ref::<TradeTick>() {
87            self.aggregator.borrow_mut().handle_trade(*trade);
88        }
89    }
90
91    fn as_any(&self) -> &dyn Any {
92        self
93    }
94}
95
96/// Message handler for processing bars through composite bar aggregators.
97///
98/// This handler receives bar messages and forwards them to the underlying
99/// bar aggregator for further processing. It's used for building composite
100/// bars from existing bars, such as creating higher timeframe bars from
101/// lower timeframe bars.
102#[derive(Debug)]
103pub struct BarBarHandler {
104    aggregator: Rc<RefCell<Box<dyn BarAggregator>>>,
105    bar_type: BarType,
106}
107
108impl BarBarHandler {
109    pub(crate) fn new(aggregator: Rc<RefCell<Box<dyn BarAggregator>>>, bar_type: BarType) -> Self {
110        Self {
111            aggregator,
112            bar_type,
113        }
114    }
115}
116
117impl MessageHandler for BarBarHandler {
118    fn id(&self) -> Ustr {
119        Ustr::from(&format!("BarBarHandler|{}", self.bar_type))
120    }
121
122    fn handle(&self, msg: &dyn Any) {
123        if let Some(bar) = msg.downcast_ref::<Bar>() {
124            self.aggregator.borrow_mut().handle_bar(*bar);
125        }
126    }
127
128    fn as_any(&self) -> &dyn Any {
129        self
130    }
131}