Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Nautechsystems Nautilus trader Data Subscription

From Leeroopedia


Field Value
sources https://github.com/nautechsystems/nautilus_trader , https://nautilustrader.io/docs/
domains algorithmic trading, event-driven architecture, data subscription, message bus, market data
last_updated 2026-02-10 12:00 GMT

Overview

Data subscription is the mechanism by which a trading strategy declares its interest in specific real-time data streams, establishing a reactive pipeline from market data sources to strategy event handlers.

Description

In an event-driven trading architecture, strategies do not poll for data. Instead, they subscribe to data streams and receive events asynchronously as new data arrives. This publish-subscribe pattern decouples data producers (exchanges, data providers) from data consumers (strategies), enabling:

  • Selective consumption: A strategy subscribes only to the instruments and data types it needs, avoiding the overhead of processing irrelevant data.
  • Automatic routing: The message bus routes published data to the correct handler method on each subscribed strategy, without the strategy needing to know the topology of the data pipeline.
  • Transparent source abstraction: Whether data comes from a live exchange feed, a historical replay engine, or a simulated matching engine, the subscription interface is identical.

The subscription model supports multiple data types:

  • Quote ticks: Best bid/ask updates for an instrument.
  • Trade ticks: Individual trade executions at a venue.
  • Bars: Aggregated OHLCV data at specified intervals and aggregation methods.
  • Order book data: Full or partial order book snapshots and delta updates.
  • Custom data: User-defined data types published on the message bus.

Each subscription creates a binding between a topic (e.g., trade ticks for instrument X) and a handler (e.g., on_trade_tick). The message bus maintains these bindings and ensures delivery guarantees within the event loop.

Usage

Apply data subscription when:

  • A strategy needs market data to make trading decisions (virtually all strategies).
  • You want to add or remove data feeds dynamically during the strategy lifecycle (e.g., subscribing to new instruments on start, unsubscribing on stop).
  • You are building strategies that react to multiple data types simultaneously (e.g., bars for trend detection and trade ticks for entry timing).

Theoretical Basis

Data subscription is an application of the Observer Pattern (also known as Publish-Subscribe):

PARTICIPANTS:
    Publisher   -- The data source (exchange adapter, data engine)
    MessageBus  -- The intermediary that manages topic-to-handler bindings
    Subscriber  -- The strategy that declares interest in a topic

FLOW:
    1. Subscriber calls subscribe(topic, handler)
    2. MessageBus records the binding: topic -> handler
    3. Publisher produces data and publishes to MessageBus
    4. MessageBus looks up all handlers bound to the matching topic
    5. MessageBus invokes each handler with the data event
    6. Subscriber processes the event in its handler method

Key properties:

  • Loose coupling: The publisher does not know about the subscriber, and the subscriber does not know about the publisher. Both know only the message bus and the topic schema.
  • Fan-out: Multiple strategies can subscribe to the same topic. Each receives an independent copy of the event.
  • Lifecycle binding: Subscriptions are typically established in on_start and torn down in on_stop, aligning data flow with strategy activity.

Pseudocode:

CLASS Strategy:
    FUNCTION on_start():
        // Subscribe to 1-minute bars for the target instrument
        bar_type = BarType(instrument_id, 1, MINUTE, LAST)
        CALL message_bus.subscribe(
            topic = bars_topic(bar_type),
            handler = self.on_bar
        )
        SEND SubscribeBars command to DataEngine

        // Subscribe to trade ticks for entry signal refinement
        CALL message_bus.subscribe(
            topic = trades_topic(instrument_id),
            handler = self.on_trade_tick
        )
        SEND SubscribeTradeTicks command to DataEngine

    FUNCTION on_stop():
        CALL message_bus.unsubscribe(bars_topic)
        CALL message_bus.unsubscribe(trades_topic)
        SEND Unsubscribe commands to DataEngine

Invariant: A strategy receives data events only for topics to which it has an active subscription. No handler is invoked for unsubscribed data.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment