Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First pass at extracting Financial Price functions for Securities #1942

Merged
merged 5 commits into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -1983,17 +1983,17 @@ class Calculation
],
'PRICE' => [
'category' => Category::CATEGORY_FINANCIAL,
'functionCall' => [Financial::class, 'PRICE'],
'functionCall' => [Financial\Securities::class, 'price'],
'argumentCount' => '6,7',
],
'PRICEDISC' => [
'category' => Category::CATEGORY_FINANCIAL,
'functionCall' => [Financial::class, 'PRICEDISC'],
'functionCall' => [Financial\Securities::class, 'discounted'],
'argumentCount' => '4,5',
],
'PRICEMAT' => [
'category' => Category::CATEGORY_FINANCIAL,
'functionCall' => [Financial::class, 'PRICEMAT'],
'functionCall' => [Financial\Securities::class, 'maturity'],
'argumentCount' => '5,6',
],
'PROB' => [
Expand Down
183 changes: 41 additions & 142 deletions src/PhpSpreadsheet/Calculation/Financial.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ class Financial

const FINANCIAL_PRECISION = 1.0e-08;

private static function isValidFrequency($frequency)
{
if (($frequency == 1) || ($frequency == 2) || ($frequency == 4)) {
return true;
}

return false;
}

private static function interestAndPrincipal($rate = 0, $per = 0, $nper = 0, $pv = 0, $fv = 0, $type = 0)
{
$pmt = self::PMT($rate, $nper, $pv, $fv, $type);
Expand Down Expand Up @@ -1370,86 +1361,50 @@ public static function PPMT($rate, $per, $nper, $pv, $fv = 0, $type = 0)
return $interestAndPrincipal[1];
}

private static function validatePrice($settlement, $maturity, $rate, $yield, $redemption, $frequency, $basis)
{
if (is_string($settlement)) {
return Functions::VALUE();
}
if (is_string($maturity)) {
return Functions::VALUE();
}
if (!is_numeric($rate)) {
return Functions::VALUE();
}
if (!is_numeric($yield)) {
return Functions::VALUE();
}
if (!is_numeric($redemption)) {
return Functions::VALUE();
}
if (!is_numeric($frequency)) {
return Functions::VALUE();
}
if (!is_numeric($basis)) {
return Functions::VALUE();
}

return '';
}

/**
* PRICE.
*
* Returns the price per $100 face value of a security that pays periodic interest.
*
* @Deprecated 1.18.0
*
* @see Use the price() method in the Financial\Securities class instead
*
* @param mixed $settlement The security's settlement date.
* The security settlement date is the date after the issue date when the security
* is traded to the buyer.
* @param mixed $maturity The security's maturity date.
* The maturity date is the date when the security expires.
* @param float $rate the security's annual coupon rate
* @param float $yield the security's annual yield
* @param float $redemption The number of coupon payments per year.
* For annual payments, frequency = 1;
* for semiannual, frequency = 2;
* for quarterly, frequency = 4.
* @param int $frequency
* @param int $basis The type of day count to use.
* 0 or omitted US (NASD) 30/360
* 1 Actual/actual
* 2 Actual/360
* 3 Actual/365
* 4 European 30/360
*
* @return float|string Result, or a string containing an error
*/
public static function PRICE($settlement, $maturity, $rate, $yield, $redemption, $frequency, $basis = 0)
{
$settlement = Functions::flattenSingleValue($settlement);
$maturity = Functions::flattenSingleValue($maturity);
$rate = Functions::flattenSingleValue($rate);
$yield = Functions::flattenSingleValue($yield);
$redemption = Functions::flattenSingleValue($redemption);
$frequency = Functions::flattenSingleValue($frequency);
$basis = Functions::flattenSingleValue($basis);

$settlement = DateTime::getDateValue($settlement);
$maturity = DateTime::getDateValue($maturity);
$rslt = self::validatePrice($settlement, $maturity, $rate, $yield, $redemption, $frequency, $basis);
if ($rslt) {
return $rslt;
}
$rate = (float) $rate;
$yield = (float) $yield;
$redemption = (float) $redemption;
$frequency = (int) $frequency;
$basis = (int) $basis;

if (
($settlement > $maturity) ||
(!self::isValidFrequency($frequency)) ||
(($basis < 0) || ($basis > 4))
) {
return Functions::NAN();
}

$dsc = self::COUPDAYSNC($settlement, $maturity, $frequency, $basis);
$e = self::COUPDAYS($settlement, $maturity, $frequency, $basis);
$n = self::COUPNUM($settlement, $maturity, $frequency, $basis);
$a = self::COUPDAYBS($settlement, $maturity, $frequency, $basis);

$baseYF = 1.0 + ($yield / $frequency);
$rfp = 100 * ($rate / $frequency);
$de = $dsc / $e;

$result = $redemption / $baseYF ** (--$n + $de);
for ($k = 0; $k <= $n; ++$k) {
$result += $rfp / ($baseYF ** ($k + $de));
}
$result -= $rfp * ($a / $e);

return $result;
return Financial\Securities::price($settlement, $maturity, $rate, $yield, $redemption, $frequency, $basis);
}

/**
* PRICEDISC.
*
* Returns the price per $100 face value of a discounted security.
*
* @Deprecated 1.18.0
*
* @see Use the discounted() method in the Financial\Securities class instead
*
* @param mixed $settlement The security's settlement date.
* The security settlement date is the date after the issue date when the security is traded to the buyer.
* @param mixed $maturity The security's maturity date.
Expand All @@ -1467,34 +1422,18 @@ public static function PRICE($settlement, $maturity, $rate, $yield, $redemption,
*/
public static function PRICEDISC($settlement, $maturity, $discount, $redemption, $basis = 0)
{
$settlement = Functions::flattenSingleValue($settlement);
$maturity = Functions::flattenSingleValue($maturity);
$discount = (float) Functions::flattenSingleValue($discount);
$redemption = (float) Functions::flattenSingleValue($redemption);
$basis = (int) Functions::flattenSingleValue($basis);

// Validate
if ((is_numeric($discount)) && (is_numeric($redemption)) && (is_numeric($basis))) {
if (($discount <= 0) || ($redemption <= 0)) {
return Functions::NAN();
}
$daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis);
if (!is_numeric($daysBetweenSettlementAndMaturity)) {
// return date error
return $daysBetweenSettlementAndMaturity;
}

return $redemption * (1 - $discount * $daysBetweenSettlementAndMaturity);
}

return Functions::VALUE();
return Financial\Securities::discounted($settlement, $maturity, $discount, $redemption, $basis);
}

