Skip to main content

Quantitative finance pricing library

Project description

qFin

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.0.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.0-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyqfin-1.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 55ce60be9c02c362a7b134dfcd69fdf9fef856df1ee6a0b3329ba3a590a78eb3
MD5 e8a936c4d444c44c699a45e85d4a9e9b
BLAKE2b-256 26fbb9c4313d62da28ce218fdf2056ee6329b9aaab1fad0a3bc5d67bf1084d30

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyqfin-1.0.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 666ec912c018f885d3e51e3fc04eb0669ae38f3f810936f34c23e1c0718f66db
MD5 adf25ae65dabb593e4358388531e171b
BLAKE2b-256 142edc50846e68cf23368b8dc34f14be7e783d700ee613a629a46ad2b5c0aade

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