Exemplo n.º 1
0
        public Date SubPeriod(string period, bool modFoll)
        {
            Period    p         = new Period(period);
            TenorType tenorType = p.TenorType;
            DateTime  dateTime  = CloneDateValue();
            Date      output    = new Date();

            switch (tenorType)
            {
            case TenorType.D:
                output = new Date(this.DateValue.AddDays(-p.Tenor));
                break;

            case TenorType.W:
                output = new Date(this.DateValue.AddDays(-p.Tenor * 7));
                break;

            case TenorType.M:
                output = new Date(this.DateValue.AddMonths(-p.Tenor));
                break;

            case TenorType.Y:
                output = new Date(this.DateValue.AddYears(-p.Tenor));
                break;

            default:
                break;
            }
            if (modFoll)
            {
                output = output.GetModifiedFollowing();
            }
            return(output);
        }
Exemplo n.º 2
0
        // Method Member: subtraction "period" to my date ("nd" will add "n" days, "nm" will add "n" months, "ny" will add "n" years), modified following can be considered or not
        // new implementation
        public Date sub_period(string period, bool modFoll)
        {
            Period    p       = new Period(period);
            TenorType T       = p.tenorType;
            DateTime  my_date = this.DateValue;
            Date      outPut  = new Date();

            switch (T)
            {
            case TenorType.D:
                outPut = new Date(this.DateValue.AddDays(-p.tenor));
                break;

            case TenorType.W:
                outPut = new Date(this.DateValue.AddDays(-p.tenor * 7));
                break;

            case TenorType.M:
                outPut = new Date(this.DateValue.AddMonths(-p.tenor));
                break;

            case TenorType.Y:
                outPut = new Date(this.DateValue.AddYears(-p.tenor));
                break;

            default:
                break;
            }

            if (modFoll)
            {
                outPut = outPut.mod_foll();
            }
            return(outPut);
        }
Exemplo n.º 3
0
        //Add Period to this date ("nd" will add "n" days, "nm" will add "n" months, "ny" will add "n" years)
        //modified following can be considered or not
        public Date AddPeriod(string periodStr, bool modFoll)
        {
            Period    period    = new Period(periodStr);
            TenorType tenorType = period.TenorType;
            DateTime  date      = this.DateValue;
            Date      output    = new Date();

            switch (tenorType)
            {
            case TenorType.D:
                output = new Date(this.DateValue.AddDays(period.Tenor));
                break;

            case TenorType.W:
                output = new Date(this.DateValue.AddDays(period.Tenor * 7));
                break;

            case TenorType.M:
                output = new Date(this.DateValue.AddMonths(period.Tenor));
                break;

            case TenorType.Y:
                output = new Date(this.DateValue.AddYears(period.Tenor));
                break;
            }

            if (modFoll)
            {
                output = output.GetModifiedFollowing();
            }
            return(output);
        }
Exemplo n.º 4
0
        public Period(string period)
        {
            char maturity = period[period.Length - 1];
            int  nPeriods = int.Parse(period.Remove(period.Length - 1, 1));

            Tenor     = nPeriods;
            TenorType = (TenorType)Enum.Parse(typeof(TenorType), Convert.ToString(maturity).ToUpper());
        }
Exemplo n.º 5
0
    public Period(string period)
    {
        char maturity  = period[period.Length - 1];
        int  n_periods = int.Parse(period.Remove(period.Length - 1, 1));

        tenor = n_periods;
        // C# 3.0 Cookbook, par 20.10
        tenorType = (TenorType)Enum.Parse(typeof(TenorType), Convert.ToString(maturity).ToUpper());
    }
Exemplo n.º 6
0
 public Tenor(int duration, TenorType tenorType)
 {
     TenorType = tenorType;
     Duration = duration;
     if (duration == 0)
     {
         TenorType = TenorType.CalendarDay;
     }
 }
        // type 으로 바꿔줌
        public string convToTenor(string tenor, TenorType type)
        {
            int len = tenor.Length;

            string periodMultiplier = tenor.Substring(0, len - 1);
            string period           = tenor.Substring(len - 1, 1);

            if (period == "D")
            {
                return(tenor);
            }
            else if (period == "M")
            {
                if (type == TenorType.Month)
                {
                    return(tenor);
                }
                else if (type == TenorType.Year)
                {
                    double p      = Convert.ToDouble(periodMultiplier) / 12.0;
                    double roundP = Math.Round(p, 1);

                    return(roundP.ToString() + "Y");
                }
            }
            else if (period == "Y")
            {
                if (type == TenorType.Month)
                {
                    double p      = Convert.ToDouble(periodMultiplier) * 12.0;
                    double roundP = Math.Round(p, 0);

                    return(roundP.ToString() + "M");
                }
                else if (type == TenorType.Year)
                {
                    double p      = Convert.ToDouble(periodMultiplier) / 12.0;
                    double roundP = Math.Round(p, 1);

                    return(roundP.ToString() + "Y");
                }
            }
            else
            {
                throw new Exception("unknown type tenor : " + tenor);
            }

            return(tenor);
        }
