Package: pyBlackScholesAnalytics
pyBlackScholesAnalyticspyBlackScholesAnalyticsis a Python package implementing analytics for options and option strategies under the Black-Scholes Model for educational purposes.SummarypyBlackScholesAnalyticspackage is a Python package designed to use the well known Black-Scholes model
to evaluate price, P&L and greeks of European options (both plain-vanilla and simple equity exotics
such as cash-or-nothing Digital options), as well as simple option strategies built on them.The package has been developed as a spin-off project of the"IT for Business and Finance"class held at the University of Siena for the Master degree in Finance in 2020.pyBlackScholesAnalyticsplaces itself in the middle between the coding style and level of a master student and that required for a junior quant at an investment bank. The aim is to address the gap between the two providing a playground
for students to master financial concepts related to options and option strategies and implementing a dedicated comprehensive
object-oriented architecture.The package itself tries to follow the style guide for Python codePEP8. Intentional as well as unintentional departures from this style may occur in code. I'd like to thank in advance anyone who will make me aware of possible improvements in style and clarity of code.InstallationYou can installpyBlackScholesAnalyticssimply typingpip install pyBlackScholesAnalyticsLatest version of the package is available onPyPI. If you encounter problems during installation please share with me.ComponentsThe current version of the package features the following components:options: definitions forEuropeanOptionabstract base-class as well
asPlainVanillaOptionandDigitalOptionderived classesportfolio: definition ofPortfolioclass implementing analytics for portfolios of optionsplotter: definitions forPlotterabstract base-class as well
asOptionPlotterandPortfolioPlotterderived classesutils: definition of general utility functionsnumeric_routines: definition ofNumericGreeksclass implementing option greeks through finite-difference methodsResourcesAs far as the educational purpose is concerned, I find thepyBlackScholesAnalyticspackage itself helpful as much as the way in which its final version can be progressively built. In my experience, the constructive approach is ubiquitous in the real life of a Quant/Strat: a business need is first formulated by a trader or another stakeholder, then tackled by the Quant/Strat team with anad hocanalysis, then a tactic short-term implementation of the response is produced and, finally, a strategic and robust long-term solution is designed and implemented. For this reason, the package is complemented by a series of 4 Tutorials in the form ofJupyter NotebooksandYoutube Videos. These tutorial aim to present the package step-by step in a constructive way building on the ideas of the Object-Oriented paradygm as improvements over sequential implementations of the same financial concepts.Moreover severalexamplesshowcase the features ofpyBlackScholesAnalyticspackage and can be used as entry-point to begin the exploration of the package.TutorialsJupyter Notebook are made interactive thanks toBinder(click on the badge here below and start playing with the notebooks from your web browser)!Jupyter NotebookYoutube Video PlaylistDerivatives Analytics - Introduction to Object Oriented Programming: in this tutorial we introduceObject-OrientedProgramming in Python. We first make a non-financial example, developing the intuition behind the need of a change of programming paradigm to be able to cohordinate together different pieces of code. Once we have established the intuition, we then leverage on some basic financial knowledge to develop a Black-Scholes pricer for European call Options, first, and then a more general pricer for Plain-Vanilla put Options as well..ipynbwatchDerivatives Analytics - Inheritance and Polymorphism: in this tutorial we introduceInheritanceandPolymorphismin Python which are two milestones in Object-Oriented programming. We present these concepts introducing Digital cash-or-nothing Options and observing their similarities with Plain-Vanilla Options. Inheritance and Polymorphism allow us to leverage on the financial analogies between these two contracts and eventually represent them more efficiently as derived classes of a uniqueEuropeanOptionabstract base class..ipynbwatchDerivatives Analytics - Objects Composition: in this tutorial we introduceCompositionwhich is an additional way to model relationships among objects, alternatively to Inheritance. We present this relationship introducing a commonMarketEnvironmentunder which Option contracts are emitted, aPortfolioclass is designed to aggregate Options and anotherPlotterclass is designed to provide visualization routines. Finally, we examine several Option Strategies which uses the architecture implemented so far..ipynbwatchDerivatives Analytics - Options Greeks: in this tutorial we introduce Option Greeks. That is, the derivatives of an option price with respect to its pricing parameters. We provide both a numeric computation usingfinite-differencemethods implemented inNumericGreeksclass and their analytic expression using the Black-Scholes model. Finally, all features introduced in this and previous tutorial are collected in thepyBlackScholesAnalyticsPython package which is hereby introduced..ipynbwatchExamplesoptions.pyThis example shows basic usage ofPlainVanillaOptionandDigitalOptionclasses. Basic instantiation examples are provided with combinations of the underlying level (S) and time parameter (t/tau). Price, P&L, first-order greeks as well as Black-Scholes implied-volatility are computed for Plain-Vanilla and Digital Option contracts.options_other_params.pyThis example shows usage ofPlainVanillaOptionandDigitalOptionclasses. Instantiation examples are provided involving combinations of the underlying level (S), strike-price (K), time parameter (t/tau), as well as underlying volatility (sigma) and short-rate (r) parameters. Price, P&L, first-order greeks as well as Black-Scholes implied-volatility are computed for Plain-Vanilla and Digital Option contracts.options_IV.pyThis example shows usage ofPlainVanillaOptionandDigitalOptionclasses to compute of Black-Scholes implied volatility surfaces for Plain-Vanilla and Digital Option contracts.options_plot.pyThis example shows basic integration ofPlainVanillaOptionandDigitalOptionclasses withPlotterclass. Price, P&L and first-order greeks plots are shown for Plain-Vanilla and Digital Option contracts.options_plot_other_params.pyThis example shows integration ofPlainVanillaOptionandDigitalOptionclasses withPlotterclass. Price, P&L and first-order greeks plots are shown for Plain-Vanilla and Digital Option contracts against underlying level (S), strike-price (K), volatility (sigma) and short-rate (r).options_plot_IV.pyThis example shows integration ofPlainVanillaOptionandDigitalOptionclasses withPlotterclass (in particular.plot_IV()method). Focus is on the visualization of Black-Scholes implied-volatility for Plain-Vanilla and Digital Option contracts both as a line plot and surface-plot.options_plot_surface.pyThis example shows integration ofPlainVanillaOptionandDigitalOptionclasses withPlotterclass. Price, P&L and first-order greeks plots are shown for Plain-Vanilla and Digital Option contracts as underlying level (S), strike-price (K), volatility (sigma) and short-rate (r) surface-plots Vs time parameter.options_numeric_greeks.pyThis example provides an example of first-order numeric greeks implemented in theNumericGreeksclass using finite-difference methods for Plain-Vanilla and Digital Option contracts.options_numeric_analytic_greeks_comparison.pyThis example provides a comparison of first-order greeks for Plain-Vanilla and Digital Option contracts implemented either through finite-difference methods inNumericGreeksclass or using their analytic expression implemented inPlainVanillaOptionandDigitalOptionclasses.portfolio.pyThis example shows basic usage ofPortfolioclass to construct a derivative portfolio of Plain-Vanilla and Digital Option contracts. Basic instantiation examples are provided with combinations of the underlying level (S) and time parameter (t/tau). Price, P&L, first-order greeks are computed for constructed portfolio and benchmarked with the corresponding metrics calculated combining constituent options metrics.portfolio_single_strike.pyThis example shows basic usage ofPortfolioclass to construct a derivative portfolio of Plain-Vanilla and Digital Option contracts. Basic instantiation examples are provided with combinations of the underlying level (S), strike-price (K), time parameter (t/tau) as well as underlying volatility (sigma) and short-rate (r) parameters. Price, P&L, first-order greeks are computed for single-strike portfolio.portfolio_multi_strikes.pyThis example shows basic usage ofPortfolioclass to construct a derivative portfolio of Plain-Vanilla and Digital Option contracts. Basic instantiation examples are provided with combinations of the underlying level (S), time parameter (t/tau) as well as underlying volatility (sigma) and short-rate (r) parameters. Price, P&L, first-order greeks are computed for multi-strike portfolio.bull_spread.pyThis example shows usage ofPortfolioclass to create a Bull-Spread option strategy. Basic instantiation examples are provided with combinations of the underlying level (S) and time parameter (t/tau). Price, P&L, first-order greeks are computed and plotted using thePlotterclass as line plots and surface-plots Vs time parameter.bull_spread_other_params.pyThis example shows usage ofPortfolioclass to create a Bull-Spread option strategy. Instantiation examples are provided with combinations of the underlying level (S) and time parameter (t/tau) as well as underlying volatility (sigma) and short-rate (r) parameters. Price, P&L, first-order greeks are computed and plotted using thePlotterclass as line plots and surfaces-plots Vs time parameter.calendar_spread.pyThis example shows usage ofPortfolioclass to create a Calendar-Spread option strategy. Basic instantiation examples are provided with combinations of the underlying level (S) and time parameter (t). Price, P&L, first-order greeks are computed and plotted using thePlotterclass as line plots and surface-plots Vs time parameter.calendar_spread_other_params.pyThis example shows usage ofPortfolioclass to create a Calendar-Spread option strategy. Instantiation examples are provided with combinations of the underlying level (S) and time parameter (t) as well as underlying volatility (sigma) and short-rate (r) parameters. Price, P&L, first-order greeks are computed and plotted using thePlotterclass as line plots and surfaces-plots Vs time parameter.GallerySeepyBlackScholesAanlytics Github repositoryfor gallery of images.ContactsThis is still the first version of this package, so if you find errors, have comments or suggestions you can reach Gabriele Pompa (gabriele.pompa@gmail.com). If you wish to contribute, please contact me throughGitHub/gabrielepompa88. If you are interested but feel a bit new to Python, I can recommend the open"IT for Business and Finance"as a reasonable starting point.Thank you in advance for your attention.