Skip to main content

No project description provided

Project description

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           │
└───────────┴────────────────┴───────────────┴─────────────────┴───────────────┘

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polars_fin-0.2.0.tar.gz (40.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

polars_fin-0.2.0-cp39-abi3-win_amd64.whl (4.2 MB view details)

Uploaded CPython 3.9+Windows x86-64

polars_fin-0.2.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.9 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

polars_fin-0.2.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.6 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

polars_fin-0.2.0-cp39-abi3-macosx_11_0_arm64.whl (4.2 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

polars_fin-0.2.0-cp39-abi3-macosx_10_12_x86_64.whl (4.5 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file polars_fin-0.2.0.tar.gz.

File metadata

  • Download URL: polars_fin-0.2.0.tar.gz
  • Upload date:
  • Size: 40.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.0

File hashes

Hashes for polars_fin-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5fc6b80014596b490e7b77f7b352fa74b997cf92566686169ec8d3e46f43563c
MD5 60b0a09a22a7fed2afb30f97c1d90a21
BLAKE2b-256 3d5b677232643a8c0db648290b57abb516df006cd891aeee2175e1edde0e4f14

See more details on using hashes here.

File details

Details for the file polars_fin-0.2.0-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for polars_fin-0.2.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 c69a8c9d35b13f89eafaea1b70e0aa2edd6d886af728218cf0bbf6eea739d17a
MD5 8388c4bc2d9165479d3af87f8bd5b8e1
BLAKE2b-256 a1146037c79f4392641270c4e63571ad29cfda8b29aa4b8d8027f2af896e67eb

See more details on using hashes here.

File details

Details for the file polars_fin-0.2.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for polars_fin-0.2.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3b640994114c51052c712aa18db87b68ba452d7e3321aefe8a1baefc5ee1ca63
MD5 f5e041aab3584ee07645118ff9cf6367
BLAKE2b-256 e395ce4b0cd9851438cbf6f7c0c68de7594b4d069d620af5335c07861e91a5f4

See more details on using hashes here.

File details

Details for the file polars_fin-0.2.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for polars_fin-0.2.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 f34bc7687c1779c1b86b9a5a3e42fa2612ab6179c180d9af7ae8a0c6f1545f55
MD5 d11608701f0665dd9b8f133cadb16408
BLAKE2b-256 680ec723f7fe7ac1965c937e3c703e9bc627c2027d10d58a7c1a01c5bfde0f5b

See more details on using hashes here.

File details

Details for the file polars_fin-0.2.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_fin-0.2.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9996b6864fe18088a1b1fa348dc5bbf638e4fc003c5657fb1ddd627a5868c991
MD5 03c43fa4cafdc91e976f84fe8cadd2bb
BLAKE2b-256 96c80328a1869b4c187d7a114b6a40ed5ae14cad28d11670e1d77caece732a79

See more details on using hashes here.

File details

Details for the file polars_fin-0.2.0-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for polars_fin-0.2.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8fc930a1ce52ec5d8da5d393d005a297eef409102b07c56e0c119e7a561d3751
MD5 f7dad3ce21f6c1417de2ac67381220eb
BLAKE2b-256 1a84ee6a9a361566db386a5ac3580bc4d45814bfc3cc7e0ef4c7da45fcae1b58

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page