Data
The data subpackage groups components relating to the data stack and data tooling for the platform.
The layered architecture of the data stack somewhat mirrors the execution stack with a central engine, cache layer beneath, database layer beneath, with alternative implementations able to be written on top.
Due to the high-performance, the core components are reusable between both backtest and live implementations - helping to ensure consistent logic for trading operations.
Aggregation
class BarAggregator
Bases: object
BarAggregator(Instrument instrument, BarType bar_type, handler: Callable[[Bar], None], bool await_partial=False) -> None
Provides a means of aggregating specified bars and sending to a registered handler.
- Parameters:
- instrument (Instrument) – The instrument for the aggregator.
- bar_type (BarType) – The bar type for the aggregator.
- handler (Callable [ [Bar ] , None ]) – The bar handler for the aggregator.
- await_partial (bool , default False) – If the aggregator should await an initial partial bar prior to aggregating.
- Raises: ValueError – If instrument.id != bar_type.instrument_id.
bar_type
The aggregators bar type.
- Returns: BarType
handle_bar(self, Bar bar) → void
Update the aggregator with the given bar.
- Parameters: bar (Bar) – The bar for the update.
handle_quote_tick(self, QuoteTick tick) → void
Update the aggregator with the given tick.
- Parameters: tick (QuoteTick) – The tick for the update.
handle_trade_tick(self, TradeTick tick) → void
Update the aggregator with the given tick.
- Parameters: tick (TradeTick) – The tick for the update.
is_running
‘bool’
- Type: is_running
set_await_partial(self, bool value)
set_partial(self, Bar partial_bar) → void
Set the initial values for a partially completed bar.
This method can only be called once per instance.
- Parameters: partial_bar (Bar) – The partial bar with values to set.
start_batch_update(self, handler: Callable[[Bar], None], uint64_t time_ns) → None
stop_batch_update(self) → None
class BarBuilder
Bases: object
BarBuilder(Instrument instrument, BarType bar_type) -> None
Provides a generic bar builder for aggregation.
- Parameters:
- instrument (Instrument) – The instrument for the builder.
- bar_type (BarType) – The bar type for the builder.
- Raises: ValueError – If instrument.id != bar_type.instrument_id.
build(self, uint64_t ts_event, uint64_t ts_init) → Bar
Return the aggregated bar with the given closing timestamp, and reset.
- Parameters:
- ts_event (uint64_t) – UNIX timestamp (nanoseconds) for the bar event.
- ts_init (uint64_t) – UNIX timestamp (nanoseconds) for the bar initialization.
- Return type: Bar
build_now(self) → Bar
Return the aggregated bar and reset.
- Return type: Bar
count
The builders current update count.
- Returns: int
initialized
If the builder is initialized.
- Returns: bool
price_precision
The price precision for the builders instrument.
- Returns: uint8
reset(self) → void
Reset the bar builder.
All stateful fields are reset to their initial value.
set_partial(self, Bar partial_bar) → void
Set the initial values for a partially completed bar.
This method can only be called once per instance.
- Parameters: partial_bar (Bar) – The partial bar with values to set.
size_precision
The size precision for the builders instrument.
- Returns: uint8
ts_last
UNIX timestamp (nanoseconds) when the builder last updated.
- Returns: uint64_t
update(self, Price price, Quantity size, uint64_t ts_event) → void
Update the bar builder.
- Parameters:
- price (Price) – The update price.
- size (Decimal) – The update size.
- ts_event (uint64_t) – UNIX timestamp (nanoseconds) of the update.
update_bar(self, Bar bar, Quantity volume, uint64_t ts_init) → void
Update the bar builder.
- Parameters: bar (Bar) – The update Bar.
class TickBarAggregator
Bases: BarAggregator
TickBarAggregator(Instrument instrument, BarType bar_type, handler: Callable[[Bar], None]) -> None
Provides a means of building tick bars from ticks.
When received tick count reaches the step threshold of the bar specification, then a bar is created and sent to the handler.
- Parameters:
- instrument (Instrument) – The instrument for the aggregator.
- bar_type (BarType) – The bar type for the aggregator.
- handler (Callable [ [Bar ] , None ]) – The bar handler for the aggregator.
- Raises: ValueError – If instrument.id != bar_type.instrument_id.
bar_type
The aggregators bar type.
- Returns: BarType
handle_bar(self, Bar bar) → void
Update the aggregator with the given bar.
- Parameters: bar (Bar) – The bar for the update.
handle_quote_tick(self, QuoteTick tick) → void
Update the aggregator with the given tick.
- Parameters: tick (QuoteTick) – The tick for the update.
handle_trade_tick(self, TradeTick tick) → void
Update the aggregator with the given tick.
- Parameters: tick (TradeTick) – The tick for the update.
is_running
‘bool’
- Type: is_running
set_await_partial(self, bool value)
set_partial(self, Bar partial_bar) → void
Set the initial values for a partially completed bar.
This method can only be called once per instance.
- Parameters: partial_bar (Bar) – The partial bar with values to set.
start_batch_update(self, handler: Callable[[Bar], None], uint64_t time_ns) → None
stop_batch_update(self) → None
class TimeBarAggregator
Bases: BarAggregator
TimeBarAggregator(Instrument instrument, BarType bar_type, handler: Callable[[Bar], None], Clock clock, str interval_type=’left-open’, bool timestamp_on_close=True, bool skip_first_non_full_bar=False, bool build_with_no_updates=True, time_bars_origin_offset: pd.Timedelta | pd.DateOffset = None, int bar_build_delay=0) -> None
Provides a means of building time bars from ticks with an internal timer.
When the time reaches the next time interval of the bar specification, then a bar is created and sent to the handler.
- Parameters:
- instrument (Instrument) – The instrument for the aggregator.
- bar_type (BarType) – The bar type for the aggregator.
- handler (Callable [ [Bar ] , None ]) – The bar handler for the aggregator.
- clock (Clock) – The clock for the aggregator.
- interval_type (str , default 'left-open') – Determines the type of interval used for time aggregation.
- ‘left-open’: start time is excluded and end time is included (default).
- ‘right-open’: start time is included and end time is excluded.
- timestamp_on_close (bool , default True) – If True, then timestamp will be the bar close time. If False, then timestamp will be the bar open time.
- skip_first_non_full_bar (bool , default False) – If will skip emitting a bar if the aggregation starts mid-interval.
- build_with_no_updates (bool , default True) – If build and emit bars with no new market updates.
- time_bars_origin_offset (pd.Timedelta or pd.DateOffset , optional) – The origin time offset.
- bar_build_delay (int , default 0) – The time delay (microseconds) before building and emitting a composite bar type. 15 microseconds can be useful in a backtest context, when aggregating internal bars from internal bars several times so all messages are processed before a timer triggers.
- Raises: ValueError – If instrument.id != bar_type.instrument_id.
bar_type
The aggregators bar type.
- Returns: BarType
get_start_time(self, datetime now: datetime) → datetime
Return the start time for the aggregators next bar.
- Returns: The timestamp (UTC).
- Return type: datetime
handle_bar(self, Bar bar) → void
Update the aggregator with the given bar.
- Parameters: bar (Bar) – The bar for the update.
handle_quote_tick(self, QuoteTick tick) → void
Update the aggregator with the given tick.
- Parameters: tick (QuoteTick) – The tick for the update.
handle_trade_tick(self, TradeTick tick) → void
Update the aggregator with the given tick.
- Parameters: tick (TradeTick) – The tick for the update.
interval
The aggregators time interval.
- Returns: timedelta
interval_ns
The aggregators time interval.
- Returns: uint64_t
is_running
‘bool’
- Type: is_running
next_close_ns
The aggregators next closing time.
- Returns: uint64_t
set_await_partial(self, bool value)
set_partial(self, Bar partial_bar) → void
Set the initial values for a partially completed bar.
This method can only be called once per instance.
- Parameters: partial_bar (Bar) – The partial bar with values to set.
start_batch_update(self, handler: Callable[[Bar], None], uint64_t time_ns) → None
stop(self) → void
Stop the bar aggregator.
stop_batch_update(self) → None
class ValueBarAggregator
Bases: BarAggregator
ValueBarAggregator(Instrument instrument, BarType bar_type, handler: Callable[[Bar], None]) -> None
Provides a means of building value bars from ticks.
When received value reaches the step threshold of the bar specification, then a bar is created and sent to the handler.
- Parameters:
- instrument (Instrument) – The instrument for the aggregator.
- bar_type (BarType) – The bar type for the aggregator.
- handler (Callable [ [Bar ] , None ]) – The bar handler for the aggregator.
- Raises: ValueError – If instrument.id != bar_type.instrument_id.
bar_type
The aggregators bar type.
- Returns: BarType
get_cumulative_value(self)
Return the current cumulative value of the aggregator.
- Return type: Decimal
handle_bar(self, Bar bar) → void
Update the aggregator with the given bar.
- Parameters: bar (Bar) – The bar for the update.
handle_quote_tick(self, QuoteTick tick) → void
Update the aggregator with the given tick.
- Parameters: tick (QuoteTick) – The tick for the update.
handle_trade_tick(self, TradeTick tick) → void
Update the aggregator with the given tick.
- Parameters: tick (TradeTick) – The tick for the update.
is_running
‘bool’
- Type: is_running
set_await_partial(self, bool value)
set_partial(self, Bar partial_bar) → void
Set the initial values for a partially completed bar.
This method can only be called once per instance.
- Parameters: partial_bar (Bar) – The partial bar with values to set.
start_batch_update(self, handler: Callable[[Bar], None], uint64_t time_ns) → None
stop_batch_update(self) → None
class VolumeBarAggregator
Bases: BarAggregator
VolumeBarAggregator(Instrument instrument, BarType bar_type, handler: Callable[[Bar], None]) -> None
Provides a means of building volume bars from ticks.
When received volume reaches the step threshold of the bar specification, then a bar is created and sent to the handler.
- Parameters:
- instrument (Instrument) – The instrument for the aggregator.
- bar_type (BarType) – The bar type for the aggregator.
- handler (Callable [ [Bar ] , None ]) – The bar handler for the aggregator.
- Raises: ValueError – If instrument.id != bar_type.instrument_id.
bar_type
The aggregators bar type.
- Returns: BarType
handle_bar(self, Bar bar) → void
Update the aggregator with the given bar.
- Parameters: bar (Bar) – The bar for the update.
handle_quote_tick(self, QuoteTick tick) → void
Update the aggregator with the given tick.
- Parameters: tick (QuoteTick) – The tick for the update.
handle_trade_tick(self, TradeTick tick) → void
Update the aggregator with the given tick.
- Parameters: tick (TradeTick) – The tick for the update.
is_running
‘bool’
- Type: is_running
set_await_partial(self, bool value)
set_partial(self, Bar partial_bar) → void
Set the initial values for a partially completed bar.
This method can only be called once per instance.
- Parameters: partial_bar (Bar) – The partial bar with values to set.
start_batch_update(self, handler: Callable[[Bar], None], uint64_t time_ns) → None
stop_batch_update(self) → None
Client
class DataClient
Bases: Component
DataClient(ClientId client_id, MessageBus msgbus, Cache cache, Clock clock, Venue venue: Venue | None = None, config: NautilusConfig | None = None)
The base class for all data clients.
- Parameters:
- client_id (ClientId) – The data client ID.
- msgbus (MessageBus) – The message bus for the client.
- clock (Clock) – The clock for the client.
- venue (Venue , optional) – The client venue. If multi-venue then can be
None
. - config (NautilusConfig , optional) – The configuration for the instance.
WARNING
This class should not be used directly, but through a concrete subclass.
degrade(self) → void
Degrade the component.
While executing on_degrade() any exception will be logged and reraised, then the component
will remain in a DEGRADING
state.
WARNING
Do not override.
If the component is not in a valid state from which to execute this method, then the component state will not change, and an error will be logged.