Consulting

Results 1 to 2 of 2

Thread: Problem with VBA code - simulating profit and loss from delta hedging.

  1. #1
    VBAX Newbie
    Joined
    Dec 2015
    Posts
    1
    Location

    Problem with VBA code - simulating profit and loss from delta hedging.

    Hello,

    Can somebody help me with the following problem?
    The point is to calculate function - profit and loss from a delta hedging.
    The code seems to be ok but there is a problem when calling it from the worksheet, there is a notification #ARG!

    The code is:

    Function PnL(N, T, S, mu, X, r, v, d)
    
    Dim Stock() As Double
    Dim delta() As Double
    Dim Sigma As Double
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim dt As Double
    
    dt = T / (N + 1)
    Stock(0) = S
    
    For i = 1 To N + 1
        Stock(i) = Stock(i - 1) * Exp((mu - 0.5 * v ^ 2) * dt + Application.WorksheetFunction.NormSInv(Application.WorksheetFunction.RandBetween(0, 1)) * v * Sqr(dt))
    Next i
    For j = 0 To N
        delta(j) = OptionDelta("C", Stock(j), X, T - j * dt, r, v, d)
    Next j
    
    Sigma = delta(0) * Stock(0)
    
    For k = 1 To N
        Sigma = Sigma * Exp(r * dt) + (delta(k) - delta(k - 1)) * Stock(k)
    Next k
    
    
    PnL = Application.WorksheetFunction.Max(0, Stock(N + 1) - X) - delta(N) * Stock(N + 1) + Sigma * Exp(r * dt) - Exp(r * T) * OptionPrice("C", S, X, T, r, v, d)
    
    End Function
    Last edited by Bob Phillips; 12-31-2015 at 10:34 AM. Reason: Added VBA tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I think you need to add this line at the head of the code, at the least

    ReDim Stock(0 To N + 1)
    Redim delta(0 to N)
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •