def test_FinBond(): import pandas as pd path = os.path.join(os.path.dirname(__file__), './data/giltBondPrices.txt') bondDataFrame = pd.read_csv(path, sep='\t') bondDataFrame['mid'] = 0.5 * (bondDataFrame['bid'] + bondDataFrame['ask']) freqType = FinFrequencyTypes.SEMI_ANNUAL settlementDate = FinDate(19, 9, 2012) face = ONE_MILLION for accrualType in FinDayCountTypes: testCases.header("MATURITY", "COUPON", "CLEAN_PRICE", "ACCD_DAYS", "ACCRUED", "YTM") for _, bond in bondDataFrame.iterrows(): dateString = bond['maturity'] matDatetime = dt.datetime.strptime(dateString, '%d-%b-%y') maturityDt = fromDatetime(matDatetime) issueDt = FinDate(maturityDt._d, maturityDt._m, 2000) coupon = bond['coupon'] / 100.0 cleanPrice = bond['mid'] bond = FinBond(issueDt, maturityDt, coupon, freqType, accrualType, 100) ytm = bond.yieldToMaturity(settlementDate, cleanPrice) accd = bond._accruedInterest accd_days = bond._accruedDays testCases.print("%18s" % maturityDt, "%8.4f" % coupon, "%10.4f" % cleanPrice, "%6.0f" % accd_days, "%10.4f" % accd, "%8.4f" % ytm) ########################################################################### # EXAMPLE FROM http://bondtutor.com/btchp4/topic6/topic6.htm accrualConvention = FinDayCountTypes.ACT_ACT_ICMA y = 0.062267 settlementDate = FinDate(19, 4, 1994) issueDate = FinDate(15, 7, 1990) maturityDate = FinDate(15, 7, 1997) coupon = 0.085 face = ONE_MILLION freqType = FinFrequencyTypes.SEMI_ANNUAL bond = FinBond(issueDate, maturityDate, coupon, freqType, accrualConvention, face) testCases.header("FIELD", "VALUE") fullPrice = bond.fullPriceFromYTM(settlementDate, y) testCases.print("Full Price = ", fullPrice) cleanPrice = bond.cleanPriceFromYTM(settlementDate, y) testCases.print("Clean Price = ", cleanPrice) accd = bond._accruedInterest testCases.print("Accrued = ", accd) ytm = bond.yieldToMaturity(settlementDate, cleanPrice) testCases.print("Yield to Maturity = ", ytm) bump = 1e-4 priceBumpedUp = bond.fullPriceFromYTM(settlementDate, y + bump) testCases.print("Price Bumped Up:", priceBumpedUp) priceBumpedDn = bond.fullPriceFromYTM(settlementDate, y - bump) testCases.print("Price Bumped Dn:", priceBumpedDn) durationByBump = -(priceBumpedUp - fullPrice) / bump testCases.print("Duration by Bump = ", durationByBump) duration = bond.dollarDuration(settlementDate, y) testCases.print("Dollar Duration = ", duration) testCases.print("Duration Difference:", duration - durationByBump) modifiedDuration = bond.modifiedDuration(settlementDate, y) testCases.print("Modified Duration = ", modifiedDuration) macauleyDuration = bond.macauleyDuration(settlementDate, y) testCases.print("Macauley Duration = ", macauleyDuration) conv = bond.convexityFromYTM(settlementDate, y) testCases.print("Convexity = ", conv) # ASSET SWAP SPREAD # When the libor curve is the flat bond curve then the ASW is zero by # definition flatCurve = FinDiscountCurveFlat(settlementDate, ytm, FinFrequencyTypes.SEMI_ANNUAL) testCases.header("FIELD", "VALUE") cleanPrice = bond.cleanPriceFromYTM(settlementDate, ytm) asw = bond.assetSwapSpread(settlementDate, cleanPrice, flatCurve) testCases.print("Discounted on Bond Curve ASW:", asw * 10000) # When the libor curve is the Libor curve then the ASW is positive liborCurve = buildIborCurve(settlementDate) asw = bond.assetSwapSpread(settlementDate, cleanPrice, liborCurve) oas = bond.optionAdjustedSpread(settlementDate, cleanPrice, liborCurve) testCases.print("Discounted on LIBOR Curve ASW:", asw * 10000) testCases.print("Discounted on LIBOR Curve OAS:", oas * 10000) p = 90.0 asw = bond.assetSwapSpread(settlementDate, p, liborCurve) oas = bond.optionAdjustedSpread(settlementDate, p, liborCurve) testCases.print("Deep discount bond at 90 ASW:", asw * 10000) testCases.print("Deep discount bond at 90 OAS:", oas * 10000) p = 100.0 asw = bond.assetSwapSpread(settlementDate, p, liborCurve) oas = bond.optionAdjustedSpread(settlementDate, p, liborCurve) testCases.print("Par bond at 100 ASW:", asw * 10000) testCases.print("Par bond at 100 OAS:", oas * 10000) p = 120.0 asw = bond.assetSwapSpread(settlementDate, p, liborCurve) oas = bond.optionAdjustedSpread(settlementDate, p, liborCurve) testCases.print("Above par bond at 120 ASW:", asw * 10000) testCases.print("Above par bond at 120 OAS:", oas * 10000) ########################################################################## # https://data.bloomberglp.com/bat/sites/3/2017/07/SF-2017_Paul-Fjeldsted.pdf # Page 10 TREASURY NOTE SCREENSHOT ########################################################################## testCases.banner("BLOOMBERG US TREASURY EXAMPLE") settlementDate = FinDate(21, 7, 2017) issueDate = FinDate(15, 5, 2010) maturityDate = FinDate(15, 5, 2027) coupon = 0.02375 freqType = FinFrequencyTypes.SEMI_ANNUAL accrualType = FinDayCountTypes.ACT_ACT_ICMA face = 100.0 bond = FinBond(issueDate, maturityDate, coupon, freqType, accrualType, face) testCases.header("FIELD", "VALUE") cleanPrice = 99.7808417 yld = bond.currentYield(cleanPrice) testCases.print("Current Yield = ", yld) ytm = bond.yieldToMaturity(settlementDate, cleanPrice, FinYTMCalcType.UK_DMO) testCases.print("UK DMO Yield To Maturity = ", ytm) ytm = bond.yieldToMaturity(settlementDate, cleanPrice, FinYTMCalcType.US_STREET) testCases.print("US STREET Yield To Maturity = ", ytm) ytm = bond.yieldToMaturity(settlementDate, cleanPrice, FinYTMCalcType.US_TREASURY) testCases.print("US TREASURY Yield To Maturity = ", ytm) fullPrice = bond.fullPriceFromYTM(settlementDate, ytm) testCases.print("Full Price = ", fullPrice) cleanPrice = bond.cleanPriceFromYTM(settlementDate, ytm) testCases.print("Clean Price = ", cleanPrice) accd = bond._accruedInterest testCases.print("Accrued = ", accd) accddays = bond._accruedDays testCases.print("Accrued Days = ", accddays) duration = bond.dollarDuration(settlementDate, ytm) testCases.print("Dollar Duration = ", duration) modifiedDuration = bond.modifiedDuration(settlementDate, ytm) testCases.print("Modified Duration = ", modifiedDuration) macauleyDuration = bond.macauleyDuration(settlementDate, ytm) testCases.print("Macauley Duration = ", macauleyDuration) conv = bond.convexityFromYTM(settlementDate, ytm) testCases.print("Convexity = ", conv) ########################################################################## # Page 11 APPLE NOTE SCREENSHOT ########################################################################## testCases.banner("BLOOMBERG APPLE CORP BOND EXAMPLE") settlementDate = FinDate(21, 7, 2017) issueDate = FinDate(13, 5, 2012) maturityDate = FinDate(13, 5, 2022) coupon = 0.027 freqType = FinFrequencyTypes.SEMI_ANNUAL accrualType = FinDayCountTypes.THIRTY_E_360_ISDA face = 100.0 bond = FinBond(issueDate, maturityDate, coupon, freqType, accrualType, face) testCases.header("FIELD", "VALUE") cleanPrice = 101.581564 yld = bond.currentYield(cleanPrice) testCases.print("Current Yield", yld) ytm = bond.yieldToMaturity(settlementDate, cleanPrice, FinYTMCalcType.UK_DMO) testCases.print("UK DMO Yield To Maturity", ytm) ytm = bond.yieldToMaturity(settlementDate, cleanPrice, FinYTMCalcType.US_STREET) testCases.print("US STREET Yield To Maturity", ytm) ytm = bond.yieldToMaturity(settlementDate, cleanPrice, FinYTMCalcType.US_TREASURY) testCases.print("US TREASURY Yield To Maturity", ytm) fullPrice = bond.fullPriceFromYTM(settlementDate, ytm) testCases.print("Full Price", fullPrice) cleanPrice = bond.cleanPriceFromYTM(settlementDate, ytm) testCases.print("Clean Price", cleanPrice) accddays = bond._accruedDays testCases.print("Accrued Days", accddays) accd = bond._accruedInterest testCases.print("Accrued", accd) duration = bond.dollarDuration(settlementDate, ytm) testCases.print("Dollar Duration", duration) modifiedDuration = bond.modifiedDuration(settlementDate, ytm) testCases.print("Modified Duration", modifiedDuration) macauleyDuration = bond.macauleyDuration(settlementDate, ytm) testCases.print("Macauley Duration", macauleyDuration) conv = bond.convexityFromYTM(settlementDate, ytm) testCases.print("Convexity", conv)
def test_FinBond(): import pandas as pd bondDataFrame = pd.read_csv('./data/giltbondprices.txt', sep='\t') bondDataFrame['mid'] = 0.5*(bondDataFrame['bid'] + bondDataFrame['ask']) frequencyType = FinFrequencyTypes.SEMI_ANNUAL settlement = FinDate(2012, 9, 19) for accrualType in FinDayCountTypes: testCases.header("MATURITY", "COUPON", "CLEAN_PRICE", "ACCD_DAYS", "ACCRUED", "YTM") for index, bond in bondDataFrame.iterrows(): dateString = bond['maturity'] matDatetime = dt.datetime.strptime(dateString, '%d-%b-%y') maturityDt = FinDate.fromDatetime(matDatetime) coupon = bond['coupon']/100.0 cleanPrice = bond['mid'] bond = FinBond(maturityDt, coupon, frequencyType, accrualType) ytm = bond.yieldToMaturity(settlement, cleanPrice) accd = bond._accrued accd_days = bond._accruedDays testCases.print("%18s" % maturityDt, "%8.4f" % coupon, "%10.4f" % cleanPrice, "%6.0f" % accd_days, "%10.4f" % accd, "%8.4f" % ytm) # EXAMPLE FROM http://bondtutor.com/btchp4/topic6/topic6.htm accrualConvention = FinDayCountTypes.ACT_ACT_ICMA y = 0.062267 settlementDate = FinDate(1994, 4, 19) maturityDate = FinDate(1997, 7, 15) coupon = 0.085 face = 1.0 freqType = FinFrequencyTypes.SEMI_ANNUAL bond = FinBond(maturityDate, coupon, freqType, accrualConvention, face) testCases.header("FIELD", "VALUE") fullPrice = bond.fullPriceFromYield(settlementDate, y) testCases.print("Full Price = ", fullPrice) cleanPrice = bond.cleanPriceFromYield(settlementDate, y) testCases.print("Clean Price = ", cleanPrice) accd = bond._accrued testCases.print("Accrued = ", accd) ytm = bond.yieldToMaturity(settlementDate, cleanPrice) testCases.print("Yield to Maturity = ", ytm) bump = 1e-4 priceBumpedUp = bond.fullPriceFromYield(settlementDate, y + bump) testCases.print("Price Bumped Up:", priceBumpedUp) priceBumpedDn = bond.fullPriceFromYield(settlementDate, y - bump) testCases.print("Price Bumped Dn:", priceBumpedDn) durationByBump = -(priceBumpedUp - fullPrice) / bump testCases.print("Duration by Bump = ", durationByBump) duration = bond.dollarDuration(settlementDate, y) testCases.print("Dollar Duration = ", duration) testCases.print("Duration Difference:", duration - durationByBump) modifiedDuration = bond.modifiedDuration(settlementDate, y) testCases.print("Modified Duration = ", modifiedDuration) macauleyDuration = bond.macauleyDuration(settlementDate, y) testCases.print("Macauley Duration = ", macauleyDuration) conv = bond.convexityFromYield(settlementDate, y) testCases.print("Convexity = ", conv) # ASSET SWAP SPREAD # When the libor curve is the flat bond curve then the ASW is zero by # definition flatCurve = FinFlatCurve(settlementDate, ytm, 2) testCases.header("FIELD", "VALUE") cleanPrice = bond.cleanPriceFromYield(settlementDate, ytm) asw = bond.assetSwapSpread(settlementDate, cleanPrice, flatCurve) testCases.print("Discounted on Bond Curve ASW:", asw * 10000) # When the libor curve is the Libor curve then the ASW is positive liborCurve = buildLiborCurve(settlementDate) asw = bond.assetSwapSpread(settlementDate, cleanPrice, liborCurve) oas = bond.optionAdjustedSpread(settlementDate, cleanPrice, liborCurve) testCases.print("Discounted on LIBOR Curve ASW:", asw * 10000) testCases.print("Discounted on LIBOR Curve OAS:", oas * 10000) p = 0.90 asw = bond.assetSwapSpread(settlementDate, p, liborCurve) oas = bond.optionAdjustedSpread(settlementDate, p, liborCurve) testCases.print("Deep discount bond at 90 ASW:", asw * 10000) testCases.print("Deep discount bond at 90 OAS:", oas * 10000) p = 1.00 asw = bond.assetSwapSpread(settlementDate, p, liborCurve) oas = bond.optionAdjustedSpread(settlementDate, p, liborCurve) testCases.print("Par bond at 100 ASW:", asw * 10000) testCases.print("Par bond at 100 OAS:", oas * 10000) p = 1.20 asw = bond.assetSwapSpread(settlementDate, p, liborCurve) oas = bond.optionAdjustedSpread(settlementDate, p, liborCurve) testCases.print("Above par bond at 120 ASW:", asw * 10000) testCases.print("Above par bond at 120 OAS:", oas * 10000) ########################################################################## # https://data.bloomberglp.com/bat/sites/3/2017/07/SF-2017_Paul-Fjeldsted.pdf # Page 10 TREASURY NOTE SCREENSHOT ########################################################################## testCases.banner("BLOOMBERG US TREASURY EXAMPLE") settlementDate = FinDate(2017, 7, 21) maturityDate = FinDate(2027, 5, 15) coupon = 0.02375 freqType = FinFrequencyTypes.SEMI_ANNUAL accrualType = FinDayCountTypes.ACT_ACT_ICMA face = 100.0 bond = FinBond(maturityDate, coupon, freqType, accrualType, face) testCases.header("FIELD", "VALUE") cleanPrice = 99.780842 yld = bond.currentYield(cleanPrice) testCases.print("Current Yield = ", yld) ytm = bond.yieldToMaturity(settlementDate, cleanPrice, FinYieldConventions.UK_DMO) testCases.print("UK DMO Yield To Maturity = ", ytm) ytm = bond.yieldToMaturity(settlementDate, cleanPrice, FinYieldConventions.US_STREET) testCases.print("US STREET Yield To Maturity = ", ytm) ytm = bond.yieldToMaturity(settlementDate, cleanPrice, FinYieldConventions.US_TREASURY) testCases.print("US TREASURY Yield To Maturity = ", ytm) fullPrice = bond.fullPriceFromYield(settlementDate, ytm) testCases.print("Full Price = ", fullPrice) cleanPrice = bond.cleanPriceFromYield(settlementDate, ytm) testCases.print("Clean Price = ", cleanPrice) accd = bond._accrued testCases.print("Accrued = ", accd) accddays = bond._accruedDays testCases.print("Accrued Days = ", accddays) duration = bond.dollarDuration(settlementDate, ytm) testCases.print("Dollar Duration = ", duration) modifiedDuration = bond.modifiedDuration(settlementDate, ytm) testCases.print("Modified Duration = ", modifiedDuration) macauleyDuration = bond.macauleyDuration(settlementDate, ytm) testCases.print("Macauley Duration = ", macauleyDuration) conv = bond.convexityFromYield(settlementDate, ytm) testCases.print("Convexity = ", conv) ########################################################################## # Page 11 APPLE NOTE SCREENSHOT ########################################################################## testCases.banner("BLOOMBERG APPLE CORP BOND EXAMPLE") settlementDate = FinDate(2017, 7, 21) maturityDate = FinDate(2022, 5, 13) coupon = 0.027 freqType = FinFrequencyTypes.SEMI_ANNUAL accrualType = FinDayCountTypes.ACT_ACT_ICMA face = 100.0 bond = FinBond(maturityDate, coupon, freqType, accrualType, face) testCases.header("FIELD", "VALUE") cleanPrice = 101.581564 yld = bond.currentYield(cleanPrice) testCases.print("Current Yield = ", yld) ytm = bond.yieldToMaturity(settlementDate, cleanPrice, FinYieldConventions.UK_DMO) testCases.print("UK DMO Yield To Maturity = ", ytm) ytm = bond.yieldToMaturity(settlementDate, cleanPrice, FinYieldConventions.US_STREET) testCases.print("US STREET Yield To Maturity = ", ytm) ytm = bond.yieldToMaturity(settlementDate, cleanPrice, FinYieldConventions.US_TREASURY) testCases.print("US TREASURY Yield To Maturity = ", ytm) fullPrice = bond.fullPriceFromYield(settlementDate, ytm) testCases.print("Full Price = ", fullPrice) cleanPrice = bond.cleanPriceFromYield(settlementDate, ytm) testCases.print("Clean Price = ", cleanPrice) # I GET 69 DAYS BUT BBG GETS 68 - CANNOT EXPLAIN!! accddays = bond._accruedDays testCases.print("Accrued Days = ", accddays) accd = bond._accrued testCases.print("Accrued = ", accd) duration = bond.dollarDuration(settlementDate, ytm) testCases.print("Dollar Duration = ", duration) modifiedDuration = bond.modifiedDuration(settlementDate, ytm) testCases.print("Modified Duration = ", modifiedDuration) macauleyDuration = bond.macauleyDuration(settlementDate, ytm) testCases.print("Macauley Duration = ", macauleyDuration) conv = bond.convexityFromYield(settlementDate, ytm) testCases.print("Convexity = ", conv)
FinYieldConventions.US_TREASURY) print("US TREASURY Yield To Maturity = ", ytm) fullPrice = bond.fullPriceFromYield(settlementDate, ytm) print("Full Price = ", fullPrice) cleanPrice = bond.cleanPriceFromYield(settlementDate, ytm) print("Clean Price = ", cleanPrice) accd = bond._accrued print("Accrued = ", accd) accddays = bond._accruedDays print("Accrued Days = ", accddays) duration = bond.dollarDuration(settlementDate, ytm) print("Dollar Duration = ", duration) modifiedDuration = bond.modifiedDuration(settlementDate, ytm) print("Modified Duration = ", modifiedDuration) macauleyDuration = bond.macauleyDuration(settlementDate, ytm) print("Macauley Duration = ", macauleyDuration) conv = bond.convexityFromYield(settlementDate, ytm) print("Convexity = ", conv) discountRate = 0.0275 flatCurve = FinFlatCurve(settlementDate, discountRate, FinCompoundingMethods.SEMI_ANNUAL)