def testTimeseries(self):
        cal = Calendar('China.SSE')
        dates = cal.bizDatesList(Date(2014, 1, 1), Date(2015, 7, 21))
        values = [0.1 * i for i, _ in enumerate(dates)]

        ts = Timeseries(dates, values)

        self.assertEqual(ts.size(), len(dates))

        firstDate = ts.firstDate()
        lastDate = ts.lastDate()
        self.assertEqual(
            dates[0], firstDate,
            "Expected first date {0} is not equal to ts's first date {1}".
            format(dates[0], firstDate))
        self.assertEqual(
            dates[-1], lastDate,
            "Expected last date {0} is not equal to ts's last date {1}".format(
                dates[-1], lastDate))

        for date, value in zip(dates, values):
            self.assertEqual(
                ts[date], value,
                "Expected value at day {0} is not equal to expected {1:f}".
                format(date, value))
    def testAdvanceDate(self):
        referenceDate = dt.date.today()

        expected = advanceDate(referenceDate, '3M')
        calculated = Date.fromDateTime(referenceDate) + '3M'
        self.assertEqual(expected, calculated.toDateTime())

        expected = advanceDate(referenceDate, '-3M')
        calculated = Date.fromDateTime(referenceDate) - '3M'
        self.assertEqual(expected, calculated.toDateTime())
    def testAdvanceDate(self):
        referenceDate = dt.date.today()

        expected = advanceDate(referenceDate, '3M')
        calculated = Date.fromDateTime(referenceDate) + '3M'
        self.assertEqual(expected, calculated.toDateTime())

        expected = advanceDate(referenceDate, '-3M')
        calculated = Date.fromDateTime(referenceDate) - '3M'
        self.assertEqual(expected, calculated.toDateTime())
 def testHolDatesList(self):
     holDtList = holDatesList('China.SSE', self.fromDate, self.toDate)
     fromDate = Date.fromDateTime(self.fromDate)
     currentDate = fromDate.toDateTime()
     cal = Calendar('China.SSE')
     while currentDate <= self.toDate:
         finpyDate = Date.fromDateTime(currentDate)
         if not cal.isBizDay(finpyDate):
             self.assertTrue(finpyDate.toDateTime() in holDtList,
                             "{0} is expected as a holiday in {1}".format(finpyDate, cal))
         currentDate = (finpyDate + 1).toDateTime()
 def testHolDatesList(self):
     holDtList = holDatesList('China.SSE', self.fromDate, self.toDate)
     fromDate = Date.fromDateTime(self.fromDate)
     currentDate = fromDate.toDateTime()
     cal = Calendar('China.SSE')
     while currentDate <= self.toDate:
         finpyDate = Date.fromDateTime(currentDate)
         if not cal.isBizDay(finpyDate):
             self.assertTrue(finpyDate.toDateTime() in holDtList,
                             "{0} is expected as a holiday in {1}".format(finpyDate, cal))
         currentDate = (finpyDate + 1).toDateTime()
    def testAdjustDateByCalendar(self):
        referenceDate = Date(2014, 10, 1)
        cal = Calendar('China.SSE')

        expected = adjustDateByCalendar('China.SSE', referenceDate.toDateTime())
        calculated = cal.adjustDate(referenceDate)
        self.assertEqual(expected, calculated.toDateTime())

        referenceDate = Date(2014, 10, 8)
        expected = adjustDateByCalendar('China.SSE', referenceDate.toDateTime(), BizDayConventions.Preceding)
        calculated = cal.adjustDate(referenceDate, BizDayConventions.Preceding)
        self.assertEqual(expected, calculated.toDateTime())
