JavaScript Function for Bond Pricing

Bonds are priced based on all present values of all coupons and the face values. Present value means how much it is worth to pay right now for something that we will get in the future. The counterpart is future value, which is the value we are expecting to get if we invest right now.

Bond Pricing Math Formula

For a 10-year bond the price is the following:
For a bond with a different maturity, we can just calculate its value with a little adjustment to the above formula by adding more coupon terms.

JavaSript Algorithm for Bond Pricing

Based on the bond pricing formula above, we see there are 4 factors that dictate the bond price, coupons, fave value, interes rate(r) and bond maturity.

Normally the fave value is $100, so in the following code, I will set the default to 100 for the face value. Coupons are the result of the face value times the coupon rate. The bond pricing function take 4 parameters, coupon rate, interest rate and maturity.

Playground

Coupon Rate: %
Interest Rate: %
Maturity: terms
The bond price is
function getBondPrice(cr,ir,m,fv=100){
    var coupon = fv * cr;
  	var presentValuesofCoupons = 0;
    for(let i = 1; i <= m; i++){
    	presentValuesofCoupons += coupon/Math.pow((1+ir),i);
    };
    presentValuesofCoupons += fv/Math.pow((1+ir),m);
    return presentValuesofCoupons;
 };

How to Use

When we are using this code, we need to match the term with interet rate and coupon rate. If the coupon payout is biannual, we need to match the coupno rate and interes rate by multiplying by 2 and dividing the term by 2.

Comments

Popular posts from this blog

How to Make A Reusable Image Slideshow HTML Component With Vanilla JavaScript

HTML Tags and Inline CSS that Work In Plotly.js Title

How to Type Spaces In HTML Input And Display In the Browser