Exemplo n.º 8
0
        /// <summary>Initializes a new instance of the <see cref="TenorTimeSpan"/> struct.
        /// </summary>
        /// <param name="years">The years.</param>
        /// <param name="months">The months.</param>
        /// <param name="days">The days.</param>
        /// <remarks><see cref="TenorType"/> is set to <see cref="Dodoni.Finance.TenorType.RegularTenor"/>.</remarks>
        /// <exception cref="ArgumentException">Thrown, if some argument is positiv and another is negative.</exception>
        public TenorTimeSpan(int years, int months, int days)
        {
            if (((years >= 0) && (months >= 0) && (days >= 0)) || ((years <= 0) && (months <= 0) && (days <= 0)))
            {
                // normalize the number of months and years:
                Years  = years + months / 12; // the sign '+' is correct in any case
                Months = months % 12;

                Days = days;
            }
            else
            {
                throw new ArgumentException();
            }
            TenorType = TenorType.RegularTenor;
        }
Exemplo n.º 9
0
 /// <summary>Initializes a new instance of the <see cref="TenorTimeSpan"/> struct.
 /// </summary>
 /// <param name="tenorType">The tenor type.</param>
 /// <remarks>
 /// <para>
 ///        This constructor will be used from the static constructor only.
 /// </para>
 /// The number of years and months will be set to <c>0</c>, the number of days is set to 1 ('ON') or 2 ('TN').</remarks>
 /// <exception cref="ArgumentException">Thrown if <paramref name="tenorType"/> is not equal to <see cref="Dodoni.Finance.TenorType.TomorrowNext"/> or <see cref="Dodoni.Finance.TenorType.Overnight"/>
 /// </exception>
 private TenorTimeSpan(TenorType tenorType)
 {
     Years  = 0;
     Months = 0;
     if (tenorType == TenorType.TomorrowNext)
     {
         Days = 2;
     }
     else if (tenorType == TenorType.Overnight)
     {
         Days = 1;
     }
     else
     {
         throw new ArgumentException("tenorType");
     }
     TenorType = tenorType;
 }
Exemplo n.º 10
0
    public TenorType tenorType; // tenor type

    // Constructors
    public Period(int tenor, TenorType tenorType)
    {
        this.tenor     = tenor;
        this.tenorType = tenorType;
    }
        // type 으로 바꿔줌
        public string convToTenor(string tenor,TenorType type)
        {
            int len = tenor.Length;

            string periodMultiplier = tenor.Substring(0, len - 1);
            string period = tenor.Substring(len - 1, 1);

            if (period == "D")
            {
                return tenor;

            }
            else if (period == "M")
            {
                if (type == TenorType.Month)
                {
                    return tenor;
                }
                else if (type == TenorType.Year)
                {
                    double p = Convert.ToDouble(periodMultiplier) / 12.0;
                    double roundP = Math.Round(p, 1);

                    return roundP.ToString() + "Y";
                }
            }
            else if (period == "Y")
            {
                if (type == TenorType.Month)
                {
                    double p = Convert.ToDouble(periodMultiplier) * 12.0;
                    double roundP = Math.Round(p, 0);

                    return roundP.ToString() + "M";
                }
                else if (type == TenorType.Year)
                {
                    double p = Convert.ToDouble(periodMultiplier) / 12.0;
                    double roundP = Math.Round(p, 1);

                    return roundP.ToString() + "Y";
                }
            }
            else
            {
                throw new Exception("unknown type tenor : " + tenor);
            }

            return tenor;
        }