示例#7
0
    def testSimpleCashFlowComparing(self):
        cf1 = SimpleCashFlow(100., Date(2017, 3, 28))
        cf2 = SimpleCashFlow(100., Date(2017, 4, 1))

        self.assertTrue(cf1 < cf2)
        self.assertFalse(cf1 > cf2)

        cf2 = SimpleCashFlow(100., Date(2017, 3, 1))
        self.assertTrue(cf1 > cf2)
        self.assertFalse(cf1 < cf2)

        cf2 = SimpleCashFlow(100., Date(2017, 3, 28))
        self.assertTrue(cf1 == cf2)
    def testConversion(self):

        d1 = Date.todaysDate()

        for case in self.cases:
            ir = InterestRate(case.r, DayCounter('Actual/360'), case.comp, case.freq)
            d2 = d1 + int(360 * case.t + 0.5)

            compoundf = ir.compoundFactor(d1, d2)
            disc = ir.discountFactor(d1, d2)

            self.assertAlmostEqual(compoundf, 1.0 / disc)

            ir2 = ir.equivalentRate(ir.dayCounter_,
                                    ir.comp_,
                                    ir.freq_,
                                    d1, d2)

            self.assertAlmostEqual(ir2.r_, ir.r_)
            self.assertEqual(ir.freq_, ir2.freq_)
            self.assertEqual(ir.comp_, ir2.comp_)

            ir3 = ir.equivalentRate(ir.dayCounter_,
                                    case.comp2, case.freq2,
                                    d1, d2)
            expectedIR = InterestRate(case.expected, ir.dayCounter_,
                                      case.comp2, case.freq2)
            self.assertAlmostEqual(ir3.r_, expectedIR.r_, case.precision)

            r3 = ir.equivalentRate(ir.dayCounter_,
                                   case.comp2, case.freq2,
                                   d1, d2)
            self.assertAlmostEqual(r3.r_, case.expected, case.precision)
    def testAdvanceDateByCalendar(self):
        referenceDate = Date(2014, 10, 1)
        cal = Calendar('China.SSE')

        expected = advanceDateByCalendar('China.SSE', referenceDate.toDateTime(), '2D')
        calculated = cal.advanceDate(referenceDate, '2D')
        self.assertEqual(expected, calculated.toDateTime())

        expected = advanceDateByCalendar('China.SSE', referenceDate.toDateTime(), '2D')
        calculated = cal.advanceDate(referenceDate, '2D')
        self.assertEqual(expected, calculated.toDateTime())

        expected = advanceDateByCalendar('China.SSE', referenceDate.toDateTime(), '2B')
        calculated = cal.advanceDate(referenceDate, '2B')
        self.assertEqual(expected, calculated.toDateTime())

        expected = advanceDateByCalendar('China.SSE', referenceDate.toDateTime(), '1Y')
        calculated = cal.advanceDate(referenceDate, '1Y')
        self.assertEqual(expected, calculated.toDateTime())
    def testDatesList(self):
        dtList = datesList(self.fromDate, self.toDate)
        fromDate = Date.fromDateTime(self.fromDate)

        for i, date in enumerate(dtList):
            expected = date
            calculated = (fromDate + i).toDateTime()
            self.assertEqual(date, (fromDate + i).toDateTime(), "at index {0:d}"
                                                                "expected:   {1}"
                                                                "calculated: {2}".format(i, expected, calculated))
    def testDatesList(self):
        dtList = datesList(self.fromDate, self.toDate)
        fromDate = Date.fromDateTime(self.fromDate)

        for i, date in enumerate(dtList):
            expected = date
            calculated = (fromDate + i).toDateTime()
            self.assertEqual(date, (fromDate + i).toDateTime(), "at index {0:d}"
                                                                "expected:   {1}"
                                                                "calculated: {2}".format(i, expected, calculated))
示例#12
0
    def testSimpleCashFlowHasOccurred(self):

        cf_date = Date.todaysDate() - 1

        cf1 = SimpleCashFlow(100., cf_date)
        self.assertTrue(cf1.hasOccurred())

        ref_date = cf_date - 1
        self.assertFalse(cf1.hasOccurred(ref_date))

        ref_date = cf_date
        self.assertTrue(cf1.hasOccurred(ref_date))
        self.assertFalse(cf1.hasOccurred(ref_date, include_ref_date=True))
    def testAdjustDateByCalendar(self):
        referenceDate = Date(2014, 10, 1)
        cal = Calendar('China.SSE')

        expected = adjustDateByCalendar('China.SSE', referenceDate.toDateTime())
        calculated = cal.adjustDate(referenceDate)
        self.assertEqual(expected, calculated.toDateTime())

        referenceDate = Date(2014, 10, 8)
        expected = adjustDateByCalendar('China.SSE', referenceDate.toDateTime(), BizDayConventions.Preceding)
        calculated = cal.adjustDate(referenceDate, BizDayConventions.Preceding)
        self.assertEqual(expected, calculated.toDateTime())
    def testAdvanceDateByCalendar(self):
        referenceDate = Date(2014, 10, 1)
        cal = Calendar('China.SSE')

        expected = advanceDateByCalendar('China.SSE', referenceDate.toDateTime(), '2D')
        calculated = cal.advanceDate(referenceDate, Period('2D'))
        self.assertEqual(expected, calculated.toDateTime())

        expected = advanceDateByCalendar('China.SSE', referenceDate.toDateTime(), '2D')
        calculated = cal.advanceDate(referenceDate, Period('2D'))
        self.assertEqual(expected, calculated.toDateTime())

        expected = advanceDateByCalendar('China.SSE', referenceDate.toDateTime(), '2B')
        calculated = cal.advanceDate(referenceDate, Period('2B'))
        self.assertEqual(expected, calculated.toDateTime())

        expected = advanceDateByCalendar('China.SSE', referenceDate.toDateTime(), '1Y')
        calculated = cal.advanceDate(referenceDate, Period('1Y'))
        self.assertEqual(expected, calculated.toDateTime())
