nautilus_trading/macros.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//! Convenience macros for implementing trading component boilerplate.
17
18/// Implements `DataActorNative`, `StrategyNative`, and `Strategy` for a strategy type.
19///
20/// The struct must contain a field of type [`StrategyCore`](crate::strategy::StrategyCore).
21/// By default the macro expects the field to be named `core`; pass a second argument
22/// to use a different name.
23/// The macro also adds an inherent `config()` method on the strategy type which
24/// returns the user-supplied [`StrategyConfig`](crate::strategy::StrategyConfig).
25///
26/// An optional brace-delimited block adds extra methods to the generated `impl Strategy`.
27/// Native runtime core access is generated through `StrategyNative`; normal strategy
28/// logic should use `Strategy` facade methods.
29///
30/// # Examples
31///
32/// ```ignore
33/// use nautilus_trading::{nautilus_strategy, strategy::StrategyCore};
34///
35/// pub struct MyStrategy {
36/// core: StrategyCore,
37/// // ...
38/// }
39///
40/// // Simple form
41/// nautilus_strategy!(MyStrategy);
42/// ```
43///
44/// With Strategy hook overrides:
45///
46/// ```ignore
47/// nautilus_strategy!(MyStrategy, {
48/// fn on_order_rejected(&mut self, event: OrderRejected) {
49/// // custom handling
50/// }
51/// });
52/// ```
53///
54/// With a custom field name and hooks:
55///
56/// ```ignore
57/// pub struct MyStrategy {
58/// strat_core: StrategyCore,
59/// // ...
60/// }
61///
62/// nautilus_strategy!(MyStrategy, strat_core, {
63/// fn external_order_claims(&self) -> Option<Vec<InstrumentId>> {
64/// None
65/// }
66/// });
67/// ```
68#[macro_export]
69macro_rules! nautilus_strategy {
70 ($ty:ty) => {
71 $crate::nautilus_strategy!($ty, core, {});
72 };
73 ($ty:ty, $field:ident) => {
74 $crate::nautilus_strategy!($ty, $field, {});
75 };
76 ($ty:ty, { $($extra:item)* }) => {
77 $crate::nautilus_strategy!($ty, core, { $($extra)* });
78 };
79 ($ty:ty, $field:ident, { $($extra:item)* }) => {
80 impl $ty {
81 /// Returns the strategy configuration.
82 #[allow(dead_code, unreachable_pub)]
83 #[must_use]
84 pub fn config(&self) -> &$crate::strategy::StrategyConfig {
85 self.$field.config()
86 }
87 }
88
89 impl $crate::_macro_reexports::DataActorNative for $ty {
90 fn core(&self) -> &$crate::_macro_reexports::DataActorCore {
91 $crate::_macro_reexports::DataActorNative::core(&self.$field)
92 }
93
94 fn core_mut(&mut self) -> &mut $crate::_macro_reexports::DataActorCore {
95 $crate::_macro_reexports::DataActorNative::core_mut(&mut self.$field)
96 }
97 }
98
99 impl $crate::strategy::StrategyNative for $ty {
100 fn strategy_core(&self) -> &$crate::strategy::StrategyCore {
101 &self.$field
102 }
103
104 fn strategy_core_mut(&mut self) -> &mut $crate::strategy::StrategyCore {
105 &mut self.$field
106 }
107 }
108
109 impl $crate::strategy::Strategy for $ty {
110 $($extra)*
111 }
112 };
113}
114
115/// Implements `DataActorNative`, `ExecutionAlgorithmNative`, and `ExecutionAlgorithm` for an
116/// execution algorithm type.
117///
118/// The struct must contain a field of type
119/// [`ExecutionAlgorithmCore`](crate::algorithm::ExecutionAlgorithmCore). By default the macro
120/// expects the field to be named `core`; pass a second argument to use a different name.
121///
122/// A brace-delimited block adds `on_order` and any extra methods to the generated
123/// `impl ExecutionAlgorithm`. Native runtime core access is generated through
124/// `ExecutionAlgorithmNative`; normal execution algorithm logic should use
125/// `ExecutionAlgorithm` facade methods.
126///
127/// # Examples
128///
129/// ```ignore
130/// use nautilus_trading::{algorithm::ExecutionAlgorithmCore, nautilus_execution_algorithm};
131///
132/// pub struct MyExecutionAlgorithm {
133/// core: ExecutionAlgorithmCore,
134/// // ...
135/// }
136///
137/// nautilus_execution_algorithm!(MyExecutionAlgorithm, {
138/// fn on_order(&mut self, order: OrderAny) -> anyhow::Result<()> {
139/// // custom handling
140/// Ok(())
141/// }
142/// });
143/// ```
144///
145/// With a custom field name and hooks:
146///
147/// ```ignore
148/// pub struct MyExecutionAlgorithm {
149/// algorithm_core: ExecutionAlgorithmCore,
150/// // ...
151/// }
152///
153/// nautilus_execution_algorithm!(MyExecutionAlgorithm, algorithm_core, {
154/// fn on_order(&mut self, order: OrderAny) -> anyhow::Result<()> {
155/// // custom handling
156/// Ok(())
157/// }
158/// });
159/// ```
160#[macro_export]
161macro_rules! nautilus_execution_algorithm {
162 ($ty:ty) => {
163 compile_error!(
164 "nautilus_execution_algorithm! requires an `on_order` implementation block"
165 );
166 };
167 ($ty:ty, $field:ident) => {
168 compile_error!(
169 "nautilus_execution_algorithm! requires an `on_order` implementation block"
170 );
171 };
172 ($ty:ty, { $($extra:item)* }) => {
173 $crate::nautilus_execution_algorithm!($ty, core, { $($extra)* });
174 };
175 ($ty:ty, $field:ident, { $($extra:item)* }) => {
176 impl $crate::_macro_reexports::DataActorNative for $ty {
177 fn core(&self) -> &$crate::_macro_reexports::DataActorCore {
178 $crate::_macro_reexports::DataActorNative::core(&self.$field)
179 }
180
181 fn core_mut(&mut self) -> &mut $crate::_macro_reexports::DataActorCore {
182 $crate::_macro_reexports::DataActorNative::core_mut(&mut self.$field)
183 }
184 }
185
186 impl $crate::algorithm::ExecutionAlgorithmNative for $ty {
187 fn exec_algorithm_core(&self) -> &$crate::algorithm::ExecutionAlgorithmCore {
188 &self.$field
189 }
190
191 fn exec_algorithm_core_mut(
192 &mut self,
193 ) -> &mut $crate::algorithm::ExecutionAlgorithmCore {
194 &mut self.$field
195 }
196 }
197
198 impl $crate::algorithm::ExecutionAlgorithm for $ty {
199 $($extra)*
200 }
201 };
202}