Standard financial calculations
Project description
finance calcs
Standard financial calculations
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=andperiod=controls rather than separate rolling, monthly, and annual variants - functions are also available through the
.financenamespace on bothpl.Exprandpl.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, IC metrics, IC summaries, quantile assignment, signal normalization, 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 notional, transaction cost, slippage, and turnover |
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-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.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file finance_calcs-0.1.0.tar.gz.
File metadata
- Download URL: finance_calcs-0.1.0.tar.gz
- Upload date:
- Size: 37.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea413b8c25004fc417d1aa6201d7a4b8554fc835cb8d50d72f0d1dc77151093f
|
|
| MD5 |
cc5f14059a15c0efbd3a63b9aef16c55
|
|
| BLAKE2b-256 |
ac8d25cfcbbde1552b943b96d74973247ecd5a65be9b4291394c9a88c8c4171e
|
File details
Details for the file finance_calcs-0.1.0-cp310-abi3-win_amd64.whl.
File metadata
- Download URL: finance_calcs-0.1.0-cp310-abi3-win_amd64.whl
- Upload date:
- Size: 135.0 kB
- Tags: CPython 3.10+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6dea3d8806c4e9be8278e5ef0e4128ecc12131c7abdf835fe425357a441e052d
|
|
| MD5 |
e868e1816943dcde4b9ed1c513986a6c
|
|
| BLAKE2b-256 |
9060ad6218e54f56b86142d675ca02a35356e99f1cdf3d21185c52d040518380
|
File details
Details for the file finance_calcs-0.1.0-cp310-abi3-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: finance_calcs-0.1.0-cp310-abi3-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 251.7 kB
- Tags: CPython 3.10+, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08e16540bc06533980e4ffdc41d60534726956b7ce7e37ebb35931e0fa96daa4
|
|
| MD5 |
767d98e132bab5a5ff2ac16341af0e90
|
|
| BLAKE2b-256 |
5ce8b42b0ac70d4891a1e1db00ea9a1d505d000a30af3d72298287ad3341f1f0
|
File details
Details for the file finance_calcs-0.1.0-cp310-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: finance_calcs-0.1.0-cp310-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 228.7 kB
- Tags: CPython 3.10+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89ce12b8cc87813016263157fe2f6a33656b4cdd4558f80cc2c3748a7677430d
|
|
| MD5 |
45810ab09e51edf099fdf222e7e508a1
|
|
| BLAKE2b-256 |
ea7e1e2361b0af4b9e9bdf930e6900d666ea585b37ce235d3db6b6bd916c870e
|