Skip to main content

Python library for calculating option prices, implied volatility and greeks.

Project description

vollib

vollib is a python library for calculating option prices, implied volatility and greeks. At its core is Peter Jaeckel's source code for LetsBeRational, an extremely fast and accurate algorithm for obtaining Black's implied volatility from option prices.

Building on this solid foundation, vollib provides functions to calculate option prices, implied volatility and greeks using Black, Black-Scholes, and Black-Scholes-Merton. vollib implements both analytical and numerical greeks for each of the three pricing formulae.

Input domain

Strike prices (K) must be strictly positive for the Black, Black-Scholes, and Black-Scholes-Merton pricing functions. A zero strike call has a well-defined mathematical limit, but vollib does not special-case that boundary; it preserves the domain and behavior of the underlying LetsBeRational implementation. If an application wants to support K = 0, handle that boundary before calling vollib.

This domain requirement applies equally to calls and puts:

Strike input Calls Puts
K = 0 raises ZeroDivisionError raises ZeroDivisionError
K < 0 raises ValueError raises ValueError

Installation

vollib 1.0.11 supports Python 3.9 through 3.13.

pip install vollib

Package history

The original vollib 0.1.x package was the Python 2.7/C-SWIG implementation. It remains available on PyPI for pinned legacy installs, but it is deprecated.

The py_vollib 1.0.x package name was introduced for the pure-Python implementation with Python 3 support. Starting with vollib 1.0.7, the canonical package name is again vollib.

Migration from py_vollib

The canonical Python package is now vollib. Existing py_vollib.* imports remain available as a compatibility namespace for this transition release, but they are deprecated. New code should import from vollib:

from vollib.black_scholes import black_scholes

Existing code like this will continue to work for now:

from py_vollib.black_scholes import black_scholes

The temporary vollib-test package was a bridge while the official package release path was being repaired. It is superseded by the official vollib package.

Implied volatility from option price

vollib can calculate implied volatility from a known option price. For example, using Black-Scholes:

from vollib.black_scholes import black_scholes
from vollib.black_scholes.implied_volatility import implied_volatility

flag = "c"
S = 100
K = 100
t = 0.5
r = 0.01
sigma = 0.2

price = black_scholes(flag, S, K, t, r, sigma)
iv = implied_volatility(price, S, K, t, r, flag)

The same pattern is available for Black and Black-Scholes-Merton through their respective implied_volatility modules.

Dependencies

  • cody-special - High-precision error functions and normal distribution
  • piecewise-rational - Shape-preserving piecewise rational cubic interpolation
  • lets_be_rational - Pure Python implementation of Peter Jaeckel's LetsBeRational
  • numpy
  • pandas
  • scipy
  • simplejson

About the reference Python implementation

vollib contains vollib.ref_python, a pure python version of the functions in vollib.*. It is provided purely as a reference implementation for sanity checking.

About "Let's be Rational"

"Let's Be Rational" is a paper by Peter Jaeckel showing "how Black's volatility can be implied from option prices with as little as two iterations to maximum attainable precision on standard (64 bit floating point) hardware for all possible inputs."

The paper is accompanied by the full C source code, which resides at www.jaeckel.org/LetsBeRational.7z.

Copyright (c) 2013-2014 Peter Jaeckel.

Permission to use, copy, modify, and distribute this software is freely granted,
provided that this notice is preserved.

WARRANTY DISCLAIMER
The Software is provided "as is" without warranty of any kind, either express or implied,
including without limitation any implied warranties of condition, uninterrupted use,
merchantability, fitness for a particular purpose, or non-infringement.

Links

License

MIT

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

py_vollib-1.0.11.tar.gz (23.0 kB view details)

Uploaded Source

Built Distribution

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

py_vollib-1.0.11-py3-none-any.whl (57.3 kB view details)

Uploaded Python 3

File details

Details for the file py_vollib-1.0.11.tar.gz.

File metadata

  • Download URL: py_vollib-1.0.11.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for py_vollib-1.0.11.tar.gz
Algorithm Hash digest
SHA256 293bd65e81c16bf9ef397e27f69edcaec93bba3d0e8c0079644a13a925754cc0
MD5 ba2f8bd7a8293e049ca8b4f291133ab4
BLAKE2b-256 960fb31e1e8cb21a4075039ec132347a2d6607fe829ebc987dfecdee9d8e390f

See more details on using hashes here.

File details

Details for the file py_vollib-1.0.11-py3-none-any.whl.

File metadata

  • Download URL: py_vollib-1.0.11-py3-none-any.whl
  • Upload date:
  • Size: 57.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for py_vollib-1.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 00822c8855e088d3a7a3be1f359f56816af34e0633e4fa8c1cca3be740606244
MD5 8256c141bbe57dbf3e485357bccafa26
BLAKE2b-256 65771a7b1b8b3e24b7fa499df6d5257ce7b550af57a8ffdb5896bcd9fecf9fac

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