示例#15
0
    def testSimpleCashFlow(self):
        cash_flow = SimpleCashFlow(100., Date(2017, 3, 28))

        self.assertAlmostEqual(cash_flow.amount(), 100.)
        self.assertEqual(cash_flow.date(), Date(2017, 3, 28))
示例#16
0
class ChinaSseImpl(object):
    _holDays = {
        Date(2005, 1, 3),
        Date(2005, 2, 7),
        Date(2005, 2, 8),
        Date(2005, 2, 9),
        Date(2005, 2, 10),
        Date(2005, 2, 11),
        Date(2005, 2, 14),
        Date(2005, 2, 15),
        Date(2005, 4, 4),
        Date(2005, 5, 2),
        Date(2005, 5, 3),
        Date(2005, 5, 4),
        Date(2005, 5, 5),
        Date(2005, 5, 6),
        Date(2005, 6, 9),
        Date(2005, 9, 15),
        Date(2005, 10, 3),
        Date(2005, 10, 4),
        Date(2005, 10, 5),
        Date(2005, 10, 6),
        Date(2005, 10, 7),
        Date(2006, 1, 2),
        Date(2006, 1, 3),
        Date(2006, 1, 26),
        Date(2006, 1, 27),
        Date(2006, 1, 30),
        Date(2006, 1, 31),
        Date(2006, 2, 1),
        Date(2006, 2, 2),
        Date(2006, 2, 3),
        Date(2006, 4, 4),
        Date(2006, 5, 1),
        Date(2006, 5, 2),
        Date(2006, 5, 3),
        Date(2006, 5, 4),
        Date(2006, 5, 5),
        Date(2006, 6, 9),
        Date(2006, 9, 15),
        Date(2006, 10, 2),
        Date(2006, 10, 3),
        Date(2006, 10, 4),
        Date(2006, 10, 5),
        Date(2006, 10, 6),
        Date(2007, 1, 1),
        Date(2007, 1, 2),
        Date(2007, 1, 3),
        Date(2007, 2, 19),
        Date(2007, 2, 20),
        Date(2007, 2, 21),
        Date(2007, 2, 22),
        Date(2007, 2, 23),
        Date(2007, 4, 4),
        Date(2007, 5, 1),
        Date(2007, 5, 2),
        Date(2007, 5, 3),
        Date(2007, 5, 4),
        Date(2007, 5, 7),
        Date(2007, 10, 1),
        Date(2007, 10, 2),
        Date(2007, 10, 3),
        Date(2007, 10, 4),
        Date(2007, 10, 5),
        Date(2007, 12, 31),
        Date(2008, 1, 1),
        Date(2008, 2, 6),
        Date(2008, 2, 7),
        Date(2008, 2, 8),
        Date(2008, 2, 11),
        Date(2008, 2, 12),
        Date(2008, 4, 4),
        Date(2008, 5, 1),
        Date(2008, 5, 2),
        Date(2008, 6, 9),
        Date(2008, 9, 15),
        Date(2008, 9, 29),
        Date(2008, 9, 30),
        Date(2008, 10, 1),
        Date(2008, 10, 2),
        Date(2008, 10, 3),
        Date(2009, 1, 1),
        Date(2009, 1, 2),
        Date(2009, 1, 26),
        Date(2009, 1, 27),
        Date(2009, 1, 28),
        Date(2009, 1, 29),
        Date(2009, 1, 30),
        Date(2009, 4, 6),
        Date(2009, 5, 1),
        Date(2009, 5, 28),
        Date(2009, 5, 29),
        Date(2009, 10, 1),
        Date(2009, 10, 2),
        Date(2009, 10, 5),
        Date(2009, 10, 6),
        Date(2009, 10, 7),
        Date(2009, 10, 8),
        Date(2010, 1, 1),
        Date(2010, 2, 15),
        Date(2010, 2, 16),
        Date(2010, 2, 17),
        Date(2010, 2, 18),
        Date(2010, 2, 19),
        Date(2010, 4, 5),
        Date(2010, 5, 3),
        Date(2010, 6, 14),
        Date(2010, 6, 15),
        Date(2010, 6, 16),
        Date(2010, 9, 22),
        Date(2010, 9, 23),
        Date(2010, 9, 24),
        Date(2010, 10, 1),
        Date(2010, 10, 4),
        Date(2010, 10, 5),
        Date(2010, 10, 6),
        Date(2010, 10, 7),
        Date(2011, 1, 3),
        Date(2011, 2, 2),
        Date(2011, 2, 3),
        Date(2011, 2, 4),
        Date(2011, 2, 7),
        Date(2011, 2, 8),
        Date(2011, 4, 4),
        Date(2011, 4, 5),
        Date(2011, 5, 2),
        Date(2011, 6, 6),
        Date(2011, 9, 12),
        Date(2011, 10, 3),
        Date(2011, 10, 4),
        Date(2011, 10, 5),
        Date(2011, 10, 6),
        Date(2011, 10, 7),
        Date(2012, 1, 2),
        Date(2012, 1, 3),
        Date(2012, 1, 23),
        Date(2012, 1, 24),
        Date(2012, 1, 25),
        Date(2012, 1, 26),
        Date(2012, 1, 27),
        Date(2012, 4, 2),
        Date(2012, 4, 3),
        Date(2012, 4, 4),
        Date(2012, 4, 30),
        Date(2012, 5, 1),
        Date(2012, 6, 22),
        Date(2012, 10, 1),
        Date(2012, 10, 2),
        Date(2012, 10, 3),
        Date(2012, 10, 4),
        Date(2012, 10, 5),
        Date(2013, 1, 1),
        Date(2013, 1, 2),
        Date(2013, 1, 3),
        Date(2013, 2, 11),
        Date(2013, 2, 12),
        Date(2013, 2, 13),
        Date(2013, 2, 14),
        Date(2013, 2, 15),
        Date(2013, 4, 4),
        Date(2013, 4, 5),
        Date(2013, 4, 29),
        Date(2013, 4, 30),
        Date(2013, 5, 1),
        Date(2013, 6, 10),
        Date(2013, 6, 11),
        Date(2013, 6, 12),
        Date(2013, 9, 19),
        Date(2013, 9, 20),
        Date(2013, 10, 1),
        Date(2013, 10, 2),
        Date(2013, 10, 3),
        Date(2013, 10, 4),
        Date(2013, 10, 7),
        Date(2014, 1, 1),
        Date(2014, 1, 31),
        Date(2014, 2, 3),
        Date(2014, 2, 4),
        Date(2014, 2, 5),
        Date(2014, 2, 6),
        Date(2014, 4, 7),
        Date(2014, 5, 1),
        Date(2014, 5, 2),
        Date(2014, 6, 2),
        Date(2014, 9, 8),
        Date(2014, 10, 1),
        Date(2014, 10, 2),
        Date(2014, 10, 3),
        Date(2014, 10, 6),
        Date(2014, 10, 7),
        Date(2015, 1, 1),
        Date(2015, 1, 2),
        Date(2015, 2, 18),
        Date(2015, 2, 19),
        Date(2015, 2, 20),
        Date(2015, 2, 23),
        Date(2015, 2, 24),
        Date(2015, 4, 6),
        Date(2015, 5, 1),
        Date(2015, 6, 22),
        Date(2015, 9, 3),
        Date(2015, 9, 4),
        Date(2015, 10, 1),
        Date(2015, 10, 2),
        Date(2015, 10, 5),
        Date(2015, 10, 6),
        Date(2015, 10, 7),
        Date(2016, 1, 1),
        Date(2016, 2, 8),
        Date(2016, 2, 9),
        Date(2016, 2, 10),
        Date(2016, 2, 11),
        Date(2016, 2, 12),
        Date(2016, 4, 4),
        Date(2016, 5, 1),
        Date(2016, 6, 9),
        Date(2016, 6, 10),
        Date(2016, 9, 15),
        Date(2016, 9, 16),
        Date(2016, 10, 3),
        Date(2016, 10, 4),
        Date(2016, 10, 5),
        Date(2016, 10, 6),
        Date(2016, 10, 7)
    }

    def __init__(self):
        pass

    def isBizDay(self, date):
        w = date.weekday()
        if self.isWeekEnd(w) or date in self._holDays:
            return False
        return True

    def isWeekEnd(self, weekDay):
        return weekDay == Weekdays.Saturday or weekDay == Weekdays.Sunday

    def __eq__(self, right):
        if isinstance(right, ChinaSseImpl):
            return True
        else:
            return False
