ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Price.php
Go to the documentation of this file.
1 <?php
2 
4 
11 
12 class Price
13 {
40  public static function price(
41  $settlement,
42  $maturity,
43  $rate,
44  $yield,
45  $redemption,
46  $frequency,
47  $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
48  ) {
49  $settlement = Functions::flattenSingleValue($settlement);
50  $maturity = Functions::flattenSingleValue($maturity);
51  $rate = Functions::flattenSingleValue($rate);
52  $yield = Functions::flattenSingleValue($yield);
53  $redemption = Functions::flattenSingleValue($redemption);
54  $frequency = Functions::flattenSingleValue($frequency);
55  $basis = ($basis === null)
56  ? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
58 
59  try {
60  $settlement = SecurityValidations::validateSettlementDate($settlement);
61  $maturity = SecurityValidations::validateMaturityDate($maturity);
62  SecurityValidations::validateSecurityPeriod($settlement, $maturity);
63  $rate = SecurityValidations::validateRate($rate);
64  $yield = SecurityValidations::validateYield($yield);
65  $redemption = SecurityValidations::validateRedemption($redemption);
66  $frequency = SecurityValidations::validateFrequency($frequency);
67  $basis = SecurityValidations::validateBasis($basis);
68  } catch (Exception $e) {
69  return $e->getMessage();
70  }
71 
72  $dsc = Coupons::COUPDAYSNC($settlement, $maturity, $frequency, $basis);
73  $e = Coupons::COUPDAYS($settlement, $maturity, $frequency, $basis);
74  $n = Coupons::COUPNUM($settlement, $maturity, $frequency, $basis);
75  $a = Coupons::COUPDAYBS($settlement, $maturity, $frequency, $basis);
76 
77  $baseYF = 1.0 + ($yield / $frequency);
78  $rfp = 100 * ($rate / $frequency);
79  $de = $dsc / $e;
80 
81  $result = $redemption / $baseYF ** (--$n + $de);
82  for ($k = 0; $k <= $n; ++$k) {
83  $result += $rfp / ($baseYF ** ($k + $de));
84  }
85  $result -= $rfp * ($a / $e);
86 
87  return $result;
88  }
89 
111  public static function priceDiscounted(
112  $settlement,
113  $maturity,
114  $discount,
115  $redemption,
116  $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
117  ) {
118  $settlement = Functions::flattenSingleValue($settlement);
119  $maturity = Functions::flattenSingleValue($maturity);
120  $discount = Functions::flattenSingleValue($discount);
121  $redemption = Functions::flattenSingleValue($redemption);
122  $basis = ($basis === null)
123  ? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
125 
126  try {
127  $settlement = SecurityValidations::validateSettlementDate($settlement);
128  $maturity = SecurityValidations::validateMaturityDate($maturity);
129  SecurityValidations::validateSecurityPeriod($settlement, $maturity);
130  $discount = SecurityValidations::validateDiscount($discount);
131  $redemption = SecurityValidations::validateRedemption($redemption);
132  $basis = SecurityValidations::validateBasis($basis);
133  } catch (Exception $e) {
134  return $e->getMessage();
135  }
136 
137  $daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis);
138  if (!is_numeric($daysBetweenSettlementAndMaturity)) {
139  // return date error
140  return $daysBetweenSettlementAndMaturity;
141  }
142 
143  return $redemption * (1 - $discount * $daysBetweenSettlementAndMaturity);
144  }
145 
168  public static function priceAtMaturity(
169  $settlement,
170  $maturity,
171  $issue,
172  $rate,
173  $yield,
174  $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
175  ) {
176  $settlement = Functions::flattenSingleValue($settlement);
177  $maturity = Functions::flattenSingleValue($maturity);
178  $issue = Functions::flattenSingleValue($issue);
179  $rate = Functions::flattenSingleValue($rate);
180  $yield = Functions::flattenSingleValue($yield);
181  $basis = ($basis === null)
182  ? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
184 
185  try {
186  $settlement = SecurityValidations::validateSettlementDate($settlement);
187  $maturity = SecurityValidations::validateMaturityDate($maturity);
188  SecurityValidations::validateSecurityPeriod($settlement, $maturity);
190  $rate = SecurityValidations::validateRate($rate);
191  $yield = SecurityValidations::validateYield($yield);
192  $basis = SecurityValidations::validateBasis($basis);
193  } catch (Exception $e) {
194  return $e->getMessage();
195  }
196 
197  $daysPerYear = Helpers::daysPerYear(DateTimeExcel\DateParts::year($settlement), $basis);
198  if (!is_numeric($daysPerYear)) {
199  return $daysPerYear;
200  }
201  $daysBetweenIssueAndSettlement = DateTimeExcel\YearFrac::fraction($issue, $settlement, $basis);
202  if (!is_numeric($daysBetweenIssueAndSettlement)) {
203  // return date error
204  return $daysBetweenIssueAndSettlement;
205  }
206  $daysBetweenIssueAndSettlement *= $daysPerYear;
207  $daysBetweenIssueAndMaturity = DateTimeExcel\YearFrac::fraction($issue, $maturity, $basis);
208  if (!is_numeric($daysBetweenIssueAndMaturity)) {
209  // return date error
210  return $daysBetweenIssueAndMaturity;
211  }
212  $daysBetweenIssueAndMaturity *= $daysPerYear;
213  $daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis);
214  if (!is_numeric($daysBetweenSettlementAndMaturity)) {
215  // return date error
216  return $daysBetweenSettlementAndMaturity;
217  }
218  $daysBetweenSettlementAndMaturity *= $daysPerYear;
219 
220  return (100 + (($daysBetweenIssueAndMaturity / $daysPerYear) * $rate * 100)) /
221  (1 + (($daysBetweenSettlementAndMaturity / $daysPerYear) * $yield)) -
222  (($daysBetweenIssueAndSettlement / $daysPerYear) * $rate * 100);
223  }
224 
246  public static function received(
247  $settlement,
248  $maturity,
249  $investment,
250  $discount,
251  $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
252  ) {
253  $settlement = Functions::flattenSingleValue($settlement);
254  $maturity = Functions::flattenSingleValue($maturity);
255  $investment = Functions::flattenSingleValue($investment);
256  $discount = Functions::flattenSingleValue($discount);
257  $basis = ($basis === null)
258  ? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
260 
261  try {
262  $settlement = SecurityValidations::validateSettlementDate($settlement);
263  $maturity = SecurityValidations::validateMaturityDate($maturity);
264  SecurityValidations::validateSecurityPeriod($settlement, $maturity);
265  $investment = SecurityValidations::validateFloat($investment);
266  $discount = SecurityValidations::validateDiscount($discount);
267  $basis = SecurityValidations::validateBasis($basis);
268  } catch (Exception $e) {
269  return $e->getMessage();
270  }
271 
272  if ($investment <= 0) {
273  return Functions::NAN();
274  }
275  $daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis);
276  if (!is_numeric($daysBetweenSettlementAndMaturity)) {
277  // return date error
278  return $daysBetweenSettlementAndMaturity;
279  }
280 
281  return $investment / (1 - ($discount * $daysBetweenSettlementAndMaturity));
282  }
283 }
$result
static COUPDAYSNC( $settlement, $maturity, $frequency, $basis=FinancialConstants::BASIS_DAYS_PER_YEAR_NASD)
COUPDAYSNC.
Definition: Coupons.php:178
static COUPDAYS( $settlement, $maturity, $frequency, $basis=FinancialConstants::BASIS_DAYS_PER_YEAR_NASD)
COUPDAYS.
Definition: Coupons.php:107
static fraction($startDate, $endDate, $method=0)
YEARFRAC.
Definition: YearFrac.php:37
static price( $settlement, $maturity, $rate, $yield, $redemption, $frequency, $basis=FinancialConstants::BASIS_DAYS_PER_YEAR_NASD)
PRICE.
Definition: Price.php:40
static received( $settlement, $maturity, $investment, $discount, $basis=FinancialConstants::BASIS_DAYS_PER_YEAR_NASD)
RECEIVED.
Definition: Price.php:246
$n
Definition: RandomTest.php:85
static priceDiscounted( $settlement, $maturity, $discount, $redemption, $basis=FinancialConstants::BASIS_DAYS_PER_YEAR_NASD)
PRICEDISC.
Definition: Price.php:111
static daysPerYear($year, $basis=0)
daysPerYear.
Definition: Helpers.php:27
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649
static COUPNUM( $settlement, $maturity, $frequency, $basis=FinancialConstants::BASIS_DAYS_PER_YEAR_NASD)
COUPNUM.
Definition: Coupons.php:297
static COUPDAYBS( $settlement, $maturity, $frequency, $basis=FinancialConstants::BASIS_DAYS_PER_YEAR_NASD)
COUPDAYBS.
Definition: Coupons.php:44
static priceAtMaturity( $settlement, $maturity, $issue, $rate, $yield, $basis=FinancialConstants::BASIS_DAYS_PER_YEAR_NASD)
PRICEMAT.
Definition: Price.php:168