volatilipy package

Submodules

volatilipy.helper_functions module

Helper functions for the package

volatilipy.options_data module

Class designed to hold a dataframe of options, and then be able to do various activities; e.g. calculate implied volatilities, create a grid of volatilies (combining across options)

class volatilipy.options_data.OptionsData(valuation_date: datetime, option_positions: DataFrame)[source]

Bases: object

Object to take a dataframe of options (called options_option_positions) and backsolve for what the implied volatility is under Black Scholes Merton

Parameters
  • valuation_date (datetime) – Date for which you want the option valued

  • option_positions (pd.DataFrame) – Dataframe containing options

calculate_volatility_grid(fit_on_calls_or_puts: str = 'calls', to_file_filename: Optional[str] = None, column_name_mapping: dict = {'expiration': 'expiration', 'implied_volatility': 'implied_volatility_1545', 'option_type': 'option_type', 'strike': 'strike'}) None[source]

Method to process a dataframe of options and return a grid of implied volatilies by expiration date and strike, which can then be fed into QuantLib to fit a volatility surface. An example of this data that you can buy this data from https://datashop.cboe.com/option-quotes-end-of-day-with-calcs-subscription or https://datashop.cboe.com/option-quotes-end-of-day-with-calcs

Parameters
  • calls_or_puts (str, optional) – Whether to fit the implied volatilties

  • puts (from calls or) –

  • "calls". (or both. Defaults to) –

  • to_file_filename (str, optional) – filepath where to save the finalized

  • file

  • None (useful for looking at results. Should be a csv file. Defaults to) –

Returns

dataframe of implied volatilities

Return type

pd.DataFrame

solve_for_implied_vol(index_values: float64, dividend_yields: float64, risk_free_rates: DataFrame, columns_to_rename: dict = {'exercise_date': 'exercise_date', 'mid_eod': 'option_price', 'option_type': 'option_type', 'strike': 'strike'}, rate_column_name: str = 'spot_rate_eff_ann') None[source]

This is where the magic happens! Backsolving for implied volatility Note that the following columns must exist in the dataframe in order for the pricing to work. If needed, use the columns_to_rename in order to rename the columns temporarily for the volatility calculation (i.e. at the end of the pricing routine, the columns will be renamed to their original values):

Parameters
  • index_values (np.float64) – Value of underlying index as of the valuation date

  • dividend_yields (np.float64) – Dividend yield for the underlying index as of the valuation date

  • risk_free_rates (pd.DataFrame) – Dataframe containing risk free rates to use in BSM

  • volatilities (pd.DataFrame) – Dateframe with some beautiful option volatilities.

  • columns_to_rename (dict) – Dictionary with columns to rename from source file. Refer to function declaration in the values fields for columns that need to exist. At the end of this method, the dictionary is inversed, and all column names will be returned to their original values

Returns

Dataframe containing the original columns, plus the parameters used in

BSM for each option

Return type

pd.DataFrame

volatilipy.volatility_surface module

Volatility Surface Class - extension of BlackVarianceSurface object from QuantLib

class volatilipy.volatility_surface.VolatilitySurface(options_data: ~typing.Optional[~volatilipy.options_data.OptionsData] = None, volatility_grid: ~typing.Optional[~pandas.core.frame.DataFrame] = None, day_count: ~QuantLib.QuantLib.DayCounter = <QuantLib.QuantLib.ActualActual; proxy of <Swig Object of type 'QuantLib::ActualActual *'> >, calendar: ~QuantLib.QuantLib.Calendar = <QuantLib.QuantLib.UnitedStates; proxy of <Swig Object of type 'QuantLib::UnitedStates *'> >, allow_extrapolation: bool = False, valuation_date: ~typing.Optional[~datetime.datetime] = None, interpolation_method: str = 'Bilinear')[source]

Bases: BlackVarianceSurface

Object to take a dataframe of implied volatilities, with expiry dates along the row indices, and strike prices across the column indices, and turn it into a BlackVarianceSurface object, which can then be used with Quantlib in order to price options.

Parameters
  • options_data (OptionsData) – Options data on which to fit the surface

  • day_count (ql.DayCounter, optional) – Day counter to use with Quantlib. Defaults to ql.ActualActual(ql.ActualActual.ISDA).

  • calendar (ql.Calendar, optional) – Calendar to use with Quantlib. Defaults to ql.UnitedStates(ql.UnitedStates.Settlement).

  • allow_extrapolation (boolean, optional) – Flag to enable extrapolation. Defaults to False, consistent with Quantlib.

  • interpolation_method (str, optional) – Define interpolation method to use. Options are “Bilinear”, “Bicubic” (and others which I have not explored). Defaults to “Bilinear”, consistent with Quantlib

Returns

Surface of vols, can then be used to price options!

Return type

ql.BlackVarianceSurface

generate_interpolated_volatility_dataframe(strike_step: int = 100, frequency_code: str = '7D', spot_price: Optional[float] = None) DataFrame[source]

Function to take an existing volatility surface, and interpolate across a specified interval of strikes (e.g. every 50 points) and days to maturity interval (e.g. every 7 days) in order to come up with an entire surface, which can then be used for visualization or for storing data into a database.

Parameters
  • vol_surface (ql.BlackVarianceSurface) – Surface object that has already been fit to market data

  • strike_step (int, optional) – Resolution of the interpolation across the strike axis. Defaults to 100.

  • frequency_code (str, optional) – Resolution of the interpolation across the date axis. Defaults to “7D”. Other options are

  • here (available) – Other options available here: https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timeseries-offset-aliases

  • spot_price (float, optional) – Index value at time zero; if this is supplied, an additional column called moneyness will be generated. Defaults to None.

Returns

Dataframe of interpolated volatility numbers

Return type

pd.DataFrame

Module contents

Top-level package for volatilipy.