Skip to main content

Standard financial calculations

Project description

finance calcs

Standard financial calculations

Build Status codecov License PyPI

Overview

finance-calcs provides financial calculations as composable Polars expressions. It is designed for lazy execution, namespace-style ergonomics, and direct interoperability with the rest of the finance-* stack.

The public API follows a few rules:

  • every expression metric accepts and returns pl.Expr
  • metrics are exposed once, with optional window= and period= controls rather than separate rolling, monthly, and annual variants
  • functions are also available through the .finance namespace on both pl.Expr and pl.Series
  • examples use synthetic but realistic fixtures from finance-datagen

Implemented coverage

Topic Functions
Returns and periods period_bucket, simple_returns, log_returns, cum_returns, cum_returns_final, returns, aggregate_returns, annualized_return, annualized_volatility
Risk and drawdown volatility, sharpe, sortino, calmar, downside_deviation, downside_risk, drawdown_series, underwater_series, max_drawdown, value_at_risk, conditional_value_at_risk, parametric_var
Technical indicators Moving averages, Bollinger/Donchian channels, momentum oscillators, range volatility, and volume indicators
Alpha and quantiles Forward returns, conditional/horizon IC, IC decay, IC summaries, quantile assignment, signal normalization, quantile returns, turnover, and long/short spreads
Factor and benchmark metrics Alpha, beta, up/down capture, batting average, tracking error, and information ratio
Distribution and tail risk Higher moments, Sharpe significance helpers, tail ratio, ulcer index, omega ratio, GPD VaR, and GPD CVaR
Portfolio and post-trade Exposure, concentration, active share, transaction costs/volume/attribution, slippage, turnover, round trips, MAE/MFE, and trade-quality metrics

See the Examples page for workflows with generated data and the API page for a complete grouped reference for every public function.

Quick start

Generate a deterministic daily equity path with finance-datagen, then compute return and risk metrics as Polars expressions.

import polars as pl
from finance_datagen import generate_prices

import finance_calcs as fc

prices = generate_prices(symbol="ACME", seed=7)

out = prices.with_columns(
    pl.col("price").finance.simple_returns().alias("ret"),
).select(
    fc.returns(pl.col("ret")).alias("total_return"),
    pl.col("ret").finance.annualized_return().alias("ann_return"),
    pl.col("ret").finance.volatility().alias("ann_vol"),
    pl.col("ret").finance.sharpe().alias("sharpe"),
    pl.col("ret").finance.max_drawdown().alias("max_drawdown"),
)

Use finance-datagen.ohlc_from_close when calculations need OHLCV bars:

from finance_datagen import ohlc_from_close

bars = ohlc_from_close(prices["price"], symbol="ACME", seed=7)

features = bars.with_columns(
    pl.col("close").finance.sma(20).alias("sma_20"),
    pl.col("close").finance.rsi(14).alias("rsi_14"),
    fc.atr(pl.col("high"), pl.col("low"), pl.col("close")).alias("atr_14"),
    fc.obv(pl.col("close"), pl.col("volume")).alias("obv"),
)

Period and frequency slices

Use period= for calendar-style slices and keep window= for rolling row-count windows. A period can be a finance_enums.Frequency, any alias accepted by finance_enums.to_frequency(), any Polars dt.truncate() duration string, or a precomputed bucket expression.

import polars as pl
from finance_enums import Frequency

monthly = prices.with_columns(
    pl.col("price").finance.simple_returns().alias("ret"),
).with_columns(
    fc.period_bucket(pl.col("timestamp"), Frequency.Month).alias("month"),
    pl.col("ret").finance.returns(period="month", date=pl.col("timestamp")).alias("month_return"),
    pl.col("ret").finance.sharpe(period="1q", date=pl.col("timestamp")).alias("quarter_sharpe"),
)

For fiscal periods, strategy regimes, or exchange-calendar grids built upstream, pass the bucket expression directly:

bucketed = prices.with_columns(
    pl.col("price").finance.simple_returns().alias("ret"),
    pl.col("timestamp").dt.year().alias("fiscal_year"),
).with_columns(
    fc.returns(pl.col("ret"), period=pl.col("fiscal_year")).alias("fiscal_return"),
)

Stack integration

finance-calcs is intended to pair with:

  • finance-datagen for synthetic fixtures and test inputs
  • finance-dates for calendar-aware date handling upstream
  • finance-enums for shared enum-backed trading semantics upstream

That keeps calculations focused on typed expressions instead of schema cleanup, string parsing, or calendar repair.

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

finance_calcs-0.1.1.tar.gz (42.9 kB view details)

Uploaded Source

Built Distributions

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

finance_calcs-0.1.1-cp310-abi3-win_amd64.whl (140.3 kB view details)

Uploaded CPython 3.10+Windows x86-64

finance_calcs-0.1.1-cp310-abi3-manylinux_2_28_x86_64.whl (257.0 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.28+ x86-64

finance_calcs-0.1.1-cp310-abi3-macosx_11_0_arm64.whl (234.0 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file finance_calcs-0.1.1.tar.gz.

File metadata

  • Download URL: finance_calcs-0.1.1.tar.gz
  • Upload date:
  • Size: 42.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for finance_calcs-0.1.1.tar.gz
Algorithm Hash digest
SHA256 00860b7c34d2179a8accb499352374b4048a55e2dbf91fad3601de808be3fff4
MD5 9b23a1f947d1569f3d6d8a48cb15f8e1
BLAKE2b-256 770ca6dcfdd9713962f1eff23d294700bdb46b16963039cb718e0e787c90c758

See more details on using hashes here.

File details

Details for the file finance_calcs-0.1.1-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for finance_calcs-0.1.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 2369faa4eac48e9b29ea1cde0aee5a055c430863351a64a527e39d7495860f14
MD5 f7bf951ea76065489ff057608245c218
BLAKE2b-256 d2a33c25199fae86c5b18958299f868369dd5d40137a17d378349b556f60836e

See more details on using hashes here.

File details

Details for the file finance_calcs-0.1.1-cp310-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for finance_calcs-0.1.1-cp310-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ab4f1115efdfb1e8ed2de098e8b71d33482fc2bf80a2683260cd78aa6290743d
MD5 1ba702deb4f38290b3c9412a128b799b
BLAKE2b-256 6c75f74ad4fd3bea3bc5a35e8ac4ced6a225c9bd35e46b84dc6c5872d6fed3ac

See more details on using hashes here.

File details

Details for the file finance_calcs-0.1.1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for finance_calcs-0.1.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0962dc68920865876aa78a7dff8d46485aebd44e00c5c4e17188d627711d9d27
MD5 f6415eaeb0e8c8e4f03b5156d45cb69e
BLAKE2b-256 a02c14e6de71d38939ab414c4b5813b50b67c9dc2bd249b944d11993162171f0

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