Documente Academic
Documente Profesional
Documente Cultură
d1 = (Log(s / k) + (r - d + v ^ 2 / 2) * t) / (v * t ^ 0.5)
d2 = d1 - v * t ^ 0.5
BS = s * Exp(-d * t) * Application.NormSDist(d1) - k * Exp(-r *
t) * Application.NormSDist(d2)
End Function
---------------------------------------------------------------------
Function bsoptionvalue(dum, S, x, r, q, tyr, sigma)
Dim eqt, ert, ndone, ndtwo
eqt = Exp(-q * tyr)
ert = Exp(-r * tyr)
If S > 0 And x > 0 And tyr > 0 And sigma > 0 Then
ndone = Application.NormSDist(dum * bsone(S, x, r, q, tyr,
sigma))
ndtwo = Application.NormSDist(dum * bstwo(S, x, r, q, tyr,
sigma))
bsoptionvalue = dum * (S * eqt * ndone - x * ert * ndtwo)
Else
bsoptionvalue = -1
End If
End Function
Function bsone(S, x, r, q, tyr, sigma)
bsone = (Log(S / x) + (r - q + 0.5 * sigma ^ 2) * tyr) / (sigma *
Sqr(tyr))
End Function
Function bstwo(S, x, r, q, tyr, sigma)
bstwo = ((Log(S / x) + (r - q + 0.5 * sigma ^ 2) * tyr) / (sigma *
Sqr(tyr))) - sigma * Sqr(tyr)
End Function
-------------------------------------------------------------------
Function bsoptionvalue(dum, s, x, r, q, tyr, sigma)
Dim eqt, ert, ndone, ndtwo
eqt = Exp(-q * tyr)
ert = Exp(-r * tyr)
If s > 0 And x > 0 And tyr > 0 And sigma > 0 Then
ndone = Application.NormSDist(dum * bsone(s, x, r, q, tyr,
sigma))
ndtwo = Application.NormSDist(dum * bstwo(s, x, r, q, tyr,
sigma))
bsoptionvalue = dum * (s * eqt * ndone - x * ert * ndtwo)
Else
bsoptionvalue = -1
End If
End Function
Function bsone(s, x, r, q, tyr, sigma)
bsone = (Log(s / x) + (r - q + 0.5 * sigma ^ 2) * tyr) / (sigma *
Sqr(tyr))
End Function
Function bstwo(s, x, r, q, tyr, sigma)
bstwo = ((Log(s / x) + (r - q + 0.5 * sigma ^ 2) * tyr) / (sigma *
Sqr(tyr))) - sigma * Sqr(tyr)
End Function
Function CashorNothing(iopt, s, k, x, r, d, t, v)
' Returns Black-Scholes Value (iopt=1 for call, -1 for put; q=div
yld)
' Uses BSDOne fn
' Uses BSDTwo fn
Dim eqt, ert, nd1, nd2
eqt = Exp(-d * t)
ert = Exp(-r * t)
If s > 0 And k > 0 And t > 0 And v > 0 Then
nd1 = Application.NormSDist(iopt * bsnd1(s, k, r, d, t, v))
nd2 = Application.NormSDist(iopt * bsnd2(s, k, r, d, t, v))
CashorNothing = x * ert * nd2
Else
CashorNothing = -1
End If
End Function
Function AssetorNothing(iopt, s, k, x, r, d, t, v)
' Returns Black-Scholes Value (iopt=1 for call, -1 for put; q=div
yld)
' Uses BSDOne fn
' Uses BSDTwo fn
Dim eqt, ert, nd1, nd2
eqt = Exp(-d * t)
ert = Exp(-r * t)
If s > 0 And k > 0 And t > 0 And v > 0 Then
nd1 = Application.NormSDist(iopt * bsnd1(s, k, r, d, t, v))
nd2 = Application.NormSDist(iopt * bsnd2(s, k, r, d, t, v))
AssetorNothing = s * eqt * nd1
Else
Sub go_home()
Sheets("home").Select
End Sub
Sub VBA_Sub()
Range("E4") = bsoptionvalue(1, Range("B3"), Range("B4"), Range("B5"),
Range("B7"), Range("B9"), Range("B11"))
Range("H4") = bsoptionvalue(-1, Range("B3"), Range("B4"),
Range("B5"), Range("B7"), Range("B9"), Range("B11"))
End Sub
---------------------------------------------------------------------
Private Sub CommandButton1_Click()
Range("A9:AB50").Select
Selection.ClearContents
Selection.Interior.ColorIndex = 0
Worksheets("Draw Tree").Activate
Dim t As Integer '
Dim q As Integer '
Dim i As Integer
Dim j As Integer
Cells(10, 2) = Cells(6, 2) '
nstep = Cells(4, 2) '
For t = 0 To Cells(4, 2)
a = 1 * t
Cells(9, 2 + t) = a
Cells(10 + t, 1) = a
Next t
For i = 2 To nstep + 1
Cells(10, i + 1) = Cells(10, i) + Cells(5, 2)
For j = 2 To i
Cells(9 + j, 1 + i) = Cells(8 + j, i) - Cells(5, 2)
Next j
Next i
End Sub
Private Sub CommandButton1_Click()
Range("A9:BZ150").Select
Selection.ClearContents
Worksheets("Jr Binomial Tree").Activate
Dim i As Integer
Dim discountfactor As Variant
nstep = Cells(8, 2)
For i = 0 To nstep
If Cells(10 + i, nstep + 1) >= 40 Then
Cells(10 + i, nstep + 3) = 1000 + (40 - 25) * 170
ElseIf Cells(10 + i, nstep + 1) >= 25 Then
Cells(10 + i, nstep + 3) = 1000 + (Cells(10 + i, nstep + 1).Value -
25) * 170
Else
Cells(10 + i, nstep + 3) = 1000
End If
Next i
For i = 0 To nstep
Cells(10 + i, nstep + 4) = Cells(10 + i, nstep + 3) * Cells(7, 5) ^
(nstep - i) * Cells(8, 5) ^ i * discountfactor
Next i
End Sub
---------------------------------------------------------------------
If imod = 0 Then
rnmut = (r - q - 0.5 * sigma ^ 2) * delt
u = Exp(rnmut + sigma * Sqr(delt))
d = Exp(rnmut - sigma * Sqr(delt))
Else
u = Exp(sigma * Sqr(delt))
d = 1 / u
End If
Smat(0, 0) = s
For i = 1 To nstep
Smat(i, 0) = ""
Next i
For j = 1 To nstep
For i = 1 To j
Smat(i, j) = d * Smat(i - 1, j - 1)
Next i
Smat(0, j) = u * Smat(0, j - 1)
For i = j + 1 To nstep
Smat(i, j) = ""
Next i
Next j
BinTree = Smat
End Function
Function bsnd1(s, X, r, q, tyr, sigma)
bsnd1 = (Log(s / X) + (r - q + sigma ^ 2 / 2) * tyr) / (sigma * tyr
^ 0.5)
End Function
End Function
---------------------------------------------------------------------
Randomize
sum = 0
For i = 1 To nsim
s1 = s * Exp((r - q - 0.5 * sigma ^ (2)) * T + sigma * Sqr(T) *
Application.NormSInv(Rnd))
s2 = s * Exp((r - q - 0.5 * sigma ^ (2)) * T - sigma * Sqr(T) *
Application.NormSInv(Rnd))
End Function
---------------------------------------------------------------------
n = Cells(10, 2)
Range(Cells(14, 1), Cells(13 + n, 2)).Select
Worksheets(" 3").ChartObjects.Delete
Charts.Add
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Sheets("
3").Range(Cells(14, 1), Cells(13 + n, 2)), _
PlotBy:=xlColumns
ActiveChart.SeriesCollection(1).Delete
ActiveChart.SeriesCollection(1).XValues = Sheets("
3").Range(Cells(14, 1), Cells(13 + n, 1))
End Sub
---------------------------------------------------------------------
Function Halton1(n, b) As Double
End Function
S = Cells(3, 2)
k = Cells(4, 2)
T = Cells(5, 2)
r = Cells(6, 2)
sigma = Cells(7, 2)
facevalue = Cells(8, 2)
nt = Cells(9, 2)
ns = Cells(10, 2)
delta_t = T / nt
AA = 0
X = 0
optionValue = 0
AA = AA + temp(i)
End If
Next i
AA = AA / 30
X = Exp(-r * T) * Application.Max(AA - k, 0) * (facevalue / S)
optionValue = optionValue + X
Next j
optionValue = optionValue / ns
Cells(3, 7) = optionValue
End Sub
---------------------------------------------------------------------
End Function
---------------------------------------------------------------------
Function BarrierOption(UpOrDown, InOrOut, iopt, s, k, b, r, q, sigma,
T, nt, ns)
Dim spath()
ReDim spath(nt, 1 To ns)
sum = 0
cross = 0
spath = AssetPaths(s, r, q, T, sigma, nt, ns)
'''''''''''''''''''''''''''''''''''''
If UpOrDown = 0 And InOrOut = 0 And iopt = -1 Then 'DOPut
If b >= s Then
MsgBox ("Barrier price must be less than current price")
ElseIf b >= k Then
MsgBox ("Barrier price must be less than strike price")
Else
For i = 1 To ns
payoff = Application.Max(iopt * (spath(nt, i) - k), 0)
For j = 1 To nt
If spath(j, i) <= b Then
payoff = 0
j = nt + 1
cross = cross + 1
End If
Next j
sum = sum + payoff
Next i
BarrierOption = temp
End If
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If b >= s Then
MsgBox ("Barrier price must be less than current price")
Else
For i = 1 To ns
payoff = Application.Max(iopt * (spath(nt, i) - k), 0)
For j = 1 To nt
Next j
sum = sum + payoff
Next i
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For i = 1 To ns
payoff = 0
For j = 1 To nt
If spath(j, i) <= b Then
payoff = Application.Max(iopt * (spath(nt, i) - k), 0)
j = nt + 1
cross = cross + 1
End If
Next j
sum = sum + payoff
Next i
BarrierOption = temp
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For i = 1 To ns
payoff = 0
For j = 1 To nt
If spath(j, i) <= b Then
payoff = Application.Max(iopt * (spath(nt, i) - k), 0)
j = nt + 1
cross = cross + 1
End If
Next j
sum = sum + payoff
Next i
BarrierOption = temp
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If b <= s Then
MsgBox ("Barrier price must be greater than current price")
Else
For i = 1 To ns
payoff = Application.Max(iopt * (spath(nt, i) - k), 0)
For j = 1 To nt
If spath(j, i) >= b Then
payoff = 0
j = nt + 1
cross = cross + 1
End If
Next j
sum = sum + payoff
Next i
temp(0) = sum * Exp(-r * T) / ns
temp(1) = cross
BarrierOption = temp
End If
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''
If b <= s Then
MsgBox ("Barrier price must be greater than current price")
ElseIf b <= k Then
MsgBox ("Barrier price must be greater than strike price")
Else
For i = 1 To ns
payoff = Application.Max(iopt * (spath(nt, i) - k), 0)
For j = 1 To nt
If spath(j, i) >= b Then
payoff = 0
j = nt + 1
cross = cross + 1
End If
Next j
sum = sum + payoff
Next i
BarrierOption = temp
End If
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''
For i = 1 To ns
payoff = 0
For j = 1 To nt
If spath(j, i) >= b Then
payoff = Application.Max(iopt * (spath(nt, i) - k), 0)
j = nt + 1
cross = cross + 1
End If
Next j
sum = sum + payoff
Next i
BarrierOption = temp
End If
For i = 1 To ns
payoff = 0
For j = 1 To nt
If spath(j, i) >= b Then
payoff = Application.Max(iopt * (spath(nt, i) - k), 0)
j = nt + 1
cross = cross + 1
End If
Next j
sum = sum + payoff
Next i
BarrierOption = temp
End If
End Function
---------------------------------------------------------------------
Private Sub CommandButton1_Click()
S = Cells(3, 2)
x = Cells(4, 2)
Smax = Cells(5, 2)
r = Cells(7, 2)
q = Cells(8, 2)
sigma = Cells(13, 2)
tyr = Cells(11, 2)
iopt = -1
dS = Cells(15, 2)
dt = Cells(16, 2)
M = Int(Smax / dS)
N = Int(tyr / dt)
Dim matval()
ReDim matval(M, N)
' Cells(22, 1)
Range(Cells(22, 1), Cells(22 + M + 1, 2 + N + 3)).Select
Selection.ClearContents
'
Cells(22, 1) = "S"
For i = 0 To M
Cells(23 + i, 1) = Smax - i * dS
Next i
':
For j = 0 To N
Cells(22, 2 + j) = "t" & j
Next j
': abc
Cells(22, 2 + N + 1) = "a"
Cells(22, 2 + N + 2) = "b"
Cells(22, 2 + N + 3) = "c"
'show abc
' abc excel show
' S0 BS
Cells(4, 5) = BSOptionValue(iopt, S, x, r, q, tyr, sigma)
' S0 finite difference option price
Mo = M - S / dS
Cells(5, 5) = matval(Mo, 0)
End Sub