Exemplo n.º 12
0
        /// <summary>Converts a string given in a tenor format, i.e. "6m", "1y6m" etc. to a number of
        /// years, months and days.
        /// </summary>
        /// <param name="tenorString">The tenor string.</param>
        /// <param name="years">The number of years (output).</param>
        /// <param name="months">The number of months (output).</param>
        /// <param name="days">The number of days (output).</param>
        /// <param name="tenorType">The type of the tenor (output).</param>
        /// <returns>Returns an instance of <see cref="TenorTimeSpan"/> with the number of years,
        /// months and days according to <paramref name="tenorString"/> or null if
        /// the given string could not be converted.</returns>
        /// <remarks>Also overnight or tomorrow next will be converted from a string.</remarks>
        /// <exception cref=" ArgumentNullException">Thrown, if <paramref name="tenorString"/> is <c>null</c>.</exception>
        internal static bool TryParse(string tenorString, out int years, out int months, out int days, out TenorType tenorType)
        {
            years     = months = days = 0;
            tenorType = TenorType.RegularTenor;

            if (tenorString == null)
            {
                throw new ArgumentNullException("tenorString");
            }
            tenorString = tenorString.Replace(" ", String.Empty);

            if (((tenorString.Length == 1) && (tenorString[0] == '0')) || (tenorString.ToIDString() == sm_NullTenorIdentifierStringRepresentation.IDString)) // special: '0' and "<null>" will be interpreted as 'null'
            {
                years     = months = days = 0;
                tenorType = TenorType.RegularTenor;
                return(true);
            }

            if (sm_TenorRegularExpression.IsMatch(tenorString))
            {
                int             sign = 1;
                MatchCollection matchCollection;

                if (tenorString[0] == '-')
                {
                    sign            = -1;
                    matchCollection = sm_TenorSubRegularExpression.Matches(tenorString, 1);
                }
                else
                {
                    matchCollection = sm_TenorSubRegularExpression.Matches(tenorString);
                }

                foreach (Match match in matchCollection)
                {
                    /* mach is of the form '5y', '7m' etc., i.e. the last character is the unitiy:*/
                    char unityAsChar = match.Value[match.Length - 1];
                    int  number      = Int32.Parse(match.Value.Remove(match.Length - 1));

                    switch (unityAsChar)
                    {
                    case 'Y':
                    case 'y':
                        years += sign * number;
                        break;

                    case 'M':
                    case 'm':
                        months += sign * number;
                        break;

                    case 'W':
                    case 'w':
                        days += sign * 7 * number;
                        break;

                    case 'D':
                    case 'd':
                        days += sign * number;
                        break;
                    }
                }
                return(true);
            }
            else   /* maybe it is overnight or tomorrow next */
            {
                days = 1;

                tenorString = tenorString.ToUpper();
                if (tenorString == OvernightTenorStringRepresentation)
                {
                    tenorType = TenorType.Overnight;
                    return(true);
                }
                else if (tenorString == TomorrowNextTenorStringRepresentation)
                {
                    tenorType = TenorType.TomorrowNext;
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 13
0
        public void Parse_TestCaseException(string tenorString, int expectedYears, int expectedMonths, int expectedDays, bool expectedIsPositive, TenorType expectedTenorType, Type expectedException)
        {
            Assert.Throws(expectedException, () =>
            {
                TenorTimeSpan actualTenor = TenorTimeSpan.Parse(tenorString);

                Assert.That(actualTenor.Years, Is.EqualTo(expectedYears), "Years component");
                Assert.That(actualTenor.Months, Is.EqualTo(expectedMonths), "Months component");
                Assert.That(actualTenor.Days, Is.EqualTo(expectedDays), "Days component");

                Assert.That(actualTenor.TenorType, Is.EqualTo(expectedTenorType), "Tenor type component");
                Assert.That(actualTenor.IsPositive, Is.EqualTo(expectedIsPositive), "IsPositive component");
            });
        }
Exemplo n.º 14
0
        public TenorType TenorType; //tenor type

        public Period(int tenor, TenorType tenorType)
        {
            Tenor     = tenor;
            TenorType = tenorType;
        }
Exemplo n.º 15
0
 public static string TenorTypeString(TenorType tenorType)
 {
     switch (tenorType)
     {
         case TenorType.CalendarDay:
             return "d";
         case TenorType.BusinessDay:
             return "b";
         case TenorType.Week:
             return "w";
         case TenorType.Month:
             return "m";
         case TenorType.Year:
             return "y";
         default:
             return "?";
     }
 }