在Excel VBA中,我需要为Black-Scholes方法欧式期权定价编写2个公式;使用选择...案例

时间:2017-03-26 14:57:18

标签: excel vba excel-vba

它是怎么回事。 我需要你的帮助 - 问题在于主题 - 这就是我所拥有的:

看涨期权定价的第一个公式:

Function CallBS(Spot As Double, Strike As Double, Maturity As Double, Vol As Double, Rf As Double, Dividend As Double) As Double

Dim D1 As Double
Dim D2 As Double

    D1 = (Application.WorksheetFunction.Ln(Spot / Strike) + (Rf - Dividend + Vol * Vol / 2) * Maturity) / Vol * Sqr(Maturity)
    D2 = D1 - Vol * Sqr(Maturity)

    CallBS = Spot * Application.WorksheetFunction.NormSDist(D1) * Exp(-Dividend * Maturity) _
    - Application.WorksheetFunction.NormSDist(D2) * Strike * Exp(-Rf * Maturity)

End Function

这部分工作正常。

看跌期权定价的第二个公式:

Function PutBS(Spot As Double, Strike As Double, Maturity As Double, Vol As Double, Rf As Double, Dividend As Double) As Double

Dim D1 As Double
Dim D2 As Double

    D1 = (Application.WorksheetFunction.Ln(Spot / Strike) + (Rf - Dividend + Vol * Vol / 2) * Maturity) / Vol * Sqr(Maturity)
    D2 = D1 - Vol * Sqr(Maturity)

    PutBS = Strike * Application.WorksheetFunction.NormSDist(-D2) * Exp(-Rf * Maturity) _
    - Application.WorksheetFunction.NormSDist(-D1) * Spot * Exp(-Dividend * Maturity)

    End Function

这部分工作也很好。

现在,我需要使用附加参数编写另一个函数:选项类型:" c"或" p"对于call和put选项,这两个选项都是(公式)通用的。以下是我的开始:

Function OptnPrcng(OType As String, Spot As Double, Strike As Double, Maturity As Double, Vol As Double, Rf As Double, Dividend As Double) As Double

Dim D1 As Double
Dim D2 As Double
Dim CallBS As Double
Dim PutBS As Double

    D1 = (Application.WorksheetFunction.Ln(Spot / Strike) + (Rf - Dividend + Vol * Vol / 2) * Maturity) / Vol * Sqr(Maturity)
    D2 = D1 - Vol * Sqr(Maturity)

    Select Case OType
        Case "c" Or "C"
            OptnPrcng = Spot * Application.WorksheetFunction.NormSDist(D1) * Exp(-Dividend * Maturity) _
            - Application.WorksheetFunction.NormSDist(D2) * Strike * Exp(-Rf * Maturity)
        Case "p" Or "P"
            OptnPrcng = Strike * Application.WorksheetFunction.NormSDist(-D2) * Exp(-Rf * Maturity) _
            - Application.WorksheetFunction.NormSDist(-D1) * Spot * Exp(-Dividend * Maturity)
        Case Else: MsgBox "Choose |c| for call option or |p| for put option valuation"
    End Select


End Function

但它不起作用。它给了我一个#ARG错误。

1 个答案:

答案 0 :(得分:2)

试试这样。我真的不知道公式是做什么的,但它给出了一些结果:)

Option Explicit

Function OptnPrcng(OType As String, _
                Spot As Double, _
                Strike As Double, _
                Maturity As Double, _
                Vol As Double, _
                Rf As Double, _
                Dividend As Double) As Double

Dim D1 As Double
Dim D2 As Double
'Dim OType As String
Dim CallBS As Double
Dim PutBS As Double

    D1 = (Application.WorksheetFunction.Ln(Spot / Strike) + (Rf - Dividend + Vol * Vol / 2) * Maturity) / Vol * Sqr(Maturity)
    D2 = D1 - Vol * Sqr(Maturity)

    Select Case LCase(OType)
        Case "c":
            OptnPrcng = Spot * Application.WorksheetFunction.NormSDist(D1) * Exp(-Dividend * Maturity) _
            - Application.WorksheetFunction.NormSDist(D2) * Strike * Exp(-Rf * Maturity)
        Case "p":
            OptnPrcng = Strike * Application.WorksheetFunction.NormSDist(-D2) * Exp(-Rf * Maturity) _
            - Application.WorksheetFunction.NormSDist(-D1) * Spot * Exp(-Dividend * Maturity)
        Case Else: MsgBox "Choose |c| for call option or |p| for put option valuation"
    End Select


End Function

我使用Select Case稍微更改了LCase