TreasuryBill.php 5.8 KB

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