A modular bond pricing engine implemented in JavaScript to compute clean price, dirty price, accrued interest, and yield-to-maturity (YTM) for both Government and Corporate bonds. A JavaScript-based bond pricing engine that calculates clean price, dirty price, yield-to-maturity (YTM), and accrued interest for government and corporate bonds. bond pricing in JavaScript
bond yield calculator JS
fixed income pricing code
YTM JavaScript formula
Supports different day count conventions and coupon frequencies such as monthly, semi-annual, and annual.
-
๐ฆ Government Bonds using 30E/360 day-count basis
-
๐ข Corporate Bonds using Actual/Actual basis
-
๐ Supports multiple coupon payment frequencies โ Monthly, Quarterly, Semi-Annual, Annual
-
๐ Calculates:
- Yield to Maturity (YTM)
- Clean and Dirty Prices
- Accrued Interest
- Future Cashflows
-
โ๏ธ Uses Newton-Raphson method for iterative yield solving
-
๐ก Modular structure with reusable classes
Provides core functionalities:
countDays30360(d1, m1, y1, d2, m2, y2)โ 30E/360 day countaccruedInterest()โ Calculates accrued interestfutureCashflows()โ Generates coupon and redemption cashflowsdiscountFactor(rate, t)โ Computes discount factor- Shared by both corporate and government bonds
Implements 30E/360 basis with government-specific logic:
_price()โ Calculates bond price given a yieldyieldFromPrice()โ Computes yield from price using NewtonโRaphson methodpriceFromYield()โ Returns clean and dirty pricenumPeriods()โ Determines remaining coupon periods- Supports half-yearly (semi-annual) coupons
Implements Actual/Actual day count with corporate bond behavior:
priceFromYield(ytm)โ Computes bond price for given yieldyieldFromPrice(cleanPrice)โ Computes yield iteratively- Uses real coupon date schedule and redemption structure
var all_payment = {
'Annual': "1",
'ANNUAL': "1",
'Once a year': "1",
'Half-yearly': "2",
'SEMI ANNUAL': "2",
"Twice a year": '2',
'Quarterly': "4",
'QUARTERLY': "4",
'Four times a year': '4',
'Monthly': "12",
"MONTHLY": '12',
'Twelve times a year': '12',
'On Maturity': "0"
}
## ๐งฎ Government Bond
ISIN='IN0020230085'
let face = 100;
let coupon = 7.18;
let freq = 2;
let redemptions = { "2033-08-14": 100 };
let settlement = new Date();
settlement.setDate(settlement.getDate() + 1);
let last_ip = new Date("2025-08-14");
let next_ip = new Date("2026-02-14");
let maturity_date = new Date("2033-08-14");
let govBond = new government(
face,
coupon,
freq,
[],
redemptions,
settlement,
"annual",
last_ip,
next_ip,
maturity_date
);
console.log(govBond.yieldFromPrice(100));Output Example:
{
"Days Since": 54,
"Coupon Days Duration": 180,
"Accrued Interest": 1.077,
"Clean Price": 100,
"Yield": 7.1777,
"Yield Annual": 7.1777
}## ๐งฎ Corporate Bond
ISIN='INE07HK07825'
let face = 100;
let coupon = 10.65;
let freq = all_payment['MONTHLY'];
let redemptions = {
"2027-05-12": 25,
"2027-06-12": 25,
"2027-07-12": 25,
"2027-08-12": 25
};
// let redemptions = {
// "2033-08-14": 100
// };
let settlement = new Date();
settlement.setDate(settlement.getDate() + 1);
let compound_based = "annual";
let last_ip = new Date("2025-10-12");
let nearest_date = new Date("2025-11-12");
let ipDates = [
"2025-10-12",
"2025-11-12",
"2025-12-12",
"2026-01-12",
"2026-02-12",
"2026-03-12",
"2026-04-12",
"2026-05-12",
"2026-06-12",
"2026-07-12",
"2026-08-12",
"2026-09-12",
"2026-10-12",
"2026-11-12",
"2026-12-12",
"2027-01-12",
"2027-02-12",
"2027-03-12",
"2027-04-12",
"2027-05-12",
"2027-06-12",
"2027-07-12",
"2027-08-12"
];
let corporate_bond = new corporate(
face,
coupon,
freq,
ipDates,
redemptions,
settlement,
compound_based,
last_ip,
nearest_date
);
console.log(corporate_bond.priceFromYield(11 / 100));
console.log('yeild: ' + corporate_bond.yieldFromPrice(100)*100);
console.log(BondPricer.prototype.newFutureCashflows.call(corporate_bond, 100000));Output Example:
{ "dirty": 100.147609, "clean": 100.2643, "accrued": -0.116712 }
yeild11.185949814866483
{
'2025-11-12': 904.5205479452055,
'2025-12-12': 875.3424657534244,
'2026-01-12': 904.5205479452055,
'2026-02-12': 904.5205479452055,
'2026-03-12': 816.986301369863,
'2026-04-12': 904.5205479452055,
'2026-05-12': 875.3424657534244,
'2026-06-12': 904.5205479452055,
'2026-07-12': 875.3424657534244,
'2026-08-12': 904.5205479452055,
'2026-09-12': 904.5205479452055,
'2026-10-12': 875.3424657534244,
'2026-11-12': 904.5205479452055,
'2026-12-12': 875.3424657534244,
'2027-01-12': 904.5205479452055,
'2027-02-12': 904.5205479452055,
'2027-03-12': 816.986301369863,
'2027-04-12': 904.5205479452055,
'2027-05-12': 25875.342465753423,
'2027-06-12': 25904.520547945205,
'2027-07-12': 25875.342465753423,
'2027-08-12': 25904.520547945205
}| Frequency Label | Value | Description |
|---|---|---|
Annual / ANNUAL |
1 | Once a year |
Half-yearly / SEMI ANNUAL |
2 | Twice a year |
Quarterly / QUARTERLY |
4 | Four times a year |
Monthly / MONTHLY |
12 | Twelve times a year |
On Maturity |
0 | Bullet repayment |
๐ฆ bond-pricing-js โโโ src/ โ โโโ BondPricer.js # Base class โ โโโ government.js # Government bond class โ โโโ corporate.js # Corporate bond class โโโ example.js # Example usage โโโ README.md # Documentation
- Yield to Maturity (YTM) โ Solved iteratively using Newton-Raphson
- Clean Price = Present Value of future cashflows
- Dirty Price = Clean Price + Accrued Interest
- Accrued Interest = Coupon ร (Days since last payment / Total days in period)
๐ฆ bond-pricing-js
โโโ BondPricer.js # Base class with core functions
โโโ government.js # Government bond logic (30E/360)
โโโ corporate.js # Corporate bond logic (Actual/Actual)
โโโ index.js # Example usage
โโโ README.md # Documentation
No external libraries required โ runs on plain JavaScript.
Sanjay ๐ India ๐ผ Web Developer transitioning to AI & Financial Engineering
This project is licensed under the MIT License โ see the LICENSE file for details. Feel free to use, modify, and distribute with attribution.