Data
class Bar
Bases: Data
Bar(BarType bar_type, Price open, Price high, Price low, Price close, Quantity volume, uint64_t ts_event, uint64_t ts_init, bool is_revision=False) -> None
Represents an aggregated bar.
- Parameters:
- bar_type (BarType) – The bar type for this bar.
- open (Price) – The bars open price.
- high (Price) – The bars high price.
- low (Price) – The bars low price.
- close (Price) – The bars close price.
- volume (Quantity) – The bars volume.
- ts_event (uint64_t) – UNIX timestamp (nanoseconds) when the data event occurred.
- ts_init (uint64_t) – UNIX timestamp (nanoseconds) when the data object was initialized.
- is_revision (bool , default False) – If this bar is a revision of a previous bar with the same ts_event.
- Raises:
- ValueError – If high is not >= low.
- ValueError – If high is not >= close.
- ValueError – If low is not <= close.
bar_type
BarType
Return the bar type of bar.
- Return type: BarType
- Type: Bar.bar_type
close
Price
Return the close price of the bar.
- Return type: Price
- Type: Bar.close
static from_dict(dict values) → Bar
Return a bar parsed from the given values.
- Parameters: values (dict *[*str , object ]) – The values for initialization.
- Return type: Bar
static from_pyo3(pyo3_bar) → Bar
Return a legacy Cython bar converted from the given pyo3 Rust object.
- Parameters: pyo3_bar (nautilus_pyo3.Bar) – The pyo3 Rust bar to convert from.
- Return type: Bar
static from_pyo3_list(list pyo3_bars) → list[Bar]
Return legacy Cython bars converted from the given pyo3 Rust objects.
- Parameters: pyo3_bars (list *[*nautilus_pyo3.Bar ]) – The pyo3 Rust bars to convert from.
- Return type: list[Bar]
static from_raw(BarType bar_type, PriceRaw open, PriceRaw high, PriceRaw low, PriceRaw close, uint8_t price_prec, QuantityRaw volume, uint8_t size_prec, uint64_t ts_event, uint64_t ts_init) → Bar
static from_raw_arrays_to_list(BarType bar_type, uint8_t price_prec, uint8_t size_prec, double[:] opens, double[:] highs, double[:] lows, double[:] closes, double[:] volumes, uint64_t[:] ts_events, uint64_t[:] ts_inits) → list[Bar]
classmethod fully_qualified_name(cls) → str
Return the fully qualified name for the Data class.
- Return type: str
high
Price
Return the high price of the bar.
- Return type: Price
- Type: Bar.high
is_revision
If this bar is a revision for a previous bar with the same ts_event.
- Returns: bool
classmethod is_signal(cls, str name='') → bool
Determine if the current class is a signal type, optionally checking for a specific signal name.
- Parameters: name (str , optional) – The specific signal name to check. If name not provided or if an empty string is passed, the method checks whether the class name indicates a general signal type. If name is provided, the method checks if the class name corresponds to that specific signal.
- Returns: True if the class name matches the signal type or the specific signal name, otherwise False.
- Return type: bool
is_single_price(self) → bool
If the OHLC are all equal to a single price.
- Return type: bool
low
Price
Return the low price of the bar.
- Return type: Price
- Type: Bar.low
open
Price
Return the open price of the bar.
- Return type: Price
- Type: Bar.open
static to_dict(Bar obj)
Return a dictionary representation of this object.
- Return type: dict[str, object]
to_pyo3(self) → nautilus_pyo3.Bar
Return a pyo3 object from this legacy Cython instance.
- Return type: nautilus_pyo3.Bar
static to_pyo3_list(list bars) → list[nautilus_pyo3.Bar]
Return pyo3 Rust bars converted from the given legacy Cython objects.
- Parameters: bars (list [Bar ]) – The legacy Cython bars to convert from.
- Return type: list[nautilus_pyo3.Bar]
ts_event
int
UNIX timestamp (nanoseconds) when the data event occurred.
- Return type: int
- Type: Bar.ts_event
ts_init
int
UNIX timestamp (nanoseconds) when the object was initialized.
- Return type: int
- Type: Bar.ts_init
volume
Quantity
Return the volume of the bar.
- Return type: Quantity
- Type: Bar.volume
class BarAggregation
Bases: IntFlag
conjugate()
Returns self, the complex conjugate of any int.
bit_length()
Number of bits necessary to represent self in binary.
>>> bin(37)
'0b100101'
>>> (37).bit_length()
6
bit_count()
Number of ones in the binary representation of the absolute value of self.
Also known as the population count.
>>> bin(13)
'0b1101'
>>> (13).bit_count()
3
to_bytes(length=1, byteorder='big', , signed=False)
Return an array of bytes representing an integer.
length : Length of bytes object to use. An OverflowError is raised if the integer is not representable with the given number of bytes. Default is length 1.
byteorder : The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use sys.byteorder as the byte order value. Default is to use ‘big’.
signed : Determines whether two’s complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised.
classmethod from_bytes(bytes, byteorder='big', , signed=False)
Return the integer represented by the given array of bytes.
bytes : Holds the array of bytes to convert. The argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol.
byteorder : The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use sys.byteorder as the byte order value. Default is to use ‘big’.
signed : Indicates whether two’s complement is used to represent the integer.
as_integer_ratio()
Return a pair of integers, whose ratio is equal to the original int.
The ratio is in lowest terms and has a positive denominator.
>>> (10).as_integer_ratio()
(10, 1)
>>> (-10).as_integer_ratio()
(-10, 1)
>>> (0).as_integer_ratio()
(0, 1)
is_integer()
Returns True. Exists for duck type compatibility with float.is_integer.
real
the real part of a complex number
imag
the imaginary part of a complex number
numerator
the numerator of a rational number in lowest terms
denominator
the denominator of a rational number in lowest terms
TICK = 1
TICK_IMBALANCE = 2
TICK_RUNS = 3
VOLUME = 4
VOLUME_IMBALANCE = 5
VOLUME_RUNS = 6
VALUE = 7
VALUE_IMBALANCE = 8
VALUE_RUNS = 9
MILLISECOND = 10
SECOND = 11
MINUTE = 12
HOUR = 13
DAY = 14
WEEK = 15
MONTH = 16
YEAR = 17
class BarIntervalType
Bases: IntFlag
conjugate()
Returns self, the complex conjugate of any int.
bit_length()
Number of bits necessary to represent self in binary.
>>> bin(37)
'0b100101'
>>> (37).bit_length()
6
bit_count()
Number of ones in the binary representation of the absolute value of self.
Also known as the population count.
>>> bin(13)
'0b1101'
>>> (13).bit_count()
3
to_bytes(length=1, byteorder='big', , signed=False)
Return an array of bytes representing an integer.
length : Length of bytes object to use. An OverflowError is raised if the integer is not representable with the given number of bytes. Default is length 1.
byteorder : The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use sys.byteorder as the byte order value. Default is to use ‘big’.
signed : Determines whether two’s complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised.
classmethod from_bytes(bytes, byteorder='big', , signed=False)
Return the integer represented by the given array of bytes.
bytes : Holds the array of bytes to convert. The argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol.
byteorder : The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use sys.byteorder as the byte order value. Default is to use ‘big’.
signed : Indicates whether two’s complement is used to represent the integer.
as_integer_ratio()
Return a pair of integers, whose ratio is equal to the original int.
The ratio is in lowest terms and has a positive denominator.
>>> (10).as_integer_ratio()
(10, 1)
>>> (-10).as_integer_ratio()
(-10, 1)
>>> (0).as_integer_ratio()
(0, 1)
is_integer()
Returns True. Exists for duck type compatibility with float.is_integer.
real
the real part of a complex number
imag
the imaginary part of a complex number
numerator
the numerator of a rational number in lowest terms
denominator
the denominator of a rational number in lowest terms
LEFT_OPEN = 0
RIGHT_OPEN = 1
class BarSpecification
Bases: object
BarSpecification(int step, BarAggregation aggregation, PriceType price_type) -> None
Represents a bar aggregation specification that defines how market data should be aggregated into bars (candlesticks).
A bar specification consists of three main components:
- Step: The quantity or interval for aggregation (e.g., 5 for 5-minute bars)
- Aggregation: The method/rule for aggregation (time, tick, volume, value, etc.)
- Price Type: Which price to use for aggregation (BID, ASK, MID, LAST)
Bar specifications are used to define different types of bars:
Time-based bars: Aggregate data over fixed time intervals
- Examples: 1-MINUTE-LAST, 5-MINUTE-MID, 1-HOUR-BID
Tick-based bars: Aggregate data after a certain number of ticks
- Examples: 100-TICK-LAST, 1000-TICK-MID
Volume-based bars: Aggregate data after a certain volume threshold
- Examples: 1000-VOLUME-LAST, 10000-VOLUME-MID
Value-based bars: Aggregate data after a certain dollar value threshold
- Examples: 100000-VALUE-LAST, 1000000-VALUE-MID
Information-based bars: Advanced aggregation based on information flow
- Examples: 1000-VALUE_IMBALANCE-MID, 500-VALUE_RUNS-LAST
The specification determines:
- What triggers bar creation (aggregation method)
- How often bars are created (step size)
- Which price level to use for OHLCV calculation (price type)
- Parameters:
- step (int) – The step size for bar aggregation. Must be positive.
- For time bars: interval in time units (1=1min, 5=5min, etc.)
- For tick bars: number of ticks per bar
- For volume/value bars: threshold amount
- aggregation (BarAggregation) – The aggregation method (MINUTE, TICK, VOLUME, VALUE, etc.)
- price_type (PriceType) – The price type to use (BID, ASK, MID, LAST)
- step (int) – The step size for bar aggregation. Must be positive.
- Raises: ValueError – If step is not valid or if invalid aggregation/price_type combinations
aggregation
BarAggregation
Return the aggregation for the specification.
- Return type: BarAggregation
- Type: BarSpecification.aggregation
static check_information_aggregated(BarAggregation aggregation)
Check if the given aggregation is an information-based aggregation type.
Information-based aggregation creates bars based on market microstructure patterns and sequential runs of similar market events. These bars capture information flow and market efficiency patterns by detecting sequences of directionally similar price movements or trading activity.
Information-based aggregation types include:
TICK_RUNS
: Bars created when runs of tick price movements occurVOLUME_RUNS
: Bars created when runs of volume patterns occurVALUE_RUNS
: Bars created when runs of value patterns occur
Runs are sequences of consecutive events with the same directional property (e.g., consecutive upticks or downticks). This aggregation method is useful for analyzing market microstructure, information flow, and detecting patterns in high-frequency trading activity.
This differs from time-based aggregation (fixed intervals) and threshold-based aggregation (activity levels), focusing instead on sequential patterns and information content of market events.
- Parameters: aggregation (BarAggregation) – The aggregation type to check.
- Returns: True if the aggregation is information-based, else False.
- Return type: bool
static check_threshold_aggregated(BarAggregation aggregation)
Check if the given aggregation is a threshold-based aggregation type.
Threshold-based aggregation creates bars when accumulated market activity reaches predefined thresholds, providing activity-driven sampling rather than time-driven sampling. These bars capture market dynamics based on actual trading patterns and volumes.
Threshold-based aggregation types include:
TICK
: Bars created after N ticks (price changes)TICK_IMBALANCE
: Bars created when tick imbalance reaches thresholdVOLUME
: Bars created after N units of volume are tradedVOLUME_IMBALANCE
: Bars created when volume imbalance reaches thresholdVALUE
: Bars created after N units of notional value are tradedVALUE_IMBALANCE
: Bars created when value imbalance reaches threshold
This differs from time-based aggregation which creates bars at fixed time intervals, and information-based aggregation which creates bars based on market microstructure patterns and runs.
- Parameters: aggregation (BarAggregation) – The aggregation type to check.
- Returns: True if the aggregation is threshold-based, else False.
- Return type: bool
static check_time_aggregated(BarAggregation aggregation)
Check if the given aggregation is a time-based aggregation type.
Time-based aggregation creates bars at fixed time intervals, where each bar represents market data for a specific time period. These bars are emitted when the time interval expires, regardless of trading activity level.
Time-based aggregation types include:
MILLISECOND
: Bars created every N millisecondsSECOND
: Bars created every N secondsMINUTE
: Bars created every N minutesHOUR
: Bars created every N hoursDAY
: Bars created every N days (calendar days)WEEK
: Bars created every N weeks (calendar weeks)MONTH
: Bars created every N months (calendar months)YEAR
: Bars created every N years (calendar years)
This is distinct from threshold-based aggregation (TICK, VOLUME, VALUE) which creates bars when activity thresholds are reached, and information-based aggregation (RUNS) which creates bars based on market microstructure patterns.
- Parameters: aggregation (BarAggregation) – The aggregation type to check.
- Returns: True if the aggregation is time-based, else False.
- Return type: bool
static from_str(str value) → BarSpecification
Return a bar specification parsed from the given string.
-
Parameters: value (str) – The bar specification string to parse.
-
Return type: BarSpecification
-
Raises: ValueError – If value is not a valid string.
static from_timedelta(timedelta duration, PriceType price_type) → BarSpecification
Return a bar specification parsed from the given timedelta and price_type.
-
Parameters:
- duration (timedelta) – The bar specification timedelta to parse.
- price_type (PriceType) – The bar specification price_type.
-
Return type: BarSpecification
-
Raises: ValueError – If duration is not rounded step of aggregation.
get_interval_ns(self) → uint64_t
Return the interval length in nanoseconds for time-based bar specifications.
Converts the bar specification’s time interval to nanoseconds based on its aggregation type and step size. This method is used for time calculations and (TODO: bar alignment).
- Returns: The interval length in nanoseconds.
- Return type: uint64_t
- Raises: ValueError – If the aggregation is MONTH or YEAR (since months and years have variable lengths 28-31 days or 365-366 days, making fixed nanosecond conversion impossible). If the aggregation is not a time-based aggregation.
is_information_aggregated(self) → bool
Return a value indicating whether the aggregation method is information-driven.
Information-based aggregation creates bars based on market microstructure patterns and sequential runs of similar market events. This aggregation method captures information flow, market efficiency patterns, and the sequential nature of trading activity by detecting directional runs.
Information-based aggregation types supported:
TICK_RUNS
: Bars based on runs of directional tick movements (upticks/downticks)VOLUME_RUNS
: Bars based on runs of volume patterns and clusteringVALUE_RUNS
: Bars based on runs of notional value patterns
A “run” is a sequence of consecutive market events with the same directional or categorical property. For example, a tick run might be 5 consecutive upticks followed by 3 consecutive downticks.
Information-based bars are ideal for:
- Market microstructure analysis and information flow studies
- Detecting patterns in high-frequency trading and market efficiency
- Analyzing sequential dependencies in market data
- Capturing information content rather than just time or activity levels
- Studying market maker behavior and order flow dynamics
This differs from time-based aggregation (fixed time intervals) and threshold-based aggregation (activity levels), focusing instead on the sequential information content and patterns within market events.
- Returns: True if the aggregation method is information-based, else False.
- Return type: bool
SEE ALSO
Get timedelta for time-based bars:
>>> spec = BarSpecification(30, BarAggregation.SECOND, PriceType.LAST)
>>> spec.timedelta
datetime.timedelta(seconds=30)
is_threshold_aggregated(self) → bool
Return a value indicating whether the aggregation method is threshold-based.
Threshold-based aggregation types trigger bar creation when cumulative activity reaches predefined levels, making them ideal for volume and value-driven analysis rather than time-based intervals.
Threshold-Based Aggregation Types
Activity threshold types supported:
TICK
: Bars based on tick count thresholds (every N ticks)VOLUME
: Bars based on volume thresholds (every N units traded)VALUE
: Bars based on notional value thresholds (every N dollars/currency traded)
Imbalance threshold types supported:
TICK_IMBALANCE
: Bars based on cumulative tick flow imbalancesVOLUME_IMBALANCE
: Bars based on cumulative volume imbalancesVALUE_IMBALANCE
: Bars based on cumulative value flow imbalances
Threshold-based bars are ideal for:
- Volume and activity-based analysis independent of time
- Capturing market activity during varying trading intensities
- Equal-activity sampling for statistical analysis
- Risk management based on position sizing and exposure levels
- Algorithmic trading strategies sensitive to market participation
This differs from time-based aggregation (fixed time intervals) and information-based aggregation (information content patterns), focusing instead on measurable activity and participation thresholds.
- Returns: True if the aggregation method is threshold-based, else False.
- Return type: bool
SEE ALSO
check_threshold_aggregated
: Static method for threshold aggregation checking
is_time_aggregated
: Check for time-based aggregation
is_information_aggregated
: Check for information-based aggregation