Skip to content
GeneratedGenerated from the built extension by scripts/generate_python_api_reference.py; CI fails if it is stale.

openquant._core.bars

Compiled from Rust (crates/pyopenquant). Signatures and types are from the shipped .pyi stubs; descriptions are the functions’ own docstrings, as help() shows them. All Python modules.

Functions

build_dollar_bars

build_dollar_bars(
    timestamps: Sequence[str],
    prices: Sequence[float],
    volumes: Sequence[float],
    dollar_value_per_bar: float,
) -> list[tuple[str, str, float, float, float, float, float, float, int]]

Build dollar bars that close when cumulative price * volume reaches a threshold.

AFML section 2.3.1.4. The trade that crosses the threshold belongs to the bar it closes, so bars overshoot. A trailing partial bar is dropped. Trades must be in increasing time order; the input is not sorted or validated.

Parameters

timestamps : list[str]

Trade timestamps as "%Y-%m-%d %H:%M:%S" (an optional fractional second is accepted) or "%Y-%m-%d" (read as midnight).

prices : list[float]

Trade prices.

volumes : list[float]

Trade sizes.

dollar_value_per_bar : float

Cumulative traded value that closes a bar; must be positive and finite.

Returns

list[tuple[str, str, float, float, float, float, float, float, int]]

One (start_timestamp, timestamp, open, high, low, close, volume, dollar_value, tick_count) row per bar, where start_timestamp and timestamp are the times of the bar's first and last trade and dollar_value is the sum of price * volume.

Raises

ValueError

If dollar_value_per_bar is not a positive finite number, the three lists differ in length, or a timestamp does not parse.

build_imbalance_bars

build_imbalance_bars(
    timestamps: Sequence[str],
    prices: Sequence[float],
    volumes: Sequence[float],
    threshold: float,
    bar_type: str,
) -> list[tuple[str, str, float, float, float, float, float, float, int]]

Build imbalance bars that close when the absolute signed imbalance reaches a threshold.

