Calculate Implied Volatility or any Options Greek in just 3 lines of Python
I tried to look for some one-line function on the internet that could calculate any greek or implied volatility, but instead, I found lengthy functions and classes which need to be written every time you want to calculate these numbers. Options trading is in itself a topic that needs a lot of brainstorming, so I decided to keep the programming part simple.
We will be using a python library — mibian, which could solve our purpose. Mibian can be used to calculate greeks using different pricing models like Black-Scholes, Garman-Kohlhagen or Merton, in this article we will focus on Black-Scholes as it is widely used by many traders and institutions.
Before we discuss different functionality available in the library — mibian, let's see what variables we might want to calculate, and what are the input variables required for those. (of course, you are aware of basics of options, if you have selected this topic to give your time, but to keep the function very simple, please bear with me….)
To start with, first we install the library in jupyter notebook, followed by importing it.
In[]: !pip install mibian
Collecting mibian
Downloading https://files.pythonhosted.org/packages/e5/74/25719d1f66b84561f209692947980660268cf601dc92766547b626eb03d5/mibian-0.1.3.tar.gz
Building wheels for collected packages: mibian
Building wheel for mibian (setup.py): started
Building wheel for mibian (setup.py): finished with status 'done'
Created wheel for mibian: filename=mibian-0.1.3-cp37-none-any.whl size=4042 sha256=204703060b7a13512adbbb97184915480acf8cf878841b3f1fa16fbc5d516bbd
Stored in directory: C:\Users\ABC\AppData\Local\pip\Cache\wheels\34\c7\51\22486b811445a01dab50193c9748e94242e55a4ce686a24240
Successfully built mibian
Installing collected packages: mibian
Successfully installed mibian-0.1.3In[]: import mibian
The syntax of the function is —
mibian.BS([Underlying Price, Strike Price, Interest Rate, Daystoexpiration], Call Price, Put Price, volatility)
Now, pay attention to the above print screen, where input and output variables are mentioned. which I will explain with the current example of Nifty Options (NSE, India) (30-Mar-2020).
Let’s consider following variables available with us, and we want to find Call & Put Implied Volatility to start with —
underlying_price = 8572
call_strike = 8700
put_strike = 8500
call_price = 616.05
put_price = 654.05
interest_rate = 0% (as we are taking futures prices as underlying)
days_to_expiry = 31
So, as given in the above print screen, To calculate Call Implied Volatility, we need 1. Call Strike, 2. Call Price, 3. Underlying Price, 4. Interest Rate, 5. Days left to Expiry…
so, we will use the above-mentioned function to create an object and store it in a variable, and later using that variable we can pull the required number (in short, feed input variables in function and get the output)
syntax to write the function to calculate implied volatility for Call Option and Put Option would be —
mibian.BS([Underlying Price, Call / Price Strike Price, Interest Rate, Days To Expiration], Call / Put Price)
In[]: c = mibian.BS([8572, 8700, 0, 31], callPrice= 616.05)
print(c.impliedVolatility)Out[]: 67.64984130859375
In[]: p = mibian.BS([8572, 8500, 0, 31], putPrice= 654.05)
print(p.impliedVolatility)Out[]: 69.59056854248047
Now, that we have Call and Put Implied Volatility with us, we can calculate any other Options Greek …
syntax to write the function to calculate other greeks for Call / Put Option —
mibian.BS([Underlying Price, Call / Put Strike Price, Interest Rate, Days To Expiration], Call / Put Volatility)
In[]: c = mibian.BS([8572, 8700, 0, 31], volatility = 67.65)
print(c.callPrice)
print(c.callDelta)
print(c.callTheta)
print(c.vega)
print(c.gamma)Out[]:616.053446760709
0.5093328435093478
-10.871360383629481
9.963404933998929
0.00023599718695036784In[]: p = mibian.BS([8572, 8500, 0, 31], volatility = 69.60)
print(p.callPrice)
print(p.callDelta)
print(p.callTheta)
print(p.vega)
print(p.gamma)Out[]:654.1415210663004
-0.4431440219718594
-11.073976077528648
9.864748804695061
0.0002271138592604518
So, as the title suggested, we can now calculate any Options Greek in just 3 lines of code. First-line, import mibian. second, feeding inputs in the function and last would be printing results.
(Github link to access above codes in Jupyter Notebook.)
In later articles, I will be coming with more of python codes which can take away complex calculation parts of options, and we can concentrate more on the trading part. Till then Happy Analysis and Happy Trading.