M Smith
M Smith

Reputation: 1

Why does #VALUE error in VBA function occur when creating a ReDim Matrix of Stock prices from a Named Range?

'This VBA binomial option code contains missing syntax intended to create an N by N matrix of stock prices using inputs for Pricing Binomial Tree Options with discrete dividends saved as a Named Range "Dividends" in MS Excel.

The idea is that the final stock matrix is used to recursively price a final option value calculation although the Function Binomial is returning a #VALUE. Please assist in helping to determine which Dim and ReDim statements, etc must be added to the ndivs, divsum, etc, to create the First Stock Matrix section, and Final Stock Matrix to calculate the Remaining entries for the Calls and Puts section. Thank you.'

Function Binomial(Spot, K, T, r, sigma, n, PutCall As String, EuroAmer As String, Dividends) 
    dt = T / n: u = Exp(sigma \* (dt \^ 0.5)):
    d=1/u a = Exp(r \* dt):
    p = (a - d) / (u - d) ndivs = Application.Count(Dividends) / 3
 
    Dim ndivs As Double

    ndivs = Application.Count(Dividends) / 3

    
    ' Tree for stock price
    Dim S() As Double
    Dim Tau() As Double
    Dim Div() As Double
    Dim Rate() As Double
    Dim divsum() As Double


    'Creates arrays with The dividend payments contained in array Div(), the time of the
    '  payments in array   Tau(), and the interest rate applied to each dividend payment in array 

    divsum = 0
    For i = 1 To ndivs
        Tau(i) = Dividends(i, 1) 
        Div(i) = Dividends(i, 2)
        Rate(i) = Dividends(i, 3)
        divsum = divsum + Div(i) * Exp(-Rate(i) * tau(i))
    Next i 

    'First Stock Matrix temp (i, j) for creating initial Stock Prices
    Dim Temp() As Double
    ReDim Temp(n + 1, n + 1) As Double
    temp(1, 1) = Spot - divsum
    For i = 1 To n + 1
        For j = i To n + 1
            temp(i, j) = temp(1, 1) * u ^ (j - i) * d ^ (i - 1)
        Next j
    Next i

    'Final Stock Matrix S( i, j) for adding first stock matrix with additional 
    '     discounted dividends from     Div()
    

    ReDim S(n + 1, n + 1) As Double
    S(1, 1) = temp(1, 1) + divsum
    For j = 2 To n + 1
        For i = 1 To j
            If tau(1) < (j - 1) * dt Then S(i, j) = temp(i, j)
            Else S(i, j) = temp(i, j) + Div(1) * Exp(-Rate(1) * (tau(1) - (j - 1) * dt))
        Next i
    Next j

    For z = 2 To ndivs
        For j = 2 To n + 1
           For i = 1 To j
             If tau(z) < (j - 1) * dt Then S(i, j) = S(i, j)
             Else S(i, j) = S(i, j) + Div(z)* Exp(-Rate(z) * (tau(z) - (j - 1) * dt))
           Next i
        Next j
    Next z
End Function

'Initially, the model contains dividend payments within in array Div(), the time of the payments in array Tau(), and the interest rate applied to each dividend payment in array Rate().

Next the option creates a First Stock Matrix temp(i, j) for creating initial Stock Prices.

Lastly, there is a Final Stock Matrix created to add the first stock matrix with additional discounted dividends to feed into the final option value calculation.

Upvotes: 0

Views: 42

Answers (0)

Related Questions