Skip to main content

nautilus_indicators/
indicator.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
16//! A common `Indicator` trait.
17
18use std::fmt::Debug;
19
20use nautilus_model::{
21    data::{Bar, OrderBookDelta, OrderBookDeltas, OrderBookDepth10, QuoteTick, TradeTick},
22    orderbook::OrderBook,
23};
24
25const IMPL_ERR: &str = "is not implemented for";
26
27#[allow(unused_variables)]
28pub trait Indicator {
29    fn name(&self) -> String;
30
31    fn has_inputs(&self) -> bool;
32
33    fn initialized(&self) -> bool;
34
35    fn handle_delta(&mut self, delta: &OrderBookDelta) {
36        panic!("`handle_delta` {IMPL_ERR} `{}`", self.name());
37    }
38
39    fn handle_deltas(&mut self, deltas: &OrderBookDeltas) {
40        panic!("`handle_deltas` {IMPL_ERR} `{}`", self.name());
41    }
42
43    fn handle_depth(&mut self, depth: &OrderBookDepth10) {
44        panic!("`handle_depth` {IMPL_ERR} `{}`", self.name());
45    }
46
47    fn handle_book(&mut self, book: &OrderBook) {
48        panic!("`handle_book_mbo` {IMPL_ERR} `{}`", self.name());
49    }
50
51    /// Updates the indicator with the given quote tick.
52    ///
53    /// # Errors
54    ///
55    /// Returns an error if the configured price type cannot be extracted from the quote.
56    fn handle_quote(&mut self, quote: &QuoteTick) -> anyhow::Result<()> {
57        anyhow::bail!("`handle_quote_tick` {IMPL_ERR} `{}`", self.name());
58    }
59
60    fn handle_trade(&mut self, trade: &TradeTick) {
61        panic!("`handle_trade_tick` {IMPL_ERR} `{}`", self.name());
62    }
63
64    fn handle_bar(&mut self, bar: &Bar) {
65        panic!("`handle_bar` {IMPL_ERR} `{}`", self.name());
66    }
67
68    fn reset(&mut self);
69}
70
71pub trait MovingAverage: Indicator {
72    fn value(&self) -> f64;
73    fn count(&self) -> usize;
74    fn update_raw(&mut self, value: f64);
75}
76
77impl Debug for dyn Indicator + Send {
78    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
79        // Implement custom formatting for the Indicator trait object
80        write!(f, "Indicator {{ ... }}")
81    }
82}
83
84impl Debug for dyn MovingAverage + Send {
85    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
86        // Implement custom formatting for the Indicator trait object
87        write!(f, "MovingAverage()")
88    }
89}