-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSwaptionLeg.jl
322 lines (305 loc) · 10 KB
/
SwaptionLeg.jl
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
"""
@enum(
SwaptionSettlement,
SwaptionCashSettlement,
SwaptionPhysicalSettlement,
)
SwaptionSettlement specifies whether swaption terminates at settlement time
or whether it is converted into a physicl swap.
For `SwaptionCashSettlement` the cash price is calculated as model price,
i.e. physical price at expiry.
"""
@enum(
SwaptionSettlement,
SwaptionCashSettlement,
SwaptionPhysicalSettlement,
)
"""
struct SwaptionLeg <: CashFlowLeg
alias::String
#
expiry_time::ModelTime
settlement_time::ModelTime
float_coupons::AbstractVector
fixed_coupons::AbstractVector
payer_receiver::ModelValue
swap_disc_curve_key::String
settlement_type::SwaptionSettlement
#
notional::ModelValue
swpt_disc_curve_key::String
swpt_fx_key::Union{String, Nothing}
swpt_long_short::ModelValue
#
fixed_times::AbstractVector
fixed_weights::AbstractVector
fixed_rate::ModelValue
exercise_indicator::Payoff
end
A European swaption referencing a Vanilla swap with forward looking or
backward looking rates.
"""
struct SwaptionLeg <: CashFlowLeg
alias::String
#
expiry_time::ModelTime
settlement_time::ModelTime
float_coupons::AbstractVector
fixed_coupons::AbstractVector
payer_receiver::ModelValue
swap_disc_curve_key::String
settlement_type::SwaptionSettlement
#
notional::ModelValue
swpt_disc_curve_key::String
swpt_fx_key::Union{String, Nothing}
swpt_long_short::ModelValue
#
fixed_times::AbstractVector
fixed_weights::AbstractVector
fixed_rate::ModelValue
exercise_indicator::Payoff
end
"""
SwaptionLeg(
alias::String,
#
expiry_time::ModelTime,
settlement_time::ModelTime,
float_coupons::AbstractVector,
fixed_coupons::AbstractVector,
payer_receiver::ModelValue,
swap_disc_curve_key::String,
settlement_type::SwaptionSettlement,
#
notional::ModelValue,
swpt_disc_curve_key::String = swap_disc_curve_key,
swpt_fx_key::Union{String, Nothing} = nothing,
swpt_long_short::ModelValue = +1.0,
)
Create a swaption object.
"""
function SwaptionLeg(
alias::String,
#
expiry_time::ModelTime,
settlement_time::ModelTime,
float_coupons::AbstractVector,
fixed_coupons::AbstractVector,
payer_receiver::ModelValue,
swap_disc_curve_key::String,
settlement_type::SwaptionSettlement,
#
notional::ModelValue,
swpt_disc_curve_key::String = swap_disc_curve_key,
swpt_fx_key::Union{String, Nothing} = nothing,
swpt_long_short::ModelValue = +1.0,
)
#
@assert expiry_time ≥ 0.0
@assert expiry_time ≤ settlement_time
@assert length(float_coupons) > 0
float_coupon_type = typeof(float_coupons[1])
@assert float_coupon_type in (SimpleRateCoupon, CompoundedRateCoupon)
for cp in float_coupons
@assert typeof(cp) == float_coupon_type
if typeof(cp) == SimpleRateCoupon
@assert expiry_time ≤ cp.fixing_time
end
if typeof(cp) == CompoundedRateCoupon
@assert expiry_time ≤ cp.period_times[begin]
end
end
#
for cp in fixed_coupons
@assert typeof(cp) == FixedRateCoupon
@assert expiry_time ≤ cp.pay_time
end
#
effective_time = nothing
maturity_time = nothing
if float_coupon_type == SimpleRateCoupon
effective_time = min((cp.start_time for cp in float_coupons)...)
maturity_time = max((cp.pay_time for cp in float_coupons)...)
@assert effective_time == float_coupons[begin].start_time
@assert maturity_time == float_coupons[end].pay_time
end
if float_coupon_type == CompoundedRateCoupon
effective_time = min((cp.period_times[begin] for cp in float_coupons)...)
maturity_time = max((cp.pay_time for cp in float_coupons)...)
@assert effective_time == float_coupons[begin].period_times[begin]
@assert maturity_time == float_coupons[end].pay_time
end
fixed_times = [cp.pay_time for cp in fixed_coupons] # without effective time
for (first, second) in zip(fixed_times[begin:end-1], fixed_times[begin+1:end])
@assert first ≤ second
end
@assert effective_time ≤ fixed_times[begin]
@assert maturity_time == fixed_times[end]
fixed_times = vcat([effective_time], fixed_times) # with effective time
#
fixed_weights = [cp.year_fraction for cp in fixed_coupons]
@assert all(fixed_weights .> 0.0) # no degenerated coupons
#
fixed_rate = fixed_coupons[begin].fixed_rate
for cp in fixed_coupons
@assert fixed_rate == cp.fixed_rate
# maybe we could relax the constant rate requirement and add some averaging
# methodology here.
end
#
@assert payer_receiver in (+1.0, -1.0)
#
@assert notional > 0.0 # no degenerated leg
@assert swpt_long_short in (+1.0, -1.0)
#
# we cashe the payoff for exercise decision to avoid repeated evaluations
O = Swaption(
expiry_time,
expiry_time,
settlement_time,
[ forward_rate(cf, expiry_time) for cf in float_coupons ],
fixed_times,
fixed_weights,
fixed_rate,
payer_receiver,
swap_disc_curve_key
)
exercise_indicator = Cache(O > 0.0)
#
return SwaptionLeg(
alias,
#
expiry_time,
settlement_time,
float_coupons,
fixed_coupons,
payer_receiver,
swap_disc_curve_key,
settlement_type,
#
notional,
swpt_disc_curve_key,
swpt_fx_key,
swpt_long_short,
#
fixed_times,
fixed_weights,
fixed_rate,
exercise_indicator
#
)
end
"""
future_cashflows(leg::SwaptionLeg, obs_time::ModelTime)
Calculate the list of future undiscounted payoffs in numeraire currency.
"""
function future_cashflows(leg::SwaptionLeg, obs_time::ModelTime)
payoffs = Payoff[]
if obs_time ≥ leg.fixed_times[end]
return payoffs
end
if (leg.settlement_type==SwaptionCashSettlement) && (obs_time ≥ leg.settlement_time)
return payoffs
end
#
forward_rates = [ forward_rate(cf, leg.expiry_time) for cf in leg.float_coupons ]
O = Swaption(
leg.expiry_time,
leg.expiry_time,
leg.settlement_time,
forward_rates,
leg.fixed_times,
leg.fixed_weights,
leg.fixed_rate,
leg.payer_receiver,
leg.swap_disc_curve_key
)
if leg.settlement_type == SwaptionCashSettlement
P = (leg.swpt_long_short * leg.notional) * O
if !isnothing(leg.swpt_fx_key)
P = Asset(leg.settlement_time, leg.swpt_fx_key) * P
end
push!(payoffs, Pay(P, leg.settlement_time))
end
if leg.settlement_type == SwaptionPhysicalSettlement
E = leg.exercise_indicator
for cf in leg.float_coupons
if pay_time(cf) > obs_time
P = (leg.swpt_long_short * leg.payer_receiver * leg.notional) * amount(cf) * E
if !isnothing(leg.swpt_fx_key)
P = Asset(pay_time(cf), leg.swpt_fx_key) * P
end
push!(payoffs, Pay(P, pay_time(cf)))
end
end
for cf in leg.fixed_coupons
if pay_time(cf) > obs_time
P = (-1.0 * leg.swpt_long_short * leg.payer_receiver * leg.notional) * amount(cf) * E # pay/receive fixed rate
if !isnothing(leg.swpt_fx_key)
P = Asset(pay_time(cf), leg.swpt_fx_key) * P
end
push!(payoffs, Pay(P, pay_time(cf)))
end
end
end
return payoffs
end
"""
discounted_cashflows(leg::SwaptionLeg, obs_time::ModelTime)
Calculate the list of future discounted payoffs in numeraire currency.
"""
function discounted_cashflows(leg::SwaptionLeg, obs_time::ModelTime)
payoffs = Payoff[]
if obs_time ≥ leg.fixed_times[end]
return payoffs
end
if (leg.settlement_type==SwaptionCashSettlement) && (obs_time ≥ leg.settlement_time)
return payoffs
end
#
forward_rates = [ forward_rate(cf, min(obs_time, leg.expiry_time)) for cf in leg.float_coupons ]
O = Swaption(
min(obs_time, leg.expiry_time),
leg.expiry_time,
leg.settlement_time,
forward_rates,
leg.fixed_times,
leg.fixed_weights,
leg.fixed_rate,
leg.payer_receiver,
leg.swap_disc_curve_key
)
if obs_time < leg.settlement_time
P = (leg.swpt_long_short * leg.notional) * O
P = ZeroBond(obs_time, leg.settlement_time, leg.swpt_disc_curve_key) * P # option discounting
if !isnothing(leg.swpt_fx_key)
P = Asset(obs_time, leg.swpt_fx_key) * P
end
push!(payoffs, Pay(P, obs_time))
end
if (obs_time ≥ leg.settlement_time) && (leg.settlement_type == SwaptionPhysicalSettlement)
E = leg.exercise_indicator
for cf in leg.float_coupons
if pay_time(cf) > obs_time
P = (leg.swpt_long_short * leg.payer_receiver * leg.notional) * expected_amount(cf, obs_time) * E
P = ZeroBond(obs_time, pay_time(cf), leg.swap_disc_curve_key) * P # swap discounting
if !isnothing(leg.swpt_fx_key)
P = Asset(obs_time, leg.swpt_fx_key) * P
end
push!(payoffs, Pay(P, obs_time))
end
end
for cf in leg.fixed_coupons
if pay_time(cf) > obs_time
P = (-1.0 * leg.swpt_long_short * leg.payer_receiver * leg.notional) * expected_amount(cf, obs_time) * E # pay/receive fixed rate
P = ZeroBond(obs_time, pay_time(cf), leg.swap_disc_curve_key) * P # swap discounting
if !isnothing(leg.swpt_fx_key)
P = Asset(obs_time, leg.swpt_fx_key) * P
end
push!(payoffs, Pay(P, obs_time))
end
end
end
return payoffs
end