简体   繁体   English

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

[英]In Excel VBA I need 2 write formula for Black-Scholes method European options pricing; Using Select … Case

how's it going. 怎么样了。 I need Your help again - question is in the topic - here is what I have: 我再次需要您的帮助-主题中的问题-这是我所拥有的:

1st formula for call option pricing: 看涨期权定价的第一个公式:

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

This part is working just fine. 这部分工作正常。

2nd formula for put option pricing: 认沽期权定价的第二个公式:

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

This part is working just fine as well. 这部分工作也很好。

Now, I need to code another function, with additional argument: option type: "c" or "p" that will be (the formula) universal for both options for call and for put option. 现在,我需要编写另一个函数,并带有附加参数:选项类型:“ c”或“ p”,它们对于调用和看跌期权都是(公式)通用的。 Here's how I start: 这是我的开始方式:

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

But it doesn't work. 但这是行不通的。 It gives me an #ARG error. 它给我一个#ARG错误。

Try it like this. 这样尝试。 I really do not know what the formula does, but it gives some result :) 我真的不知道该公式是做什么的,但是它给出了一些结果:)

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

I have changed the Select Case a bit, using LCase . 我使用LCase稍微更改了Select Case

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM