Ejemplo n.º 1
0
        //! next ASX date following the given date

        /*! returns the 1st delivery date for next contract listed in the
         * Australian Securities Exchange.
         */
        public static Date nextDate(Date date = null, bool mainCycle = true)
        {
            Date refDate = date ?? Settings.Instance.evaluationDate();
            int  y       = refDate.year();
            int  m       = refDate.month();

            int offset     = mainCycle ? 3 : 1;
            int skipMonths = offset - (m % offset);

            if (skipMonths != offset || refDate.Day > 14)
            {
                skipMonths += m;
                if (skipMonths <= 12)
                {
                    m = skipMonths;
                }
                else
                {
                    m  = (skipMonths - 12);
                    y += 1;
                }
            }

            Date result = Date.nthWeekday(2, DayOfWeek.Friday, m, y);

            if (result <= refDate)
            {
                result = nextDate(new Date(15, m, y), mainCycle);
            }
            return(result);
        }
Ejemplo n.º 2
0
        private Date previousTwentieth(Date d, DateGeneration.Rule rule)
        {
            Date result = new Date(20, d.month(), d.year());

            if (result > d)
            {
                result -= new Period(1, TimeUnit.Months);
            }
            if (rule == DateGeneration.Rule.TwentiethIMM ||
                rule == DateGeneration.Rule.OldCDS ||
                rule == DateGeneration.Rule.CDS ||
                rule == DateGeneration.Rule.CDS2015)
            {
                int m = result.month();
                if (m % 3 != 0)
                {
                    // not a main IMM nmonth
                    int skip = m % 3;
                    result -= new Period(skip, TimeUnit.Months);
                }
            }
            return(result);
        }
Ejemplo n.º 3
0
        public EventSetSimulation(List <KeyValuePair <Date, double> > events, Date eventsStart, Date eventsEnd, Date start, Date end)
            : base(start, end)
        {
            events_      = events;
            eventsStart_ = eventsStart;
            eventsEnd_   = eventsEnd;
            i_           = 0;

            years_ = end_.year() - start_.year();
            if (eventsStart_.month() < start_.month() ||
                (eventsStart_.month() == start_.month() && eventsStart_.Day <= start_.Day))
            {
                periodStart_ = new Date(start_.Day, start_.Month, eventsStart_.Year);
            }
            else
            {
                periodStart_ = new Date(start_.Day, start_.month(), eventsStart_.year() + 1);
            }
            periodEnd_ = new Date(end_.Day, end_.Month, periodStart_.Year + years_);
            while (i_ < events_.Count && (events_)[i_].Key < periodStart_)
            {
                ++i_; //i points to the first element after the start of the relevant period.
            }
        }
Ejemplo n.º 4
0
        /*! returns the ECB date for the given ECB code
         * (e.g. March xxth, 2013 for MAR10).
         *
         * \warning It raises an exception if the input
         *          string is not an ECB code
         */
        public static Date date(string ecbCode, Date refDate = null)
        {
            Utils.QL_REQUIRE(isECBcode(ecbCode), () => ecbCode + " is not a valid ECB code");

            string code        = ecbCode.ToUpper();
            string monthString = code.Substring(0, 3);
            Month  m           = Month.Jan;

            if (monthString == "JAN")
            {
                m = Month.January;
            }
            else if (monthString == "FEB")
            {
                m = Month.February;
            }
            else if (monthString == "MAR")
            {
                m = Month.March;
            }
            else if (monthString == "APR")
            {
                m = Month.April;
            }
            else if (monthString == "MAY")
            {
                m = Month.May;
            }
            else if (monthString == "JUN")
            {
                m = Month.June;
            }
            else if (monthString == "JUL")
            {
                m = Month.July;
            }
            else if (monthString == "AUG")
            {
                m = Month.August;
            }
            else if (monthString == "SEP")
            {
                m = Month.September;
            }
            else if (monthString == "OCT")
            {
                m = Month.October;
            }
            else if (monthString == "NOV")
            {
                m = Month.November;
            }
            else if (monthString == "DEC")
            {
                m = Month.December;
            }
            else
            {
                Utils.QL_FAIL("not an ECB month (and it should have been)");
            }

            // lexical_cast causes compilation errors with x64
            int  y             = int.Parse(code.Substring(3, 2));
            Date referenceDate = (refDate ?? Settings.Instance.evaluationDate());
            int  referenceYear = (referenceDate.year() % 100);

            y += referenceDate.year() - referenceYear;
            if (y < Date.minDate().year())
            {
                return(ECB.nextDate(Date.minDate()));
            }

            return(ECB.nextDate(new Date(1, m, y)));
        }
