我正在构建一个债券计算器,我需要以我使用Excel作为指南和=YIELD()
函数的百分比来输出收益率。
在这些给定值的excel中,我返回5.181800
=YIELD(19-02-2015, 21-02-2019, 5.75/100, 102.031, 100, 2, 1)*100
=YIELD(SettlementDate, MaturityDate, CouponsRate, BondPrice, 100, Coupons PA, Basis) * 100
在Javascript中我写过
function dayDiff(date1, date2) {
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
return diffDays;
}
var settlement = '2015-02-19',
maturity = '2019-02-21',
settlement = new Date(settlement);
maturity = new Date(maturity);
var timeToMaturity = dayDiff(settlement, maturity);
var coupon = 5.75 / 100;
var yield = 102.031;
var redemption = 100;
var frequency = 2;
var price = 0.0;
if(timeToMaturity > 0) {
price += redemption;
}
var paymentTime = timeToMaturity;
while(paymentTime > 0) {
price += coupon/frequency;
// Discount back
price = price / (1.0 + yield / frequency);
paymentTime -= 1.0 / frequency;
}
var accrualPeriod = 0.0-paymentTime;
price *= Math.pow(1.0 + yield / frequency, accrualPeriod*frequency);
price -= coupon/frequency * accrualPeriod*frequency;
console.log(price * 100 * 100);
这会返回5.635542139153786
任何人都可以告诉我哪里出错了吗?