nautilus_indicators/
indicator.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
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    fn handle_quote(&mut self, quote: &QuoteTick) {
52        panic!("`handle_quote_tick` {IMPL_ERR} `{}`", self.name());
53    }
54
55    fn handle_trade(&mut self, trade: &TradeTick) {
56        panic!("`handle_trade_tick` {IMPL_ERR} `{}`", self.name());
57    }
58
59    fn handle_bar(&mut self, bar: &Bar) {
60        panic!("`handle_bar` {IMPL_ERR} `{}`", self.name());
61    }
62
63    fn reset(&mut self);
64}
65
66pub trait MovingAverage: Indicator {
67    fn value(&self) -> f64;
68    fn count(&self) -> usize;
69    fn update_raw(&mut self, value: f64);
70}
71
72impl Debug for dyn Indicator + Send {
73    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
74        // Implement custom formatting for the Indicator trait object
75        write!(f, "Indicator {{ ... }}")
76    }
77}
78
79impl Debug for dyn MovingAverage + Send {
80    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
81        // Implement custom formatting for the Indicator trait object
82        write!(f, "MovingAverage()")
83    }
84}