A fixed-threshold simplification of AFML sections 2.3.2.1-2 (AFML's threshold is an exponentially weighted expectation). Each trade after the first contributes its tick-rule sign (up-tick +1, down-tick -1, unchanged price keeps the previous sign) times a weight: 1, its volume, or its price * volume, per bar_type. A trailing partial bar is dropped, and fewer than two trades yield no bars. Trades must be in increasing time order; the input is not sorted or validated.

Parameters

timestamps : list[str]

Trade timestamps as "%Y-%m-%d %H:%M:%S" (an optional fractional second is accepted) or "%Y-%m-%d" (read as midnight).

prices : list[float]

Trade prices.

volumes : list[float]

Trade sizes.

threshold : float

Absolute imbalance that closes a bar; must be positive and finite.

bar_type : str

Imbalance weight: "tick", "volume" or "dollar" (case-insensitive).

Returns

list[tuple[str, str, float, float, float, float, float, float, int]]

One (start_timestamp, timestamp, open, high, low, close, volume, dollar_value, tick_count) row per bar, where start_timestamp and timestamp are the times of the bar's first and last trade and dollar_value is the sum of price * volume.

Raises

ValueError

If threshold is not a positive finite number, bar_type is not one of the accepted values, the three lists differ in length, or a timestamp does not parse.

build_run_bars

build_run_bars(
    timestamps: Sequence[str],
    prices: Sequence[float],
    volumes: Sequence[float],
    threshold: int,
) -> list[tuple[str, str, float, float, float, float, float, float, int]]

Build run bars that close after a run of same-direction ticks.

A fixed-threshold simplification of AFML section 2.3.2.3 (AFML uses an adaptive expected threshold). Trade direction follows the tick rule: up-tick +1, down-tick -1, an unchanged price keeps the previous sign and so extends the current run. A bar closes when threshold consecutive moves occur in the same direction. A trailing partial bar is dropped, and fewer than two trades yield no bars. Trades must be in increasing time order; the input is not sorted or validated.

Parameters

timestamps : list[str]

Trade timestamps as "%Y-%m-%d %H:%M:%S" (an optional fractional second is accepted) or "%Y-%m-%d" (read as midnight).

prices : list[float]

Trade prices.

volumes : list[float]

Trade sizes.

threshold : int

Run length that closes a bar; must be positive.

Returns

list[tuple[str, str, float, float, float, float, float, float, int]]

One (start_timestamp, timestamp, open, high, low, close, volume, dollar_value, tick_count) row per bar, where start_timestamp and timestamp are the times of the bar's first and last trade and dollar_value is the sum of price * volume.

Raises

ValueError

If threshold is 0, the three lists differ in length, or a timestamp does not parse.

build_tick_bars

build_tick_bars(
    timestamps: Sequence[str],
    prices: Sequence[float],
    volumes: Sequence[float],
    ticks_per_bar: int,
) -> list[tuple[str, str, float, float, float, float, float, float, int]]

Build tick bars that close after a fixed number of trades (AFML section 2.3.1.2).

A bar closes once ticks_per_bar trades have accumulated. A trailing partial bar is dropped. Trades must be in increasing time order; the input is not sorted or validated.

Parameters

timestamps : list[str]

Trade timestamps as "%Y-%m-%d %H:%M:%S" (an optional fractional second is accepted) or "%Y-%m-%d" (read as midnight).

prices : list[float]

Trade prices.

volumes : list[float]

Trade sizes.

ticks_per_bar : int

Number of trades per bar; must be positive.

Returns

list[tuple[str, str, float, float, float, float, float, float, int]]

One (start_timestamp, timestamp, open, high, low, close, volume, dollar_value, tick_count) row per bar, where start_timestamp and timestamp are the times of the bar's first and last trade and dollar_value is the sum of price * volume.

Raises

ValueError

If ticks_per_bar is 0, the three lists differ in length, or a timestamp does not parse.

build_time_bars

build_time_bars(
    timestamps: Sequence[str],
    prices: Sequence[float],
    volumes: Sequence[float],
    interval_seconds: int,
) -> list[tuple[str, str, float, float, float, float, float, float, int]]

Build time bars from a trade stream (AFML section 2.3.1.1).

A bar closes on the first trade at least interval_seconds after the bar's first trade; that trade belongs to the closing bar. Bars are anchored to each bar's first trade, not to the wall clock. A trailing partial bar is emitted as a final, shorter bar. Trades must be in increasing time order; the input is not sorted or validated.

Parameters

timestamps : list[str]

Trade timestamps as "%Y-%m-%d %H:%M:%S" (an optional fractional second is accepted) or "%Y-%m-%d" (read as midnight).

prices : list[float]

Trade prices.

volumes : list[float]

Trade sizes.

interval_seconds : int

Bar length in seconds; must be positive.

Returns

list[tuple[str, str, float, float, float, float, float, float, int]]

One (start_timestamp, timestamp, open, high, low, close, volume, dollar_value, tick_count) row per bar, where start_timestamp and timestamp are the times of the bar's first and last trade and dollar_value is the sum of price * volume.

Raises

ValueError

If interval_seconds <= 0, the three lists differ in length, or a timestamp does not parse.

build_volume_bars

build_volume_bars(
    timestamps: Sequence[str],
    prices: Sequence[float],
    volumes: Sequence[float],
    volume_per_bar: float,
) -> list[tuple[str, str, float, float, float, float, float, float, int]]

Build volume bars that close when cumulative volume reaches a threshold.

AFML section 2.3.1.3. The trade that crosses the threshold belongs to the bar it closes, so bars overshoot. A trailing partial bar is dropped. Trades must be in increasing time order; the input is not sorted or validated.

Parameters

timestamps : list[str]

Trade timestamps as "%Y-%m-%d %H:%M:%S" (an optional fractional second is accepted) or "%Y-%m-%d" (read as midnight).

prices : list[float]

Trade prices.

volumes : list[float]

Trade sizes.

volume_per_bar : float

Cumulative volume that closes a bar; must be positive and finite.

Returns

list[tuple[str, str, float, float, float, float, float, float, int]]

One (start_timestamp, timestamp, open, high, low, close, volume, dollar_value, tick_count) row per bar, where start_timestamp and timestamp are the times of the bar's first and last trade and dollar_value is the sum of price * volume.

Raises

ValueError

If volume_per_bar is not a positive finite number, the three lists differ in length, or a timestamp does not parse.