1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial\Securities; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Helpers; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
10
|
|
|
use 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
|
10 |
|
public static function yieldDiscounted( |
36
|
|
|
mixed $settlement, |
37
|
|
|
mixed $maturity, |
38
|
|
|
mixed $price, |
39
|
|
|
mixed $redemption, |
40
|
|
|
mixed $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
41
|
|
|
) { |
42
|
10 |
|
$settlement = Functions::flattenSingleValue($settlement); |
43
|
10 |
|
$maturity = Functions::flattenSingleValue($maturity); |
44
|
10 |
|
$price = Functions::flattenSingleValue($price); |
45
|
10 |
|
$redemption = Functions::flattenSingleValue($redemption); |
46
|
10 |
|
$basis = Functions::flattenSingleValue($basis) ?? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD; |
47
|
|
|
|
48
|
|
|
try { |
49
|
10 |
|
$settlement = SecurityValidations::validateSettlementDate($settlement); |
50
|
9 |
|
$maturity = SecurityValidations::validateMaturityDate($maturity); |
51
|
8 |
|
SecurityValidations::validateSecurityPeriod($settlement, $maturity); |
52
|
8 |
|
$price = SecurityValidations::validatePrice($price); |
53
|
6 |
|
$redemption = SecurityValidations::validateRedemption($redemption); |
54
|
4 |
|
$basis = SecurityValidations::validateBasis($basis); |
55
|
6 |
|
} catch (Exception $e) { |
56
|
6 |
|
return $e->getMessage(); |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
$daysPerYear = Helpers::daysPerYear(Functions::scalar(DateTimeExcel\DateParts::year($settlement)), $basis); |
60
|
4 |
|
if (!is_numeric($daysPerYear)) { |
61
|
|
|
return $daysPerYear; |
62
|
|
|
} |
63
|
4 |
|
$daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis)); |
64
|
4 |
|
if (!is_numeric($daysBetweenSettlementAndMaturity)) { |
65
|
|
|
// return date error |
66
|
|
|
return StringHelper::convertToString($daysBetweenSettlementAndMaturity); |
67
|
|
|
} |
68
|
4 |
|
$daysBetweenSettlementAndMaturity *= $daysPerYear; |
69
|
|
|
|
70
|
4 |
|
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
|
10 |
|
public static function yieldAtMaturity( |
96
|
|
|
mixed $settlement, |
97
|
|
|
mixed $maturity, |
98
|
|
|
mixed $issue, |
99
|
|
|
mixed $rate, |
100
|
|
|
mixed $price, |
101
|
|
|
mixed $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
102
|
|
|
) { |
103
|
10 |
|
$settlement = Functions::flattenSingleValue($settlement); |
104
|
10 |
|
$maturity = Functions::flattenSingleValue($maturity); |
105
|
10 |
|
$issue = Functions::flattenSingleValue($issue); |
106
|
10 |
|
$rate = Functions::flattenSingleValue($rate); |
107
|
10 |
|
$price = Functions::flattenSingleValue($price); |
108
|
10 |
|
$basis = Functions::flattenSingleValue($basis) ?? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD; |
109
|
|
|
|
110
|
|
|
try { |
111
|
10 |
|
$settlement = SecurityValidations::validateSettlementDate($settlement); |
112
|
9 |
|
$maturity = SecurityValidations::validateMaturityDate($maturity); |
113
|
8 |
|
SecurityValidations::validateSecurityPeriod($settlement, $maturity); |
114
|
8 |
|
$issue = SecurityValidations::validateIssueDate($issue); |
115
|
7 |
|
$rate = SecurityValidations::validateRate($rate); |
116
|
5 |
|
$price = SecurityValidations::validatePrice($price); |
117
|
3 |
|
$basis = SecurityValidations::validateBasis($basis); |
118
|
7 |
|
} catch (Exception $e) { |
119
|
7 |
|
return $e->getMessage(); |
120
|
|
|
} |
121
|
|
|
|
122
|
3 |
|
$daysPerYear = Helpers::daysPerYear(Functions::scalar(DateTimeExcel\DateParts::year($settlement)), $basis); |
123
|
3 |
|
if (!is_numeric($daysPerYear)) { |
124
|
|
|
return $daysPerYear; |
125
|
|
|
} |
126
|
3 |
|
$daysBetweenIssueAndSettlement = Functions::scalar(DateTimeExcel\YearFrac::fraction($issue, $settlement, $basis)); |
127
|
3 |
|
if (!is_numeric($daysBetweenIssueAndSettlement)) { |
128
|
|
|
// return date error |
129
|
|
|
return StringHelper::convertToString($daysBetweenIssueAndSettlement); |
130
|
|
|
} |
131
|
3 |
|
$daysBetweenIssueAndSettlement *= $daysPerYear; |
132
|
3 |
|
$daysBetweenIssueAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($issue, $maturity, $basis)); |
133
|
3 |
|
if (!is_numeric($daysBetweenIssueAndMaturity)) { |
134
|
|
|
// return date error |
135
|
|
|
return StringHelper::convertToString($daysBetweenIssueAndMaturity); |
136
|
|
|
} |
137
|
3 |
|
$daysBetweenIssueAndMaturity *= $daysPerYear; |
138
|
3 |
|
$daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis)); |
139
|
3 |
|
if (!is_numeric($daysBetweenSettlementAndMaturity)) { |
140
|
|
|
// return date error |
141
|
|
|
return StringHelper::convertToString($daysBetweenSettlementAndMaturity); |
142
|
|
|
} |
143
|
3 |
|
$daysBetweenSettlementAndMaturity *= $daysPerYear; |
144
|
|
|
|
145
|
3 |
|
return ((1 + (($daysBetweenIssueAndMaturity / $daysPerYear) * $rate) |
146
|
3 |
|
- (($price / 100) + (($daysBetweenIssueAndSettlement / $daysPerYear) * $rate))) |
147
|
3 |
|
/ (($price / 100) + (($daysBetweenIssueAndSettlement / $daysPerYear) * $rate))) |
148
|
3 |
|
* ($daysPerYear / $daysBetweenSettlementAndMaturity); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|