Skip to main content

polars_pbv: Polars Price by Volume (PBV) Extension

The Polars Price by Volume (PBV) extension is a powerful tool for analyzing and visualizing financial data. This extension provides various functions to calculate and visualize the price by volume (PBV) data using the Polars DataFrame library.

Installation

To use the Polars PBV extension, you need to install the following packages:

•	polars
•	polars_pbv

You can install these packages using pip:

pip install polars polars_pbv

If you want to visualize the pbv chart, you need to install the following packages:

•	hvplot
•	holoviews
•	panel

You can install these packages using pip:

pip install hvplot holoviews panel

PBV Extension Functions

The PBV extension provides several functions:

1.	pbv
2.	pbv_pct
3.	pbv_topn_vp
4.	pbv_topn_v

Function Definitions

pbv

Calculates the price by volume over a specified window size and bins.

def pbv(
    price: IntoExpr,
    volume: IntoExpr,
    window_size: int,
    bins: int,
    center: bool = True,
    round: int = -1,
) -> pl.Expr:

Return type will be the struct contain the key with price and volume and the value both list

pbv_pct

Calculates the percentage price by volume over a specified window size and bins.

def pbv_pct(
    price: IntoExpr,
    volume: IntoExpr,
    window_size: int,
    bins: int,
    center: bool = True,
    round: int = -1,
) -> pl.Expr:

Same as above return type with the volume norm as percentage.

pbv_topn_vp

Calculates the top N price by volume over a specified window size and bins.

def pbv_topn_vp(
    price: IntoExpr,
    volume: IntoExpr,
    window_size: int,
    bins: int,
    n: int,
    center: bool = True,
    round: int = -1,
) -> pl.Expr:

Return will be list of price.

pbv_topn_v

Calculates the top N price by volume over a specified window size and bins, with an option to calculate percentages.

def pbv_topn_v(
    price: IntoExpr,
    volume: IntoExpr,
    window_size: int,
    bins: int,
    n: int,
    center: bool = True,
    round: int = -1,
    pct: bool = False,
) -> pl.Expr:

Return will be list of volume or volume norm as pct.

Usage Example

demo

demo

Below is an example of how to use the Polars PBV extension. This example demonstrates how to calculate the PBV data and visualize it using hvplot and holoviews.

import numpy as np
import polars as pl
import polars_pbv as pl_pbv
import hvplot.pandas
import holoviews as hv
import panel as pn

# Initialize Panel extension
pn.extension()

# Define parameters
n = 1000
w = 120
b = 20

# Create a Polars DataFrame with example data
df = pl.DataFrame({
    "index": np.arange(0, n),
    "price": np.random.normal(size=n),
    "volume": np.random.randint(0, 1000, n),
}).with_columns(
    pl.col("price").cum_sum()
).with_columns(
    pl_pbv.pbv_pct("price", "volume", w, b, center=False).alias("pbv")
)

# Display the widget and the plot
i_slider = pn.widgets.IntSlider(name='i', start=0, end=n-w, step=1, value=0)

def plot_pbv(i):
    print(f"use {i}")
    df_pbv = df.slice(i+w, 1).select(
        pl.col("pbv").struct.field("price").list.explode(),
        pl.col("pbv").struct.field("volume").list.explode() * n,
    )
    pbv_data = df_pbv.to_dicts()
    step = pbv_data[1]["price"] - pbv_data[0]["price"]
    pbv_v_max = df_pbv["volume"].max()
    return ((df.slice(i, w).plot.line(x="index", y="price", height=400) * 
      hv.Rectangles([[i, r["price"], i + r["volume"], r["price"] + step] for r in pbv_data], ).opts(alpha=0.5)
     ) + df.slice(i, w).plot.step(x="index", y="volume", height=200)).cols(1)

interactive_plot = pn.bind(plot_pbv, i=i_slider)

# Create a dashboard with the slider and the plot
dashboard = pn.Column(i_slider, interactive_plot)
dashboard.servable()

# If running in a Jupyter notebook or JupyterLab
dashboard

This example demonstrates how to create a Polars DataFrame, calculate the PBV data, and visualize it using hvPlot and HoloViews. The interactive slider allows you to dynamically update the plot by changing the value of i.

Metadata

Release files for polars-pbv 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for polars-pbv 0.1.0
File Size Uploaded
polars_pbv-0.1.0.tar.gz 3.6 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for polars-pbv 0.1.0
File
polars_pbv-0.1.0-cp38-abi3-win_amd64.whl CPython 3.8 abi3 Windows x86-64 Details
polars_pbv-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.8 abi3 Linux glibc 2.17+ x86-64 Details
polars_pbv-0.1.0-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl CPython 3.8 abi3 Linux glibc 2.17+ x86-32 Details
polars_pbv-0.1.0-cp38-abi3-macosx_11_0_arm64.whl CPython 3.8 abi3 macOS 11.0+ ARM64 Details
polars_pbv-0.1.0-cp38-abi3-macosx_10_12_x86_64.whl CPython 3.8 abi3 macOS 10.12+ x86-64 Details

Total release size: 27.8 MB

Release files / polars_pbv-0.1.0.tar.gz

Download URL polars_pbv-0.1.0.tar.gz
Size 3.6 MB
Tags Source
SHA-256 checksum
How to use checksums
f333b203afe7cd0487c46ee93ad79fdcc290912cabb0b7e25d3b20b75a42bb28
BLAKE2b-256 checksum
How to use checksums
cb6c221a010cbfba3d602bf22f300b114d412c9a9ed6b9e345a3c41bed14f5a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.6.0

Release files / polars_pbv-0.1.0-cp38-abi3-win_amd64.whl

Download URL polars_pbv-0.1.0-cp38-abi3-win_amd64.whl
Size 3.3 MB
Tags CPython 3.8 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
bf2513eb8b950fa328bd5289ad38dd8741f8e513795eb16002678de14304ee28
BLAKE2b-256 checksum
How to use checksums
1e3211081d1c4639717ea7fa00d91a61ec749ea0ae04a81d91d4c844fd214c98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.6.0

Release files / polars_pbv-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL polars_pbv-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 6.9 MB
Tags CPython 3.8 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
d8f8a10894eef00c80549b27f1dadb49f27c4fa268af926840741573ec9304df
BLAKE2b-256 checksum
How to use checksums
48b1f5046ae91c0f036d9ee1bad1a20ca4ac5cf7102d8eb49e4a797f29985f98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.6.0

Release files / polars_pbv-0.1.0-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL polars_pbv-0.1.0-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Size 7.0 MB
Tags CPython 3.8 Linux glibc 2.17+ x86-32 abi3
SHA-256 checksum
How to use checksums
b06390330b022816f63d27260272efa846c8d7f73798ccb46df3a1fdf20604ea
BLAKE2b-256 checksum
How to use checksums
be818b5da2e709a7ca77b88a5ef5f99266dd5a879476ec38cc7038bcc44e1850
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.6.0

Release files / polars_pbv-0.1.0-cp38-abi3-macosx_11_0_arm64.whl

Download URL polars_pbv-0.1.0-cp38-abi3-macosx_11_0_arm64.whl
Size 3.4 MB
Tags CPython 3.8 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
3e07ced73e53846cb2f444d90dd3ab1b4578bfe106437505b4f2d451b27f705d
BLAKE2b-256 checksum
How to use checksums
3cd9582c4f470c8132467b7077f527b825449d693ac7d007699e7388ff2baac5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.6.0

Release files / polars_pbv-0.1.0-cp38-abi3-macosx_10_12_x86_64.whl

Download URL polars_pbv-0.1.0-cp38-abi3-macosx_10_12_x86_64.whl
Size 3.6 MB
Tags CPython 3.8 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
286bb67e02c6cd02c59d39ef6d8e3547a33108040448cbe15ab26eb660683fcb
BLAKE2b-256 checksum
How to use checksums
08c7498a1816193528ec2dc4474c52c5b027e7e2e5d882e3a7d2806059aab778
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.6.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

6 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page