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
.fcalcsnamespace 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, 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.
=
=
Use finance-datagen.ohlc_from_close when calculations need OHLCV bars:
=
=
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.
=
For fiscal periods, strategy regimes, or exchange-calendar grids built upstream, pass the bucket expression directly:
=
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.