Ejemplo n.º 1
0
def test_FinScheduleAlignmentEff31():
    ''' EOM schedule so all unadjusted dates fall on month end.'''

    eomFlag = True
    valuationDate = FinDate(29, 7, 2006)
    effDate = valuationDate.addTenor("2D")
    freqType = FinFrequencyTypes.SEMI_ANNUAL
    busDayAdjustType = FinBusDayAdjustTypes.MODIFIED_FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD
    calendarType = FinCalendarTypes.UNITED_STATES
    adjustTerminationDate = True

    matDate1 = effDate.addTenor("4Y")
    matDate2 = effDate.addTenor("50Y")

    #    print(matDate1, matDate2)

    sched1 = FinSchedule(effDate, matDate1, freqType, calendarType,
                         busDayAdjustType, dateGenRuleType,
                         adjustTerminationDate, eomFlag)

    sched2 = FinSchedule(effDate, matDate2, freqType, calendarType,
                         busDayAdjustType, dateGenRuleType,
                         adjustTerminationDate, eomFlag)

    #    print(sched1._adjustedDates)
    #    print(sched2._adjustedDates[:len(sched1._adjustedDates)])

    compare = (sched1._adjustedDates[-1] == sched2._adjustedDates[
        len(sched1._adjustedDates) - 1])
    assert (compare == True)
Ejemplo n.º 2
0
def test_FinScheduleAlignmentLeapYearNotEOM():
    ''' Effective date on leap year. Not EOM. '''

    eomFlag = False

    valuationDate = FinDate(26, 2, 2006)
    effDate = valuationDate.addTenor("2D")
    freqType = FinFrequencyTypes.SEMI_ANNUAL
    busDayAdjustType = FinBusDayAdjustTypes.MODIFIED_FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD
    calendarType = FinCalendarTypes.UNITED_STATES
    adjustTerminationDate = True

    matDate1 = effDate.addTenor("4Y")
    matDate2 = effDate.addTenor("50Y")

    #    print(matDate1, matDate2)

    sched1 = FinSchedule(effDate, matDate1, freqType, calendarType,
                         busDayAdjustType, dateGenRuleType,
                         adjustTerminationDate, eomFlag)

    sched2 = FinSchedule(effDate, matDate2, freqType, calendarType,
                         busDayAdjustType, dateGenRuleType,
                         adjustTerminationDate, eomFlag)

    #    print(sched1._adjustedDates)
    #    print(sched2._adjustedDates[:len(sched1._adjustedDates)])

    compare = (sched1._adjustedDates[-1] == sched2._adjustedDates[
        len(sched1._adjustedDates) - 1])
    assert (compare == True)
Ejemplo n.º 3
0
def test_FinDateAdjust():

    startDate = FinDate(28, 2, 2008)
    endDate = FinDate(28, 2, 2011)

    frequencyType = FinFrequencyTypes.SEMI_ANNUAL
    calendarType = FinCalendarTypes.NONE
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD

    testCases.header("NO ADJUSTMENTS", "DATE")
    schedule = FinSchedule(startDate, endDate, frequencyType, calendarType,
                           busDayAdjustType, dateGenRuleType)

    for dt in schedule._adjustedDates:
        testCases.print("Date:", dt)

    testCases.banner("")
    testCases.header("NO WEEKENDS AND FOLLOWING", "DATE")
    frequencyType = FinFrequencyTypes.SEMI_ANNUAL
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD

    schedule = FinSchedule(startDate, endDate, frequencyType, calendarType,
                           busDayAdjustType, dateGenRuleType)

    for dt in schedule._adjustedDates:
        testCases.print("Date:", dt)

    testCases.banner("")
    testCases.header("NO WEEKENDS AND MODIFIED FOLLOWING", "DATE")
    frequencyType = FinFrequencyTypes.SEMI_ANNUAL
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.MODIFIED_FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD

    schedule = FinSchedule(startDate, endDate, frequencyType, calendarType,
                           busDayAdjustType, dateGenRuleType)

    for dt in schedule._adjustedDates:
        testCases.print("Date:", dt)

    testCases.banner("")
    testCases.header("NO WEEKENDS AND US HOLIDAYS AND MODIFIED FOLLOWING",
                     "DATE")
    frequencyType = FinFrequencyTypes.SEMI_ANNUAL
    calendarType = FinCalendarTypes.US
    busDayAdjustType = FinBusDayAdjustTypes.MODIFIED_FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD

    startDate = FinDate(4, 7, 2008)
    endDate = FinDate(4, 7, 2011)

    schedule = FinSchedule(startDate, endDate, frequencyType, calendarType,
                           busDayAdjustType, dateGenRuleType)

    for dt in schedule._adjustedDates:
        testCases.print("Date:", dt)
