Skip to content

Commit b6dd01e

Browse files
committed
Using Modified Duration to Estimate Gains or Losses
1 parent 8ece847 commit b6dd01e

File tree

3 files changed

+164
-132
lines changed

3 files changed

+164
-132
lines changed

financepy/products/rates/ibor_swap.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,18 @@ def receiver_side_modified_duration(self, value_dt, discount_curve,payment_perio
450450

451451
###########################################################################
452452

453+
def payer_side_profits(self,
454+
value_dt,
455+
discount_curve,payment_periods: float,
456+
swap_rate_changes: float ):
457+
"""Computation of the Profits for the Fixed-Rate Payer's Perspective in Interest Rate Swap
458+
"""
459+
# Get coupon frequency
460+
coupon_frequency = self.fixed_leg.freq_type.value
461+
annualized_modi_dur = self.payer_side_modified_duration(value_dt, discount_curve,payment_periods)/coupon_frequency
462+
return (-1)*(annualized_modi_dur*self.fixed_leg.notional*swap_rate_changes)
463+
464+
453465
def __repr__(self):
454466

455467
s = label_to_string("OBJECT TYPE", type(self).__name__)

notebooks/products/rates/FINIBORSWAP_ReplicationgBONDMATHDurationExample.ipynb

Lines changed: 65 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,25 @@
66
"metadata": {
77
"collapsed": true,
88
"ExecuteTime": {
9-
"end_time": "2025-05-23T01:25:52.331390Z",
10-
"start_time": "2025-05-23T01:25:52.328042Z"
9+
"end_time": "2025-05-23T01:37:56.175867Z",
10+
"start_time": "2025-05-23T01:37:56.171771Z"
1111
}
1212
},
1313
"source": [
1414
"from financepy.utils import *\n",
1515
"from financepy.products.rates import *\n",
1616
"from financepy.market.curves import *\n",
1717
"import datetime as dt\n",
18-
"\n",
19-
"from notebooks.products.rates.FINIBORSWAP_ReplicationgBONDMATHDurationExample3M import payment_period"
18+
"\n"
2019
],
2120
"outputs": [],
22-
"execution_count": 22
21+
"execution_count": 2
2322
},
2423
{
2524
"metadata": {
2625
"ExecuteTime": {
27-
"end_time": "2025-05-23T01:25:53.315940Z",
28-
"start_time": "2025-05-23T01:25:53.311144Z"
26+
"end_time": "2025-05-23T01:37:57.664887Z",
27+
"start_time": "2025-05-23T01:37:57.637136Z"
2928
}
3029
},
3130
"cell_type": "code",
@@ -65,13 +64,13 @@
6564
],
6665
"id": "1ebf43316efee835",
6766
"outputs": [],
68-
"execution_count": 23
67+
"execution_count": 3
6968
},
7069
{
7170
"metadata": {
7271
"ExecuteTime": {
73-
"end_time": "2025-05-23T01:25:55.512229Z",
74-
"start_time": "2025-05-23T01:25:55.509220Z"
72+
"end_time": "2025-05-23T01:38:00.243014Z",
73+
"start_time": "2025-05-23T01:38:00.238262Z"
7574
}
7675
},
7776
"cell_type": "code",
@@ -84,13 +83,13 @@
8483
],
8584
"id": "8e46f142f3cba5d7",
8685
"outputs": [],
87-
"execution_count": 24
86+
"execution_count": 4
8887
},
8988
{
9089
"metadata": {
9190
"ExecuteTime": {
92-
"end_time": "2025-05-23T01:25:57.156649Z",
93-
"start_time": "2025-05-23T01:25:57.152272Z"
91+
"end_time": "2025-05-23T01:38:01.679542Z",
92+
"start_time": "2025-05-23T01:38:01.673799Z"
9493
}
9594
},
9695
"cell_type": "code",
@@ -108,13 +107,13 @@
108107
],
109108
"id": "290b7a06932da41d",
110109
"outputs": [],
111-
"execution_count": 25
110+
"execution_count": 5
112111
},
113112
{
114113
"metadata": {
115114
"ExecuteTime": {
116-
"end_time": "2025-05-23T01:25:58.324864Z",
117-
"start_time": "2025-05-23T01:25:58.320708Z"
115+
"end_time": "2025-05-23T01:38:03.960837Z",
116+
"start_time": "2025-05-23T01:38:03.952526Z"
118117
}
119118
},
120119
"cell_type": "code",
@@ -127,18 +126,18 @@
127126
"0.03404344249149676"
128127
]
129128
},
130-
"execution_count": 26,
129+
"execution_count": 6,
131130
"metadata": {},
132131
"output_type": "execute_result"
133132
}
134133
],
135-
"execution_count": 26
134+
"execution_count": 6
136135
},
137136
{
138137
"metadata": {
139138
"ExecuteTime": {
140-
"end_time": "2025-05-23T01:25:59.653219Z",
141-
"start_time": "2025-05-23T01:25:59.647340Z"
139+
"end_time": "2025-05-23T01:38:05.697242Z",
140+
"start_time": "2025-05-23T01:38:05.690197Z"
142141
}
143142
},
144143
"cell_type": "code",
@@ -151,18 +150,18 @@
151150
"5070.7014320408925"
152151
]
153152
},
154-
"execution_count": 27,
153+
"execution_count": 7,
155154
"metadata": {},
156155
"output_type": "execute_result"
157156
}
158157
],
159-
"execution_count": 27
158+
"execution_count": 7
160159
},
161160
{
162161
"metadata": {
163162
"ExecuteTime": {
164-
"end_time": "2025-05-23T01:33:14.904831Z",
165-
"start_time": "2025-05-23T01:33:14.899446Z"
163+
"end_time": "2025-05-23T01:38:07.096647Z",
164+
"start_time": "2025-05-23T01:38:07.090478Z"
166165
}
167166
},
168167
"cell_type": "code",
@@ -178,93 +177,90 @@
178177
"-6.767653343606554"
179178
]
180179
},
181-
"execution_count": 40,
180+
"execution_count": 8,
182181
"metadata": {},
183182
"output_type": "execute_result"
184183
}
185184
],
186-
"execution_count": 40
185+
"execution_count": 8
187186
},
188187
{
189188
"metadata": {
190189
"ExecuteTime": {
191-
"end_time": "2025-05-23T01:27:02.073420Z",
192-
"start_time": "2025-05-23T01:27:02.060189Z"
190+
"end_time": "2025-05-23T01:38:09.247259Z",
191+
"start_time": "2025-05-23T01:38:09.242767Z"
193192
}
194193
},
195194
"cell_type": "code",
196195
"source": "swap.receiver_side_macaulay_duration(value_dt, zero_curve,payment_periods)",
197196
"id": "96d568f7a71861fe",
198197
"outputs": [
199198
{
200-
"ename": "TypeError",
201-
"evalue": "IborSwap.receiver_side_macaulay_duration() takes 3 positional arguments but 4 were given",
202-
"output_type": "error",
203-
"traceback": [
204-
"\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
205-
"\u001B[1;31mTypeError\u001B[0m Traceback (most recent call last)",
206-
"Cell \u001B[1;32mIn[36], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[43mswap\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mreceiver_side_macaulay_duration\u001B[49m\u001B[43m(\u001B[49m\u001B[43mvalue_dt\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mzero_curve\u001B[49m\u001B[43m,\u001B[49m\u001B[43mpayment_periods\u001B[49m\u001B[43m)\u001B[49m\n",
207-
"\u001B[1;31mTypeError\u001B[0m: IborSwap.receiver_side_macaulay_duration() takes 3 positional arguments but 4 were given"
208-
]
199+
"data": {
200+
"text/plain": [
201+
"6.767653343606554"
202+
]
203+
},
204+
"execution_count": 9,
205+
"metadata": {},
206+
"output_type": "execute_result"
209207
}
210208
],
211-
"execution_count": 36
209+
"execution_count": 9
212210
},
213211
{
214212
"metadata": {
215213
"ExecuteTime": {
216-
"end_time": "2025-05-23T01:27:08.799220Z",
217-
"start_time": "2025-05-23T01:27:08.788672Z"
214+
"end_time": "2025-05-23T01:38:11.105175Z",
215+
"start_time": "2025-05-23T01:38:11.098720Z"
218216
}
219217
},
220218
"cell_type": "code",
221219
"source": "swap.payer_side_modified_duration(value_dt, zero_curve,payment_periods)",
222220
"id": "ec2187c7336c1bc3",
223221
"outputs": [
224222
{
225-
"ename": "TypeError",
226-
"evalue": "IborSwap.payer_side_modified_duration() takes 3 positional arguments but 4 were given",
227-
"output_type": "error",
228-
"traceback": [
229-
"\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
230-
"\u001B[1;31mTypeError\u001B[0m Traceback (most recent call last)",
231-
"Cell \u001B[1;32mIn[37], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[43mswap\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mpayer_side_modified_duration\u001B[49m\u001B[43m(\u001B[49m\u001B[43mvalue_dt\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mzero_curve\u001B[49m\u001B[43m,\u001B[49m\u001B[43mpayment_periods\u001B[49m\u001B[43m)\u001B[49m\n",
232-
"\u001B[1;31mTypeError\u001B[0m: IborSwap.payer_side_modified_duration() takes 3 positional arguments but 4 were given"
233-
]
223+
"data": {
224+
"text/plain": [
225+
"-6.710540865595371"
226+
]
227+
},
228+
"execution_count": 10,
229+
"metadata": {},
230+
"output_type": "execute_result"
234231
}
235232
],
236-
"execution_count": 37
233+
"execution_count": 10
237234
},
238235
{
239236
"metadata": {
240237
"ExecuteTime": {
241-
"end_time": "2025-05-23T01:27:12.992002Z",
242-
"start_time": "2025-05-23T01:27:12.980577Z"
238+
"end_time": "2025-05-23T01:38:12.408437Z",
239+
"start_time": "2025-05-23T01:38:12.401251Z"
243240
}
244241
},
245242
"cell_type": "code",
246243
"source": "swap.receiver_side_modified_duration(value_dt, zero_curve,payment_periods)",
247244
"id": "2c1b845afaf5e18f",
248245
"outputs": [
249246
{
250-
"ename": "TypeError",
251-
"evalue": "IborSwap.receiver_side_modified_duration() takes 3 positional arguments but 4 were given",
252-
"output_type": "error",
253-
"traceback": [
254-
"\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
255-
"\u001B[1;31mTypeError\u001B[0m Traceback (most recent call last)",
256-
"Cell \u001B[1;32mIn[38], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[43mswap\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mreceiver_side_modified_duration\u001B[49m\u001B[43m(\u001B[49m\u001B[43mvalue_dt\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mzero_curve\u001B[49m\u001B[43m,\u001B[49m\u001B[43mpayment_periods\u001B[49m\u001B[43m)\u001B[49m\n",
257-
"\u001B[1;31mTypeError\u001B[0m: IborSwap.receiver_side_modified_duration() takes 3 positional arguments but 4 were given"
258-
]
247+
"data": {
248+
"text/plain": [
249+
"6.710540865595371"
250+
]
251+
},
252+
"execution_count": 11,
253+
"metadata": {},
254+
"output_type": "execute_result"
259255
}
260256
],
261-
"execution_count": 38
257+
"execution_count": 11
262258
},
263259
{
264260
"metadata": {
265261
"ExecuteTime": {
266-
"end_time": "2025-05-23T01:25:42.381647100Z",
267-
"start_time": "2025-05-22T06:57:55.610805Z"
262+
"end_time": "2025-05-23T01:38:15.309142Z",
263+
"start_time": "2025-05-23T01:38:15.301795Z"
268264
}
269265
},
270266
"cell_type": "code",
@@ -316,13 +312,13 @@
316312
]
317313
}
318314
],
319-
"execution_count": 65
315+
"execution_count": 12
320316
},
321317
{
322318
"metadata": {
323319
"ExecuteTime": {
324-
"end_time": "2025-05-23T01:25:42.389019500Z",
325-
"start_time": "2025-05-22T07:19:36.402004Z"
320+
"end_time": "2025-05-23T01:38:16.922391Z",
321+
"start_time": "2025-05-23T01:38:16.915012Z"
326322
}
327323
},
328324
"cell_type": "code",
@@ -343,12 +339,12 @@
343339
"dtype: object"
344340
]
345341
},
346-
"execution_count": 70,
342+
"execution_count": 13,
347343
"metadata": {},
348344
"output_type": "execute_result"
349345
}
350346
],
351-
"execution_count": 70
347+
"execution_count": 13
352348
},
353349
{
354350
"metadata": {},

0 commit comments

Comments
 (0)