Can anyone tell me what is wrong with my code?
I ran it in the immdeiate window and it jamed up Excel and I had to end process in the task manager.
It also said that a sub or function was not defined?
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 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
Dim Vega As Double
Dim OptPriceTheory As Double
Dim X_Next As Double
Dim X_N As Double
Dim Error_Val As Double
Dim ctr As Double
Vol = 0.05
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.1415926535) ^ 0.5)) * Exp((-d1 ^ 2) * 0.5)
Vega = (Spot * ert * N_Dash_d1 * TimeYears ^ 0.5)
If OptType = "C" Then
OptPriceTheory = ert * (Spot * Application.NormSDist(d1) - Strike * Application.NormSDist(d2))
ElseIf OptType = "P" Then
OptPriceTheory = ert * (Strike * Application.NormSDist(-d2) - Spot * Application.NormSDist(-d1))
End If
X_N = OptPrice
ctr = 0
Do
X_Next = X_N - OptPriceTheory / Vega
Error_Val = X_Next - X_N
X_N = X_Next
ctr = ctr + 1
Loop Until (Abs(Error_Val) <= 0.0000001 Or crt = 1000)
AA_ImpliedVol_FutOpt = X_Next
End Function
I ran it in the immdeiate window and it jamed up Excel and I had to end process in the task manager.
It also said that a sub or function was not defined?
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 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
Dim Vega As Double
Dim OptPriceTheory As Double
Dim X_Next As Double
Dim X_N As Double
Dim Error_Val As Double
Dim ctr As Double
Vol = 0.05
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.1415926535) ^ 0.5)) * Exp((-d1 ^ 2) * 0.5)
Vega = (Spot * ert * N_Dash_d1 * TimeYears ^ 0.5)
If OptType = "C" Then
OptPriceTheory = ert * (Spot * Application.NormSDist(d1) - Strike * Application.NormSDist(d2))
ElseIf OptType = "P" Then
OptPriceTheory = ert * (Strike * Application.NormSDist(-d2) - Spot * Application.NormSDist(-d1))
End If
X_N = OptPrice
ctr = 0
Do
X_Next = X_N - OptPriceTheory / Vega
Error_Val = X_Next - X_N
X_N = X_Next
ctr = ctr + 1
Loop Until (Abs(Error_Val) <= 0.0000001 Or crt = 1000)
AA_ImpliedVol_FutOpt = X_Next
End Function