我使用二分法编写了一个函数来求解欧洲看涨期权的隐含波动率。该函数的输入是单元格引用。当我尝试使用该功能时,我得到一个#Value!错误,我不知道为什么。我的代码如下。
Function BSCall(ByVal S As Double, ByVal K As Double, ByVal T As Double, _
ByVal vol As Double, Optional ByVal R As Double = 0, Optional ByVal Q As Double = 0)
Dim d1 As Double
Dim d2 As Double
d1 = Log(S / K) + (R - Q + (vol) * (vol) * 0.5) * T / (vol * Sqr(T))
d2 = d1 - (vol * Sqr(T))
BSCall = S * Exp(-Q * T) * Application.WorksheetFunction.Norm_S_Dist(d1, True) _
- K * Exp(-R * T) * Application.WorksheetFunction.Norm_S_Dist(d2, True)
End Function
Function ErrorCalc(ByVal Price As Double, ByVal vol As Double, ByVal S As Double, ByVal K As Double, ByVal T As Double, _
Optional ByVal R As Double = 0, Optional ByVal Q As Double = 0)
Error = Price - BSCall(S, K, T, vol, R, Q)
End Function
Function IVCall(ByVal Price As Double, ByVal S As Double, ByVal K As Double, ByVal T As Double, _
Optional ByVal R As Double = 0, Optional ByVal Q As Double = 0, _
Optional tol = 0.000000001, Optional iter = 1000, _
Optional minSig = 0.0001, Optional maxSig = 4) As Double
Dim b As Double
Dim a As Double
Dim temp As Double
Dim mid As Double
Dim counter As Long
Dim sigma As Double
a = minSig
b = maxSig
If ErrorCalc(Price, a, S, K, T) * ErrorCalc(Price, b, S, K, T) < 0 Then
Exit Function
End If
If ErrorCalc(Price, a, S, K, T) > 0 Then
temp = a
a = b
b = temp
End If
Do Until (tol >= 10^ - 9 Or iter <> counter)
mid = (a + b) / 2
Error = ErrorCalc(Price, mid, S, K, T)
If Abs(Error) <= tol Then
sigma = mid
Exit Do
ElseIf Error > tol Then
b = mid
ElseIf Error < tol Then
a = mid
End If
counter = counter + 1
Loop
IVCall = sigma
End Function
答案 0 :(得分:0)
更新以下功能并运行代码。
Function BSCall(ByVal S As Double, ByVal K As Double, ByVal T As Double, _
ByVal vol As Double, Optional ByVal R As Double = 0, Optional ByVal Q As Double = 0) As Double
Dim d1 As Double
Dim d2 As Double
d1 = Log(S / K) + (R - Q + (vol) * (vol) * 0.5) * T / (vol * Sqr(T))
d2 = d1 - (vol * Sqr(T))
BSCall = Round(S * Exp(-Q * T) * Application.WorksheetFunction.Norm_S_Dist(d1, True) _
- K * Exp(-R * T) * Application.WorksheetFunction.Norm_S_Dist(d2, True), 4)
End Function
Function ErrorCalc(ByVal Price As Double, ByVal vol As Double, ByVal S As Double, ByVal K As Double, ByVal T As Double, _
Optional ByVal R As Double = 0, Optional ByVal Q As Double = 0) As Double
ErrorCalc = Price - BSCall(S, K, T, vol, R, Q)
End Function