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.

Implementation:Nautechsystems Nautilus trader Actor Subscribe Bars

From Leeroopedia


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

Overview

Concrete tool for subscribing to real-time bar data, trade tick, and quote tick streams provided by NautilusTrader.

Description

NautilusTrader provides subscription methods on the Actor base class (inherited by Strategy) that allow strategies to declare interest in specific data streams. The primary methods are:

  • subscribe_bars: Subscribe to aggregated bar data for a given BarType. Once subscribed, every matching bar published on the message bus is forwarded to the strategy's on_bar handler.
  • subscribe_trade_ticks: Subscribe to individual trade tick data for a given InstrumentId. Events are forwarded to on_trade_tick.
  • subscribe_quote_ticks: Subscribe to quote tick (best bid/ask) data for a given InstrumentId. Events are forwarded to on_quote_tick.

Each method performs two actions internally:

  1. Registers a handler binding on the message bus (topic to handler mapping).
  2. Sends a subscribe command to the DataEngine, which in turn requests the data from the appropriate data client (e.g., exchange adapter).

All subscription methods accept an optional client_id parameter to explicitly route the subscription to a specific data client. If omitted, the client is inferred from the venue in the instrument ID.

Usage

Call these methods in your strategy's on_start method to begin receiving data. Call the corresponding unsubscribe_* methods in on_stop to cease receiving data.

Code Reference

Source Location

nautilus_trader/common/actor.pyx, lines 1625-1913.

Signatures

subscribe_bars:

cpdef void subscribe_bars(
    self,
    BarType bar_type,
    ClientId client_id = None,
    bint update_catalog = False,
    dict[str, object] params = None,
)

subscribe_trade_ticks:

cpdef void subscribe_trade_ticks(
    self,
    InstrumentId instrument_id,
    ClientId client_id = None,
    bint update_catalog = False,
    dict[str, object] params = None,
)

subscribe_quote_ticks:

cpdef void subscribe_quote_ticks(
    self,
    InstrumentId instrument_id,
    ClientId client_id = None,
    bint update_catalog = False,
    bint aggregate_spread_quotes = False,
    dict[str, object] params = None,
)

Import

These methods are inherited by Strategy from Actor. No separate import is needed; they are called as self.subscribe_bars(...) within any Strategy subclass.

from nautilus_trader.trading.strategy import Strategy  # subscribe methods available via self

I/O Contract

Inputs (subscribe_bars)

Parameter Type Required Description
bar_type BarType Yes The bar type to subscribe to (instrument, step, aggregation, price type).
client_id ClientId or None No Specific data client ID. If None, inferred from venue.
update_catalog bool No Whether to update a data catalog with received bars. Default False.
params dict[str, object] or None No Additional parameters for the data client.

Inputs (subscribe_trade_ticks)

Parameter Type Required Description
instrument_id InstrumentId Yes The instrument to subscribe trade ticks for.
client_id ClientId or None No Specific data client ID. If None, inferred from venue.
update_catalog bool No Whether to update a data catalog. Default False.
params dict[str, object] or None No Additional parameters for the data client.

Inputs (subscribe_quote_ticks)

Parameter Type Required Description
instrument_id InstrumentId Yes The instrument to subscribe quote ticks for.
client_id ClientId or None No Specific data client ID. If None, inferred from venue.
update_catalog bool No Whether to update a data catalog. Default False.
aggregate_spread_quotes bool No Whether to aggregate spread quotes from legs. Default False.
params dict[str, object] or None No Additional parameters for the data client.

Outputs

Output Type Description
None void These methods return nothing. Their effect is to register a message bus subscription and send a subscribe command to the DataEngine.

Side effects:

  • A handler binding is created on the message bus linking the data topic to the strategy's handler method.
  • A subscribe command is sent to the DataEngine, which requests data from the appropriate data client.

Usage Examples

Subscribing to bars in on_start:

from nautilus_trader.model.data import BarType


class MyStrategy(Strategy):
    def on_start(self):
        bar_type = BarType.from_str("AAPL.XNAS-1-MINUTE-LAST-EXTERNAL")
        self.subscribe_bars(bar_type)

Subscribing to trade ticks and quote ticks:

from nautilus_trader.model.identifiers import InstrumentId


class MultiDataStrategy(Strategy):
    def on_start(self):
        instrument_id = InstrumentId.from_str("AAPL.XNAS")

        # Subscribe to trade ticks for signal generation
        self.subscribe_trade_ticks(instrument_id)

        # Subscribe to quote ticks for spread monitoring
        self.subscribe_quote_ticks(instrument_id)

        # Subscribe to 5-minute bars for trend analysis
        bar_type = BarType.from_str("AAPL.XNAS-5-MINUTE-LAST-EXTERNAL")
        self.subscribe_bars(bar_type)

Subscribing with an explicit client ID:

from nautilus_trader.model.identifiers import ClientId


class ExplicitClientStrategy(Strategy):
    def on_start(self):
        bar_type = BarType.from_str("BTCUSDT.BINANCE-1-MINUTE-LAST-EXTERNAL")
        client = ClientId("BINANCE")
        self.subscribe_bars(bar_type, client_id=client)

Related Pages

Page Connections

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