/**
* PRICEMAT.
*
* Returns the price per $100 face value of a security that pays interest at maturity.
*
* @Deprecated 1.18.0
*
* @see Use the maturity() method in the Financial\Securities class instead
*
* @param mixed $settlement The security's settlement date.
* The security's settlement date is the date after the issue date when the security is traded to the buyer.
* @param mixed $maturity The security's maturity date.
Expand All @@ -1513,47 +1452,7 @@ public static function PRICEDISC($settlement, $maturity, $discount, $redemption,
*/
public static function PRICEMAT($settlement, $maturity, $issue, $rate, $yield, $basis = 0)
{
$settlement = Functions::flattenSingleValue($settlement);
$maturity = Functions::flattenSingleValue($maturity);
$issue = Functions::flattenSingleValue($issue);
$rate = Functions::flattenSingleValue($rate);
$yield = Functions::flattenSingleValue($yield);
$basis = (int) Functions::flattenSingleValue($basis);

// Validate
if (is_numeric($rate) && is_numeric($yield)) {
if (($rate <= 0) || ($yield <= 0)) {
return Functions::NAN();
}
$daysPerYear = Financial\Helpers::daysPerYear(DateTime::YEAR($settlement), $basis);
if (!is_numeric($daysPerYear)) {
return $daysPerYear;
}
$daysBetweenIssueAndSettlement = DateTime::YEARFRAC($issue, $settlement, $basis);
if (!is_numeric($daysBetweenIssueAndSettlement)) {
// return date error
return $daysBetweenIssueAndSettlement;
}
$daysBetweenIssueAndSettlement *= $daysPerYear;
$daysBetweenIssueAndMaturity = DateTime::YEARFRAC($issue, $maturity, $basis);
if (!is_numeric($daysBetweenIssueAndMaturity)) {
// return date error
return $daysBetweenIssueAndMaturity;
}
$daysBetweenIssueAndMaturity *= $daysPerYear;
$daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis);
if (!is_numeric($daysBetweenSettlementAndMaturity)) {
// return date error
return $daysBetweenSettlementAndMaturity;
}
$daysBetweenSettlementAndMaturity *= $daysPerYear;

return (100 + (($daysBetweenIssueAndMaturity / $daysPerYear) * $rate * 100)) /
(1 + (($daysBetweenSettlementAndMaturity / $daysPerYear) * $yield)) -
(($daysBetweenIssueAndSettlement / $daysPerYear) * $rate * 100);
}

return Functions::VALUE();
return Financial\Securities::maturity($settlement, $maturity, $issue, $rate, $yield, $basis);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/Financial/Coupons.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ private static function validateFrequency($frequency): int
return $frequency;
}

private static function validateBasis($basis)
private static function validateBasis($basis): int
{
if (!is_numeric($basis)) {
throw new Exception(Functions::NAN());
Expand Down
Loading