|
Function findIV(ByVal stockprice As Single, ByVal strikeprice
As Single, ByVal timeremain As Single, ByVal interestrate As Single,
ByVal dividend As Single, ByVal optval As Double, ByVal opttype
As String) As Single
'uses a brute force method to find the IV that matches an option
price
Dim callval, putval, result As Single
If opttype.Contains("Call") Then
For iv = 0.0001 To 9 Step 0.0001 'changes the volatility .01%
per run, from .01% to 900%
callval = optionvalue(stockprice, strikeprice, timeremain, interestrate,
iv, dividend,opttype)
If callval > optval Then 'this way works better for calls
result = Math.Round(iv * 100, 2) 'returns IV to 2 decimals
Exit For
End If
Next
End If
If opttype.Contains("Put") Then
For iv = 0.0001 To 9 Step 0.0001
putval = optionvalue(stockprice, strikeprice, timeremain, interestrate,
iv, dividend,opttype)
If putval + 0.01 > optval Then
result = Math.Round(iv * 100, 2)
Exit For
End If
Next
End If
Return result
End Function
|