|
|
| Next: VBA for bisection method to find volatility-->.. |
| Author |
Message |
eliasjohnk External

Since: Jun 23, 2007 Posts: 2
|
Posted: Sat Jun 23, 2007 10:58 am Post subject: Bisection method for volatility-> VBA function not working, please help Archived from groups: microsoft>public>excel>querydao (more info?) |
|
|
Returns error-> whats wrong with the first function-> I defined the
option pricing formula in the other function
Option Explicit
Function ImpliedVol2(optType, S0, K, rcinterest, q, T, trueprice)
Dim optionpricelow As Double
Dim optionpricehigh As Double
Dim lowervol As Double
Dim uppervol As Double
Dim sigma As Double
Dim EJ As Double
sigma = 5
optionpricelow = BSMOptPrice(optType, S0, K, rcinterest, q, T, sigma)
sigma = 200
optionpricehigh = BSMOptPrice(optType, S0, K, rcinterest, q, T, sigma)
If optionpricehigh - trueprice > trueprice - optionpricelow Then
uppervol = (200 + 5) / 2
lowervol = 5
Else
uppervol = 200
lowervol = (200 + 5) / 2
End If
Do While EJ > 0.0001
sigma = lowervol
optionpricelow = BSMOptPrice(optType, S0, K, rcinterest, q, T, sigma)
sigma = uppervol
optionpricehigh = BSMOptPrice(optType, S0, K, rcinterest, q, T, sigma)
If optionpricehigh - trueprice > trueprice - optionpricelow Then
uppervol = (uppervol + lowervol) / 2
lowervol = lowervol
Else
uppervol = uppervol
lowervol = (uppervol + lowervol) / 2
End If
EJ = optionpricehigh - optionpricelow
Loop
ImpliedVol2 = sigma
End Function
Function BSMOptPrice(optType, S0, K, rcinterest, q, T, sigma)
' Calculates Black-Scholes-Merton option price
' optType = 1 for call, -1 for put
' This function uses Functions BSD1 and BSD2
Dim exprT, expqT, ND1, ND2
If S0 > 0 And K > 0 And T > 0 And sigma > 0 Then
exprT = Exp(-rcinterest * T)
expqT = Exp(-q * T)
ND1 = Application.NormSDist(optType * _
BSD1(S0, K, rcinterest, q, T, sigma))
ND2 = Application.NormSDist(optType * _
BSD2(S0, K, rcinterest, q, T, sigma))
BSMOptPrice = optType * (S0 * expqT * ND1 - _
K * exprT * ND2)
ElseIf S0 > 0 And K > 0 And sigma > 0 And T = 0 Then
BSMOptPrice = Application.Max(0, optType * (S0 - K))
Else
'MsgBox "One of the inputs provided is invalid"
BSMOptPrice = 0
End If
End Function
Private Function BSD1(S0, K, rcinterest, q, T, sigma)
' Calculates D1 for Balck-Scholes-Merton option pricing
BSD1 = (Log(S0 / K) + (rcinterest - q + 0.5 * sigma ^ 2) * T) / _
(sigma * Sqr(T))
End Function
Private Function BSD2(S0, K, rcinterest, q, T, sigma)
' Calculates D2 for Balck-Scholes-Merton option pricing
BSD2 = BSD1(S0, K, rcinterest, q, T, sigma) - (sigma * Sqr(T))
End Function |
|
| Back to top |
|
 |
Marc Hillman External

Since: Aug 03, 2007 Posts: 4
|
Posted: Fri Aug 03, 2007 5:57 pm Post subject: Re: Bisection method for volatility-> VBA function not working, please help [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
The code is all Greek to me, but I notice you are using EJ on the LHS of an
equation before it appears on a RHS, i.e. it is not set before you first use
it.
<eliasjohnk DeleteThis @yahoo.com> wrote in message
news:1182621537.765941.276400@p77g2000hsh.googlegroups.com...
> Returns error-> whats wrong with the first function-> I defined the
> option pricing formula in the other function
>
>
> Option Explicit
> Function ImpliedVol2(optType, S0, K, rcinterest, q, T, trueprice)
>
> Dim optionpricelow As Double
>
>
> Dim optionpricehigh As Double
>
> Dim lowervol As Double
> Dim uppervol As Double
>
>
> Dim sigma As Double
> Dim EJ As Double
>
> sigma = 5
> optionpricelow = BSMOptPrice(optType, S0, K, rcinterest, q, T, sigma)
> sigma = 200
> optionpricehigh = BSMOptPrice(optType, S0, K, rcinterest, q, T, sigma)
>
>
> If optionpricehigh - trueprice > trueprice - optionpricelow Then
> uppervol = (200 + 5) / 2
> lowervol = 5
> Else
> uppervol = 200
> lowervol = (200 + 5) / 2
> End If
>
> Do While EJ > 0.0001
>
>
> sigma = lowervol
> optionpricelow = BSMOptPrice(optType, S0, K, rcinterest, q, T, sigma)
> sigma = uppervol
> optionpricehigh = BSMOptPrice(optType, S0, K, rcinterest, q, T, sigma)
>
> If optionpricehigh - trueprice > trueprice - optionpricelow Then
> uppervol = (uppervol + lowervol) / 2
> lowervol = lowervol
> Else
> uppervol = uppervol
> lowervol = (uppervol + lowervol) / 2
>
> End If
>
> EJ = optionpricehigh - optionpricelow
> Loop
>
> ImpliedVol2 = sigma
>
> End Function
>
>
>
> Function BSMOptPrice(optType, S0, K, rcinterest, q, T, sigma)
> ' Calculates Black-Scholes-Merton option price
> ' optType = 1 for call, -1 for put
> ' This function uses Functions BSD1 and BSD2
>
> Dim exprT, expqT, ND1, ND2
>
> If S0 > 0 And K > 0 And T > 0 And sigma > 0 Then
> exprT = Exp(-rcinterest * T)
> expqT = Exp(-q * T)
> ND1 = Application.NormSDist(optType * _
> BSD1(S0, K, rcinterest, q, T, sigma))
> ND2 = Application.NormSDist(optType * _
> BSD2(S0, K, rcinterest, q, T, sigma))
> BSMOptPrice = optType * (S0 * expqT * ND1 - _
> K * exprT * ND2)
> ElseIf S0 > 0 And K > 0 And sigma > 0 And T = 0 Then
> BSMOptPrice = Application.Max(0, optType * (S0 - K))
> Else
> 'MsgBox "One of the inputs provided is invalid"
> BSMOptPrice = 0
> End If
>
> End Function
>
>
> Private Function BSD1(S0, K, rcinterest, q, T, sigma)
> ' Calculates D1 for Balck-Scholes-Merton option pricing
>
> BSD1 = (Log(S0 / K) + (rcinterest - q + 0.5 * sigma ^ 2) * T) / _
> (sigma * Sqr(T))
> End Function
>
>
> Private Function BSD2(S0, K, rcinterest, q, T, sigma)
> ' Calculates D2 for Balck-Scholes-Merton option pricing
>
> BSD2 = BSD1(S0, K, rcinterest, q, T, sigma) - (sigma * Sqr(T))
> End Function
> |
|
| Back to top |
|
 |
dave12

Joined: Apr 05, 2009 Posts: 1
|
Posted: Sun Apr 05, 2009 7:06 pm Post subject: [Login to view extended thread Info.] |
|
|
| Did you ever get this working? i am looking for a bisection method for vba as well. |
|
| Back to top |
|
 |
|
|