# Function OPTPrice

Function OPTPrice(N, K, T, stock, r, vol, H, OpType As String, ExType As String)

D_T = T / N

u = Exp(vol * D_T ^ 0.5)

dd = 1 / u

discnt = Exp(-r * D_T)

q = (discnt ^ -1 – dd) / (u – dd)

Dim i, j As Integer

Dim S() As Double

ReDim S(N + 1, N + 1) As Double

For i = 1 To N + 1

For j = i To N + 1

S(i, j) = stock * u ^ (j – i) * dd ^ (i – 1)

Next j

Next i

Dim OPT() As Double

ReDim OPT(N + 1, N + 1) As Double

For i = 1 To N + 1

Select Case OpType

Case “C”

OPT(i, N + 1) = Application.Max(S(i, N + 1) – K, 0)

Case “P”

OPT(i, N + 1) = Application.Max(K – S(i, N + 1), 0)

End Select

Next i

For j = N To 1 Step -1

For i = 1 To j

Select Case ExType

Case “A”

If OpType = “C” Then

OPT(i, j) = Application.Max(discnt * (q * OPT(i, j + 1) + (1 – q) * OPT(i + 1, j + 1)), S(i, j) – K)

ElseIf OpType = “P” Then

OPT(i, j) = Application.Max(discnt * (q * OPT(i, j + 1) + (1 – q) * OPT(i + 1, j + 1)), K – S(i, j))

End If

Case “E”

OPT(i, j) = discnt * (q * OPT(i, j + 1) + (1 – q) * OPT(i + 1, j + 1))

Case “B”

If S(i, j) > H Then

OPT(i, j) = discnt * (q * OPT(i, j + 1) + (1 – q) * OPT(i + 1, j + 1))

Else

OPT(i, j) = 0

End If

End Select

Next i

Next j

OPTPrice = OPT(1, 1)

End Function