Skip to main content

nautilus_common/
signal.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 user signal type.
17
18use std::{any::Any, sync::Arc};
19
20use nautilus_core::UnixNanos;
21use nautilus_model::data::{HasTsInit, custom::CustomDataTrait};
22use serde::{Deserialize, Serialize};
23use ustr::Ustr;
24
25/// Represents a generic signal.
26#[repr(C)]
27#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
28#[cfg_attr(
29    feature = "python",
30    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.common", from_py_object)
31)]
32#[cfg_attr(
33    feature = "python",
34    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.common")
35)]
36pub struct Signal {
37    pub name: Ustr,
38    pub value: String,
39    pub ts_event: UnixNanos,
40    pub ts_init: UnixNanos,
41}
42
43impl Signal {
44    /// Creates a new [`Signal`] instance.
45    #[must_use]
46    pub const fn new(name: Ustr, value: String, ts_event: UnixNanos, ts_init: UnixNanos) -> Self {
47        Self {
48            name,
49            value,
50            ts_event,
51            ts_init,
52        }
53    }
54}
55
56impl HasTsInit for Signal {
57    fn ts_init(&self) -> UnixNanos {
58        self.ts_init
59    }
60}
61
62impl CustomDataTrait for Signal {
63    fn type_name(&self) -> &'static str {
64        "Signal"
65    }
66
67    fn as_any(&self) -> &dyn Any {
68        self
69    }
70
71    fn ts_event(&self) -> UnixNanos {
72        self.ts_event
73    }
74
75    fn to_json(&self) -> anyhow::Result<String> {
76        Ok(serde_json::to_string(self)?)
77    }
78
79    fn clone_arc(&self) -> Arc<dyn CustomDataTrait> {
80        Arc::new(self.clone())
81    }
82
83    fn eq_arc(&self, other: &dyn CustomDataTrait) -> bool {
84        other
85            .as_any()
86            .downcast_ref::<Self>()
87            .is_some_and(|o| self == o)
88    }
89
90    #[cfg(feature = "python")]
91    fn to_pyobject(&self, py: pyo3::Python<'_>) -> pyo3::PyResult<pyo3::Py<pyo3::PyAny>> {
92        use pyo3::IntoPyObjectExt;
93        self.clone().into_py_any(py)
94    }
95}