Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

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