Skip to content

Commit 0cef3e4

Browse files
committed
Add Strategy config facade
- Add `StrategyCore` and macro-generated `config()` accessors - Return `StrategyConfig` from macro-wired strategy calls - Classify `StrategyCore` config as plug-in host-owned - Document strategy config access in the Rust how-to
1 parent 7d5fdb1 commit 0cef3e4

5 files changed

Lines changed: 24 additions & 1 deletion

File tree

crates/plugin/tests/surface_alignment.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ const PLUGIN_STRATEGY_CORE_BRIDGED_METHODS: &[(&str, &str)] = &[("strategy_id",
192192
const PLUGIN_STRATEGY_CORE_DEFERRED_METHODS: &[&str] = &[];
193193

194194
const PLUGIN_STRATEGY_CORE_HOST_OWNED_METHODS: &[&str] = &[
195-
// Construction and registration mutate host runtime state
195+
// Construction, config reads, and registration stay host-owned
196196
"new",
197+
"config",
197198
"change_id",
198199
"change_order_id_tag",
199200
"order_id_tag",

crates/trading/src/macros.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
/// The struct must contain a field of type [`StrategyCore`](crate::strategy::StrategyCore).
2121
/// By default the macro expects the field to be named `core`; pass a second argument
2222
/// 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).
2325
///
2426
/// An optional brace-delimited block adds extra methods to the generated `impl Strategy`.
2527
/// Do not redefine `core` or `core_mut` inside the block; they are already generated
@@ -75,6 +77,15 @@ macro_rules! nautilus_strategy {
7577
$crate::nautilus_strategy!($ty, core, { $($extra)* });
7678
};
7779
($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+
7889
impl $crate::_macro_reexports::DataActorNative for $ty {
7990
fn core(&self) -> &$crate::_macro_reexports::DataActorCore {
8091
$crate::_macro_reexports::DataActorNative::core(&self.$field)

crates/trading/src/strategy/core.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ impl StrategyCore {
178178
}
179179
}
180180

181+
/// Returns the strategy configuration.
182+
#[must_use]
183+
pub fn config(&self) -> &StrategyConfig {
184+
&self.config
185+
}
186+
181187
/// Changes the strategy ID before registration.
182188
pub fn change_id(&mut self, strategy_id: StrategyId) {
183189
let strategy_id = strategy_id_with_order_id_tag(strategy_id, self.order_id_tag());

crates/trading/src/strategy/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4609,18 +4609,21 @@ mod tests {
46094609
core: StrategyCore::new(config.clone()),
46104610
};
46114611
assert_eq!(simple.strategy_id(), config.strategy_id);
4612+
assert_eq!(simple.config().order_id_tag, config.order_id_tag);
46124613
assert_eq!(simple.actor_id(), ActorId::from("MACRO-001"));
46134614

46144615
let hooks = MacroTestWithHooks {
46154616
core: StrategyCore::new(config.clone()),
46164617
};
46174618
assert_eq!(hooks.strategy_id(), config.strategy_id);
4619+
assert_eq!(hooks.config().order_id_tag, config.order_id_tag);
46184620
assert_eq!(hooks.actor_id(), ActorId::from("MACRO-001"));
46194621

46204622
let custom = MacroTestCustomField {
46214623
inner: StrategyCore::new(config.clone()),
46224624
};
46234625
assert_eq!(custom.strategy_id(), config.strategy_id);
4626+
assert_eq!(custom.config().order_id_tag, config.order_id_tag);
46244627
assert_eq!(custom.actor_id(), ActorId::from("MACRO-001"));
46254628
assert!(custom.external_order_claims().is_none());
46264629
}

docs/how_to/write_rust_strategy.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ The `nautilus_strategy!` macro generates the native runtime wiring used by
5959
registration and the `Strategy` trait impl. By default it delegates to a field
6060
named `core`; pass a second argument for a different field name. The macro
6161
does not make your strategy or its `StrategyCore` deref to runtime internals.
62+
It also adds `config()`, which returns the `StrategyConfig` passed to
63+
`StrategyCore::new`.
6264

6365
Runtime registration uses blanket `Actor` and `Component` implementations that
6466
require native wiring and `Debug`. The macro supplies the native wiring;

0 commit comments

Comments
 (0)