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 strategy boilerplate.
17
18/// Implements `Deref<Target = DataActorCore>`, `DerefMut`, 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///
24/// An optional brace-delimited block adds extra methods to the generated `impl Strategy`.
25/// Do not redefine `core` or `core_mut` inside the block; they are already generated
26/// by the macro and duplicates will cause a compile error.
27///
28/// # Examples
29///
30/// ```ignore
31/// use nautilus_trading::{nautilus_strategy, strategy::StrategyCore};
32///
33/// pub struct MyStrategy {
34/// core: StrategyCore,
35/// // ...
36/// }
37///
38/// // Simple form
39/// nautilus_strategy!(MyStrategy);
40/// ```
41///
42/// With Strategy hook overrides:
43///
44/// ```ignore
45/// nautilus_strategy!(MyStrategy, {
46/// fn on_order_rejected(&mut self, event: OrderRejected) {
47/// // custom handling
48/// }
49/// });
50/// ```
51///
52/// With a custom field name and hooks:
53///
54/// ```ignore
55/// pub struct MyStrategy {
56/// strat_core: StrategyCore,
57/// // ...
58/// }
59///
60/// nautilus_strategy!(MyStrategy, strat_core, {
61/// fn external_order_claims(&self) -> Option<Vec<InstrumentId>> {
62/// None
63/// }
64/// });
65/// ```
66#[macro_export]
67macro_rules! nautilus_strategy {
68 ($ty:ty) => {
69 $crate::nautilus_strategy!($ty, core, {});
70 };
71 ($ty:ty, $field:ident) => {
72 $crate::nautilus_strategy!($ty, $field, {});
73 };
74 ($ty:ty, { $($extra:item)* }) => {
75 $crate::nautilus_strategy!($ty, core, { $($extra)* });
76 };
77 ($ty:ty, $field:ident, { $($extra:item)* }) => {
78 impl ::std::ops::Deref for $ty {
79 type Target = $crate::_macro_reexports::DataActorCore;
80
81 fn deref(&self) -> &Self::Target {
82 &self.$field
83 }
84 }
85
86 impl ::std::ops::DerefMut for $ty {
87 fn deref_mut(&mut self) -> &mut Self::Target {
88 &mut self.$field
89 }
90 }
91
92 impl $crate::strategy::Strategy for $ty {
93 fn core(&self) -> &$crate::strategy::StrategyCore {
94 &self.$field
95 }
96
97 fn core_mut(&mut self) -> &mut $crate::strategy::StrategyCore {
98 &mut self.$field
99 }
100
101 $($extra)*
102 }
103 };
104}