1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
from quantlib.time.api import (
WeekendsOnly,
today,
Years,
Months,
Period,
Date,
Actual365Fixed,
Actual360,
Quarterly,
Following,
Unadjusted,
Schedule,
Rule,
)
from quantlib.instruments.api import CreditDefaultSwap, Side
from quantlib.pricingengines.credit.isda_cds_engine import (
IsdaCdsEngine,
ForwardsInCouponPeriod,
NumericalFix,
AccrualBias,
)
from quantlib.termstructures.credit.api import (
SpreadCdsHelper,
PiecewiseDefaultCurve,
FlatHazardRate,
)
from quantlib.settings import Settings
from serenitas.analytics.yieldcurve import YC, rate_helpers
from pyisda.curve import SpreadCurve
from pyisda.utils import build_yc
from pyisda.legs import ContingentLeg, FeeLeg
import datetime
import array
import math
def snac_pv(spread, term_date, fixed_coupon=0.01, recovery=0.4, ts=YC()):
settings = Settings()
calendar = WeekendsOnly()
cds_helper = SpreadCdsHelper(
spread,
Period(57, Months),
1,
calendar,
Quarterly,
Following,
Rule.CDS,
Actual360(),
recovery,
ts,
lastperiod=Actual360(True),
)
cds_helper.set_isda_engine_parameters(
int(NumericalFix.Taylor),
int(AccrualBias.HalfDayBias),
int(ForwardsInCouponPeriod.Flat),
)
pdc = PiecewiseDefaultCurve(
"SurvivalProbability",
"LogLinear",
settings.evaluation_date,
[cds_helper],
Actual365Fixed(),
)
isda_pricer = IsdaCdsEngine(
pdc,
recovery,
ts,
False,
forwards_in_coupon_period=ForwardsInCouponPeriod.Piecewise,
accrual_bias=AccrualBias.HalfDayBias,
)
protect_start = settings.evaluation_date + 1
cds_schedule = Schedule(
protect_start,
term_date,
Period(Quarterly),
calendar,
Following,
Unadjusted,
Rule.CDS,
)
cds_trade = CreditDefaultSwap(
Side.BUYER,
100,
fixed_coupon,
cds_schedule,
Following,
Actual360(),
protection_start=protect_start,
last_period_day_counter=Actual360(True),
)
cds_trade.set_pricing_engine(isda_pricer)
return cds_trade, cds_helper, isda_pricer
def jpmorgan_curves(trade_date, value_date, start_date, end_date, spread, recovery=0.4):
yc = build_yc(trade_date, True)
step_in_date = trade_date + datetime.timedelta(days=1)
spread = array.array("d", [spread])
recovery = array.array("d", [recovery])
sc = SpreadCurve(
trade_date,
yc,
start_date,
step_in_date,
value_date,
[end_date],
spread,
recovery,
True,
)
return yc, sc
if __name__ == "__main__":
settings = Settings()
settings.evaluation_date = Date(21, 5, 2009)
yield_helpers = rate_helpers()
ts = YC(helpers=yield_helpers)
tenor = Period(5, Years)
trade_date = datetime.date(2009, 5, 21)
stepin_date = trade_date + datetime.timedelta(days=1)
value_date = datetime.date(2009, 5, 26)
term_date = datetime.date(2019, 6, 20)
start_date = datetime.date(2009, 3, 20)
spread = 0.001
yc, sc = jpmorgan_curves(
trade_date, value_date, start_date, term_date, spread, recovery=0.4
)
sc_data = sc.inspect()["data"]
hazard_rate = math.log(1 + sc_data[0][1])
contingent_leg = ContingentLeg(start_date, term_date, 10000000)
fee_leg = FeeLeg(start_date, term_date, True, 10000000, 0.01)
flat_curve = FlatHazardRate(0, WeekendsOnly(), hazard_rate, Actual365Fixed())
cds_schedule = Schedule(
Date.from_datetime(trade_date),
Date.from_datetime(term_date),
Period(Quarterly),
WeekendsOnly(),
Following,
Unadjusted,
Rule.CDS,
)
cds_trade = CreditDefaultSwap.from_upfront(
Side.BUYER,
10000000,
0.0,
0.01,
cds_schedule,
Following,
Actual360(),
protection_start=Date.from_datetime(trade_date) + 1,
last_period_day_counter=Actual360(True),
)
isda_pricer = IsdaCdsEngine(
flat_curve,
0.4,
ts,
accrual_bias=AccrualBias.HalfDayBias,
forwards_in_coupon_period=ForwardsInCouponPeriod.Piecewise,
)
# 795915.9787
cds_trade.set_pricing_engine(isda_pricer)
cds_trade2 = CreditDefaultSwap(
Side.BUYER,
10000000,
spread,
cds_schedule,
Following,
Actual360(),
protection_start=Date.from_datetime(trade_date) + 1,
last_period_day_counter=Actual360(True),
)
# h = cds_trade2.implied_hazard_rate(0., ts)
h = 0.00168276528775
flat_curve2 = FlatHazardRate(0, WeekendsOnly(), h, Actual365Fixed())
isda_pricer2 = IsdaCdsEngine(flat_curve2, 0.4, ts)
cds_trade.set_pricing_engine(isda_pricer2)
print(cds_trade.fair_upfront)
# hazard_rate = 0.12649393489974806
# cds_trade.set_pricing_engine(isda_pricer)
# cds_trade2 = CreditDefaultSwap.from_upfront(BUYER, 10000000, 0., 0.01, cds_schedule,
# Following, Actual360(),
# protection_start = Date.from_datetime(trade_date) + 1,
# last_period_day_counter = Actual360(True))
# cds_trade3 = CreditDefaultSwap(BUYER, 10000000, 0.05, cds_schedule,
# Following, Actual360(),
# protection_start = Date.from_datetime(trade_date) + 1,
# last_period_day_counter = Actual360(True))
|