Bursa Malaysia

Pin Menu

Click here to unpin menu.
You're here: Market » Javascripts » bond_api.js

bond_api.js

var bond = { duration_fraction: function (sd,sm,sy,md,prevpm,nextpm) { var PDate = new Date((prevpm>0 ? sy:sy-1), (prevpm>0 ? prevpm-1:prevpm+11), md); var NDate = new Date((nextpm<12 ? sy:sy+1), (nextpm<12 ? nextpm-1:nextpm-13), md); var SDate = new Date(sy,sm-1,sd); return (NDate - SDate)/(NDate - PDate); }, duration: function (apf,sd,sm,sy,md,mm,my) { var mpp=12/apf; var n=(12*(my-sy)+mm-sm)/mpp; var ni=parseInt(n); if(ni==n) { return (sd == md) ? ni : (sd0?sm+dm:sm+dm+mpp; return ni + this.duration_fraction(sd,sm,sy,md,npm-mpp,npm); } }, price: function (ytm,coupon_rate,m,n) { var i = coupon_rate/m; var x = 1/(1+ytm/m); var xn = Math.pow(x,n); var ni = parseInt(n) == n ? n-1 : parseInt(n); var nf = n-ni; return ytm==0 ? (1 + i*(ni+1)) : (xn + i*(Math.pow(x,nf)-x*xn)/(1-x)); }, yp: function (ni,nf,pf,i,x) { var xf= nf==1 ? x : Math.pow(x,nf); return (((i+1)*x-1)*Math.pow(x,ni)-i)*xf-pf*(x-1); }, dp: function (ni,nf,pf,i,x) { var xf= nf==1 ? x : Math.pow(x,nf); var n = ni + nf; return (((i+1)*(n+1)-n/x)*Math.pow(x,ni)-i*nf/x)*xf-pf; }, ytm: function (pf,coupon_rate,m,n) { var i = coupon_rate/m; var ni= parseInt(n)==n ? n-1 : parseInt(n); var nf = n-ni; var x0 = 0.5; var tol_error = 0.000001; while (Math.abs(this.yp(ni,nf,pf,i,x0)) > tol_error) { x0 -= this.yp(ni,nf,pf,i,x0)/this.dp(ni,nf,pf,i, x0); } return m*(1/x0-1); } };