Ejemplo n.º 1
0
        //! The factor returned is NOT normalized relative to ANYTHING.
        public virtual double seasonalityFactor(Date to)
        {
            Date      from            = seasonalityBaseDate();
            Frequency factorFrequency = frequency();
            int       nFactors        = seasonalityFactors().Count;
            Period    factorPeriod    = new Period(factorFrequency);
            int       which           = 0;

            if (from == to)
            {
                which = 0;
            }
            else
            {
                // days, weeks, months, years are the only time unit possibilities
                int diffDays = Math.Abs(to - from); // in days
                int dir      = 1;
                if (from > to)
                {
                    dir = -1;
                }
                int diff = 0;
                if (factorPeriod.units() == TimeUnit.Days)
                {
                    diff = dir * diffDays;
                }
                else if (factorPeriod.units() == TimeUnit.Weeks)
                {
                    diff = dir * (diffDays / 7);
                }
                else if (factorPeriod.units() == TimeUnit.Months)
                {
                    KeyValuePair <Date, Date> lim = Utils.inflationPeriod(to, factorFrequency);
                    diff = diffDays / (31 * factorPeriod.length());
                    Date go = from + dir * diff * factorPeriod;
                    while (!(lim.Key <= go && go <= lim.Value))
                    {
                        go += dir * factorPeriod;
                        diff++;
                    }
                    diff = dir * diff;
                }
                else if (factorPeriod.units() == TimeUnit.Years)
                {
                    Utils.QL_FAIL("seasonality period time unit is not allowed to be : " + factorPeriod.units());
                }
                else
                {
                    Utils.QL_FAIL("Unknown time unit: " + factorPeriod.units());
                }
                // now adjust to the available number of factors, direction dependent

                if (dir == 1)
                {
                    which = diff % nFactors;
                }
                else
                {
                    which = (nFactors - (-diff % nFactors)) % nFactors;
                }
            }
            return(seasonalityFactors()[which]);
        }
Ejemplo n.º 2
0
 private bool allowsEndOfMonth(Period tenor)
 {
     return((tenor.units() == TimeUnit.Months || tenor.units() == TimeUnit.Years) &&
            tenor >= new Period(1, TimeUnit.Months));
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Advances the given date as specified by the given period and
 /// returns the result.
 /// </summary>
 /// <remarks>The input date is not modified.</remarks>
 public Date advance(Date d, Period p, BusinessDayConvention c = BusinessDayConvention.Following, bool endOfMonth = false)
 {
     return(advance(d, p.length(), p.units(), c, endOfMonth));
 }