Ejemplo n.º 4
0
def test_FinScheduleAlignment(eomFlag):
        
    valuationDate = FinDate(29, 3, 2005)
    effDate = valuationDate.addTenor("2d")
    freqType = FinFrequencyTypes.SEMI_ANNUAL
    busDayAdjustType = FinBusDayAdjustTypes.MODIFIED_FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD
    calendarType = FinCalendarTypes.UNITED_STATES
    adjustTerminationDate = False

    matDate1 = effDate.addTenor("4Y")
    matDate2 = effDate.addTenor("50Y")

#    print(matDate1)
#    print(matDate2)

    myCal = FinCalendar(calendarType)

    adjustedMatDate1 = myCal.adjust(matDate1, busDayAdjustType)
    adjustedMatDate2 = myCal.adjust(matDate2, busDayAdjustType)

#    print(adjustedMatDate1)
#    print(adjustedMatDate2)
    
    sched1 = FinSchedule(effDate,
                         adjustedMatDate1,
                         freqType,
                         calendarType,
                         busDayAdjustType,
                         dateGenRuleType,
                         adjustTerminationDate, 
                         eomFlag)
    
#    print(sched1)
    
    sched2 = FinSchedule(effDate,
                         adjustedMatDate2,
                         freqType,
                         calendarType,
                         busDayAdjustType,
                         dateGenRuleType,
                         adjustTerminationDate, 
                         eomFlag)

    compare = (sched1._adjustedDates[-1] == sched2._adjustedDates[len(sched1._adjustedDates)-1])
    assert(compare == eomFlag)
Ejemplo n.º 5
0
def test_FinIborCapFloorVolCurve():
    ''' Aim here is to price cap and caplets using cap and caplet vols and to
    demonstrate they are the same - NOT SURE THAT HULLS BOOKS FORMULA WORKS FOR
    OPTIONS. '''

    todayDate = FinDate(20, 6, 2019)
    valuationDate = todayDate
    maturityDate = valuationDate.addTenor("3Y")
    dayCountType = FinDayCountTypes.THIRTY_E_360
    frequency = FinFrequencyTypes.ANNUAL

    k = 0.04
    capFloorType = FinCapFloorTypes.CAP
    capFloor = FinIborCapFloor(valuationDate,
                                maturityDate,
                                capFloorType,
                                k,
                                None,
                                frequency,
                                dayCountType)

    capVolDates = FinSchedule(valuationDate,
                              valuationDate.addTenor("10Y"),
                              frequency)._generate()

    flatRate = 0.04
    liborCurve = FinDiscountCurveFlat(valuationDate,
                                      flatRate,
                                      frequency,
                                      dayCountType)

    flat = False
    if flat is True:
        capVolatilities = [20.0] * 11
        capVolatilities[0] = 0.0
    else:
        capVolatilities = [0.00, 15.50, 18.25, 17.91, 17.74, 17.27,
                           16.79, 16.30, 16.01, 15.76, 15.54]

    capVolatilities = np.array(capVolatilities)/100.0
    capVolatilities[0] = 0.0

    volCurve = FinIborCapVolCurve(valuationDate,
                                   capVolDates,
                                   capVolatilities,
                                   dayCountType)

