用于数值迭代的调试循环

时间:2015-06-24 01:27:45

标签: vba excel-vba excel

我正在创造美国风格选项的临界值近似值。我收到了错误" #Value!"仅在大约40次循环后(与计数器保持跟踪)。

经过一些试验和错误,我意识到它来自调用BlackScholes定价功能的循环部分。理论上,我希望迭代地针对现货价格运行一系列值,同时将其他变量固定在Black Scholes欧洲价格计算中。在修补之后,我把问题简化为这样一个事实,即在第一个循环后,它不再像我刚刚使用该迭代上的值那样计算Black Scholes,而我得到的值只是增加1,然后剔除经过40次错误值循环后出于一些非显而易见的原因。

所以下面我将代码截断为一个非常简单的骨架,这是我问题的本质。任何帮助将不胜感激。

 Function Looper(S As Double, K As Double, r As Double, t As Double, q As Double, Vol As Double) As Double
  Dim i As Double
For i = 100 To 150 Step 1#

MsgBox i
MsgBox BS(i, K, r, t, q, Vol, "Call") 'After the first loop the values are wrong, 
'What I'd like is, BS(100,...), BS(101,...),BS(102,...) which it is not. 
'Not sure what it's actually calculating, since the values are way off

  Next i

End Function

Public Function BS(S As Double, K As Double, r As Double, t As Double, q As Double, Vol As Double, CP As String) As Double

Dim volrootime As Double
Dim d1 As Double
Dim d2 As Double
Dim DiscF As Double
Dim DivF As Double
Dim topline1 As Double
Dim topline2 As Double
Dim topline As Double
Dim Price As Double

t = t / 365
r = r / 100
q = q / 100



DiscF = Exp(-r * t)
DivF = Exp(-q * t)
volrootime = (t ^ 0.5) * Vol


    topline1 = Log(S / K)
    topline2 = ((r - q) + ((Vol ^ 2) / 2)) * t
    topline = topline1 + topline2
    d1 = topline / volrootime
    d2 = d1 - volrootime

    If CP = "Call" Then

        ' Theta is in terms of Calendar days, changing the denominator to 252 changes it to trading days


        Price = (S * DivF * Bign(d1)) - (K * DiscF * Bign(d2))


 Else

    ' Theta is in terms of Calendar days, changing the denominator to 252 changes it to trading days



        Price = K * DiscF * Bign(-d2) - S * DivF * Bign(-d1)


End If
BS = Price
 End Function

1 个答案:

答案 0 :(得分:1)

每次调用BS函数时,r,t,q的值都会改变。如果它们必须保持不变,你应该在BS函数声明中使用ByVal,如下所示:

BS(S As Double, K As Double, ByVal r As Double, ByVal t As Double, ByVal q As Double, ...

默认情况下,参数通过引用传递,被调用函数的任何更改都会反映在调用函数中。

顺便说一句,在这个例子中,我在调试时不会使用消息框,而是使用debug.print这样:

Debug.Print "i=" & i & vbTab & "BS=" & BS(i, K, r, t, q, Vol, "Call")

通过按Ctl + G(转到)打开窗口进行打印。