Bursa Malaysia

Pin Menu

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

bond_calculator.js

(function($){ function get_bond_period(settlement_date, maturity_date, payment_per_year) { var settlement_date_arr = settlement_date.split('/'); var maturity_date_arr = maturity_date.split('/'); return bond.duration(payment_per_year, parseInt(settlement_date_arr[0]), parseInt(settlement_date_arr[1]), parseInt(settlement_date_arr[2]), parseInt(maturity_date_arr[0]), parseInt(maturity_date_arr[1]), parseInt(maturity_date_arr[2])); } function addCommas(nStr) { nStr += ''; var x = nStr.split('.'); var x1 = x[0]; var x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; } var brokerage_charge = 0.3/100; var clearing_charge = 0.03/100; var trading_charge = 0.0025/100; $(document).ready(function(){ $("#bm_bond_calculator input.date").datepicker({ dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true, dpSetSelected: new Date() }); $("#bm_bond_calculator input[name='yield_cal[settlement_date]']").datepicker('setDate', new Date()); $("#bm_bond_calculator input[name='price_cal[settlement_date]']").datepicker('setDate', new Date()); $('#bm_bond_calculator').live('submit', function(event){ event.preventDefault(); var jele = $(this); var calculators = ['yield_cal', 'price_cal']; for(var i = 0; i < calculators.length; i++) { var calculator = calculators[i]; var settlement_date = jele.find("input[name='" + calculator + "[settlement_date]']").val(); var maturity_date = jele.find("input[name='" + calculator + "[maturity_date]']").val(); var coupon_rate = parseFloat(jele.find("input[name='" + calculator + "[coupon_rate]']").val())/100.0; var price = parseFloat(jele.find("input[name='" + calculator + "[price]']").val()); //calculate yield var payment_per_year = 2;//default to 2 var facevalue = 100.0; var period = get_bond_period(settlement_date, maturity_date, payment_per_year); var accrued = (Math.ceil(period) - period) * coupon_rate * facevalue/2; //default face value 100 var quantity = parseFloat(jele.find("input[name='" + calculator + "[quantity]']").val()); var stamp_duty = parseFloat(jele.find("input[name='" + calculator + "[stamp_duty]']").val()); var net_proceed = quantity * price; var brokerage_fee = net_proceed * brokerage_charge; var clearing_fee = net_proceed * clearing_charge; var trading_fee = net_proceed * trading_charge; if (calculator == 'yield_cal') { var ytm = 100 * bond.ytm((price + accrued)/facevalue, coupon_rate, payment_per_year, period); jele.find("input[name='" + calculator + "[yield]']").val(ytm.toFixed(3)); var total_proceeds = net_proceed + brokerage_fee + clearing_fee + trading_fee + stamp_duty; } else if (calculator == 'price_cal') { var ytm = parseFloat(jele.find("input[name='" + calculator + "[yield]']").val())/100.0; var price = facevalue * bond.price(ytm, coupon_rate, payment_per_year, period) - accrued; jele.find("input[name='" + calculator + "[price]']").val(price.toFixed(3)); var total_proceeds = net_proceed - brokerage_fee - clearing_fee - trading_fee - stamp_duty; } jele.find("input[name='" + calculator + "[tran_price]']").val(price.toFixed(3)); jele.find("input[name='" + calculator + "[net_proceed]']").val(addCommas(net_proceed.toFixed(2))); jele.find("input[name='" + calculator + "[brokerage_fee]']").val(brokerage_fee.toFixed(2)); jele.find("input[name='" + calculator + "[clearing_fee]']").val(clearing_fee.toFixed(2)); jele.find("input[name='" + calculator + "[trading_fee]']").val(trading_fee.toFixed(2)); jele.find("input[name='" + calculator + "[total_proceeds]']").val(addCommas(total_proceeds.toFixed(2))); } }); }); })(jQuery);