Skip to main content

polars-fin

PyPI Changelog License

A plugin for polars to efficiently calculate financial metrics.

To install the plugin, run pip install polars-fin.

Capital Gains Calculation

Currently, the only function in the plugin cap_gains(...) is for calculating capital gains based on the average cost basis of a security.

Usage

The cap_gains function calculates capital gains using the average cost method. It takes three parameters:

  • ttype: Transaction type ("buy" for buying logic, "sell" for selling logic, other values are ignored)
  • qty: Quantity of the transaction
  • amt: Total amount of the transaction

Returns a Polars Struct with fields for cumulative quantity, average cost, unit cost, cost of units sold, and realized gain. Use unnest() to flatten into separate columns.

import polars as pl
from datetime import date
import polars_fin as pf

data = {
    "date": [
        date(2023, 1, 10),
        date(2023, 2, 15),
        date(2023, 3, 20),
        date(2023, 4, 5),
        date(2023, 5, 5),
        date(2023, 6, 1),
    ],
    "security": ["XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ"],
    "type": ["buy", "buy", "sell", "dividend", "sell", "buy"],
    "quantity": [100, 50, 30, 0, 120, 100],
    "transaction_value": [1000.0, 600.0, 450.0, 12, 960, 700.0],
}
df = pl.DataFrame(data)
result = df.with_columns(
    pf.cap_gains("type", "quantity", "transaction_value").alias("cap_gains")
).select("cap_gains").unnest("cap_gains")
result

Output:

shape: (6, 5)
┌───────────┬────────────────┬───────────────┬─────────────────┬───────────────┐
│ cumul_qty ┆ cumul_avg_cost ┆ avg_unit_cost ┆ cost_units_sold ┆ realized_gain │
│ ---       ┆ ---            ┆ ---           ┆ ---             ┆ ---           │
│ f64       ┆ f64            ┆ f64           ┆ f64             ┆ f64           │
╞═══════════╪════════════════╪═══════════════╪═════════════════╪═══════════════╡
│ 100.0     ┆ 1000.0         ┆ 10.0          ┆ 0.0             ┆ 0.0           │
│ 150.0     ┆ 1600.0         ┆ 10.666667     ┆ 0.0             ┆ 0.0           │
│ 120.0     ┆ 1280.0         ┆ 10.666667     ┆ 320.0           ┆ 130.0         │
│ 120.0     ┆ 1280.0         ┆ 10.666667     ┆ 0.0             ┆ 0.0           │
│ 0.0       ┆ 0.0            ┆ 0.0           ┆ 1280.0          ┆ -320.0        │
│ 100.0     ┆ 700.0          ┆ 7.0           ┆ 0.0             ┆ 0.0           │
└───────────┴────────────────┴───────────────┴─────────────────┴───────────────┘

Metadata

Release files for polars-fin 0.2.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-fin 0.2.0
File Size Uploaded
polars_fin-0.2.0.tar.gz 40.8 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for polars-fin 0.2.0
File
polars_fin-0.2.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details
polars_fin-0.2.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details
polars_fin-0.2.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details
polars_fin-0.2.0-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details
polars_fin-0.2.0-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details

Total release size: 22.4 MB

Release files / polars_fin-0.2.0.tar.gz

Download URL polars_fin-0.2.0.tar.gz
Size 40.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5fc6b80014596b490e7b77f7b352fa74b997cf92566686169ec8d3e46f43563c
BLAKE2b-256 checksum
How to use checksums
3d5b677232643a8c0db648290b57abb516df006cd891aeee2175e1edde0e4f14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.9.0

Release files / polars_fin-0.2.0-cp39-abi3-win_amd64.whl

Download URL polars_fin-0.2.0-cp39-abi3-win_amd64.whl
Size 4.2 MB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
c69a8c9d35b13f89eafaea1b70e0aa2edd6d886af728218cf0bbf6eea739d17a
BLAKE2b-256 checksum
How to use checksums
a1146037c79f4392641270c4e63571ad29cfda8b29aa4b8d8027f2af896e67eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.9.0

Release files / polars_fin-0.2.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL polars_fin-0.2.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 4.9 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
3b640994114c51052c712aa18db87b68ba452d7e3321aefe8a1baefc5ee1ca63
BLAKE2b-256 checksum
How to use checksums
e395ce4b0cd9851438cbf6f7c0c68de7594b4d069d620af5335c07861e91a5f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.9.0

Release files / polars_fin-0.2.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL polars_fin-0.2.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 4.6 MB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
f34bc7687c1779c1b86b9a5a3e42fa2612ab6179c180d9af7ae8a0c6f1545f55
BLAKE2b-256 checksum
How to use checksums
680ec723f7fe7ac1965c937e3c703e9bc627c2027d10d58a7c1a01c5bfde0f5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.9.0

Release files / polars_fin-0.2.0-cp39-abi3-macosx_11_0_arm64.whl

Download URL polars_fin-0.2.0-cp39-abi3-macosx_11_0_arm64.whl
Size 4.2 MB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
9996b6864fe18088a1b1fa348dc5bbf638e4fc003c5657fb1ddd627a5868c991
BLAKE2b-256 checksum
How to use checksums
96c80328a1869b4c187d7a114b6a40ed5ae14cad28d11670e1d77caece732a79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.9.0

Release files / polars_fin-0.2.0-cp39-abi3-macosx_10_12_x86_64.whl

Download URL polars_fin-0.2.0-cp39-abi3-macosx_10_12_x86_64.whl
Size 4.5 MB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
8fc930a1ce52ec5d8da5d393d005a297eef409102b07c56e0c119e7a561d3751
BLAKE2b-256 checksum
How to use checksums
1a84ee6a9a361566db386a5ac3580bc4d45814bfc3cc7e0ef4c7da45fcae1b58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.9.0

Release history Release notifications | RSS feed

This release

0.2.0 This release

6 release files

0.1.0

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