#    print(volCurve._capletGammas)

    # Value cap using a single flat cap volatility
    tcap = (maturityDate - valuationDate) / gDaysInYear
    vol = volCurve.capVol(maturityDate)
    model = FinModelBlack(vol)
    valueCap = capFloor.value(valuationDate, liborCurve, model)
#    print("CAP T", tcap, "VOL:", vol, "VALUE OF CAP:", valueCap)

    # Value cap by breaking it down into caplets using caplet vols
    vCaplets = 0.0
    capletStartDate = capFloor._capFloorLetDates[1]
    testCases.header("START", "END", "VOL", "VALUE")

    for capletEndDate in capFloor._capFloorLetDates[2:]:
        vol = volCurve.capletVol(capletEndDate)
        modelCaplet = FinModelBlack(vol)
        vCaplet = capFloor.valueCapletFloorLet(valuationDate,
                                               capletStartDate,
                                               capletEndDate,
                                               liborCurve,
                                               modelCaplet)

        vCaplets += vCaplet
        testCases.print("%12s" % capletStartDate,
                        "%s" % capletEndDate,
                        "%9.5f" % (vol*100.0),
                        "%9.5f" % vCaplet)

        capletStartDate = capletEndDate

    testCases.header("LABEL", "VALUE")
    testCases.print("CAPLETS->CAP: ", vCaplets)
