二分法を使用して、ヨーロピアン コール オプションのインプライド ボラティリティを解く関数を作成しました。関数の入力はセル参照です。関数を使用しようとすると、 #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