java implementation of excel price,yield functions

2019-07-17 03:29发布

Was wondering if anyone can point me to an open source java quant library that provides implementation for Excel Price and Yield functions.

Thanks, Tapasvi

标签: java excel
2条回答
成全新的幸福
2楼-- · 2019-07-17 03:33

You may want to have a look at QuantLib. It's a free/open source library for quantitative finance.

查看更多
该账号已被封号
3楼-- · 2019-07-17 03:34

The Excel PRICE function is a simple sum of discounted cash flows, discounted assuming a flat zero rate discounting curve. It is documented here: Excel Help / PRICE.

I wrote a small Java version of the function, which I am posting below. The Java version just takes a "time to maturity" and does not support different daycountings, while the Excel PRICE function takes a settlement date and maturity date and supports different daycountings. However, you can attribute for the different daycountings by converting the coupon. Note also that the Excel function has a strange hardcoded notional of 100 in front of the coupon.

An Excel sheet benchmarking the two implementations can be downloaded here. The sheet requires "Obba".

The Excel YIELD function is just a Newton solver applied to the PRICE function, see Excel Help / YIELD. A Java implementation of the Newton solver can be found at finmath.net.

/*
 * Created on 07.04.2012
 */

/**
 * This class implements some functions as static class methods.
 *
 * (c) Copyright 2012 Christian Fries.
 *
 * @author Christian Fries
 * @version 1.0
 */
public class SpreadsheetFunctions {

    /**
     * Re-implementation of the Excel PRICE function (a rather primitive bond price formula).
     * The reimplementation is not exact, because this function does not consider daycount conventions.
     * We assume we have (int)timeToMaturity/frequency future periods and the running period has
     * an accrual period of timeToMaturity - frequency * ((int)timeToMaturity/frequency).
     * 
     * @param timeToMaturity The time to maturity.
     * @param coupon Coupon payment.
     * @param yield Yield (discount factor, using frequency: 1/(1 + yield/frequency).
     * @param redemption Redemption (notional repayment).
     * @param frequency Frequency (1,2,4).
     * @return price Clean price.
     */
    public static double price(
            double timeToMaturity,
            double coupon,
            double yield,
            double redemption,
            int frequency)
    {
        double price = 0.0;

        if(timeToMaturity > 0) {
            price += redemption;
        }

        double paymentTime = timeToMaturity;
        while(paymentTime > 0) {
            price += coupon/frequency;

            // Discount back
            price = price / (1.0 + yield / frequency);
            paymentTime -= 1.0 / frequency;
        }

        // Accrue running period
        double accrualPeriod = 0.0-paymentTime; // amount of running period which lies in the past (before settlement)
        price *= Math.pow(1.0 + yield / frequency, accrualPeriod*frequency);
        price -= coupon/frequency * accrualPeriod*frequency;

        return price;
    }
}
查看更多
登录 后发表回答