The functions in the API are provided as a convenience to the user who would prefer not to use the py_vollib syntax.

API functions

py_vollib_vectorized.api.get_all_greeks(flag, S, K, t, r, sigma, q=None, *, model='black_scholes', return_as='dataframe', dtype=<class 'numpy.float64'>)

Utility function that returns all contract greeks, as specified by the pricing model model. Broadcasting is applied on the inputs.

Parameters
  • flag – For each contract, this should be specified as c for a call option and p for a put option.

  • S – The price of the underlying asset.

  • K – The strike price.

  • t – The annualized time to expiration. Must be positive. For small TTEs, use a small value (1e-3).

  • r – The Interest Free Rate.

  • sigma – The Implied Volatility.

  • q – The annualized continuous dividend yield.

  • model – Must be one of ‘black’, ‘black_scholes’ or ‘black_scholes_merton’.

  • return_as – To return as a pd.DataFrame object, use “dataframe”. To return as a json object, use “json”. Any other value will return a python dictionary.

  • dtype – Data type.

Returns

pd.DataFrame, json string, or dict object containing the greeks for each contract.

>>> flag = ['c', 'p']
>>> S = 95
>>> K = [100, 90]
>>> t = .2
>>> r = .2
>>> sigma = .2
>>> get_all_greeks(flag, S, K, t, r, sigma, model='black_scholes', return_as='dict')
{'delta': array([ 0.46750566, -0.1364465 ]),
 'gamma': array([0.0467948, 0.0257394]),
 'theta': array([-0.04589963, -0.00533543]),
 'rho': array([ 0.0830349 , -0.02715114]),
 'vega': array([0.16892575, 0.0928379 ])}
py_vollib_vectorized.api.price_dataframe(df, *, flag_col=None, underlying_price_col=None, strike_col=None, annualized_tte_col=None, riskfree_rate_col=None, sigma_col=None, price_col=None, dividend_col=None, model='black_scholes', inplace=False, dtype=<class 'numpy.float64'>)

Utility function to price a DataFrame of option contracts by specifying the columns corresponding to each value. This function automatically calculates option price, option implied volatility and greeks in one call. Specifying a sigma_col will return the option prices and greeks. Specifying a price_col will return implied volatilities and greeks. Specifying both will return only greeks.

Parameters
  • df (pd.DataFrame) – Input DataFrame.

  • flag_col (str) – Column containing the flags (‘c’ for call, ‘p’ for puts)

  • underlying_price_col (str) – Column containing the price of the underlying.

  • strike_col (str) – Column containing the strike price.

  • annualized_tte_col (str) – Column containing the annualized time to expiration.

  • riskfree_rate_col (str) – Column containing the risk-free rate.

  • sigma_col (str) – Column containing the implied volatility (if unspecified, will be calculated).

  • price_col (str) – Column containing the price of the option (if unspecified, will be calculated).

  • dividend_col (str) – Column containing the implied volatility (only for Black-Scholes-Merton).

  • model (str) – Must be one of ‘black’, ‘black_scholes’ or ‘black_scholes_merton’.

  • inplace (bool) – Whether to modify the input dataframe inplace (columns will be added) or return a pd.DataFrame with the result.

  • dtype (dtype) – Data type

Returns

None if inplace is True or a pd.DataFrame object containing the desired calculations if inplace is False.

Return type

pd.DataFrame

>>> df = pd.DataFrame()
>>> df['Flag'] = ['c', 'p']
>>> df['S'] = 95
>>> df['K'] = [100, 90]
>>> df['T'] = 0.2
>>> df['R'] = 0.2
>>> df['IV'] = 0.2
>>> price_dataframe(df, flag_col='Flag', underlying_price_col='S', strike_col='K', annualized_tte_col='T', riskfree_rate_col='R', sigma_col='IV', model='black_scholes', inplace=False)
    Price       delta           gamma           theta           rho             vega
0   2.895588        0.467506        0.046795        -0.045900       0.083035        0.168926
1   0.611094        -0.136447       0.025739        -0.005335       -0.027151       0.092838