Ejemplo n.º 6
0
def test_FinSchedule():

    ###########################################################################
    # BACKWARD SCHEDULES TESTING DIFFERENT FREQUENCIES
    ###########################################################################
    
    d1 = FinDate(20, 6, 2018)
    d2 = FinDate(20, 6, 2020)
    freqType = FinFrequencyTypes.SEMI_ANNUAL
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD
    terminationDateAdjust = True

    schedule = FinSchedule(d1,
                           d2,
                           freqType,
                           calendarType,
                           busDayAdjustType,
                           dateGenRuleType,
                           terminationDateAdjust)

    dumpSchedule("BACKWARD SEMI-ANNUAL FREQUENCY", schedule)

    d1 = FinDate(20, 6, 2018)
    d2 = FinDate(20, 6, 2020)
    freqType = FinFrequencyTypes.QUARTERLY
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD

    schedule = FinSchedule(d1,
                           d2,
                           freqType,
                           calendarType,
                           busDayAdjustType,
                           dateGenRuleType,
                           terminationDateAdjust)

    dumpSchedule("BACKWARD QUARTERLY FREQUENCY", schedule)

    d1 = FinDate(20, 6, 2018)
    d2 = FinDate(20, 6, 2020)
    freqType = FinFrequencyTypes.MONTHLY
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD

    schedule = FinSchedule(d1,
                           d2,
                           freqType,
                           calendarType,
                           busDayAdjustType,
                           dateGenRuleType,
                           terminationDateAdjust)

    dumpSchedule("BACKWARD MONTHLY FREQUENCY", schedule)

    ###########################################################################
    # FORWARD SCHEDULES TESTING DIFFERENT FREQUENCIES
    ###########################################################################

    d1 = FinDate(20, 6, 2018)
    d2 = FinDate(20, 6, 2020)
    freqType = FinFrequencyTypes.ANNUAL
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.FORWARD

    schedule = FinSchedule(d1,
                           d2,
                           freqType,
                           calendarType,
                           busDayAdjustType,
                           dateGenRuleType,
                           terminationDateAdjust)
    
    dumpSchedule("FORWARD ANNUAL", schedule)

    d1 = FinDate(20, 6, 2018)
    d2 = FinDate(20, 6, 2020)
    freqType = FinFrequencyTypes.SEMI_ANNUAL
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD

    schedule = FinSchedule(d1,
                           d2,
                           freqType,
                           calendarType,
                           busDayAdjustType,
                           dateGenRuleType)
    
    dumpSchedule("FORWARD SEMI-ANNUAL", schedule)

    d1 = FinDate(20, 6, 2018)
    d2 = FinDate(20, 6, 2020)
    freqType = FinFrequencyTypes.MONTHLY
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD

    schedule = FinSchedule(d1,
                           d2,
                           freqType,
                           calendarType,
                           busDayAdjustType,
                           dateGenRuleType,
                           terminationDateAdjust)
    
    dumpSchedule("FORWARD MONTHLY", schedule)

    ###########################################################################
    # BACKWARD SHORT STUB AT FRONT
    ###########################################################################

    d1 = FinDate(20, 8, 2018)
    d2 = FinDate(20, 6, 2020)
    freqType = FinFrequencyTypes.QUARTERLY
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD

    schedule = FinSchedule(d1,
                           d2,
                           freqType,
                           calendarType,
                           busDayAdjustType,
                           dateGenRuleType,
                           terminationDateAdjust)
    dumpSchedule("BACKWARD GEN WITH SHORT END STUB", schedule)

    ###########################################################################
    # BACKWARD SUPER SHORT STUB AT FRONT
    ###########################################################################

    d1 = FinDate(19, 9, 2018)
    d2 = FinDate(20, 6, 2020)
    freqType = FinFrequencyTypes.QUARTERLY
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD

    schedule = FinSchedule(d1,
                           d2,
                           freqType,
                           calendarType,
                           busDayAdjustType,
                           dateGenRuleType,
                           terminationDateAdjust)

    dumpSchedule("BACKWARD GEN WITH VERY SHORT END STUB", schedule)

    ###########################################################################
    # FORWARD SHORT STUB AT END
    ###########################################################################

    d1 = FinDate(2018, 8, 20)
    d2 = FinDate(2020, 6, 20)
    freqType = FinFrequencyTypes.SEMI_ANNUAL
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.FORWARD

    schedule = FinSchedule(d1,
                           d2,
                           freqType,
                           calendarType,
                           busDayAdjustType,
                           dateGenRuleType,
                           terminationDateAdjust)

    dumpSchedule("FORWARD GEN WITH END STUB", schedule)

    d1 = FinDate(19, 9 , 2018)
    d2 = FinDate(20, 6, 2020)
    freqType = FinFrequencyTypes.QUARTERLY
    calendarType = FinCalendarTypes.TARGET
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.FORWARD

    schedule = FinSchedule(d1,
                           d2,
                           freqType,
                           calendarType,
                           busDayAdjustType,
                           dateGenRuleType)

    dumpSchedule("FORWARD GEN WITH VERY SHORT END STUB", schedule)

    d1 = FinDate(20, 6, 2018)
    d2 = FinDate(20, 6, 2020)
    freqType = FinFrequencyTypes.SEMI_ANNUAL
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD
    terminationDateAdjust = True
    
    schedule = FinSchedule(d1,
                           d2,
                           freqType,
                           calendarType,
                           busDayAdjustType,
                           dateGenRuleType,
                           terminationDateAdjust)

    dumpSchedule("TERMINATION DATE ADJUSTED", schedule)

    d1 = FinDate(20, 6, 2018)
    d2 = FinDate(20, 6, 2020)
    freqType = FinFrequencyTypes.SEMI_ANNUAL
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.MODIFIED_FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD
    terminationDateAdjust = True
    eomFlag = True

    schedule = FinSchedule(d1,
                           d2,
                           freqType,
                           calendarType,
                           busDayAdjustType,
                           dateGenRuleType,
                           terminationDateAdjust,
                           eomFlag)

    dumpSchedule("END OF MONTH - NOT EOM TERM DATE - USING MOD FOLL", schedule)

    d1 = FinDate(30, 6, 2018)
    d2 = FinDate(30, 6, 2020)
    freqType = FinFrequencyTypes.SEMI_ANNUAL
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.MODIFIED_FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD
    terminationDateAdjust = True
    eomFlag = True

    schedule = FinSchedule(d1,
                           d2,
                           freqType,
                           calendarType,
                           busDayAdjustType,
                           dateGenRuleType,
                           terminationDateAdjust,
                           eomFlag)

    dumpSchedule("END OF MONTH - EOM TERM DATE - USING MOD FOLL", schedule)
