Monday, November 21, 2011

Calculate Bond Price in Java

Calculate Bond Price in Java

We tried all the tools to calculate the BondPrice .finaly nothing achived with third party tools .
Luckly we found the following class to calculate the yeild price of the Bond. This class is more accurate
than any other tool.
  java.text.DateFormat;
import
java.text.SimpleDateFormat;
import
java.util.Calendar;
import
 
java.util.Date;
public
Calendar maturity = Calendar.getInstance();
maturity.setTime(formatDate(
Calendar settlement = Calendar.getInstance();
settlement.setTime(formatDate(
Price price =
class Price {public static void main(String[] args) {"08/28/2012"));"02/29/2012"));new Price();try {double priceVa2 = price.tbondPrice( new Date(settlement.getTimeInMillis()), // settlementDate
new Date(maturity.getTimeInMillis()), // maturityDate
0.0865,
// rate
0.0895205173068493,
// yield
100.,
// redemption
2,
// frequency
0
// basis
);
System.
}
e.printStackTrace();
}
}
{
Calendar cal=
Date date1 =
out.println(priceVa2);catch (Exception e) {public static Date formatDate(String date)null;null; try
{
DateFormat formatter ;
formatter =
date1 = (Date)formatter.parse(date);
cal=Calendar.getInstance();
cal.setTime(date1);
}
e.printStackTrace();
}
}
{
{
{
paramCalendar1.add(5, -1);
}
{
paramCalendar2.add(5, 1);
}
{
paramCalendar2.add(5, -1);
}
}
new SimpleDateFormat("MM/dd/yyyy");catch(Exception e){return date1; private long days360(Calendar paramCalendar1, Calendar paramCalendar2, boolean paramBoolean)int i = paramCalendar1.get(5);int j = paramCalendar2.get(5);int k = paramCalendar1.get(2);int l = paramCalendar2.get(2);int i1 = paramCalendar1.get(1);int i2 = paramCalendar2.get(1);if (!(paramBoolean))if (i > 30)if ((j == 31) && (i > 30))else if (j == 31)else
{
{
paramCalendar1.add(5, -1);
}
{
paramCalendar2.add(5, -1);
}
}
i = paramCalendar1.get(5);
j = paramCalendar2.get(5);
}
{
{
{
d1 += paramDouble2 * Math.pow(d3, d2 * -1.0D);
d2 += 1.0D;
}
}
d1 -= paramDouble4;
}
{
d1 = 0.0D;
d1 = 100.0D * paramDouble1 / frequency;
}
Calendar SettlementDt = Calendar.getInstance();
SettlementDt.setTimeInMillis(paramDate1.getTime());
Calendar maturityDt = Calendar.getInstance();
maturityDt.setTimeInMillis(paramDate2.getTime());
Calendar prevCpnDt = Calendar.getInstance();
{
prevCpnDt.set(SettlementDt.get(1) + 1, maturityDt.get(2), maturityDt.get(5) - 1);
}
if (i > 30)if (j > 30)int i3 = (i2 - i1) * 12 + l - k;return (i3 * 30 + j - i);private double calculatedPrice(double paramDouble1, double paramDouble2, double paramDouble3, double paramDouble4, double paramDouble5, int paramInt1, int paramInt2)int j = paramInt1;double d3 = 1.0D + paramDouble1 / paramInt2;double d2 = paramDouble5;double d1 = paramDouble3 * Math.pow(d3, (j - 1 + d2) * -1.0D);if (paramDouble2 > 0.0D)for (int i = 1; i <= j; ++i)return d1;public double tbondPrice(Date paramDate1, Date paramDate2, double paramDouble1, double paramDouble2, double paramDouble3, int frequency, int paramInt2)throws Exceptiondouble d1;if (paramDouble1 == 0.0D)else {int j = 12 / frequency;if (((SettlementDt.get(1) + 1) % 4 != 0) && (maturityDt.get(2) == 2) && (maturityDt.get(5) == 29))else
{
prevCpnDt.set(SettlementDt.get(1) + 1, maturityDt.get(2), maturityDt.get(5));
}
}
}
{
prevCpnDt.set(maturityDt.get(1), maturityDt.get(2), maturityDt.get(5));
}
{
prevCpnDt.add(2, j * -1);
}
Calendar nextCpnDt = Calendar.getInstance();
nextCpnDt.set(prevCpnDt.get(1), prevCpnDt.get(2), prevCpnDt.get(5));
nextCpnDt.add(2, j);
{
l2 = days360(prevCpnDt, nextCpnDt,
l1 = days360(SettlementDt, nextCpnDt,
}
if (maturityDt.getTimeInMillis() < SettlementDt.getTimeInMillis()) {throw new Exception("The maturity date mast be greater then settlement date.");if (paramDouble3 < 0.0D)throw new Exception("The redemption price must be grater than zero.");if ((frequency != 1) && (frequency != 2) && (frequency != 3) && (frequency != 4) && (frequency != 6) && (frequency != 12))throw new Exception("The frequency must be 1, 2, 3, 4, 6, or 12.");if ((paramInt2 != 1) && (paramInt2 != 0)) {throw new Exception("The basis must be 1 or 0.");if (prevCpnDt.getTimeInMillis() > maturityDt.getTimeInMillis())while (prevCpnDt.getTimeInMillis() > SettlementDt.getTimeInMillis())int maxPeriod = (maturityDt.get(1) - prevCpnDt.get(1)) * 12 + maturityDt.get(2) - prevCpnDt.get(2);int i = maxPeriod / j;long l2;long l1;if (paramInt2 == 0)true);true);else
{
l2 = nextCpnDt.getTimeInMillis() - prevCpnDt.getTimeInMillis();
l1 = nextCpnDt.getTimeInMillis() - SettlementDt.getTimeInMillis();
}
{
d4 = (paramDouble3 + d1) / (paramDouble2 * d5 + 1.0D) - d3;
}
d4 = calculatedPrice(paramDouble2, d1, paramDouble3, d3, d2, i, frequency);
}
}
{
d += paramArrayOfDouble1[j] * Math.exp(-paramArrayOfDouble3[j] * paramArrayOfDouble2[j]);
}
}
{
d5 = 0.0D;
d5 = 100.0D * paramDouble1 / paramInt1;
}
Calendar localCalendar1 = Calendar.getInstance();
localCalendar1.setTimeInMillis(paramDate1.getTime());
Calendar localCalendar2 = Calendar.getInstance();
localCalendar2.setTimeInMillis(paramDate2.getTime());
}
Calendar localCalendar3 = Calendar.getInstance();
{
localCalendar3.set(localCalendar1.get(1) + 1, localCalendar2.get(2), localCalendar2.get(5) - 1);
}
double d2 = Double.parseDouble(""+l1) / Double.parseDouble(""+l2);double d3 = d1 * (1.0D - d2);double d4;if ((i == 1) || (i == 0))double d5 = d2 / frequency;else {return d4;public double priceOfBond(double paramDouble1, double paramDouble2, double paramDouble3, double[] paramArrayOfDouble1, double[] paramArrayOfDouble2, double[] paramArrayOfDouble3)double d = 0.0D;int i = paramArrayOfDouble1.length;for (int j = 0; j < i; ++j) {return (paramDouble1 * Math.exp(-paramDouble3 * paramDouble2) + d);public double yieldToMaturityFromPrice(Date paramDate1, Date paramDate2, double paramDouble1, double paramDouble2, double paramDouble3, int paramInt1, int paramInt2)throws Exceptiondouble d5;if (paramDouble1 == 0.0D)else {int k = 12 / paramInt1;if (localCalendar2.getTimeInMillis() < localCalendar1.getTimeInMillis())throw new Exception("The maturity date mast be greater then settlement date");if (paramDouble2 < 0.0D)throw new Exception("The price must be strictly positive.");if (paramDouble3 < 0.0D)throw new Exception("The redemption price must be grater than zero.");if ((paramInt1 != 1) && (paramInt1 != 2) && (paramInt1 != 3) && (paramInt1 != 4) && (paramInt1 != 6) && (paramInt1 != 12))throw new Exception("The frequency must be 1, 2, 3, 4, 6, or 12.");if ((paramInt2 != 1) && (paramInt2 != 0)) {throw new Exception("The basis must be 1 or 0.");if (((localCalendar1.get(1) + 1) % 4 != 0) && (localCalendar2.get(2) == 2) && (localCalendar2.get(5) == 29))else
{
localCalendar3.set(localCalendar1.get(1) + 1, localCalendar2.get(2), localCalendar2.get(5));
}
{
localCalendar3.set(localCalendar2.get(1), localCalendar2.get(2), localCalendar2.get(5));
}
{
localCalendar3.add(2, k * -1);
}
Calendar localCalendar4 = Calendar.getInstance();
localCalendar4.set(localCalendar3.get(1), localCalendar3.get(2), localCalendar3.get(5));
localCalendar4.add(2, k);
{
l2 = days360(localCalendar3, localCalendar4,
l1 = days360(localCalendar1, localCalendar4,
}
if (localCalendar3.getTimeInMillis() > localCalendar2.getTimeInMillis())while (localCalendar3.getTimeInMillis() > localCalendar1.getTimeInMillis())int l = (localCalendar2.get(1) - localCalendar3.get(1)) * 12 + localCalendar2.get(2) - localCalendar3.get(2);int j = l / k;long l2;long l1;if (paramInt2 == 0)false);false);else
{
l2 = localCalendar4.getTimeInMillis() - localCalendar3.getTimeInMillis();
l1 = localCalendar4.getTimeInMillis() - localCalendar1.getTimeInMillis();
}
{
d4 = yieldWith1Coupon(d5, paramDouble3, paramDouble2, d7, d6, paramInt1);
}
double d6 = l1 / l2;double d7 = d5 * (1.0D - d6);double d4;if ((j == 1) || (j == 0))else
{
{
d2 = 0.1D;
}
{
d2 *= 2.0D;
}
d4 = 0.5D * (d3 + d2);
{
}
{
d3 = d4;
}
double d3 = -1.0D;double d2 = paramDouble1;if (d2 == 0.0D)while (calculatedPrice(d2, d5, paramDouble3, d7, d6, j, paramInt1) > paramDouble2)for (int i = 1; i <= 200; ++i)double d1 = calculatedPrice(d4, d5, paramDouble3, d7, d6, j, paramInt1) - paramDouble2;if (Math.abs(d1) < 0.0001D) {break;if (d1 > 0.0D)else
{
d2 = d4;
}
d4 = 0.5D * (d3 + d2);
}
}
}
{
}
}
return d4;private double yieldWith1Coupon(double paramDouble1, double paramDouble2, double paramDouble3, double paramDouble4, double paramDouble5, int paramInt)double d3 = paramDouble2 + paramDouble1;double d1 = paramDouble3 + paramDouble4;double d2 = d3 / d1 - 1.0D;double d4 = paramDouble5 / paramInt;return (d2 / d4);


import