Python package designed for security returns analysis.
Project description
pyfinance
pyfinance is a Python package for investment management and analysis of security returns. It complements existing quantitative-finance packages such as pandas-datareader with a focused, batteries-included statistical toolkit.
Status
- Latest release: 2.0.0
- Python: 3.10, 3.11, 3.12, 3.13, 3.14
- License: MIT
Installation
uv add pyfinance
Or with pip:
pip install pyfinance
Modules
| Module | Description |
|---|---|
pyfinance.returns |
Statistical analysis of financial time series via the CAPM framework. TSeries / TFrame are Pandas subclasses that add performance statistics: annualized return/vol, Sharpe, Sortino, drawdown, capture ratios, alpha/beta, Information Ratio, and so on. |
pyfinance.ols |
Ordinary least-squares regression. OLS, RollingOLS (NumPy-backed), and PandasRollingOLS (Pandas-indexed wrapper). |
pyfinance.options |
Vectorized Black-Scholes-Merton valuation, Greeks, and implied volatility via BSM. Option strategies (Straddle, BullSpread, Butterfly, Condor, variants). |
pyfinance.general |
General-purpose computations: active share, amortization schedules, best-fit distribution, PCA on returns, portfolio simulation, tracking-error optimization, VIF. |
pyfinance.datasets |
A small set of dataset loaders: load_13f (SEC EDGAR), load_industries (Ken French), load_rates (FRED H.15), load_rf (3-month T-bill total-return series). |
pyfinance.utils |
Frequency conversion, rolling-window construction, one-hot encoding, random ticker/weights generation, availability reporting. |
Quick tutorial
TSeries — performance statistics
import numpy as np
import pandas as pd
from pyfinance import TSeries
rng = np.random.default_rng(444)
s = rng.standard_normal(400) / 100 + 0.0008
idx = pd.date_range(start="2016-01-01", periods=len(s))
ts = TSeries(s, index=idx)
ts.anlzd_ret() # annualized geometric return
ts.anlzd_stdev("D") # annualized stdev of returns
ts.max_drawdown() # worst peak-to-trough decline
ts.sharpe_ratio() # Sharpe, annualized
BSM — Black-Scholes-Merton options pricing
from pyfinance.options import BSM
op = BSM(S0=100, K=100, T=1, r=0.04, sigma=0.20)
op.value() # European call value
op.delta() # Greeks
op.implied_vol(10) # implied vol at a target price
# Vectorized across arrays of strikes.
import numpy as np
ops = BSM(S0=100, K=np.arange(100, 110), T=1, r=0.04, sigma=0.20)
ops.value()
OLS — regression
from pyfinance.ols import OLS
# y: 1-d array; x: 2-d array of explanatory variables.
model = OLS(y=y, x=x)
model.beta, model.alpha
model.rsq_adj, model.fstat, model.std_err
datasets.load_13f — SEC EDGAR 13F parser
SEC EDGAR requires a descriptive User-Agent
(policy). Supply one:
from pyfinance import datasets
url = (
"https://www.sec.gov/Archives/edgar/data/1040273/"
"000108514617001787/form13fInfoTable.xml"
)
df = datasets.load_13f(url, user_agent="Jane Doe (jane@example.com)")
Development
This repo is managed with uv:
git clone https://github.com/bsolomon1124/pyfinance
cd pyfinance
uv sync # install pinned runtime + dev deps
uv run pytest # run tests
uv run ruff check # lint
uv run ruff format # format
uv build # build sdist + wheel
Type checking uses ty:
uv run ty check
CI runs these across Python 3.10–3.14 on every push; see
.github/workflows/ci.yml.
Changelog
See CHANGELOG.md. Notably, pyfinance 2.0 dropped
load_factors, load_shiller, and load_retaildata because their
upstream sources had drifted past reasonable repair. See the 2.0
release notes for details and migration guidance.
License
MIT — see LICENSE.txt.
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 Distribution
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 pyfinance-2.0.0.tar.gz.
File metadata
- Download URL: pyfinance-2.0.0.tar.gz
- Upload date:
- Size: 75.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f582e431acdc5e3bd016df8338f2e41020907f7a59b08aebb9e12bfced415afa
|
|
| MD5 |
40b50f8977b1b0202a5b2f14b8c75e39
|
|
| BLAKE2b-256 |
8a5acf9f6d46ee7dc4506a035694dedaafe5f678a83bdbb4c3165c51d5fe7e93
|
Provenance
The following attestation bundles were made for pyfinance-2.0.0.tar.gz:
Publisher:
publish.yml on bsolomon1124/pyfinance
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyfinance-2.0.0.tar.gz -
Subject digest:
f582e431acdc5e3bd016df8338f2e41020907f7a59b08aebb9e12bfced415afa - Sigstore transparency entry: 1340081165
- Sigstore integration time:
-
Permalink:
bsolomon1124/pyfinance@9a8bd2fc22b8d9ea8f111256be4d0aa580e672a7 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/bsolomon1124
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9a8bd2fc22b8d9ea8f111256be4d0aa580e672a7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pyfinance-2.0.0-py3-none-any.whl.
File metadata
- Download URL: pyfinance-2.0.0-py3-none-any.whl
- Upload date:
- Size: 51.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ba23834f686203742db47ed8436ab2609bae94acded525072a9005cd54c8f99
|
|
| MD5 |
6faaa42bb9c6a929293ae3f336a32ed5
|
|
| BLAKE2b-256 |
836a2e13764b3555d023fc679d3548b4a84868b184231346ccde9d4c051ac5c8
|
Provenance
The following attestation bundles were made for pyfinance-2.0.0-py3-none-any.whl:
Publisher:
publish.yml on bsolomon1124/pyfinance
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyfinance-2.0.0-py3-none-any.whl -
Subject digest:
9ba23834f686203742db47ed8436ab2609bae94acded525072a9005cd54c8f99 - Sigstore transparency entry: 1340081167
- Sigstore integration time:
-
Permalink:
bsolomon1124/pyfinance@9a8bd2fc22b8d9ea8f111256be4d0aa580e672a7 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/bsolomon1124
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9a8bd2fc22b8d9ea8f111256be4d0aa580e672a7 -
Trigger Event:
push
-
Statement type: