| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Financial; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 9 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 10 |
|
| 11 |
class TreasuryBill |
| 12 |
{ |
| 13 |
/** |
| 14 |
* TBILLEQ. |
| 15 |
* |
| 16 |
* Returns the bond-equivalent yield for a Treasury bill. |
| 17 |
* |
| 18 |
* @param mixed $settlement The Treasury bill's settlement date. |
| 19 |
* The Treasury bill's settlement date is the date after the issue date |
| 20 |
* when the Treasury bill is traded to the buyer. |
| 21 |
* @param mixed $maturity The Treasury bill's maturity date. |
| 22 |
* The maturity date is the date when the Treasury bill expires. |
| 23 |
* @param mixed $discount The Treasury bill's discount rate |
| 24 |
* |
| 25 |
* @return float|string Result, or a string containing an error |
| 26 |
*/ |
| 27 |
public static function bondEquivalentYield($settlement, $maturity, $discount) |
| 28 |
{ |
| 29 |
$settlement = Functions::flattenSingleValue($settlement); |
| 30 |
$maturity = Functions::flattenSingleValue($maturity); |
| 31 |
$discount = Functions::flattenSingleValue($discount); |
| 32 |
|
| 33 |
try { |
| 34 |
$settlement = FinancialValidations::validateSettlementDate($settlement); |
| 35 |
$maturity = FinancialValidations::validateMaturityDate($maturity); |
| 36 |
$discount = FinancialValidations::validateFloat($discount); |
| 37 |
} catch (Exception $e) { |
| 38 |
return $e->getMessage(); |
| 39 |
} |
| 40 |
|
| 41 |
if ($discount <= 0) { |
| 42 |
return ExcelError::NAN(); |
| 43 |
} |
| 44 |
|
| 45 |
$daysBetweenSettlementAndMaturity = $maturity - $settlement; |
| 46 |
$daysPerYear = Helpers::daysPerYear( |
| 47 |
Functions::scalar(DateTimeExcel\DateParts::year($maturity)), |
| 48 |
FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL |
| 49 |
); |
| 50 |
|
| 51 |
if ($daysBetweenSettlementAndMaturity > $daysPerYear || $daysBetweenSettlementAndMaturity < 0) { |
| 52 |
return ExcelError::NAN(); |
| 53 |
} |
| 54 |
|
| 55 |
return (365 * $discount) / (360 - $discount * $daysBetweenSettlementAndMaturity); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* TBILLPRICE. |
| 60 |
* |
| 61 |
* Returns the price per $100 face value for a Treasury bill. |
| 62 |
* |
| 63 |
* @param mixed $settlement The Treasury bill's settlement date. |
| 64 |
* The Treasury bill's settlement date is the date after the issue date |
| 65 |
* when the Treasury bill is traded to the buyer. |
| 66 |
* @param mixed $maturity The Treasury bill's maturity date. |
| 67 |
* The maturity date is the date when the Treasury bill expires. |
| 68 |
* @param mixed $discount The Treasury bill's discount rate |
| 69 |
* |
| 70 |
* @return float|string Result, or a string containing an error |
| 71 |
*/ |
| 72 |
public static function price($settlement, $maturity, $discount) |
| 73 |
{ |
| 74 |
$settlement = Functions::flattenSingleValue($settlement); |
| 75 |
$maturity = Functions::flattenSingleValue($maturity); |
| 76 |
$discount = Functions::flattenSingleValue($discount); |
| 77 |
|
| 78 |
try { |
| 79 |
$settlement = FinancialValidations::validateSettlementDate($settlement); |
| 80 |
$maturity = FinancialValidations::validateMaturityDate($maturity); |
| 81 |
$discount = FinancialValidations::validateFloat($discount); |
| 82 |
} catch (Exception $e) { |
| 83 |
return $e->getMessage(); |
| 84 |
} |
| 85 |
|
| 86 |
if ($discount <= 0) { |
| 87 |
return ExcelError::NAN(); |
| 88 |
} |
| 89 |
|
| 90 |
$daysBetweenSettlementAndMaturity = $maturity - $settlement; |
| 91 |
$daysPerYear = Helpers::daysPerYear( |
| 92 |
Functions::scalar(DateTimeExcel\DateParts::year($maturity)), |
| 93 |
FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL |
| 94 |
); |
| 95 |
|
| 96 |
if ($daysBetweenSettlementAndMaturity > $daysPerYear || $daysBetweenSettlementAndMaturity < 0) { |
| 97 |
return ExcelError::NAN(); |
| 98 |
} |
| 99 |
|
| 100 |
$price = 100 * (1 - (($discount * $daysBetweenSettlementAndMaturity) / 360)); |
| 101 |
if ($price < 0.0) { |
| 102 |
return ExcelError::NAN(); |
| 103 |
} |
| 104 |
|
| 105 |
return $price; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* TBILLYIELD. |
| 110 |
* |
| 111 |
* Returns the yield for a Treasury bill. |
| 112 |
* |
| 113 |
* @param mixed $settlement The Treasury bill's settlement date. |
| 114 |
* The Treasury bill's settlement date is the date after the issue date when |
| 115 |
* the Treasury bill is traded to the buyer. |
| 116 |
* @param mixed $maturity The Treasury bill's maturity date. |
| 117 |
* The maturity date is the date when the Treasury bill expires. |
| 118 |
* @param float|string $price The Treasury bill's price per $100 face value |
| 119 |
* @return string|float |
| 120 |
*/ |
| 121 |
public static function yield($settlement, $maturity, $price) |
| 122 |
{ |
| 123 |
$settlement = Functions::flattenSingleValue($settlement); |
| 124 |
$maturity = Functions::flattenSingleValue($maturity); |
| 125 |
$price = Functions::flattenSingleValue($price); |
| 126 |
|
| 127 |
try { |
| 128 |
$settlement = FinancialValidations::validateSettlementDate($settlement); |
| 129 |
$maturity = FinancialValidations::validateMaturityDate($maturity); |
| 130 |
$price = FinancialValidations::validatePrice($price); |
| 131 |
} catch (Exception $e) { |
| 132 |
return $e->getMessage(); |
| 133 |
} |
| 134 |
|
| 135 |
$daysBetweenSettlementAndMaturity = $maturity - $settlement; |
| 136 |
$daysPerYear = Helpers::daysPerYear( |
| 137 |
Functions::scalar(DateTimeExcel\DateParts::year($maturity)), |
| 138 |
FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL |
| 139 |
); |
| 140 |
|
| 141 |
if ($daysBetweenSettlementAndMaturity > $daysPerYear || $daysBetweenSettlementAndMaturity < 0) { |
| 142 |
return ExcelError::NAN(); |
| 143 |
} |
| 144 |
|
| 145 |
return ((100 - $price) / $price) * (360 / $daysBetweenSettlementAndMaturity); |
| 146 |
} |
| 147 |
} |
| 148 |
|