示例#17
0
class ChinaIBImpl(object):
    _working_weekends = {
        # 2005
        Date.westernStyle(5, Months.February, 2005),
        Date.westernStyle(6, Months.February, 2005),
        Date.westernStyle(30, Months.April, 2005),
        Date.westernStyle(8, Months.May, 2005),
        Date.westernStyle(8, Months.October, 2005),
        Date.westernStyle(9, Months.October, 2005),
        Date.westernStyle(31, Months.December, 2005),
        # 2006
        Date.westernStyle(28, Months.January, 2006),
        Date.westernStyle(29, Months.April, 2006),
        Date.westernStyle(30, Months.April, 2006),
        Date.westernStyle(30, Months.September, 2006),
        Date.westernStyle(30, Months.December, 2006),
        Date.westernStyle(31, Months.December, 2006),
        # 2007
        Date.westernStyle(17, Months.February, 2007),
        Date.westernStyle(25, Months.February, 2007),
        Date.westernStyle(28, Months.April, 2007),
        Date.westernStyle(29, Months.April, 2007),
        Date.westernStyle(29, Months.September, 2007),
        Date.westernStyle(30, Months.September, 2007),
        Date.westernStyle(29, Months.December, 2007),
        # 2008
        Date.westernStyle(2, Months.February, 2008),
        Date.westernStyle(3, Months.February, 2008),
        Date.westernStyle(4, Months.May, 2008),
        Date.westernStyle(27, Months.September, 2008),
        Date.westernStyle(28, Months.September, 2008),
        # 2009
        Date.westernStyle(4, Months.January, 2009),
        Date.westernStyle(24, Months.January, 2009),
        Date.westernStyle(1, Months.February, 2009),
        Date.westernStyle(31, Months.May, 2009),
        Date.westernStyle(27, Months.September, 2009),
        Date.westernStyle(10, Months.October, 2009),
        # 2010
        Date.westernStyle(20, Months.February, 2010),
        Date.westernStyle(21, Months.February, 2010),
        Date.westernStyle(12, Months.June, 2010),
        Date.westernStyle(13, Months.June, 2010),
        Date.westernStyle(19, Months.September, 2010),
        Date.westernStyle(25, Months.September, 2010),
        Date.westernStyle(26, Months.September, 2010),
        Date.westernStyle(9, Months.October, 2010),
        # 2011
        Date.westernStyle(30, Months.January, 2011),
        Date.westernStyle(12, Months.February, 2011),
        Date.westernStyle(2, Months.April, 2011),
        Date.westernStyle(8, Months.October, 2011),
        Date.westernStyle(9, Months.October, 2011),
        Date.westernStyle(31, Months.December, 2011),
        # 2012
        Date.westernStyle(21, Months.January, 2012),
        Date.westernStyle(29, Months.January, 2012),
        Date.westernStyle(31, Months.March, 2012),
        Date.westernStyle(1, Months.April, 2012),
        Date.westernStyle(28, Months.April, 2012),
        Date.westernStyle(29, Months.September, 2012),
        # 2013
        Date.westernStyle(5, Months.January, 2013),
        Date.westernStyle(6, Months.January, 2013),
        Date.westernStyle(16, Months.February, 2013),
        Date.westernStyle(17, Months.February, 2013),
        Date.westernStyle(7, Months.April, 2013),
        Date.westernStyle(27, Months.April, 2013),
        Date.westernStyle(28, Months.April, 2013),
        Date.westernStyle(8, Months.June, 2013),
        Date.westernStyle(9, Months.June, 2013),
        Date.westernStyle(22, Months.September, 2013),
        Date.westernStyle(29, Months.September, 2013),
        Date.westernStyle(12, Months.October, 2013),
        # 2014
        Date.westernStyle(26, Months.January, 2014),
        Date.westernStyle(8, Months.February, 2014),
        Date.westernStyle(4, Months.May, 2014),
        Date.westernStyle(28, Months.September, 2014),
        Date.westernStyle(11, Months.October, 2014),
        # 2015
        Date.westernStyle(4, Months.January, 2015),
        Date.westernStyle(15, Months.February, 2015),
        Date.westernStyle(28, Months.February, 2015),
        Date.westernStyle(6, Months.September, 2015),
        Date.westernStyle(10, Months.October, 2015),
        # 2016
        Date.westernStyle(6, Months.February, 2016),
        Date.westernStyle(14, Months.February, 2016),
        Date.westernStyle(12, Months.June, 2016),
        Date.westernStyle(18, Months.September, 2016),
        Date.westernStyle(8, Months.October, 2016),
        Date.westernStyle(9, Months.October, 2016),
    }

    def __init__(self):
        self._sseImpl = ChinaSseImpl()

    def isBizDay(self, date):
        return self._sseImpl.isBizDay(
            date) or date in ChinaIBImpl._working_weekends

    def isWeekEnd(self, weekDay):
        return weekDay == Weekdays.Saturday or weekDay == Weekdays.Sunday

    def __eq__(self, right):
        if isinstance(right, ChinaIBImpl):
            return True
        else:
            return False
