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_instrument_ids(&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 fn dispatch_start(&mut self) -> $crate::_macro_reexports::anyhow::Result<()> {
99 $crate::strategy::Strategy::on_start(self)?;
100 $crate::_macro_reexports::DataActor::on_start(self)
101 }
102 }
103
104 impl $crate::strategy::StrategyNative for $ty {
105 fn strategy_core(&self) -> &$crate::strategy::StrategyCore {
106 &self.$field
107 }
108
109 fn strategy_core_mut(&mut self) -> &mut $crate::strategy::StrategyCore {
110 &mut self.$field
111 }
112 }
113
114 impl $crate::strategy::Strategy for $ty {
115 $($extra)*
116 }
117 };
118}
119
120/// Implements `DataActorNative`, `ExecutionAlgorithmNative`, and `ExecutionAlgorithm` for an
121/// execution algorithm type.
122///
123/// The struct must contain a field of type
124/// [`ExecutionAlgorithmCore`](crate::algorithm::ExecutionAlgorithmCore). By default the macro
125/// expects the field to be named `core`; pass a second argument to use a different name.
126///
127/// A brace-delimited block adds `on_order` and any extra methods to the generated
128/// `impl ExecutionAlgorithm`. Native runtime core access is generated through
129/// `ExecutionAlgorithmNative`; normal execution algorithm logic should use
130/// `ExecutionAlgorithm` facade methods.
131///
132/// # Examples
133///
134/// ```ignore
135/// use nautilus_trading::{algorithm::ExecutionAlgorithmCore, nautilus_execution_algorithm};
136///
137/// pub struct MyExecutionAlgorithm {
138/// core: ExecutionAlgorithmCore,
139/// // ...
140/// }
141///
142/// nautilus_execution_algorithm!(MyExecutionAlgorithm, {
143/// fn on_order(&mut self, order: OrderAny) -> anyhow::Result<()> {
144/// // custom handling
145/// Ok(())
146/// }
147/// });
148/// ```
149///
150/// With a custom field name and hooks:
151///
152/// ```ignore
153/// pub struct MyExecutionAlgorithm {
154/// algorithm_core: ExecutionAlgorithmCore,
155/// // ...
156/// }
157///
158/// nautilus_execution_algorithm!(MyExecutionAlgorithm, algorithm_core, {
159/// fn on_order(&mut self, order: OrderAny) -> anyhow::Result<()> {
160/// // custom handling
161/// Ok(())
162/// }
163/// });
164/// ```
165#[macro_export]
166macro_rules! nautilus_execution_algorithm {
167 ($ty:ty) => {
168 compile_error!(
169 "nautilus_execution_algorithm! requires an `on_order` implementation block"
170 );
171 };
172 ($ty:ty, $field:ident) => {
173 compile_error!(
174 "nautilus_execution_algorithm! requires an `on_order` implementation block"
175 );
176 };
177 ($ty:ty, { $($extra:item)* }) => {
178 $crate::nautilus_execution_algorithm!($ty, core, { $($extra)* });
179 };
180 ($ty:ty, $field:ident, { $($extra:item)* }) => {
181 impl $crate::_macro_reexports::DataActorNative for $ty {
182 fn core(&self) -> &$crate::_macro_reexports::DataActorCore {
183 $crate::_macro_reexports::DataActorNative::core(&self.$field)
184 }
185
186 fn core_mut(&mut self) -> &mut $crate::_macro_reexports::DataActorCore {
187 $crate::_macro_reexports::DataActorNative::core_mut(&mut self.$field)
188 }
189 }
190
191 impl $crate::algorithm::ExecutionAlgorithmNative for $ty {
192 fn exec_algorithm_core(&self) -> &$crate::algorithm::ExecutionAlgorithmCore {
193 &self.$field
194 }
195
196 fn exec_algorithm_core_mut(
197 &mut self,
198 ) -> &mut $crate::algorithm::ExecutionAlgorithmCore {
199 &mut self.$field
200 }
201 }
202
203 impl $crate::algorithm::ExecutionAlgorithm for $ty {
204 $($extra)*
205 }
206 };
207}