Skip to main content

Quantitative finance pricing library

Project description

pyqfin

A professional, vectorised quantitative finance library for pricing options, forwards, futures, and multi-asset derivatives.

Features

  • Dictionary-based Quick API (Pricer): Easily price single instruments and portfolios.
  • Multiple Engines: Black-Scholes (analytical), Monte Carlo, Heston (stochastic vol), and Cox-Ross-Rubinstein Binomial Trees.
  • Exotics & Multi-Asset: Support for Asian, Barrier, Basket, Rainbow, and Spread options with Cholesky decompositions for correlated assets.
  • American Options: Early exercise evaluation using Binomial Trees.
  • Risk Management: Finite-difference Greeks calculation and implied volatility root-finding (Newton-Raphson, Bisection).
  • Term Structure: Interpolated and flat yield curves.

Installation

pip install pyqfin

Quick Start

Single Option Pricing

The Pricer handles all the internal wiring (engines, yield curves, payoff classes).

from pyqfin import Pricer

# Vanilla Call (defaults to analytical BSM)
result = Pricer({
    'type': 'vanilla',
    'option_type': 'c',
    'S': 100, 'K': 105, 'T': 1.0, 
    'vol': 0.20, 'r': 0.05,
    'greeks': True
}).run()

print(f"Price: {result.price:.4f}")
print(f"Delta: {result.greeks['delta']:.4f}")

American Options

Automatically utilizes the binomial tree engine:

# American Put
result = Pricer({
    'type': 'american',
    'option_type': 'p',
    'S': 100, 'K': 105, 'T': 1.0, 
    'vol': 0.20, 'r': 0.05,
    'n_steps': 500  # Tree depth
}).run()
print(f"American Premium Price: {result.price:.4f}")

Multi-Asset Basket Option

Provide arrays for S and vol, and a correlation matrix. The library automatically uses Cholesky-based Monte Carlo.

import numpy as np

corr_matrix = np.array([
    [1.0, 0.6, 0.3], 
    [0.6, 1.0, 0.5], 
    [0.3, 0.5, 1.0]
])

result = Pricer({
    'type': 'basket',
    'option_type': 'c',
    'S': [100, 110, 90],
    'K': 100, 'T': 1.0,
    'vol': [0.20, 0.25, 0.30],
    'corr': corr_matrix,
    'weights': [1/3, 1/3, 1/3],
    'r': 0.05,
    'n_paths': 50000
}).run()

Portfolio Pricing

Pass a list of configurations (must include 'quantity') to price an entire book at once and aggregate risks.

portfolio = Pricer.portfolio([
    {'type': 'vanilla', 'option_type': 'c', 'S': 100, 'K': 105, 'T': 1.0, 'vol': 0.20, 'r': 0.05, 'quantity': 10},
    {'type': 'american', 'option_type': 'p', 'S': 100, 'K': 90, 'T': 0.5, 'vol': 0.25, 'r': 0.05, 'quantity': -5},
], greeks=True)

print(f"Total Portfolio Value: {portfolio.total_value:.2f}")
print(f"Net Delta: {portfolio.total_greeks['delta']:.2f}")

Heston Model (Stochastic Volatility)

If you select the heston engine, provide the specific variance parameters instead of standard volatility.

result = Pricer({
    'type': 'vanilla',
    'option_type': 'c',
    'S': 100, 'K': 105, 'T': 1.0, 'r': 0.05,
    'engine': 'heston',
    'v0': 0.04,        # initial variance
    'kappa': 2.0,      # mean-reversion speed
    'theta': 0.04,     # long-run variance
    'xi': 0.3,         # vol-of-vol
    'rho_heston': -0.7 # correlation
}).run()

Running Tests

If you cloned the repository and want to run the tests locally:

pip install pyqfin[dev]
pytest tests/ -v

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

pyqfin-1.0.1.tar.gz (30.0 kB view details)

Uploaded Source

Built Distribution

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

pyqfin-1.0.1-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

File details

Details for the file pyqfin-1.0.1.tar.gz.

File metadata

  • Download URL: pyqfin-1.0.1.tar.gz
  • Upload date:
  • Size: 30.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for pyqfin-1.0.1.tar.gz
Algorithm Hash digest
SHA256 89f3af379c3a0108c65154bcd84810daf6e1490cfcede2729a9c6a73d9713a96
MD5 8ca78ab734fb1c072a63d1996afcf244
BLAKE2b-256 14d4ed48bcec01a2f70648ae076391f86ba1d0897b5b43d51aa921b5c958d0ea

See more details on using hashes here.

File details

Details for the file pyqfin-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pyqfin-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 27.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for pyqfin-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8e87748bcab312c100d716964e9831a6283205b81c9be6a5425efb1e801e01da
MD5 24b6a640e6e74a2511d10bb571c93799
BLAKE2b-256 4f7bc6cb1fe624278b00d558727c3c38257b9ce2d3835c3dc7eff43891f36521

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