campo-sirio/gfm/bondprc.c

132 lines
3.5 KiB
C
Raw Normal View History

/* DEC *BondPrice(price, acc,yield, coupon, ppy, mop, dap, yrp, mom, dam, yrm)
*
* ARGUMENT
* DEC *price;
* DEC *yield;
* DEC *coupon;
* int ppy;
* int mop, dap, yrp;
* int mom, dam, yrm;
*
* DESCRIPTION
* Calculate the price of a bond given the desired yield, coupon rate
* for receiving interest on a bond, number of payments per year, and the
* puchase and maturity dates.
*
* SIDE EFFECTS
* None.
*
* RETURNS
* price if successful, otherwise GM_NULL
*
* POSSIBLE ERRORS
* GM_NULLPOINTER
* GM_INIT
* GM_ARGVAL
*
* AUTHOR
* Jared Levy
* Copyright (C) 1988-1990 Greenleaf Software Inc. All rights reserved.
*
* MODIFICATIONS
*
* Mark Nelson
* Fixed bond pricing to accurately convert effective to nominal before
* making calculations.
*
*/
#include <stdio.h>
#include "gmsystem.h"
DEC *BondPrice(price,acc,yield,coupon, ppy, mop, dap, yrp, mom, dam, yrm)
DEC *price, *yield, *coupon, *acc;
int ppy, mop, dap, yrp, mom, dam, yrm;
{
DEC dtemp, *temp=&dtemp, dtemp2, *temp2=&dtemp2;
DEC dnper, *nper=&dnper, dpmt, *pmt=&dpmt;
DEC dnyield, *nyield=&dnyield, dfvopi, *fvopi=&dfvopi;
DEC ddsc, *dsc=&ddsc, ddcs, *dcs=&ddcs;
DEC dopi, *opi=&dopi, dopinm1, *opinm1=&dopinm1;
int ndays, npay;
_MacStart(GM_BONDPRC);
_MacInVarD(yield);
_MacInVarD(coupon);
_MacOutVarD(price);
if (ppy<1||ppy>12||CompareDecimal(yield,&decMinusHundred)<=0) {
_MacErr(GM_ARGVAL);
_MacRet(GM_NULL);
}
/* calculate # of periods, including partial period */
ndays = DaysBetweenDates360(mom, dam, yrm, mop, dap, yrp);
if (ndays < 0) {
if (ndays!=GM_ARGVAL&&ndays!=GM_OVERFLOW)
_MacErr(GM_ARGVAL);
_MacRet(GM_NULL);
}
/* calculate number of interest payments remaining */
(void) ConvLongToDecimal(temp, (long)ppy * (long) ndays);
(void) ConvLongToDecimal(temp2, 360L);
(void) _DivDec80Bit(nper, temp, temp2);
(void) _TruncateDec80Bit(temp, nper, 0);
npay=temp->dc.sl[0]+1; /* int number of interest payments */
(void) _SubDec80Bit(dsc, nper, temp); /* days purchase - next int */
(void) _SubDec80Bit(dcs, &decOne, dsc); /* days last int - purchass */
if (_MacIsDecZ(dsc))
_MacDZero(dcs);
/* calculate coupon rate per period */
(void) ConvLongToDecimal(temp2, (long)ppy);
(void) _DivDec80Bit(pmt, coupon, temp2);
/* calculate yield per period */
_MacDCopy( nyield, yield );
nyield->dc.id += 2;
_AddDec80Bit( nyield, nyield, &decOne );
_LnDec80Bit( nyield, nyield);
_DivDec80Bit( nyield, nyield, temp2 );
_ExpDec80Bit( nyield, nyield );
_SubDec80Bit( nyield, nyield, &decOne );
if (CompareDecimal(nper, &decOne)<=0) { /* < 1 period to maturity */
(void) _AddDec80Bit(temp, &decHundred, pmt);
(void) _MulDec80Bit(temp2, dsc, nyield);
(void) _AddDec80Bit(temp2, temp2, &decOne);
(void) _DivDec80Bit(price, temp, temp2);
}
else { /* > 1 period to maturity */
(void) _AddDec80Bit(opi, nyield, &decOne);
(void) _IntPwrDec80Bit(opinm1, opi, -(npay-1));
(void) _MulDec80Bit(fvopi, &decHundred, opinm1);
if (_MacIsDecZ(nyield))
ConvLongToDecimal(temp, (long) npay);
else {
(void) _SubDec80Bit(temp, opi, opinm1);
(void) _DivDec80Bit(temp, temp, nyield);
}
(void) _MulDec80Bit(temp, temp, pmt);
(void) _AddDec80Bit(temp, temp, fvopi);
_MacDChgs(dsc);
(void) PowerDecimal(temp2, opi, dsc);
(void) _MulDec80Bit(price, temp, temp2);
}
(void) _MulDec80Bit(acc, pmt, dcs);
(void) _Sq5UnsTo4Uns(acc);
if (_MacIsDecZ(dsc))
(void) _SubDec80Bit(price, price, pmt);
else
(void) _SubDec80Bit(price, price, acc);
(void) _Sq5UnsTo4Uns(price);
_MacRet(price);
}