Ejemplo n.º 7
0
def test_FinSchedule():

    d1 = FinDate(2018, 6, 20)
    d2 = FinDate(2018, 6, 20)
    freqType = FinFrequencyTypes.SEMI_ANNUAL
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD

    schedule = FinSchedule(d1, d2, freqType, calendarType, busDayAdjustType,
                           dateGenRuleType)

    testCases.header("SEMI-ANNUAL FREQUENCY")
    for dt in schedule._adjustedDates:
        testCases.print(str(dt))

    d1 = FinDate(2018, 6, 20)
    d2 = FinDate(2028, 6, 20)
    freqType = FinFrequencyTypes.SEMI_ANNUAL
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD

    schedule = FinSchedule(d1, d2, freqType, calendarType, busDayAdjustType,
                           dateGenRuleType)

    testCases.header("QUARTERLY FREQUENCY")
    for dt in schedule._adjustedDates:
        testCases.print(str(dt))

    d1 = FinDate(2018, 6, 20)
    d2 = FinDate(2028, 6, 20)
    freqType = FinFrequencyTypes.MONTHLY
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD

    schedule = FinSchedule(d1, d2, freqType, calendarType, busDayAdjustType,
                           dateGenRuleType)

    testCases.header("MONTHLY FREQUENCY")
    for dt in schedule._adjustedDates:
        testCases.print(str(dt))

    d1 = FinDate(2018, 6, 20)
    d2 = FinDate(2028, 6, 20)
    freqType = FinFrequencyTypes.ANNUAL
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.FORWARD

    schedule = FinSchedule(d1, d2, freqType, calendarType, busDayAdjustType,
                           dateGenRuleType)

    testCases.header("FORWARD GEN")
    for dt in schedule._adjustedDates:
        testCases.print(str(dt))

    d1 = FinDate(2018, 6, 20)
    d2 = FinDate(2028, 6, 20)
    freqType = FinFrequencyTypes.ANNUAL
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD

    schedule = FinSchedule(d1, d2, freqType, calendarType, busDayAdjustType,
                           dateGenRuleType)

    testCases.header("BACKWARD GEN")
    for dt in schedule._adjustedDates:
        testCases.print(str(dt))

    d1 = FinDate(2018, 6, 20)
    d2 = FinDate(2028, 6, 20)
    freqType = FinFrequencyTypes.SEMI_ANNUAL
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD

    schedule = FinSchedule(d1, d2, freqType, calendarType, busDayAdjustType,
                           dateGenRuleType)

    testCases.header("BACKWARD GEN WITH SHORT END STUB")
    for dt in schedule._adjustedDates:
        testCases.print(str(dt))

    d1 = FinDate(2018, 6, 20)
    d2 = FinDate(2028, 6, 20)
    freqType = FinFrequencyTypes.SEMI_ANNUAL
    calendarType = FinCalendarTypes.WEEKEND
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.FORWARD

    schedule = FinSchedule(d1, d2, freqType, calendarType, busDayAdjustType,
                           dateGenRuleType)

    testCases.header("FORWARD GEN WITH LONG END STUB")
    for dt in schedule._adjustedDates:
        testCases.print(str(dt))

    testCases.header("BACKWARD GEN WITH TARGET CALENDAR")

    d1 = FinDate(2018, 6, 20)
    d2 = FinDate(2028, 6, 20)
    freqType = FinFrequencyTypes.SEMI_ANNUAL
    calendarType = FinCalendarTypes.TARGET
    busDayAdjustType = FinBusDayAdjustTypes.FOLLOWING
    dateGenRuleType = FinDateGenRuleTypes.BACKWARD

    schedule = FinSchedule(d1, d2, freqType, calendarType, busDayAdjustType,
                           dateGenRuleType)

    for dt in schedule._adjustedDates:
        testCases.print(str(dt))