Пример #1
0
        public void testJointCalendars()
        {
            Calendar c1 = new TARGET(),
                     c2 = new UnitedKingdom(),
                     c3 = new UnitedStates(UnitedStates.Market.NYSE),
                     c4 = new Japan();

            Calendar c12h = new JointCalendar(c1, c2, JointCalendar.JointCalendarRule.JoinHolidays),
                     c12b = new JointCalendar(c1,c2,JointCalendar.JointCalendarRule.JoinBusinessDays),
                     c123h = new JointCalendar(c1,c2,c3,JointCalendar.JointCalendarRule.JoinHolidays),
                     c123b = new JointCalendar(c1,c2,c3,JointCalendar.JointCalendarRule.JoinBusinessDays),
                     c1234h = new JointCalendar(c1,c2,c3,c4,JointCalendar.JointCalendarRule.JoinHolidays),
                     c1234b = new JointCalendar(c1,c2,c3,c4,JointCalendar.JointCalendarRule.JoinBusinessDays);

            // test one year, starting today
            Date firstDate = Date.Today,
                 endDate = firstDate + new Period(1, TimeUnit.Years);

            for (Date d = firstDate; d < endDate; d++) {

                bool b1 = c1.isBusinessDay(d),
                     b2 = c2.isBusinessDay(d),
                     b3 = c3.isBusinessDay(d),
                     b4 = c4.isBusinessDay(d);

                if ((b1 && b2) != c12h.isBusinessDay(d))
                    Assert.Fail("At date " + d + ":\n"
                               + "    inconsistency between joint calendar "
                               + c12h.name() + " (joining holidays)\n"
                               + "    and its components");

                if ((b1 || b2) != c12b.isBusinessDay(d))
                    Assert.Fail("At date " + d + ":\n"
                               + "    inconsistency between joint calendar "
                               + c12b.name() + " (joining business days)\n"
                               + "    and its components");

                if ((b1 && b2 && b3) != c123h.isBusinessDay(d))
                    Assert.Fail("At date " + d + ":\n"
                               + "    inconsistency between joint calendar "
                               + c123h.name() + " (joining holidays)\n"
                               + "    and its components");

                if ((b1 || b2 || b3) != c123b.isBusinessDay(d))
                    Assert.Fail("At date " + d + ":\n"
                               + "    inconsistency between joint calendar "
                               + c123b.name() + " (joining business days)\n"
                               + "    and its components");

                if ((b1 && b2 && b3 && b4) != c1234h.isBusinessDay(d))
                    Assert.Fail("At date " + d + ":\n"
                               + "    inconsistency between joint calendar "
                               + c1234h.name() + " (joining holidays)\n"
                               + "    and its components");

                if ((b1 || b2 || b3 || b4) != c1234b.isBusinessDay(d))
                    Assert.Fail("At date " + d + ":\n"
                               + "    inconsistency between joint calendar "
                               + c1234b.name() + " (joining business days)\n"
                               + "    and its components");

            }
        }
Пример #2
0
        public void testModifiedCalendars()
        {
            Calendar c1 = new TARGET();
            Calendar c2 = new UnitedStates(UnitedStates.Market.NYSE);
            Date d1 = new Date(1, Month.May, 2004);      // holiday for both calendars
            Date d2 = new Date(26, Month.April, 2004);   // business day

            Assert.IsTrue(c1.isHoliday(d1), "wrong assumption---correct the test");
            Assert.IsTrue(c1.isBusinessDay(d2), "wrong assumption---correct the test");

            Assert.IsTrue(c2.isHoliday(d1), "wrong assumption---correct the test");
            Assert.IsTrue(c2.isBusinessDay(d2), "wrong assumption---correct the test");

            // modify the TARGET calendar
            c1.removeHoliday(d1);
            c1.addHoliday(d2);

            // test
            Assert.IsFalse(c1.isHoliday(d1), d1 + " still a holiday for original TARGET instance");
            Assert.IsFalse(c1.isBusinessDay(d2), d2 + " still a business day for original TARGET instance");

            // any instance of TARGET should be modified...
            Calendar c3 = new TARGET();
            Assert.IsFalse(c3.isHoliday(d1), d1 + " still a holiday for generic TARGET instance");
            Assert.IsFalse(c3.isBusinessDay(d2), d2 + " still a business day for generic TARGET instance");

            // ...but not other calendars
            Assert.IsFalse(c2.isBusinessDay(d1), d1 + " business day for New York");
            Assert.IsFalse(c2.isHoliday(d2), d2 + " holiday for New York");

            // restore original holiday set---test the other way around
            c3.addHoliday(d1);
            c3.removeHoliday(d2);

            Assert.IsFalse(c1.isBusinessDay(d1), d1 + " still a business day");
            Assert.IsFalse(c1.isHoliday(d2), d2 + " still a holiday");
        }
Пример #3
0
        public DateTime ExpirationDate(int year, int month, string calendarType = "US")
        {
            DateTime referenceDay = new DateTime(year, month, 1);
            referenceDay = referenceDay.AddMonths((int)Rule.ReferenceRelativeMonth);

            Calendar calendar = new UnitedStates(UnitedStates.Market.NYSE); //todo add functionality to allow changing this to other countries

            int day;
            if (Rule.ReferenceDayIsLastBusinessDayOfMonth)
            {
                var tmpDay = referenceDay.AddMonths(1);
                tmpDay = tmpDay.AddDays(-1);
                while (!calendar.isBusinessDay(tmpDay))
                {
                    tmpDay = tmpDay.AddDays(-1);
                }
                day = tmpDay.Day;
            }
            else if (Rule.ReferenceUsesDays) //we use a fixed number of days from the start of the month
            {
                day = Rule.ReferenceDays;
            }
            else //we use a number of weeks and then a weekday of that week
            {
                if (Rule.ReferenceWeekDayCount == WeekDayCount.Last) //the last week of the month
                {
                    var tmpDay = referenceDay.AddMonths(1);
                    tmpDay = tmpDay.AddDays(-1);
                    while (tmpDay.DayOfWeek.ToInt() != (int)Rule.ReferenceWeekDay)
                    {
                        tmpDay = tmpDay.AddDays(-1);
                    }
                    day = tmpDay.Day;
                }
                else //1st to 4th week of the month, just loop until we find the right day
                {
                    int weekCount = 0;
                    while (weekCount < (int)Rule.ReferenceWeekDayCount + 1)
                    {
                        if (referenceDay.DayOfWeek.ToInt() == (int)Rule.ReferenceWeekDay)
                            weekCount++;

                        referenceDay = referenceDay.AddDays(1);
                    }

                    day = referenceDay.Day - 1;
                }
            }

            referenceDay = new DateTime(year, month, day);
            referenceDay = referenceDay.AddMonths((int)Rule.ReferenceRelativeMonth);

            if (Rule.DayType == DayType.BusinessDay)
            {
                int daysLeft = Rule.DaysBefore;
                int daysBack = 0;
                while (daysLeft > 0)
                {
                    daysBack++;

                    if (calendar.isBusinessDay(referenceDay.AddDays(-daysBack))) //todo fix here...
                        daysLeft--;
                }
                return referenceDay.AddDays(-daysBack);
            }
            else if (Rule.DayType == DayType.CalendarDay)
            {
                return referenceDay.AddDays(-Rule.DaysBefore);
            }
            return referenceDay;
        }