-
-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
833 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import os | ||
import pandas as pd | ||
import matplotlib.pyplot as plt | ||
|
||
import sys | ||
sys.path.append("..") | ||
|
||
from financepy.utils.global_vars import gPercent | ||
from financepy.utils.calendar import CalendarTypes | ||
from financepy.utils.day_count import DayCountTypes | ||
from financepy.utils.frequency import FrequencyTypes | ||
from financepy.utils.date import Date, from_datetime | ||
from financepy.market.curves.interpolator import InterpTypes | ||
from financepy.market.curves.discount_curve import DiscountCurve | ||
from financepy.market.curves.discount_curve_flat import DiscountCurveFlat | ||
from financepy.products.bonds.bond import Bond | ||
from financepy.products.rates.ibor_single_curve import IborSingleCurve | ||
from financepy.products.rates.ibor_benchmarks_report import dataframe_to_benchmarks | ||
|
||
# Set to True to run this file spandalone and see some useful info | ||
DIAGNOSTICS_MODE = False | ||
|
||
from FinTestCases import FinTestCases, globalTestCaseMode | ||
|
||
test_cases = FinTestCases(__file__, globalTestCaseMode) | ||
|
||
|
||
def test_z_spread_flat_curve(): | ||
|
||
settlement = Date(19, 9, 2012) | ||
base_curve = DiscountCurveFlat(settlement, flat_rate=1*gPercent) | ||
return _test_z_spread_for_curve(base_curve) | ||
|
||
|
||
def test_z_spread_actual_curve(): | ||
|
||
path = os.path.join(os.path.dirname(__file__), './data/GBP_OIS_20120919.csv') | ||
dfbm = pd.read_csv(path, index_col=0) | ||
|
||
dfbm['base_date'] = pd.to_datetime(dfbm['base_date'], errors='ignore', format='%d/%m/%Y') | ||
dfbm['start_date'] = pd.to_datetime(dfbm['start_date'], errors='ignore', format='%d/%m/%Y') # allow tenors | ||
dfbm['maturity_date'] = pd.to_datetime(dfbm['maturity_date'], errors='ignore', format='%d/%m/%Y') # allow tenors | ||
|
||
valuation_date = from_datetime(dfbm.loc[0, 'base_date']) | ||
cal = CalendarTypes.UNITED_KINGDOM | ||
bms = dataframe_to_benchmarks(dfbm, asof_date=valuation_date, calendar_type=cal) | ||
depos = bms['IborDeposit'] | ||
fras = bms['IborFRA'] | ||
swaps = bms['IborSwap'] | ||
|
||
fras.sort(key=lambda fra: fra.maturity_dt) | ||
libor_curve = IborSingleCurve(valuation_date, depos, fras, swaps, InterpTypes.LINEAR_ZERO_RATES) | ||
|
||
return _test_z_spread_for_curve(libor_curve) | ||
|
||
|
||
def _test_z_spread_for_curve(base_curve: DiscountCurve): | ||
path = os.path.join(os.path.dirname(__file__), './data/gilt_bond_prices.txt') | ||
bondDataFrame = pd.read_csv(path, sep='\t') | ||
bondDataFrame['mid'] = 0.5*(bondDataFrame['bid'] + bondDataFrame['ask']) | ||
|
||
bondDataFrame['maturity'] = pd.to_datetime(bondDataFrame['maturity'], format="%d-%b-%y") | ||
freq_type = FrequencyTypes.SEMI_ANNUAL | ||
accrual_type = DayCountTypes.ACT_ACT_ICMA | ||
|
||
for bdfIndex, bondRow in bondDataFrame.iterrows(): | ||
matDatetime = bondRow['maturity'] | ||
maturityDt = from_datetime(matDatetime) | ||
issueDt = Date(maturityDt.d, maturityDt.m, 2000) | ||
coupon = bondRow['coupon']/100.0 | ||
clean_price = bondRow['mid'] | ||
bond = Bond(issueDt, maturityDt, coupon, freq_type, accrual_type) | ||
z_spread = bond.z_spread(base_curve.value_dt, clean_price, base_curve) | ||
asset_swap_spread = bond.asset_swap_spread(base_curve.value_dt, clean_price, base_curve) | ||
bondDataFrame.loc[bdfIndex, 'z_spread'] = z_spread | ||
bondDataFrame.loc[bdfIndex, 'asset_swap_spread'] = asset_swap_spread | ||
|
||
if DIAGNOSTICS_MODE: | ||
print(bondDataFrame) | ||
plt.plot(bondDataFrame['maturity'], bondDataFrame['gross redemption yield'], '.', label='yield') | ||
plt.plot(bondDataFrame['maturity'], bondDataFrame['z_spread']*100, '.', label='z_spread') | ||
plt.plot(bondDataFrame['maturity'], bondDataFrame['asset_swap_spread']*100, '.', label='asset_swap_spread') | ||
plt.legend(loc='best') | ||
plt.show() | ||
|
||
assert bondDataFrame['z_spread'].isnull().values.any() == False | ||
|
||
|
||
if __name__ == '__main__': | ||
test_z_spread_flat_curve() | ||
test_z_spread_actual_curve() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
,base_date,type,start_date,maturity_date,day_count_type,notional,contract_rate,fixed_leg_type,fixed_freq_type,curve,currency | ||
0,19/09/2012,IborDeposit,19/09/2012,19/12/2012,ACT_360,10000,0.003815,PAY,,OIS,GBP | ||
1,19/09/2012,IborFRA,19/12/2012,19/03/2013,ACT_360,10000,0.003322,PAY,,OIS,GBP | ||
2,19/09/2012,IborFRA,20/03/2013,20/06/2013,ACT_360,10000,0.003205,PAY,,OIS,GBP | ||
3,19/09/2012,IborFRA,19/06/2013,19/09/2013,ACT_360,10000,0.003265,PAY,,OIS,GBP | ||
4,19/09/2012,IborFRA,18/09/2013,18/12/2013,ACT_360,10000,0.00341,PAY,,OIS,GBP | ||
5,19/09/2012,IborFRA,18/12/2013,18/03/2014,ACT_360,10000,0.003675,PAY,,OIS,GBP | ||
6,19/09/2012,IborFRA,19/03/2014,19/06/2014,ACT_360,10000,0.003995,PAY,,OIS,GBP | ||
7,19/09/2012,IborFRA,18/06/2014,18/09/2014,ACT_360,10000,0.00447,PAY,,OIS,GBP | ||
8,19/09/2012,IborFRA,17/09/2014,17/12/2014,ACT_360,10000,0.00502,PAY,,OIS,GBP | ||
9,19/09/2012,IborFRA,17/12/2014,17/03/2015,ACT_360,10000,0.005595,PAY,,OIS,GBP | ||
10,19/09/2012,IborFRA,18/03/2015,18/06/2015,ACT_360,10000,0.00611,PAY,,OIS,GBP | ||
11,19/09/2012,IborFRA,17/06/2015,17/09/2015,ACT_360,10000,0.006715,PAY,,OIS,GBP | ||
12,19/09/2012,IborFRA,16/09/2015,16/12/2015,ACT_360,10000,0.007415,PAY,,OIS,GBP | ||
13,19/09/2012,IborSwap,19/09/2012,19/09/2016,ACT_360,10000,0.005579274,PAY,ANNUAL,OIS,GBP | ||
14,19/09/2012,IborSwap,19/09/2012,19/09/2017,ACT_360,10000,0.007227034,PAY,ANNUAL,OIS,GBP | ||
15,19/09/2012,IborSwap,19/09/2012,19/09/2018,ACT_360,10000,0.009073759,PAY,ANNUAL,OIS,GBP | ||
16,19/09/2012,IborSwap,19/09/2012,19/09/2019,ACT_360,10000,0.011003603,PAY,ANNUAL,OIS,GBP | ||
17,19/09/2012,IborSwap,19/09/2012,19/09/2020,ACT_360,10000,0.01292279,PAY,ANNUAL,OIS,GBP | ||
18,19/09/2012,IborSwap,19/09/2012,19/09/2021,ACT_360,10000,0.01476606,PAY,ANNUAL,OIS,GBP | ||
19,19/09/2012,IborSwap,19/09/2012,19/09/2022,ACT_360,10000,0.016469326,PAY,ANNUAL,OIS,GBP | ||
20,19/09/2012,IborSwap,19/09/2012,19/09/2024,ACT_360,10000,0.019226422,PAY,ANNUAL,OIS,GBP | ||
21,19/09/2012,IborSwap,19/09/2012,19/09/2027,ACT_360,10000,0.022052563,PAY,ANNUAL,OIS,GBP | ||
22,19/09/2012,IborSwap,19/09/2012,19/09/2032,ACT_360,10000,0.025029801,PAY,ANNUAL,OIS,GBP | ||
23,19/09/2012,IborSwap,19/09/2012,19/09/2037,ACT_360,10000,0.026712763,PAY,ANNUAL,OIS,GBP | ||
24,19/09/2012,IborSwap,19/09/2012,19/09/2042,ACT_360,10000,0.027739254,PAY,ANNUAL,OIS,GBP | ||
25,19/09/2012,IborSwap,19/09/2012,19/09/2047,ACT_360,10000,0.028452841,PAY,ANNUAL,OIS,GBP | ||
26,19/09/2012,IborSwap,19/09/2012,19/09/2052,ACT_360,10000,0.028903302,PAY,ANNUAL,OIS,GBP | ||
27,19/09/2012,IborSwap,19/09/2012,19/09/2057,ACT_360,10000,0.029121657,PAY,ANNUAL,OIS,GBP | ||
28,19/09/2012,IborSwap,19/09/2012,19/09/2062,ACT_360,10000,0.029225286,PAY,ANNUAL,OIS,GBP | ||
29,19/09/2012,IborSwap,19/09/2012,19/09/2072,ACT_360,10000,0.029407055,PAY,ANNUAL,OIS,GBP | ||
30,19/09/2012,IborSwap,19/09/2012,19/09/2082,ACT_360,10000,0.029593652,PAY,ANNUAL,OIS,GBP | ||
31,19/09/2012,IborSwap,19/09/2012,19/09/2092,ACT_360,10000,0.029716109,PAY,ANNUAL,OIS,GBP |
Oops, something went wrong.