computeFactor method

  1. @override
DayCountFactor computeFactor(
  1. DateTime d1,
  2. DateTime d2
)

Computes the time interval between dates, expressed in periods defined by the cash flow series frequency. Where the interval between dates used in the calculation cannot be expressed in whole periods, the interval is expressed in whole periods and remaining number of days divided by 365 (or 366 in a leap-year), calculated backwards from the cash flow date to the initial drawdown date.

d1 the initial drawdown date

d2 post date of the cash flow

Implementation

@override
DayCountFactor computeFactor(DateTime d1, DateTime d2) {
  // Compute whole periods
  var wholePeriods = 0;
  final int periodsInYear;
  switch (timePeriod) {
    case EUTimePeriod.year:
      periodsInYear = 1;
      break;
    case EUTimePeriod.week:
      periodsInYear = 52;
      break;
    case EUTimePeriod.month:
    default:
      periodsInYear = 12;
      break;
  }
  final initialDrawdown = utcDate(d1);
  var startWholePeriod = utcDate(d2);
  final operandLog = <String>[];
  while (true) {
    DateTime tempDate;
    switch (timePeriod) {
      case EUTimePeriod.year:
        tempDate = rollMonth(startWholePeriod, -12);
        break;
      case EUTimePeriod.week:
        tempDate = rollDay(startWholePeriod, -7);
        break;
      case EUTimePeriod.month:
      default:
        tempDate = rollMonth(startWholePeriod, -1);
        break;
    }
    if (!initialDrawdown.isAfter(tempDate)) {
      startWholePeriod = tempDate;
      wholePeriods++;
    } else {
      break;
    }
  }
  var factor = 0.0;
  if (wholePeriods > 0) {
    factor = wholePeriods / periodsInYear;
    operandLog.add(
      DayCountFactor.operandsToString(wholePeriods, periodsInYear),
    );
  }

  // Compute days remaining if necessary
  if (!initialDrawdown.isAfter(startWholePeriod)) {
    final numerator = actualDays(initialDrawdown, startWholePeriod);
    final startDenPeriod = rollMonth(startWholePeriod, -12);
    final denominator = actualDays(startDenPeriod, startWholePeriod);
    factor += numerator / denominator;

    if (numerator > 0 || operandLog.isEmpty) {
      operandLog.add(
        DayCountFactor.operandsToString(numerator, denominator),
      );
    }
  }

  return DayCountFactor(factor, operandLog);
}