py_vollib.ref_python.black.implied_volatility

A library for option pricing, implied volatility, and greek calculation. py_vollib is based on lets_be_rational, a Python wrapper for LetsBeRational by Peter Jaeckel as described below.

copyright:

© 2023 Larry Richards

license:

MIT, see LICENSE for more details.

Module Contents

Functions

implied_volatility(price, F, K, r, t, flag)

Returns the Black delta of an option.

implied_volatility(price, F, K, r, t, flag)[source]

Returns the Black delta of an option.

Parameters:
  • price (float) –

  • F (float) – underlying futures price

  • K (float) – strike price

  • r (float) – annual risk-free interest rate

  • t (float) – time to expiration in years

  • flag (str) – ‘c’ or ‘p’ for call or put.

Returns:

float

>>> F = 101.0
>>> K = 102.0
>>> t = .5
>>> r = .01
>>> flag = 'p'
>>> sigma_in = 0.2
>>> price = black(flag, F, K, t, r, sigma_in)
>>> expected_price = 6.20451158097
>>> abs(expected_price - price) < 0.00001
True
>>> sigma_out = implied_volatility(price, F, K, r, t, flag)
>>> sigma_in == sigma_out or abs(sigma_in - sigma_out) < 0.00001
True
>>> F = 100
>>> K = 100
>>> sigma = .2
>>> flag = 'c'
>>> t = .5
>>> r = .02
>>> discounted_call_price = black(flag, F, K, t, r, sigma)
>>> iv = implied_volatility(discounted_call_price, F, K, r, t, flag)
>>> expected_discounted_call_price = 5.5811067246
>>> expected_iv = 0.2
>>> abs(expected_discounted_call_price - discounted_call_price) < 0.00001
True
>>> abs(expected_iv - iv) < 0.00001
True