def test_FinBondPortfolio(): 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 accrualType = FinDayCountTypes.ACT_ACT_ICMA settlement = FinDate(2012, 9, 19) testCases.header("DCTYPE", "MATDATE", "CPN", "PRICE", "ACCD", "YTM") for accrualType in FinDayCountTypes: 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 testCases.print(accrualType, maturityDt, coupon * 100.0, cleanPrice, accd, ytm * 100.0)
def test_FinBondZeroCurve(): 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 accrualType = FinDayCountTypes.ACT_ACT_ICMA settlement = FinDate(2012, 9, 19) bonds = [] cleanPrices = [] for index, bondRow in bondDataFrame.iterrows(): dateString = bondRow['maturity'] matDatetime = dt.datetime.strptime(dateString, '%d-%b-%y') maturityDt = FinDate.fromDatetime(matDatetime) coupon = bondRow['coupon']/100.0 cleanPrice = bondRow['mid'] bond = FinBond(maturityDt, coupon, frequencyType, accrualType) bonds.append(bond) cleanPrices.append(cleanPrice) ############################################################################### bondCurve = FinBondZeroCurve(settlement, bonds, cleanPrices) testCases.header("DATE", "ZERO RATE") for index, bond in bondDataFrame.iterrows(): dateString = bond['maturity'] matDatetime = dt.datetime.strptime(dateString, '%d-%b-%y') maturityDt = FinDate.fromDatetime(matDatetime) zeroRate = bondCurve.zeroRate(maturityDt) testCases.print(maturityDt, zeroRate) bondCurve.plot("BOND CURVE")
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)
# SPECIFY UK BOND CONVENTIONS frequencyType = FinFrequencyTypes.SEMI_ANNUAL accrualType = FinDayCountTypes.ACT_ACT_ICMA settlement = FinDate(2012, 9, 19) bonds = [] ylds = [] # LOAD BONDS AND CREATE A VECTOR OF FINBOND AND THEIR CORRESPONDING YIELDS 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) yld = bond.yieldToMaturity(settlement, cleanPrice) bonds.append(bond) ylds.append(yld) # FIT THE BOND YIELDS TO A CUBIC POLYNOMIAL curveFitMethod = FinCurveFitMethodPolynomial() fittedCurve1 = FinBondYieldCurve(settlement, bonds, ylds, curveFitMethod) fittedCurve1.display("GBP Yield Curve") # FIT THE BOND YIELDS TO A QUINTIC POLYNOMIAL curveFitMethod = FinCurveFitMethodPolynomial(5) fittedCurve2 = FinBondYieldCurve(settlement, bonds, ylds, curveFitMethod)
def test_FinBondYieldCurve(): ########################################################################### 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 accrualType = FinDayCountTypes.ACT_ACT_ICMA settlement = FinDate(2012, 9, 19) bonds = [] ylds = [] 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) yld = bond.yieldToMaturity(settlement, cleanPrice) bonds.append(bond) ylds.append(yld) ############################################################################### curveFitMethod = FinCurveFitPolynomial() fittedCurve1 = FinBondYieldCurve(settlement, bonds, ylds, curveFitMethod) # fittedCurve1.display("GBP Yield Curve") curveFitMethod = FinCurveFitPolynomial(5) fittedCurve2 = FinBondYieldCurve(settlement, bonds, ylds, curveFitMethod) # fittedCurve2.display("GBP Yield Curve") curveFitMethod = FinCurveFitNelsonSiegel() fittedCurve3 = FinBondYieldCurve(settlement, bonds, ylds, curveFitMethod) # fittedCurve3.display("GBP Yield Curve") curveFitMethod = FinCurveFitNelsonSiegelSvensson() fittedCurve4 = FinBondYieldCurve(settlement, bonds, ylds, curveFitMethod) # fittedCurve4.display("GBP Yield Curve") curveFitMethod = FinCurveFitBSpline() fittedCurve5 = FinBondYieldCurve(settlement, bonds, ylds, curveFitMethod) # fittedCurve5.display("GBP Yield Curve") ############################################################################### testCases.header("PARAMETER", "VALUE") testCases.print("beta1", fittedCurve3._curveFit._beta1) testCases.print("beta2", fittedCurve3._curveFit._beta2) testCases.print("beta3", fittedCurve3._curveFit._beta3) testCases.print("tau", fittedCurve3._curveFit._tau) testCases.header("PARAMETER", "VALUE") testCases.print("beta1", fittedCurve4._curveFit._beta1) testCases.print("beta2", fittedCurve4._curveFit._beta2) testCases.print("beta3", fittedCurve4._curveFit._beta3) testCases.print("beta4", fittedCurve4._curveFit._beta4) testCases.print("tau1", fittedCurve4._curveFit._tau1) testCases.print("tau2", fittedCurve4._curveFit._tau2) ############################################################################### maturityDate = FinDate(2030, 9, 19) interpolatedYield = fittedCurve5.interpolatedYield(maturityDate) testCases.print(maturityDate, interpolatedYield)