二叉樹

三叉樹VBA程式碼

  • March 27, 2017

我正在研究二叉樹,並在 VBA 中實現它們以查看它們與 BS 模型的收斂性。我在網上搜尋了 3-4 個小時;我知道的唯一好的網站是 Volopta。

一個初學者非常簡單的問題:我在哪裡可以找到三叉樹的 VBA 函式程式碼?

編輯:我在Rouah-Veinberg 書中找到了三叉樹的 VBA 函式程式碼;我在VBA上報告如下

Function Trinomial(Spot, K, T, r, v, n, PutCall As String, EuroAmer As String)

dt = T / n: u = Exp(v * Sqr(2 * dt)): d = 1 / u
pu = (Exp(r * dt / 2) - Exp(-v * Sqr(dt / 2))) ^ 2 / (Exp(v * Sqr(dt / 2)) - Exp(-v * Sqr(dt / 2))) ^ 2

pd = (Exp(v * Sqr(dt / 2)) - Exp(r * dt / 2)) ^ 2 / (Exp(v * Sqr(dt / 2)) - Exp(-v * Sqr(dt / 2))) ^ 2
pm = 1 - pu - pd
S(1, 1) = Spot
For j = 2 To (n + 1)
For i = 1 To (2 * j - 1)
S(i, j) = S(1, 1) * u ^ j * d ^ i
Next i
Next j


For i = 1 To (2 * n + 1)
Select Case PutCall
Case "Call"
Op(i, n + 1) = Application.Max(S(i, n + 1) - K, 0)
Case "Put"
Op(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 (2 * j - 1)
Select Case EuroAmer
Case "Amer":
If PutCall = "Call" Then
Op(i, j) = Application.Max(S(i, j) - K, Exp(-r * dt) * (pu * Op(i, j + 1) + pm * Op(i + 1, j + 1) + pd * Op(i + 2, j + 1)))
ElseIf PutCall = "Put" Then
Op(i, j) = Application.Max(K - S(i, j), Exp(-r * dt) * (pu * Op(i, j + 1) + pm * Op(i + 1, j + 1) + pd * Op(i + 2, j + 1)))
End If
Case "Euro":
Op(i, j) = Exp(-r * dt) * (pu * Op(i, j + 1) + pm * Op(i + 1, j + 1) + pd * Op(i + 2, j + 1))
End Select
Next i
Next j

End Function

但是當我回到 Excel 並將參數插入函式並按 Enter 時,出現以下視窗

編譯錯誤子或函式未定義

這段程式碼有什麼問題?我懷疑錯誤是否存在於我送出的數據中,因為我已經使用相同的數據成功實現了兩個不同的二叉樹。但是,我再說一遍,我是初學者,所以任何幫助將不勝感激。也許在網路上的某個地方我可以複製粘貼程式碼。

非常感謝

EDIT2我將程式碼的開頭更改如下:

Function Trinomial(Spot As Double, K As Double, T As Double, r As Double, v As Double, n As Integer, PutCall As String, EuroAmer As String)

然後,在行之前

dt = T / n: u = Exp(v * Sqr(2 * dt)): d = 1 / u

我添加了

Dim S() As Double
ReDim S(i, j) As Double

Dim Op() As Double
ReDim Op(n + 1, n + 1) As Double

Dim dt As Double
Dim Pu As Double
Dim Pd As Double
Dim Pm As Double

不碰其他任何東西。這樣我在VBA端就沒有問題了(特別是“編譯錯誤子或未定義函式”的消息不再出現);這很好。

現在問題出在EXCEL方面:當我使用這個功能時,出了點問題;在單元格中,出現的不是此函式產生的數字

“#價值!”

但我根本不知道出了什麼問題。

喬,

我前段時間寫了這個,可以稍微清理一下。它適用於歐洲看漲期權和看跌期權。我有幾行註釋掉了。我可能會加入美國定價,但從未這樣做。我剛剛測試了它,它仍然對我有用。請注意,我在電子表格上的輸入是以天為單位的,而不是以年為單位的。我在 VBA 中轉換為年數。見下文。

Function TrinomialTree(OptionType As String, S As Double, K As Double, _
               sigma As Double, rfr As Double, T As Double, _
               div As Double, n As Integer)

Dim Val() As Double
Dim up As Double
Dim dn As Double
Dim pUp As Double
Dim pDn As Double
Dim pStat As Double
Dim cst As Double
Dim dt As Double
Dim df As Double
Dim T1 As Double
Dim i As Integer
Dim j As Integer
Dim x As Integer
Dim OpType As Integer

ReDim Val(n * 2 + 1)

T1 = T / 365
dt = T1 / n
df = Exp(-rfr * dt)
cst = rfr - div

Select Case OptionType
   Case "EC"
       OpType = 1
   Case "EP"
       OpType = -1
'        Case "AC"
'            OpType = 1
'        Case "AP"
'            OpType = -1
End Select

up = Exp(sigma * Sqr(2 * dt))
dn = Exp(-sigma * Sqr(2 * dt))

pUp = ((Exp(cst * dt / 2) - Exp(-sigma * Sqr(dt / 2))) / _
       (Exp(sigma * Sqr(dt / 2)) - Exp(-sigma * Sqr(dt / 2)))) ^ 2
pDn = ((Exp(sigma * Sqr(dt / 2)) - Exp(cst * dt / 2)) / _
       (Exp(sigma * Sqr(dt / 2)) - Exp(-sigma * Sqr(dt / 2)))) ^ 2
pStat = 1 - (pUp + pDn)

For i = 0 To (n * 2)
   Val(i) = Application.WorksheetFunction.Max(0, OpType * (S * up ^ _
               Application.WorksheetFunction.Max(i - n, 0) * dn ^ _
               Application.WorksheetFunction.Max(n * 2 - n - i, 0) - K))
Next i

For x = n - 1 To 0 Step -1
   For i = 0 To (x * 2)
       Val(i) = (pUp * Val(i + 2) + pStat * Val(i + 1) + pDn * Val(i)) * df
   Next i
Next x

TrinomialTree = Val(0)

End Function

由於這已經偏離主題,如果您遇到困難,請隨時與我聯繫,我會盡力幫助您。

引用自:https://quant.stackexchange.com/questions/33223