finance calcs
Standard financial calculations
Overview
finance-calcs provides composable Polars expression metrics plus a smaller
set of explicitly eager statistical, preprocessing, post-trade, and native
kernel helpers. It is designed for lazy execution where the algorithm permits
it, 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=andperiod=controls rather than separate rolling, monthly, and annual variants - functions are also available through the
.fcalcsnamespace on bothpl.Exprandpl.Series - examples use synthetic but realistic fixtures from
finance-datagen
Return, risk, and tail expression metrics accept periodic returns rather than
prices. Convert prices first with simple_returns or log_returns. These
metrics treat floating-point NaN and Polars null values as missing. Compound
returns treat missing observations as neutral, while statistical aggregations
exclude them.
Materializing helpers are not available through .fcalcs: native ADX, SAR,
and GARCH kernels accept numeric sequences and return NumPy arrays; GPD fits
and several statistical helpers accept pl.Series; preprocessing and
post-trade summaries accept concrete pl.DataFrame inputs.
Implemented coverage
| Topic | Functions |
|---|---|
| Returns and periods | period_bucket, simple_returns, log_returns, cumulative_returns, cumulative_return, annualized_return, annualized_volatility |
| Risk and drawdown | sharpe, sortino, calmar, downside_deviation, drawdown_series, max_drawdown, drawdown_details, and historical, parametric, and generalized-Pareto tail risk |
| Report metrics | Best/worst returns, average wins/losses, gain-to-pain, recovery factor, and Kelly criterion |
| 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, benchmark R-squared, 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").fcalcs.simple_returns().alias("ret"),
).select(
fc.cumulative_return(pl.col("ret")).alias("total_return"),
pl.col("ret").fcalcs.annualized_return().alias("annualized_return"),
pl.col("ret").fcalcs.annualized_volatility().alias("annualized_volatility"),
pl.col("ret").fcalcs.sharpe().alias("sharpe"),
pl.col("ret").fcalcs.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").fcalcs.sma(20).alias("sma_20"),
pl.col("close").fcalcs.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").fcalcs.simple_returns().alias("ret"),
).with_columns(
fc.period_bucket(pl.col("timestamp"), Frequency.Month).alias("month"),
pl.col("ret").fcalcs.cumulative_return(period="month", date=pl.col("timestamp")).alias("month_return"),
pl.col("ret").fcalcs.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").fcalcs.simple_returns().alias("ret"),
pl.col("timestamp").dt.year().alias("fiscal_year"),
).with_columns(
fc.cumulative_return(pl.col("ret"), period=pl.col("fiscal_year")).alias("fiscal_return"),
)
Stack integration
finance-calcs is intended to pair with:
finance-datagenfor synthetic fixtures and test inputsfinance-datesfor calendar-aware date handling upstreamfinance-enumsfor shared enum-backed trading semantics upstream
That keeps calculations focused on typed expressions instead of schema cleanup, string parsing, or calendar repair.
Metadata
Release files for finance-calcs 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| finance_calcs-0.3.0.tar.gz | 54.3 kB | Details |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| finance_calcs-0.3.0-cp310-abi3-win_amd64.whl | CPython 3.10 | abi3 | Windows x86-64 | Details |
| finance_calcs-0.3.0-cp310-abi3-manylinux_2_28_x86_64.whl | CPython 3.10 | abi3 | Linux glibc 2.28+ x86-64 | Details |
| finance_calcs-0.3.0-cp310-abi3-macosx_11_0_arm64.whl | CPython 3.10 | abi3 | macOS 11.0+ ARM64 | Details |
Total release size: 700.5 kB
Release files / finance_calcs-0.3.0.tar.gz
| Download URL | finance_calcs-0.3.0.tar.gz |
|---|---|
| Size | 54.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c5502010f0121e780a77c91dd807bb64cb57508f67eaebc5cf57b34489f977a8
|
|
BLAKE2b-256 checksum How to use checksums |
cb7f53b0fb352bdbae2011b4d5e3ddd7e6691621d5e122fd93ddc4c5685147ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.13
|
Release files / finance_calcs-0.3.0-cp310-abi3-win_amd64.whl
| Download URL | finance_calcs-0.3.0-cp310-abi3-win_amd64.whl |
|---|---|
| Size | 147.4 kB |
| Tags | CPython 3.10 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
7490c26a753380ddacf2938aafdbb0726685306a57d781755e3bf8c74e755cd5
|
|
BLAKE2b-256 checksum How to use checksums |
c1c958480660d1409527adceebab7acdfcc067d072e9a3074abf043b5881adf3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.13
|
Release files / finance_calcs-0.3.0-cp310-abi3-manylinux_2_28_x86_64.whl
| Download URL | finance_calcs-0.3.0-cp310-abi3-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 257.9 kB |
| Tags | CPython 3.10 Linux glibc 2.28+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
dca482fda64592816c00b729183f3efc4d77ec3a1fe8f749f064f582cd1dbbbf
|
|
BLAKE2b-256 checksum How to use checksums |
f14b18641081888af803c388eee5cee29b6c5ab665f0c344998f4b6b8a9e0f97
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.13
|
Release files / finance_calcs-0.3.0-cp310-abi3-macosx_11_0_arm64.whl
| Download URL | finance_calcs-0.3.0-cp310-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 240.8 kB |
| Tags | CPython 3.10 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
d352dcad72314029ea64e28a27261b5257ea3e8e8aad1c183e10ba6d6a4ef4c9
|
|
BLAKE2b-256 checksum How to use checksums |
f6893f726d63b2eaa041099baba4f36009df1489fb285be81101062f85d414d7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.13
|