示例#18
0
 def endOfMonth(self, d):
     return self.adjustDate(Date.endOfMonth(d), BizDayConventions.Preceding)
示例#19
0
 def endOfMonth(self, d):
     return self.adjustDate(Date.endOfMonth(d), BizDayConventions.Preceding)
    def testActualActual(self):
        cases = [
            # first example
            SingleCase(Date(2003, 11, 1), Date(2004, 5, 1), 0.500000000000, Date(2003, 11, 1), Date(2004, 5, 1)),
            # short first calculation period(first period)
            SingleCase(Date(1999, 2, 1), Date(1999, 7, 1), 0.410958904110, Date(1998, 7, 1), Date(1999, 7, 1)),
            # short first calculation period(second period)
            SingleCase(Date(1999, 7, 1), Date(2000, 7, 1), 1.000000000000, Date(1999, 7, 1), Date(2000, 7, 1)),
            # long first calculation period(first period)
            SingleCase(Date(2002, 8, 15), Date(2003, 7, 15), 0.915760869565, Date(2003, 1, 15), Date(2003, 7, 15)),
            # long first calculation period(second period) / *Warning:
            # the ISDA case is in disagreement with mktc1198.pdf *
            SingleCase(Date(2003, 7, 15), Date(2004, 1, 15), 0.500000000000, Date(2003, 7, 15), Date(2004, 1, 15)),
            # short final calculation period(penultimate period)
            SingleCase(Date(1999, 7, 30), Date(2000, 1, 30), 0.500000000000, Date(1999, 7, 30), Date(2000, 1, 30)),
            # short final calculation period(final period)
            SingleCase(Date(2000, 1, 30), Date(2000, 6, 30), 0.417582417582, Date(2000, 1, 30), Date(2000, 7, 30))
        ]

        for case in cases:
            dayCounter = DayCounter('Actual/Actual (ISMA)')
            d1 = case.start
            d2 = case.end
            rd1 = case.refStart
            rd2 = case.refEnd

            calculated = dayCounter.yearFraction(d1, d2, rd1, rd2)
            self.assertAlmostEqual(case.result, calculated)
示例#21
0
 def setUp(self):
     cal = Calendar('China.SSE')
     self.dates = cal.bizDatesList(Date(2014, 1, 1), Date(2015, 7, 21))
     self.rets = [0.05 for _ in self.dates]