Solving For Implied Volatility using a UDF and Newton-Raphson

K-MAN69

Board Regular
Joined
Mar 1, 2012
Messages
64
Hi People,
I am struggling to code up a UDF that can be used to find out the implied volatility for a futures option given the following variables:

Option Price
Spot Price
Strike Price
Risk Free Rate (RFR)
Option Type ie Call or Put
Expiry Date
Deal Date

I have found the following code online but I cannot understand the Variables named "Seed" and I also don't understand "ctr" as I have highlighted below:

Function BlackScholesNewton(ByVal Seed As Double, ByVal
Precision As Double, ByVal S As Double, ByVal K As Double, ByVal r
As Double, ByVal q As Double, ByVal t As Double, ByVal sigma As
Double) As Double
Dim x_next As Double
Dim x_n As Double
Dim error_val As Double
Dim ctr As Integer
x_n = Seed
ctr = 0
Do
x_next = x_n - Black_Scholes(S, K, r, q, t, sigma) / Vega(S, K, r, q, t,
sigma)
error_val = x_next - x_n
x_n = x_next
ctr = ctr + 1
Loop Until (Abs(error_val) <= Precision Or ctr = 1000)
BlackScholesNewton = x_next​
End Function

Can anyone tell me what this means?
:confused:
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
It looks like "Seed" is just used as the starting point for an iterative process. So long as the chosen seed isn't an outlier, the function should converge. The "ctr" variable is a counter that's counting how many iterations have occurred. The assumption in this UDF is that if it hasn't converged after 1000 iterations, it's not going to.

Full and open disclosure - my math isn't what it used to be so you ought to take this with a grain of salt.
 
Upvote 0
Thank you for that explaination it certainly makes sense from what I could guess as well - thanks again
 
Upvote 0
Thanks so much that is a great website. Best code for Implied Volatility that I have seen so far.
 
Upvote 0
I created the code below that is much better than above

Code:
Option Explicit

Function OptionPriceBlack76(Expiry As Date, DealDate As Date, Spot As Double, Strike As Double, RFR As Double, Vol As Double, OptType As String)
Dim TimeYears As Double
Dim d1 As Double
Dim d2 As Double
Dim ert As Double
Dim N_Dash_d1 As Double 'The normal distribution curve
 
TimeYears = (Expiry - DealDate + 1) / 360
d1 = (Application.Ln(Spot / Strike) + Vol ^ 2 * TimeYears * 0.5) / (Vol * TimeYears ^ 0.5)
d2 = d1 - Vol * TimeYears ^ 0.5
ert = Exp(-RFR * TimeYears)
N_Dash_d1 = (1 / ((2 * 3.14159265358979) ^ 0.5)) * Exp((-d1 ^ 2) * 0.5)
 
'Price
If OptType = "C" Then
            OptionPriceBlack76 = ert * (Spot * Application.NormSDist(d1) - Strike * Application.NormSDist(d2))
ElseIf OptType = "P" Then
            OptionPriceBlack76 = ert * (Strike * Application.NormSDist(-d2) - Spot * Application.NormSDist(-d1))
End If
End Function

Function Vega_Black76(Expiry As Date, DealDate As Date, Spot As Double, Strike As Double, Vol As Double, RFR As Double, OptType As String) As Double
Dim TimeYears As Double
Dim d1 As Double
Dim d2 As Double
Dim ert As Double
Dim N_Dash_d1 As Double 'The normal distribution curve
 
TimeYears = (Expiry - DealDate + 1) / 360
d1 = (Application.Ln(Spot / Strike) + Vol ^ 2 * TimeYears * 0.5) / (Vol * TimeYears ^ 0.5)
d2 = d1 - Vol * TimeYears ^ 0.5
ert = Exp(-RFR * TimeYears)
N_Dash_d1 = (1 / ((2 * 3.14159265358979) ^ 0.5)) * Exp((-d1 ^ 2) * 0.5)
Vega_Black76 = Spot * ert * N_Dash_d1 * TimeYears ^ 0.5
End Function

Function AA_ImpliedVol_FutOpt(Expiry As Date, DealDate As Date, Spot As Double, Strike As Double, OptPrice As Double, RFR As Double, OptType As String)
Dim Vol As Double 'This is our first guess for Vol
Dim Accuracy As Double 'How accurate you want the calc to be
Dim i As Integer 'This is the iteration counter
Dim MaxIter As Integer 'This is the maximum allowable iterations the calc will do
Dim Value_1 As Double
Dim Vega_1 As Double
Dim NRF As Double '(OptPrice [Using Guess] - Observed OptPrice)/Vega [Using Guess]
Vol = 0.1
Accuracy = 0.00000000001
MaxIter = 100
i = 1
Do
   Value_1 = OptionPriceBlack76(Expiry, DealDate, Spot, Strike, RFR, Vol, OptType)
   Vega_1 = Vega_Black76(Expiry, DealDate, Spot, Strike, Vol, RFR, OptType)
   NRF = (Value_1 - OptPrice) / Vega_1
   Vol = Vol - NRF
   i = i + 1
   
Debug.Print "Value_1: " & Value_1
Debug.Print "Vega_1: " & Vega_1
Debug.Print "NRF: " & NRF
Debug.Print "Vol: " & Vol
Debug.Print "Iteration: " & i
    
Loop Until Abs(NRF) <= Accuracy Or i = MaxIter
AA_ImpliedVol_FutOpt = Vol
 
End Function
[CODE/]
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,103
Members
452,379
Latest member
IainTru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top