program not working...need somehelp
-
A quick explanation of what I am trying to achieve: for each bond that is selling in the stock market, there will be an issue date, maturity date, frequency of interest payments and lastly the interest rate. What I want is to have a function that allows me to input a date any time before the bond matures, and give me as an output a series of outstanding cashflows and the corresponding dates. Explanation of the variables in the code Valdate = todays date Ipos = indicator +1 or -1 depending if I have bought or borrowed the bond Notional = amount of bond D1 = date of bond issue D2 = date of maturity Freq = frequency of interest payments Coupon = interest rate V() = vector of all cashflow dates for the bond Cashflow = 2 by p matrix of the bond (dates and corresponding amounts) Code: Function bondcashflow(valdate, ipos, notional, d1, d2, freq, coupon) Dim p, m As Integer Dim v() Dim cashflow() ReDim cashflow(2, p) As Variant Dim n As Long n = Application.Round(DateDiff("d", d1, d2) / 365 * freq, 0) For i = 0 To n - 1 If freq = 1 Then v(i) = DateAdd("yyyy", 1 * i, d1) ElseIf freq = 2 Then v(i) = DateAdd("q", 2 * i, d1) ElseIf freq = 4 Then v(i) = DateAdd("q", 1 * i, d1) ElseIf freq = 12 Then v(i) = DateAdd("m", 1 * i, d1) End If m = 0 If valdate < v(i) Then m = m + 1 End If Next i p = n - m For k = 1 To p cashflow(1, k) = v(k + m - 1) Do While k < p cashflow(2, k) = ipos * coupon * notional Loop Next k cashflow(2, p) = ipos * (notional + coupon * notional) bondcashflow = cashflow End Function
-
A quick explanation of what I am trying to achieve: for each bond that is selling in the stock market, there will be an issue date, maturity date, frequency of interest payments and lastly the interest rate. What I want is to have a function that allows me to input a date any time before the bond matures, and give me as an output a series of outstanding cashflows and the corresponding dates. Explanation of the variables in the code Valdate = todays date Ipos = indicator +1 or -1 depending if I have bought or borrowed the bond Notional = amount of bond D1 = date of bond issue D2 = date of maturity Freq = frequency of interest payments Coupon = interest rate V() = vector of all cashflow dates for the bond Cashflow = 2 by p matrix of the bond (dates and corresponding amounts) Code: Function bondcashflow(valdate, ipos, notional, d1, d2, freq, coupon) Dim p, m As Integer Dim v() Dim cashflow() ReDim cashflow(2, p) As Variant Dim n As Long n = Application.Round(DateDiff("d", d1, d2) / 365 * freq, 0) For i = 0 To n - 1 If freq = 1 Then v(i) = DateAdd("yyyy", 1 * i, d1) ElseIf freq = 2 Then v(i) = DateAdd("q", 2 * i, d1) ElseIf freq = 4 Then v(i) = DateAdd("q", 1 * i, d1) ElseIf freq = 12 Then v(i) = DateAdd("m", 1 * i, d1) End If m = 0 If valdate < v(i) Then m = m + 1 End If Next i p = n - m For k = 1 To p cashflow(1, k) = v(k + m - 1) Do While k < p cashflow(2, k) = ipos * coupon * notional Loop Next k cashflow(2, p) = ipos * (notional + coupon * notional) bondcashflow = cashflow End Function
Danisto wrote:
What I want is to have a function that allows me to input a date any time before the bond matures, and give me as an output a series of outstanding cashflows and the corresponding dates.
OK. So what's the problem?
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
A quick explanation of what I am trying to achieve: for each bond that is selling in the stock market, there will be an issue date, maturity date, frequency of interest payments and lastly the interest rate. What I want is to have a function that allows me to input a date any time before the bond matures, and give me as an output a series of outstanding cashflows and the corresponding dates. Explanation of the variables in the code Valdate = todays date Ipos = indicator +1 or -1 depending if I have bought or borrowed the bond Notional = amount of bond D1 = date of bond issue D2 = date of maturity Freq = frequency of interest payments Coupon = interest rate V() = vector of all cashflow dates for the bond Cashflow = 2 by p matrix of the bond (dates and corresponding amounts) Code: Function bondcashflow(valdate, ipos, notional, d1, d2, freq, coupon) Dim p, m As Integer Dim v() Dim cashflow() ReDim cashflow(2, p) As Variant Dim n As Long n = Application.Round(DateDiff("d", d1, d2) / 365 * freq, 0) For i = 0 To n - 1 If freq = 1 Then v(i) = DateAdd("yyyy", 1 * i, d1) ElseIf freq = 2 Then v(i) = DateAdd("q", 2 * i, d1) ElseIf freq = 4 Then v(i) = DateAdd("q", 1 * i, d1) ElseIf freq = 12 Then v(i) = DateAdd("m", 1 * i, d1) End If m = 0 If valdate < v(i) Then m = m + 1 End If Next i p = n - m For k = 1 To p cashflow(1, k) = v(k + m - 1) Do While k < p cashflow(2, k) = ipos * coupon * notional Loop Next k cashflow(2, p) = ipos * (notional + coupon * notional) bondcashflow = cashflow End Function
Danisto, Just a brief glance through your code shows that in the For loop at the end you've embedded "Do While k < p". The loop has no exit; that is, it's an infinite loop. Also, the ReDim of the cashflow array uses p as the second dimension, but p hasn't been assigned a value at that point. Is your Code block really supposed to be pseudo-code? Regards, Steve Erbach Neenah, WI