Ejemplo n.º 5
0
        /*! returns the ECB code for the given date
         * (e.g. MAR10 for March xxth, 2010).
         *
         * \warning It raises an exception if the input
         *          date is not an ECB date
         */
        public static string code(Date ecbDate)
        {
            Utils.QL_REQUIRE(isECBdate(ecbDate), () => ecbDate + " is not a valid ECB date");

            string ECBcode = string.Empty;
            int    y       = ecbDate.year() % 100;
            string padding = string.Empty;

            if (y < 10)
            {
                padding = "0";
            }
            switch (ecbDate.month())
            {
            case (int)Month.January:
                ECBcode += "JAN" + padding + y;
                break;

            case (int)Month.February:
                ECBcode += "FEB" + padding + y;
                break;

            case (int)Month.March:
                ECBcode += "MAR" + padding + y;
                break;

            case (int)Month.April:
                ECBcode += "APR" + padding + y;
                break;

            case (int)Month.May:
                ECBcode += "MAY" + padding + y;
                break;

            case (int)Month.June:
                ECBcode += "JUN" + padding + y;
                break;

            case (int)Month.July:
                ECBcode += "JUL" + padding + y;
                break;

            case (int)Month.August:
                ECBcode += "AUG" + padding + y;
                break;

            case (int)Month.September:
                ECBcode += "SEP" + padding + y;
                break;

            case (int)Month.October:
                ECBcode += "OCT" + padding + y;
                break;

            case (int)Month.November:
                ECBcode += "NOV" + padding + y;
                break;

            case (int)Month.December:
                ECBcode += "DEC" + padding + y;
                break;

            default:
                Utils.QL_FAIL("not an ECB month (and it should have been)");
                break;
            }

#if (QL_EXTRA_SAFETY_CHECKS)
            QL_ENSURE(isECBcode(ECBcode.str()),
                      "the result " << ECBcode.str() <<
                      " is an invalid ECB code");
#endif
            return(ECBcode);
        }
Ejemplo n.º 6
0
        /*! returns the ASX date for the given ASX code
         * (e.g. June 12th, 2015 for M5).
         *
         * \warning It raises an exception if the input
         *          string is not an ASX code
         */
        public static Date date(String asxCode, Date refDate = null)
        {
            Utils.QL_REQUIRE(isASXcode(asxCode, false), () =>
                             asxCode + " is not a valid ASX code");

            Date referenceDate = refDate ?? Settings.Instance.evaluationDate();

            String code = asxCode.ToUpper();
            String ms   = code.Substring(0, 1);
            Month  m    = 0;

            if (ms == "F")
            {
                m = Month.January;
            }
            else if (ms == "G")
            {
                m = Month.February;
            }
            else if (ms == "H")
            {
                m = Month.March;
            }
            else if (ms == "J")
            {
                m = Month.April;
            }
            else if (ms == "K")
            {
                m = Month.May;
            }
            else if (ms == "M")
            {
                m = Month.June;
            }
            else if (ms == "N")
            {
                m = Month.July;
            }
            else if (ms == "Q")
            {
                m = Month.August;
            }
            else if (ms == "U")
            {
                m = Month.September;
            }
            else if (ms == "V")
            {
                m = Month.October;
            }
            else if (ms == "X")
            {
                m = Month.November;
            }
            else if (ms == "Z")
            {
                m = Month.December;
            }
            else
            {
                Utils.QL_FAIL("invalid ASX month letter");
            }

//       Year y = boost::lexical_cast<Year>(); // lexical_cast causes compilation errors with x64

            int y = int.Parse(code.Substring(1, 1));

            /* year<1900 are not valid QuantLib years: to avoid a run-time
             * exception few lines below we need to add 10 years right away */
            if (y == 0 && referenceDate.year() <= 1909)
            {
                y += 10;
            }
            int referenceYear = (referenceDate.year() % 10);

            y += referenceDate.year() - referenceYear;
            Date result = ASX.nextDate(new Date(1, m, y), false);

            if (result < referenceDate)
            {
                return(ASX.nextDate(new Date(1, m, y + 10), false));
            }

            return(result);
        }
Ejemplo n.º 7
0
        /*! returns the ASX code for the given date
         * (e.g. M5 for June 12th, 2015).
         */
        public static String code(Date date)
        {
            Utils.QL_REQUIRE(isASXdate(date, false), () => date + " is not an ASX date");

            String ASXcode = String.Empty;
            String y       = (date.year() % 10).ToString();

            switch ((Month)date.month())
            {
            case Month.January:
                ASXcode = 'F' + y;
                break;

            case Month.February:
                ASXcode = 'G' + y;
                break;

            case Month.March:
                ASXcode = 'H' + y;
                break;

            case Month.April:
                ASXcode = 'J' + y;
                break;

            case Month.May:
                ASXcode = 'K' + y;
                break;

            case Month.June:
                ASXcode = 'M' + y;
                break;

            case Month.July:
                ASXcode = 'N' + y;
                break;

            case Month.August:
                ASXcode = 'Q' + y;
                break;

            case Month.September:
                ASXcode = 'U' + y;
                break;

            case Month.October:
                ASXcode = 'V' + y;
                break;

            case Month.November:
                ASXcode = 'X' + y;
                break;

            case Month.December:
                ASXcode = 'Z' + y;
                break;

            default:
                Utils.QL_FAIL("not an ASX month (and it should have been)");
                break;
            }

            return(ASXcode);
        }