Skip to content

Commit

Permalink
Update example messaging with msgbus (#2406)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansimik authored Mar 5, 2025
1 parent d53c78d commit 22512f8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
19 changes: 0 additions & 19 deletions examples/backtest/example_09_custom_event_with_msgbus/README.md

This file was deleted.

21 changes: 21 additions & 0 deletions examples/backtest/example_09_messaging_with_msgbus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Example: Self-Communication Using Message Bus

A practical demonstration of using NautilusTrader's message bus for self-communication within a strategy.
The example implements a "10th bar notification system" where the strategy:

1. Creates a custom event (using Python's dataclass) to represent the 10th bar occurrence
2. Publishes this event to the message bus when the 10th bar arrives
3. Subscribes to and handles these events within the same strategy

Key learning points:
- Creating custom events with the message bus
- Implementing publish/subscribe pattern for self-communication
- Using events for condition-based notifications
- Handling state changes through message bus events

This pattern provides a clean, event-driven approach to handle conditional notifications
and state changes within your trading strategies.

Note:
While this example shows both publisher and subscriber roles within a single strategy, in practice these roles
can be distributed - any component can be a publisher and any other component can be a subscriber of events.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,29 @@
class Each10thBarEvent(Event):
"""
A custom event that is published every 10th bar.
By inheriting from `Event` class, we automatically get important attributes:
- `id`: A unique string identifier for each event (in UUID format)
- `ts_event`: Timestamp when the event occurred (used for event ordering)
- `ts_init`: Timestamp when the event was initialized
These attributes are crucial for correct event processing and ordering in the message bus,
especially during backtesting where event timing is important.
Event class offers complete flexibility in terms of attributes:
- Can contain attributes of any Python type (int, float, str, custom objects, etc.)
"""

bar: Bar # The 10th bar related to this event
TOPIC: str = "each_10th_bar_event" # Topic name for message bus publish/subscribe


class DemoStrategyConfig(StrategyConfig, frozen=True):
"""
Configuration for the demo strategy.
"""

instrument: Instrument
bar_type: BarType

Expand All @@ -56,6 +72,10 @@ def on_start(self):
self.subscribe_bars(self.config.bar_type)
self.log.info(f"Subscribed to {self.config.bar_type}", color=LogColor.YELLOW)

# The message bus implements a topic-based publish/subscribe pattern:
# - Publishers can publish events to one or more named topics
# - Subscribers can subscribe to one or more topics of interest

# Subscribe to our custom event
# First argument is the topic name to subscribe to, second is the custom handler method
self.msgbus.subscribe(Each10thBarEvent.TOPIC, self.on_each_10th_bar)
Expand Down Expand Up @@ -85,11 +105,7 @@ def on_bar(self, bar: Bar):

def on_each_10th_bar(self, event: Each10thBarEvent):
"""
Event handler for Each10thBarEvent.
This method is called automatically when an Each10thBarEvent is published,
thanks to our subscription in on_start.
Handle each 10th bar event received from the message bus.
"""
# Log the event details
self.log.info(
Expand Down

0 comments on commit 22512f8

Please sign in to comment.