使用Simulaitons定价欧洲期权

时间:2016-11-23 02:29:19

标签: vba excel-vba excel-formula userform finance

我创建了一个用户表单,允许用户更改选项定价所涉及的各种变量(行使价,波动率......等),同时允许用户更改达到价格所需的模拟(或平均值)在这种情况下的价格)。但是,单击确定按钮后,我无法在代码中调用公共子设备。任何关于我做错的建议都将不胜感激。 [我还在下面附上了我的用户表格的图片]

User Form

   Option Explicit
      Private cancel As Boolean

        Public Function ShowInputsDialog(currentPrice As Single, _
        exercisePrice As Single, riskfreeRate As Double, _
        volatility As Single, duration As Single, simulation As Double) As Boolean

    Call Initialize
    Me.Show
    If Not cancel Then

     'Capture the other inputs.
        currentPrice = txtCurrentPrice.Text
        exercisePrice = txtExercisePrice.Text
        riskfreeRate = txtRiskfreeRate.Text
        volatility = txtVolatility.Text
        duaration = txtDuration.Text
        simulation = txtSimulation.Text

     ShowInputsDialog = Not cancel
    Unload Me
    End Function

     Public Sub ErrorCheck()
    ' Perform error checking for user inputs.

    If IsNumeric(currentPrice) = False Or currentPrice < 0 Then
        MsgBox ("Please enter a numeric value for the Current Price")
    End If
    If IsNumeric(exercisePrice) = False Or exercusePrice < 0 Then
         MsgBox ("Please enter a positive numeric value for the exercise price")
    End If
    If IsNumeric(riskfreeRate) = False Then
        MsgBox ("Please enter a numerical value for the risk-free rate")
    End If
    If IsNumeric(volatility) = False Then
        MsgBox ("Please enter a numerical value for the Standard deviation")
    End If
    If IsNumeric(duration) = False Then
        MsgBox ("Please enter a numerical valye for duration")
    End If

    End Sub

    Public Sub Call_Eur(currentPrice As Single, _
        exercisePrice As Single, riskfreeRate As Double, _
        volatility As Single, duration As Single, simulation As Double)
        Dim stockPrice As Single
        Dim CallcashflowTermination As Single
        Dim PutcashflowTermination As Single
        Dim CalldiscountedValue As Double
        Dim PutdiscountedValue As Double
        Dim i As Integer
        Dim CallMean As Double
        Dim PutMean As Double
        Dim arrayCallPrice() As Integer
        Dim arrayPutPrice() As Integer
    For i = 1 To simulation
    ' stock price
    stockPrice = currentPrice * Exp((riskfreeRate - 0.5 * volatility ^ 2) * duration + volatility * Application.WorksheetFunction.Norm_Inv(Rnd(), 0, 1) * Sqr(duration))

    ' option cash flow at termination
    CallcashflowTermination = Application.WorksheetFunction.Max(0, stockPrice - exercisePrice)
    PutcashflowTerminatio = Application.WorksheetFunction.Funciton.Max(0, exercisePrice - stockPrice)

    ' discounted value of the option
    CalldiscountedValue = CallcashflowTermination * Exp(-duration * riskfreeRate)
    PutdiscountedValue = PutcashflowTermination * Exp(-duration * riskfreeRate)

    arrayCallPrice(i) = CalldiscountedValue
    arrayPutPrice(i) = PutdiscountedValue

    CallMean = Application.WorsheetFunction.Average(arrayCallPrice)
    PutMean = Application.WorksheetFunction.Average(arrayPutPrice)

    Next i

    MsgBox "The Call option price is " & CallMean & " the Put option price is " & PutMean

End Sub
 Private Sub CmdCancel_Click()
    Me.Hide
    cancel = True
End Sub

Private Sub CmdOK_Click() '<--- ERROR!!!
Call Call_Eur(currentPrice As Single, _
        exercisePrice As Single, riskfreeRate As Double, _
        volatility As Single, duration As Single, simulation As Double)
End Sub

Private Sub UserForm_Click()

End Sub

1 个答案:

答案 0 :(得分:0)

大红旗!!!!

enter image description here

调用子程序时。你需要将值传递给它。不重新定义它的参数。

Private Sub CmdOK_Click() '<--- ERROR!!!
    Call Call_Eur(12.50, 13.43, 14, 33.56, 100, 13.67)
End Sub

我更喜欢删除括号而不使用Call。

Private Sub CmdOK_Click() '<--- ERROR!!!
    Call_Eur 12.50, 13.43, 14, 33.56, 100, 13.67
End Sub