No project description provided
Project description
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
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file polars_pbv-0.1.0.tar.gz
.
File metadata
- Download URL: polars_pbv-0.1.0.tar.gz
- Upload date:
- Size: 3.6 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f333b203afe7cd0487c46ee93ad79fdcc290912cabb0b7e25d3b20b75a42bb28 |
|
MD5 | b95eaccad2e455354da620413c4d04f1 |
|
BLAKE2b-256 | cb6c221a010cbfba3d602bf22f300b114d412c9a9ed6b9e345a3c41bed14f5a4 |
File details
Details for the file polars_pbv-0.1.0-cp38-abi3-win_amd64.whl
.
File metadata
- Download URL: polars_pbv-0.1.0-cp38-abi3-win_amd64.whl
- Upload date:
- Size: 3.3 MB
- Tags: CPython 3.8+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf2513eb8b950fa328bd5289ad38dd8741f8e513795eb16002678de14304ee28 |
|
MD5 | 298aaf808e654810b097b026934f95da |
|
BLAKE2b-256 | 1e3211081d1c4639717ea7fa00d91a61ec749ea0ae04a81d91d4c844fd214c98 |
File details
Details for the file polars_pbv-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: polars_pbv-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 6.9 MB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8f8a10894eef00c80549b27f1dadb49f27c4fa268af926840741573ec9304df |
|
MD5 | 61aff15dd69d15947db80d942767dfa7 |
|
BLAKE2b-256 | 48b1f5046ae91c0f036d9ee1bad1a20ca4ac5cf7102d8eb49e4a797f29985f98 |
File details
Details for the file polars_pbv-0.1.0-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
.
File metadata
- Download URL: polars_pbv-0.1.0-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
- Upload date:
- Size: 7.0 MB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ i686
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b06390330b022816f63d27260272efa846c8d7f73798ccb46df3a1fdf20604ea |
|
MD5 | f3a69548b4d69416006a87dbe5dbb8bf |
|
BLAKE2b-256 | be818b5da2e709a7ca77b88a5ef5f99266dd5a879476ec38cc7038bcc44e1850 |
File details
Details for the file polars_pbv-0.1.0-cp38-abi3-macosx_11_0_arm64.whl
.
File metadata
- Download URL: polars_pbv-0.1.0-cp38-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 3.4 MB
- Tags: CPython 3.8+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e07ced73e53846cb2f444d90dd3ab1b4578bfe106437505b4f2d451b27f705d |
|
MD5 | 58de50ff976b8b2c0e3ab121ceb838b9 |
|
BLAKE2b-256 | 3cd9582c4f470c8132467b7077f527b825449d693ac7d007699e7388ff2baac5 |
File details
Details for the file polars_pbv-0.1.0-cp38-abi3-macosx_10_12_x86_64.whl
.
File metadata
- Download URL: polars_pbv-0.1.0-cp38-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 3.6 MB
- Tags: CPython 3.8+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 286bb67e02c6cd02c59d39ef6d8e3547a33108040448cbe15ab26eb660683fcb |
|
MD5 | 06cd987a63186084313e3bb89bde25ea |
|
BLAKE2b-256 | 08c7498a1816193528ec2dc4474c52c5b027e7e2e5d882e3a7d2806059aab778 |