VBA

In this lecture we calculate the convexity for an annual-paying bond.

We do it first by building up an Excel grid, showing how each element of convexity is calculated.

Because Excel (rather strangely) does not have its own built-in CONVEXITY function (similar to DURATION or MDURATION), we also cover how to build your own personal convexity function within Excel’s VBA development environment.

The code for that demonstration function, for you to copy and paste, can be found below:

Public Function AndysAnnualConvexity(dYTM As Double, _
                                     dCouponRate As Double, _
                                     iBondMaturity As Integer, _
                                     dFaceValue As Double) As Double
Dim dConvexity As Double
Dim dAnnualPayment As Double
Dim dPresentValue As Double
Dim dTotalPVs As Double
Dim dTotalConvexity As Double

dConvexity = 0
dAnnualPayment = 0
dPresentValue = 0
dTotalPVs = 0
dTotalConvexity = 0

Dim dCouponPayment As Double

dCouponPayment = dCouponRate * dFaceValue

Dim dYearCounter As Double

dYearCounter = 1

Do While dYearCounter <= iBondMaturity

    dAnnualPayment = dCouponPayment
    
    If dYearCounter = iBondMaturity Then
    
        dAnnualPayment = dAnnualPayment + dFaceValue
        
    End If
    
    dPresentValue = dAnnualPayment / _
                    (Application.WorksheetFunction.Power(1 + dYTM, dYearCounter))
    
    dTotalPVs = dTotalPVs + dPresentValue
    
    dConvexity = (1 / (Application.WorksheetFunction.Power(1 + dYTM, 2))) * _
                 (dPresentValue) * _
                 (Application.WorksheetFunction.Power(dYearCounter, 2) + dYearCounter)
                 
    dTotalConvexity = dTotalConvexity + dConvexity
    
    dYearCounter = dYearCounter + 1
    
Loop

AndysAnnualConvexity = (dTotalConvexity / dTotalPVs)

End Function

In our next lecture, we will implement convexity to predict future bond prices.

The full YouTube playlist of Securities Investment 101 lecture videos can be found by clicking here.

Please read our disclaimer.

{ 0 comments }

Commenting Excel Cells with VBA, Excel Library

by Andy Duncan on 24/05/2013

In this MithrilMoney Excel Library video, we explain how to use Excel’s built-in VBA programming environment to create a tool which can comment all the formulas in Excel spreadsheets.

No knowledge of VBA programming is required.

The code segment mentioned in the video is as follows:

Sub AndysComment()
    Dim Cmt As Comment
    Set Cmt = ActiveCell.Comment
    If Not Cmt Is Nothing Then Cmt.Delete
    ActiveCell.AddComment ActiveCell.Formula
    ActiveCell.Comment.Visible = True
    ActiveCell.Comment.Shape.TextFrame.AutoSize = True
    ActiveCell.Comment.Shape.TextFrame.Characters.Font.Bold = True
    ActiveCell.Comment.Shape.TextFrame.Characters.Font.Size = 12
End Sub

The full YouTube playlist of Excel Library lecture videos can be found by clicking here.

Please read our disclaimer.

{ 0 comments }