Skip to content

quantpylib.hft.bars

Bars

The Bars class serves as a base class for implementing various types of candlestick bars, such as time-based, tick-based, volume-based, and dollar-based bars. It provides the core functionality for managing bar data, including updating, finalizing custom candlesticks from raw tick data.

  • Maintains a buffer of finalized bars using a ring buffer.
  • Tracks bar attributes such as timestamp, open, high, low, close, volume, tick count, VWAP, and end time.
  • Provides methods for updating the current bar with trade data and finalizing it when a rolling condition is met.
  • Supports subclassing to implement specific bar sampling methods by overriding _should_roll and other methods.

Behavior:

  • The update method processes incoming trade data and determines whether the current bar should roll.
  • The _finalize_bar method computes the finalized bar attributes and resets the state for the next bar.
  • Subclasses define the rolling logic by implementing the _should_roll method.

__init__(buffer_size=100, **kwargs)

A class representing a buffer for storing bar data with attributes for timestamp, open, high, low, close, volume, tick_count, VWAP, and end time. Child classes implement different kinds of bar sampling methods, such as time-based, tick-based, volume-based, etc.

Parameters:

Name Type Description Default
buffer_size int

The size of the buffer for storing live, finalized bars. Defaults to 100.

100

as_df()

Return the buffer as a pandas DataFrame.

buffer_len()

Return the number of bars in the buffer.

clear_buffer()

Clear the bar buffer.

get_buffer()

Return the buffer as a numpy array.

get_sample(n=-1, tau=-1, T=-1)

Return a sample of the buffer.

sample_len(n=-1, tau=-1, T=-1)

Get the length of the samples.

Parameters:

Name Type Description Default
n int

The number of samples to take.

-1
tau int

T - t, where t is the earliest sample time.

-1
T int

The terminal time. Defaults to last timestamp observed.

-1

Returns:

Name Type Description
int

The length of the samples.

update(trade)

Update the current bar with trade data and finalize if rolling condition is met.

Parameters:

Name Type Description Default
trade

Tuple of (ts, price, size, dir)

required

Returns:

Name Type Description
list

Finalized bar if completed, else None

DistanceBars

Bases: Bars

Distance-based bars triggered by a percentage move from the open price.

__init__(percent=0.005, **kwargs)

A class representing distance-based bars triggered by a percentage move from the open price.

Parameters:

Name Type Description Default
percent float

The relative percentage change (e.g., 0.005 for 0.5%) that triggers a new bar.

0.005
**kwargs

Additional arguments for the Bars class.

{}

DollarBars

Bases: Bars

Dollar-based bars with a specified dollar amount.

__init__(n_dollars=10000, **kwargs)

A class representing dollar-based bars with a specified number of dollars.

Parameters:

Name Type Description Default
n_dollars int

The dollar amount per bar.

10000
**kwargs

Additional arguments for the Bars class.

{}

ProbabilisticSignedTickBars

Bases: Bars

Probabilistic signed tick-based bars with an adaptive threshold.

__init__(lamb=0.06, n_ticks=100, Ebt=0.5, **kwargs)

A class representing probabilistic signed tick-based bars with a specified number of ticks.

Parameters:

Name Type Description Default
lamb float

The smoothing factor for the exponential moving average of the tick imbalance.

0.06
n_ticks int

The number of ticks per bar.

100
Ebt float

The expected tick imbalance.

0.5
**kwargs

Additional arguments for the Bars class.

{}

SignedDollarBars

Bases: Bars

Signed dollar-based bars with a specified signed dollar amount.

__init__(n_dollars=10000, **kwargs)

A class representing signed dollar-based bars with a specified signed dollar amount.

Parameters:

Name Type Description Default
n_dollars int

The signed dollar amount per bar.

10000
**kwargs

Additional arguments for the Bars class.

{}

SignedTickBars

Bases: Bars

Signed tick-based bars with a specified number of signed ticks.

__init__(n_ticks=100, **kwargs)

A class representing signed tick-based bars with a specified number of ticks.

Parameters:

Name Type Description Default
n_ticks int

The number of signed ticks per bar.

100
**kwargs

Additional arguments for the Bars class.

{}

SignedVolumeBars

Bases: Bars

Signed volume-based bars with a specified signed volume.

__init__(n_volume=100, **kwargs)

A class representing signed volume-based bars with a specified signed volume.

Parameters:

Name Type Description Default
n_volume int

The signed volume per bar.

100
**kwargs

Additional arguments for the Bars class.

{}

TickBars

Bases: Bars

Tick-based bars with a specified number of ticks.

__init__(n_ticks=100, **kwargs)

A class representing tick-based bars with a specified number of ticks.

Parameters:

Name Type Description Default
n_ticks int

The number of ticks per bar.

100
**kwargs

Additional arguments for the Bars class.

{}

TimeBars

Bases: Bars

Time-based bars with a specified granularity.

__init__(granularity='s', granularity_multiplier=10, **kwargs)

A class representing time-based bars with a specified granularity.

Parameters:

Name Type Description Default
granularity str or Period

The granularity of the bars (e.g., 's', 'm', 'h', 'd').

's'
granularity_multiplier int

Multiplier for the granularity. 'm', 5 represents 5 minutes candles.

10
**kwargs

Additional arguments for the Bars class.

{}

VolumeBars

Bases: Bars

Volume-based bars with a specified volume threshold.

__init__(n_volume=100, **kwargs)

A class representing volume-based bars with a specified number of volume. Args: n_volume (int): The volume per bar. **kwargs: Additional arguments for the Bars class.