Skip to main content

nautilus_data/option_chains/
handlers.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//! Typed handlers for routing market data events to the option chain manager.
17
18use std::{cell::RefCell, rc::Rc};
19
20use nautilus_common::{msgbus::Handler, timer::TimeEvent};
21use nautilus_core::WeakCell;
22use nautilus_model::{
23    data::{QuoteTick, option_chain::OptionGreeks},
24    identifiers::OptionSeriesId,
25};
26use ustr::Ustr;
27
28use super::OptionChainManager;
29
30/// Routes incoming quote ticks to the `OptionChainManager` for aggregation.
31///
32/// Follows the same `WeakCell` pattern as `BarQuoteHandler`.
33#[derive(Debug)]
34pub struct OptionChainQuoteHandler {
35    manager: WeakCell<OptionChainManager>,
36    id: Ustr,
37}
38
39impl OptionChainQuoteHandler {
40    pub fn new(manager: &Rc<RefCell<OptionChainManager>>, series_id: OptionSeriesId) -> Self {
41        let id = Ustr::from(&format!("OptionChainQuoteHandler({series_id})"));
42        Self {
43            manager: WeakCell::from(Rc::downgrade(manager)),
44            id,
45        }
46    }
47}
48
49impl Handler<QuoteTick> for OptionChainQuoteHandler {
50    fn id(&self) -> Ustr {
51        self.id
52    }
53
54    fn handle(&self, quote: &QuoteTick) {
55        if let Some(mgr) = self.manager.upgrade() {
56            mgr.borrow_mut().handle_quote(quote);
57        }
58    }
59}
60
61/// Routes incoming option greeks to the `OptionChainManager` for aggregation.
62#[derive(Debug)]
63pub struct OptionChainGreeksHandler {
64    manager: WeakCell<OptionChainManager>,
65    id: Ustr,
66}
67
68impl OptionChainGreeksHandler {
69    pub fn new(manager: &Rc<RefCell<OptionChainManager>>, series_id: OptionSeriesId) -> Self {
70        let id = Ustr::from(&format!("OptionChainGreeksHandler({series_id})"));
71        Self {
72            manager: WeakCell::from(Rc::downgrade(manager)),
73            id,
74        }
75    }
76}
77
78impl Handler<OptionGreeks> for OptionChainGreeksHandler {
79    fn id(&self) -> Ustr {
80        self.id
81    }
82
83    fn handle(&self, greeks: &OptionGreeks) {
84        if let Some(mgr) = self.manager.upgrade() {
85            mgr.borrow_mut().handle_greeks(greeks);
86        }
87    }
88}
89
90/// Timer callback that triggers snapshot publishing for a per-series manager.
91///
92/// Follows the same closure-based timer pattern as `BookSnapshotter`.
93#[derive(Debug)]
94pub struct OptionChainSlicePublisher {
95    manager: WeakCell<OptionChainManager>,
96}
97
98impl OptionChainSlicePublisher {
99    pub fn new(manager: &Rc<RefCell<OptionChainManager>>) -> Self {
100        Self {
101            manager: WeakCell::from(Rc::downgrade(manager)),
102        }
103    }
104
105    /// Called by the timer -- takes the accumulated snapshot and publishes it.
106    pub fn publish(&self, event: &TimeEvent) {
107        if let Some(mgr) = self.manager.upgrade() {
108            mgr.borrow_mut().publish_slice(event.ts_event);
109        }
110    }
111}