tablepress
/
libraries
/
vendor
/
PhpSpreadsheet
/
Calculation
/
Financial
/
Securities
/
Yields.php
Yields.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Calculation/Financial/Securities/Yields.php
| 1 | <?php |
| 2 | |
| 3 | namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Financial\Securities; |
| 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\Financial\Helpers; |
| 9 | use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 10 | use TablePress\PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
| 11 | |
| 12 | class Yields |
| 13 | { |
| 14 | /** |
| 15 | * YIELDDISC. |
| 16 | * |
| 17 | * Returns the annual yield of a security that pays interest at maturity. |
| 18 | * |
| 19 | * @param mixed $settlement The security's settlement date. |
| 20 | * The security's settlement date is the date after the issue date when the security |
| 21 | * is traded to the buyer. |
| 22 | * @param mixed $maturity The security's maturity date. |
| 23 | * The maturity date is the date when the security expires. |
| 24 | * @param mixed $price The security's price per $100 face value |
| 25 | * @param mixed $redemption The security's redemption value per $100 face value |
| 26 | * @param mixed $basis The type of day count to use. |
| 27 | * 0 or omitted US (NASD) 30/360 |
| 28 | * 1 Actual/actual |
| 29 | * 2 Actual/360 |
| 30 | * 3 Actual/365 |
| 31 | * 4 European 30/360 |
| 32 | * |
| 33 | * @return float|string Result, or a string containing an error |
| 34 | */ |
| 35 | public static function yieldDiscounted( |
| 36 | $settlement, |
| 37 | $maturity, |
| 38 | $price, |
| 39 | $redemption, |
| 40 | $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
| 41 | ) { |
| 42 | $settlement = Functions::flattenSingleValue($settlement); |
| 43 | $maturity = Functions::flattenSingleValue($maturity); |
| 44 | $price = Functions::flattenSingleValue($price); |
| 45 | $redemption = Functions::flattenSingleValue($redemption); |
| 46 | $basis = Functions::flattenSingleValue($basis) ?? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD; |
| 47 | |
| 48 | try { |
| 49 | $settlement = SecurityValidations::validateSettlementDate($settlement); |
| 50 | $maturity = SecurityValidations::validateMaturityDate($maturity); |
| 51 | SecurityValidations::validateSecurityPeriod($settlement, $maturity); |
| 52 | $price = SecurityValidations::validatePrice($price); |
| 53 | $redemption = SecurityValidations::validateRedemption($redemption); |
| 54 | $basis = SecurityValidations::validateBasis($basis); |
| 55 | } catch (Exception $e) { |
| 56 | return $e->getMessage(); |
| 57 | } |
| 58 | |
| 59 | $daysPerYear = Helpers::daysPerYear(Functions::scalar(DateTimeExcel\DateParts::year($settlement)), $basis); |
| 60 | if (!is_numeric($daysPerYear)) { |
| 61 | return $daysPerYear; |
| 62 | } |
| 63 | $daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis)); |
| 64 | if (!is_numeric($daysBetweenSettlementAndMaturity)) { |
| 65 | // return date error |
| 66 | return StringHelper::convertToString($daysBetweenSettlementAndMaturity); |
| 67 | } |
| 68 | $daysBetweenSettlementAndMaturity *= $daysPerYear; |
| 69 | |
| 70 | return (($redemption - $price) / $price) * ($daysPerYear / $daysBetweenSettlementAndMaturity); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * YIELDMAT. |
| 75 | * |
| 76 | * Returns the annual yield of a security that pays interest at maturity. |
| 77 | * |
| 78 | * @param mixed $settlement The security's settlement date. |
| 79 | * The security's settlement date is the date after the issue date when the security |
| 80 | * is traded to the buyer. |
| 81 | * @param mixed $maturity The security's maturity date. |
| 82 | * The maturity date is the date when the security expires. |
| 83 | * @param mixed $issue The security's issue date |
| 84 | * @param mixed $rate The security's interest rate at date of issue |
| 85 | * @param mixed $price The security's price per $100 face value |
| 86 | * @param mixed $basis The type of day count to use. |
| 87 | * 0 or omitted US (NASD) 30/360 |
| 88 | * 1 Actual/actual |
| 89 | * 2 Actual/360 |
| 90 | * 3 Actual/365 |
| 91 | * 4 European 30/360 |
| 92 | * |
| 93 | * @return float|string Result, or a string containing an error |
| 94 | */ |
| 95 | public static function yieldAtMaturity( |
| 96 | $settlement, |
| 97 | $maturity, |
| 98 | $issue, |
| 99 | $rate, |
| 100 | $price, |
| 101 | $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
| 102 | ) { |
| 103 | $settlement = Functions::flattenSingleValue($settlement); |
| 104 | $maturity = Functions::flattenSingleValue($maturity); |
| 105 | $issue = Functions::flattenSingleValue($issue); |
| 106 | $rate = Functions::flattenSingleValue($rate); |
| 107 | $price = Functions::flattenSingleValue($price); |
| 108 | $basis = Functions::flattenSingleValue($basis) ?? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD; |
| 109 | |
| 110 | try { |
| 111 | $settlement = SecurityValidations::validateSettlementDate($settlement); |
| 112 | $maturity = SecurityValidations::validateMaturityDate($maturity); |
| 113 | SecurityValidations::validateSecurityPeriod($settlement, $maturity); |
| 114 | $issue = SecurityValidations::validateIssueDate($issue); |
| 115 | $rate = SecurityValidations::validateRate($rate); |
| 116 | $price = SecurityValidations::validatePrice($price); |
| 117 | $basis = SecurityValidations::validateBasis($basis); |
| 118 | } catch (Exception $e) { |
| 119 | return $e->getMessage(); |
| 120 | } |
| 121 | |
| 122 | $daysPerYear = Helpers::daysPerYear(Functions::scalar(DateTimeExcel\DateParts::year($settlement)), $basis); |
| 123 | if (!is_numeric($daysPerYear)) { |
| 124 | return $daysPerYear; |
| 125 | } |
| 126 | $daysBetweenIssueAndSettlement = Functions::scalar(DateTimeExcel\YearFrac::fraction($issue, $settlement, $basis)); |
| 127 | if (!is_numeric($daysBetweenIssueAndSettlement)) { |
| 128 | // return date error |
| 129 | return StringHelper::convertToString($daysBetweenIssueAndSettlement); |
| 130 | } |
| 131 | $daysBetweenIssueAndSettlement *= $daysPerYear; |
| 132 | $daysBetweenIssueAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($issue, $maturity, $basis)); |
| 133 | if (!is_numeric($daysBetweenIssueAndMaturity)) { |
| 134 | // return date error |
| 135 | return StringHelper::convertToString($daysBetweenIssueAndMaturity); |
| 136 | } |
| 137 | $daysBetweenIssueAndMaturity *= $daysPerYear; |
| 138 | $daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis)); |
| 139 | if (!is_numeric($daysBetweenSettlementAndMaturity)) { |
| 140 | // return date error |
| 141 | return StringHelper::convertToString($daysBetweenSettlementAndMaturity); |
| 142 | } |
| 143 | $daysBetweenSettlementAndMaturity *= $daysPerYear; |
| 144 | |
| 145 | return ((1 + (($daysBetweenIssueAndMaturity / $daysPerYear) * $rate) |
| 146 | - (($price / 100) + (($daysBetweenIssueAndSettlement / $daysPerYear) * $rate))) |
| 147 | / (($price / 100) + (($daysBetweenIssueAndSettlement / $daysPerYear) * $rate))) |
| 148 | * ($daysPerYear / $daysBetweenSettlementAndMaturity); |
| 149 | } |
| 150 | } |
| 151 |