forked from lballabio/QuantLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassetswap.cpp
4413 lines (4039 loc) · 218 KB
/
assetswap.cpp
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2007 Chiara Fornarola
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include "toplevelfixture.hpp"
#include "utilities.hpp"
#include <ql/time/schedule.hpp>
#include <ql/instruments/assetswap.hpp>
#include <ql/instruments/bond.hpp>
#include <ql/instruments/bonds/fixedratebond.hpp>
#include <ql/instruments/bonds/floatingratebond.hpp>
#include <ql/instruments/bonds/cmsratebond.hpp>
#include <ql/instruments/bonds/zerocouponbond.hpp>
#include <ql/index.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/time/calendars/nullcalendar.hpp>
#include <ql/time/calendars/target.hpp>
#include <ql/time/daycounters/thirty360.hpp>
#include <ql/time/daycounters/actual365fixed.hpp>
#include <ql/time/daycounters/actual360.hpp>
#include <ql/time/daycounters/actualactual.hpp>
#include <ql/time/daycounters/simpledaycounter.hpp>
#include <ql/indexes/ibor/euribor.hpp>
#include <ql/indexes/ibor/estr.hpp>
#include <ql/indexes/swapindex.hpp>
#include <ql/cashflows/fixedratecoupon.hpp>
#include <ql/cashflows/iborcoupon.hpp>
#include <ql/cashflows/cmscoupon.hpp>
#include <ql/cashflows/couponpricer.hpp>
#include <ql/cashflows/conundrumpricer.hpp>
#include <ql/termstructures/volatility/optionlet/constantoptionletvol.hpp>
#include <ql/termstructures/volatility/swaption/swaptionconstantvol.hpp>
#include <ql/termstructures/volatility/swaption/swaptionvolmatrix.hpp>
#include <ql/termstructures/volatility/swaption/swaptionvolcube.hpp>
#include <ql/utilities/dataformatters.hpp>
#include <ql/cashflows/cashflows.hpp>
#include <ql/cashflows/simplecashflow.hpp>
#include <ql/pricingengines/bond/discountingbondengine.hpp>
#include <ql/pricingengines/bond/bondfunctions.hpp>
#include <ql/pricingengines/swap/discountingswapengine.hpp>
#include <ql/quotes/simplequote.hpp>
using namespace QuantLib;
using namespace boost::unit_test_framework;
BOOST_FIXTURE_TEST_SUITE(QuantLibTests, TopLevelFixture)
BOOST_AUTO_TEST_SUITE(AssetSwapTests)
struct CommonVars {
// common data
ext::shared_ptr<IborIndex> iborIndex;
ext::shared_ptr<SwapIndex> swapIndex;
ext::shared_ptr<IborCouponPricer> pricer;
ext::shared_ptr<CmsCouponPricer> cmspricer;
Spread spread;
Spread nonnullspread;
Real faceAmount;
Compounding compounding;
RelinkableHandle<YieldTermStructure> termStructure;
// initial setup
CommonVars() {
Natural swapSettlementDays = 2;
faceAmount = 100.0;
BusinessDayConvention fixedConvention = Unadjusted;
compounding = Continuous;
Frequency fixedFrequency = Annual;
Frequency floatingFrequency = Semiannual;
iborIndex = ext::shared_ptr<IborIndex>(
new Euribor(Period(floatingFrequency), termStructure));
Calendar calendar = iborIndex->fixingCalendar();
swapIndex = ext::make_shared<SwapIndex>(
"EuriborSwapIsdaFixA", 10*Years, swapSettlementDays,
iborIndex->currency(), calendar,
Period(fixedFrequency), fixedConvention,
iborIndex->dayCounter(), iborIndex);
spread = 0.0;
nonnullspread = 0.003;
Date today(24,April,2007);
Settings::instance().evaluationDate() = today;
termStructure.linkTo(flatRate(today, 0.05, Actual365Fixed()));
pricer = ext::shared_ptr<IborCouponPricer>(new BlackIborCouponPricer);
Handle<SwaptionVolatilityStructure> swaptionVolatilityStructure(
ext::shared_ptr<SwaptionVolatilityStructure>(new
ConstantSwaptionVolatility(today, NullCalendar(),Following,
0.2, Actual365Fixed())));
Handle<Quote> meanReversionQuote(ext::shared_ptr<Quote>(new
SimpleQuote(0.01)));
cmspricer = ext::shared_ptr<CmsCouponPricer>(new
AnalyticHaganPricer(swaptionVolatilityStructure,
GFunctionFactory::Standard,
meanReversionQuote));
}
};
BOOST_AUTO_TEST_CASE(testConsistency) {
BOOST_TEST_MESSAGE(
"Testing consistency between fair price and fair spread...");
CommonVars vars;
Calendar bondCalendar = TARGET();
Natural settlementDays = 3;
// Fixed Underlying bond (Isin: DE0001135275 DBR 4 01/04/37)
// maturity doesn't occur on a business day
Schedule bondSchedule(Date(4,January,2005),
Date(4,January,2037),
Period(Annual), bondCalendar,
Unadjusted, Unadjusted,
DateGeneration::Backward, false);
ext::shared_ptr<Bond> bond(new
FixedRateBond(settlementDays, vars.faceAmount,
bondSchedule,
std::vector<Rate>(1, 0.04),
ActualActual(ActualActual::ISDA),
Following,
100.0, Date(4,January,2005)));
bool payFixedRate = true;
Real bondPrice = 95.0;
bool isPar = true;
AssetSwap parAssetSwap(payFixedRate,
bond, bondPrice,
vars.iborIndex, vars.spread,
Schedule(),
vars.iborIndex->dayCounter(),
isPar);
ext::shared_ptr<PricingEngine> swapEngine(new
DiscountingSwapEngine(vars.termStructure,
true,
bond->settlementDate(),
Settings::instance().evaluationDate()));
parAssetSwap.setPricingEngine(swapEngine);
Real fairCleanPrice = parAssetSwap.fairCleanPrice();
Spread fairSpread = parAssetSwap.fairSpread();
Real tolerance = 1.0e-13;
AssetSwap assetSwap2(payFixedRate,
bond, fairCleanPrice,
vars.iborIndex, vars.spread,
Schedule(),
vars.iborIndex->dayCounter(),
isPar);
assetSwap2.setPricingEngine(swapEngine);
if (std::fabs(assetSwap2.NPV())>tolerance) {
BOOST_FAIL("\npar asset swap fair clean price doesn't zero the NPV: " <<
std::fixed << std::setprecision(4) <<
"\n clean price: " << bondPrice <<
"\n fair clean price: " << fairCleanPrice <<
"\n NPV: " << assetSwap2.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap2.fairCleanPrice() - fairCleanPrice)>tolerance) {
BOOST_FAIL("\npar asset swap fair clean price doesn't equal input "
"clean price at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input clean price: " << fairCleanPrice <<
"\n fair clean price: " << assetSwap2.fairCleanPrice() <<
"\n NPV: " << assetSwap2.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap2.fairSpread() - vars.spread)>tolerance) {
BOOST_FAIL("\npar asset swap fair spread doesn't equal input spread "
"at zero NPV: " << std::fixed << std::setprecision(4) <<
"\n input spread: " << vars.spread <<
"\n fair spread: " << assetSwap2.fairSpread() <<
"\n NPV: " << assetSwap2.NPV() <<
"\n tolerance: " << tolerance);
}
AssetSwap assetSwap3(payFixedRate,
bond, bondPrice,
vars.iborIndex, fairSpread,
Schedule(),
vars.iborIndex->dayCounter(),
isPar);
assetSwap3.setPricingEngine(swapEngine);
if (std::fabs(assetSwap3.NPV())>tolerance) {
BOOST_FAIL("\npar asset swap fair spread doesn't zero the NPV: " <<
std::fixed << std::setprecision(4) <<
"\n spread: " << vars.spread <<
"\n fair spread: " << fairSpread <<
"\n NPV: " << assetSwap3.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap3.fairCleanPrice() - bondPrice)>tolerance) {
BOOST_FAIL("\npar asset swap fair clean price doesn't equal input "
"clean price at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input clean price: " << bondPrice <<
"\n fair clean price: " << assetSwap3.fairCleanPrice() <<
"\n NPV: " << assetSwap3.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap3.fairSpread() - fairSpread)>tolerance) {
BOOST_FAIL("\npar asset swap fair spread doesn't equal input spread at"
" zero NPV: " << std::fixed << std::setprecision(4) <<
"\n input spread: " << fairSpread <<
"\n fair spread: " << assetSwap3.fairSpread() <<
"\n NPV: " << assetSwap3.NPV() <<
"\n tolerance: " << tolerance);
}
// let's change the npv date
swapEngine = ext::shared_ptr<PricingEngine>(new
DiscountingSwapEngine(vars.termStructure,
true,
bond->settlementDate(),
bond->settlementDate()));
parAssetSwap.setPricingEngine(swapEngine);
// fair clean price and fair spread should not change
if (std::fabs(parAssetSwap.fairCleanPrice() - fairCleanPrice)>tolerance) {
BOOST_FAIL("\npar asset swap fair clean price changed with NpvDate:" <<
std::fixed << std::setprecision(4) <<
"\n expected clean price: " << fairCleanPrice <<
"\n fair clean price: "<<parAssetSwap.fairCleanPrice()<<
"\n tolerance: " << tolerance);
}
if (std::fabs(parAssetSwap.fairSpread() - fairSpread)>tolerance) {
BOOST_FAIL("\npar asset swap fair spread changed with NpvDate:" <<
std::fixed << std::setprecision(4) <<
"\n expected spread: " << fairSpread <<
"\n fair spread: " << parAssetSwap.fairSpread() <<
"\n tolerance: " << tolerance);
}
assetSwap2 = AssetSwap(payFixedRate,
bond, fairCleanPrice,
vars.iborIndex, vars.spread,
Schedule(),
vars.iborIndex->dayCounter(),
isPar);
assetSwap2.setPricingEngine(swapEngine);
if (std::fabs(assetSwap2.NPV())>tolerance) {
BOOST_FAIL("\npar asset swap fair clean price doesn't zero the NPV: " <<
std::fixed << std::setprecision(4) <<
"\n clean price: " << bondPrice <<
"\n fair clean price: " << fairCleanPrice <<
"\n NPV: " << assetSwap2.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap2.fairCleanPrice() - fairCleanPrice)>tolerance) {
BOOST_FAIL("\npar asset swap fair clean price doesn't equal input "
"clean price at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input clean price: " << fairCleanPrice <<
"\n fair clean price: " << assetSwap2.fairCleanPrice() <<
"\n NPV: " << assetSwap2.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap2.fairSpread() - vars.spread)>tolerance) {
BOOST_FAIL("\npar asset swap fair spread doesn't equal input spread at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input spread: " << vars.spread <<
"\n fair spread: " << assetSwap2.fairSpread() <<
"\n NPV: " << assetSwap2.NPV() <<
"\n tolerance: " << tolerance);
}
assetSwap3 = AssetSwap(payFixedRate,
bond, bondPrice,
vars.iborIndex, fairSpread,
Schedule(),
vars.iborIndex->dayCounter(),
isPar);
assetSwap3.setPricingEngine(swapEngine);
if (std::fabs(assetSwap3.NPV())>tolerance) {
BOOST_FAIL("\npar asset swap fair spread doesn't zero the NPV: " <<
std::fixed << std::setprecision(4) <<
"\n spread: " << vars.spread <<
"\n fair spread: " << fairSpread <<
"\n NPV: " << assetSwap3.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap3.fairCleanPrice() - bondPrice)>tolerance) {
BOOST_FAIL("\npar asset swap fair clean price doesn't equal input "
"clean price at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input clean price: " << bondPrice <<
"\n fair clean price: " << assetSwap3.fairCleanPrice() <<
"\n NPV: " << assetSwap3.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap3.fairSpread() - fairSpread)>tolerance) {
BOOST_FAIL("\npar asset swap fair spread doesn't equal input spread at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input spread: " << fairSpread <<
"\n fair spread: " << assetSwap3.fairSpread() <<
"\n NPV: " << assetSwap3.NPV() <<
"\n tolerance: " << tolerance);
}
// using overnight index
auto overnight = ext::make_shared<Estr>(vars.termStructure);
Schedule overnightSchedule(bond->settlementDate(),
bond->maturityDate(),
6 * Months,
overnight->fixingCalendar(),
overnight->businessDayConvention(),
overnight->businessDayConvention(),
DateGeneration::Backward,
false);
parAssetSwap = AssetSwap(payFixedRate,
bond, bondPrice,
overnight, vars.spread,
overnightSchedule,
overnight->dayCounter(),
isPar);
swapEngine = ext::make_shared<DiscountingSwapEngine>(
vars.termStructure,
true,
bond->settlementDate(),
Settings::instance().evaluationDate());
parAssetSwap.setPricingEngine(swapEngine);
fairCleanPrice = parAssetSwap.fairCleanPrice();
fairSpread = parAssetSwap.fairSpread();
assetSwap2 = AssetSwap(payFixedRate,
bond, fairCleanPrice,
overnight, vars.spread,
overnightSchedule,
overnight->dayCounter(),
isPar);
assetSwap2.setPricingEngine(swapEngine);
if (std::fabs(assetSwap2.NPV())>tolerance) {
BOOST_FAIL("\npar asset swap fair clean price doesn't zero the NPV: " <<
std::fixed << std::setprecision(4) <<
"\n clean price: " << bondPrice <<
"\n fair clean price: " << fairCleanPrice <<
"\n NPV: " << assetSwap2.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap2.fairCleanPrice() - fairCleanPrice)>tolerance) {
BOOST_FAIL("\npar asset swap fair clean price doesn't equal input "
"clean price at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input clean price: " << fairCleanPrice <<
"\n fair clean price: " << assetSwap2.fairCleanPrice() <<
"\n NPV: " << assetSwap2.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap2.fairSpread() - vars.spread)>tolerance) {
BOOST_FAIL("\npar asset swap fair spread doesn't equal input spread "
"at zero NPV: " << std::fixed << std::setprecision(4) <<
"\n input spread: " << vars.spread <<
"\n fair spread: " << assetSwap2.fairSpread() <<
"\n NPV: " << assetSwap2.NPV() <<
"\n tolerance: " << tolerance);
}
assetSwap3 = AssetSwap(payFixedRate,
bond, bondPrice,
overnight, fairSpread,
overnightSchedule,
overnight->dayCounter(),
isPar);
assetSwap3.setPricingEngine(swapEngine);
if (std::fabs(assetSwap3.NPV())>tolerance) {
BOOST_FAIL("\npar asset swap fair spread doesn't zero the NPV: " <<
std::fixed << std::setprecision(4) <<
"\n spread: " << vars.spread <<
"\n fair spread: " << fairSpread <<
"\n NPV: " << assetSwap3.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap3.fairCleanPrice() - bondPrice)>tolerance) {
BOOST_FAIL("\npar asset swap fair clean price doesn't equal input "
"clean price at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input clean price: " << bondPrice <<
"\n fair clean price: " << assetSwap3.fairCleanPrice() <<
"\n NPV: " << assetSwap3.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap3.fairSpread() - fairSpread)>tolerance) {
BOOST_FAIL("\npar asset swap fair spread doesn't equal input spread at"
" zero NPV: " << std::fixed << std::setprecision(4) <<
"\n input spread: " << fairSpread <<
"\n fair spread: " << assetSwap3.fairSpread() <<
"\n NPV: " << assetSwap3.NPV() <<
"\n tolerance: " << tolerance);
}
// now market asset swap
isPar = false;
AssetSwap mktAssetSwap(payFixedRate,
bond, bondPrice,
vars.iborIndex, vars.spread,
Schedule(),
vars.iborIndex->dayCounter(),
isPar);
swapEngine = ext::shared_ptr<PricingEngine>(new
DiscountingSwapEngine(vars.termStructure,
true,
bond->settlementDate(),
Settings::instance().evaluationDate()));
mktAssetSwap.setPricingEngine(swapEngine);
fairCleanPrice = mktAssetSwap.fairCleanPrice();
fairSpread = mktAssetSwap.fairSpread();
AssetSwap assetSwap4(payFixedRate,
bond, fairCleanPrice,
vars.iborIndex, vars.spread,
Schedule(),
vars.iborIndex->dayCounter(),
isPar);
assetSwap4.setPricingEngine(swapEngine);
if (std::fabs(assetSwap4.NPV())>tolerance) {
BOOST_FAIL("\nmarket asset swap fair clean price doesn't zero the NPV: " <<
std::fixed << std::setprecision(4) <<
"\n clean price: " << bondPrice <<
"\n fair clean price: " << fairCleanPrice <<
"\n NPV: " << assetSwap4.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap4.fairCleanPrice() - fairCleanPrice)>tolerance) {
BOOST_FAIL("\nmarket asset swap fair clean price doesn't equal input "
"clean price at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input clean price: " << fairCleanPrice <<
"\n fair clean price: " << assetSwap4.fairCleanPrice() <<
"\n NPV: " << assetSwap4.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap4.fairSpread() - vars.spread)>tolerance) {
BOOST_FAIL("\nmarket asset swap fair spread doesn't equal input spread"
" at zero NPV: " << std::fixed << std::setprecision(4) <<
"\n input spread: " << vars.spread <<
"\n fair spread: " << assetSwap4.fairSpread() <<
"\n NPV: " << assetSwap4.NPV() <<
"\n tolerance: " << tolerance);
}
AssetSwap assetSwap5(payFixedRate,
bond, bondPrice,
vars.iborIndex, fairSpread,
Schedule(),
vars.iborIndex->dayCounter(),
isPar);
assetSwap5.setPricingEngine(swapEngine);
if (std::fabs(assetSwap5.NPV())>tolerance) {
BOOST_FAIL("\nmarket asset swap fair spread doesn't zero the NPV: " <<
std::fixed << std::setprecision(4) <<
"\n spread: " << vars.spread <<
"\n fair spread: " << fairSpread <<
"\n NPV: " << assetSwap5.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap5.fairCleanPrice() - bondPrice)>tolerance) {
BOOST_FAIL("\nmarket asset swap fair clean price doesn't equal input "
"clean price at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input clean price: " << bondPrice <<
"\n fair clean price: " << assetSwap5.fairCleanPrice() <<
"\n NPV: " << assetSwap5.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap5.fairSpread() - fairSpread)>tolerance) {
BOOST_FAIL("\nmarket asset swap fair spread doesn't equal input spread at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input spread: " << fairSpread <<
"\n fair spread: " << assetSwap5.fairSpread() <<
"\n NPV: " << assetSwap5.NPV() <<
"\n tolerance: " << tolerance);
}
// let's change the npv date
swapEngine = ext::shared_ptr<PricingEngine>(new
DiscountingSwapEngine(vars.termStructure,
true,
bond->settlementDate(),
bond->settlementDate()));
mktAssetSwap.setPricingEngine(swapEngine);
// fair clean price and fair spread should not change
if (std::fabs(mktAssetSwap.fairCleanPrice() - fairCleanPrice)>tolerance) {
BOOST_FAIL("\nmarket asset swap fair clean price changed with NpvDate:" <<
std::fixed << std::setprecision(4) <<
"\n expected clean price: " << fairCleanPrice <<
"\n fair clean price: " << mktAssetSwap.fairCleanPrice() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(mktAssetSwap.fairSpread() - fairSpread)>tolerance) {
BOOST_FAIL("\nmarket asset swap fair spread changed with NpvDate:" <<
std::fixed << std::setprecision(4) <<
"\n expected spread: " << fairSpread <<
"\n fair spread: " << mktAssetSwap.fairSpread() <<
"\n tolerance: " << tolerance);
}
assetSwap4 = AssetSwap(payFixedRate,
bond, fairCleanPrice,
vars.iborIndex, vars.spread,
Schedule(),
vars.iborIndex->dayCounter(),
isPar);
assetSwap4.setPricingEngine(swapEngine);
if (std::fabs(assetSwap4.NPV())>tolerance) {
BOOST_FAIL("\nmarket asset swap fair clean price doesn't zero the NPV: " <<
std::fixed << std::setprecision(4) <<
"\n clean price: " << bondPrice <<
"\n fair clean price: " << fairCleanPrice <<
"\n NPV: " << assetSwap4.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap4.fairCleanPrice() - fairCleanPrice)>tolerance) {
BOOST_FAIL("\nmarket asset swap fair clean price doesn't equal input "
"clean price at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input clean price: " << fairCleanPrice <<
"\n fair clean price: " << assetSwap4.fairCleanPrice() <<
"\n NPV: " << assetSwap4.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap4.fairSpread() - vars.spread)>tolerance) {
BOOST_FAIL("\nmarket asset swap fair spread doesn't equal input spread at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input spread: " << vars.spread <<
"\n fair spread: " << assetSwap4.fairSpread() <<
"\n NPV: " << assetSwap4.NPV() <<
"\n tolerance: " << tolerance);
}
assetSwap5 = AssetSwap(payFixedRate,
bond, bondPrice,
vars.iborIndex, fairSpread,
Schedule(),
vars.iborIndex->dayCounter(),
isPar);
assetSwap5.setPricingEngine(swapEngine);
if (std::fabs(assetSwap5.NPV())>tolerance) {
BOOST_FAIL("\nmarket asset swap fair spread doesn't zero the NPV: " <<
std::fixed << std::setprecision(4) <<
"\n spread: " << vars.spread <<
"\n fair spread: " << fairSpread <<
"\n NPV: " << assetSwap5.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap5.fairCleanPrice() - bondPrice)>tolerance) {
BOOST_FAIL("\nmarket asset swap fair clean price doesn't equal input "
"clean price at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input clean price: " << bondPrice <<
"\n fair clean price: " << assetSwap5.fairCleanPrice() <<
"\n NPV: " << assetSwap5.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap5.fairSpread() - fairSpread)>tolerance) {
BOOST_FAIL("\nmarket asset swap fair spread doesn't equal input spread at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input spread: " << fairSpread <<
"\n fair spread: " << assetSwap5.fairSpread() <<
"\n NPV: " << assetSwap5.NPV() <<
"\n tolerance: " << tolerance);
}
// using overnight index
mktAssetSwap = AssetSwap(payFixedRate,
bond, bondPrice,
overnight, vars.spread,
overnightSchedule,
overnight->dayCounter(),
isPar);
swapEngine = ext::make_shared<DiscountingSwapEngine>(
vars.termStructure,
true,
bond->settlementDate(),
Settings::instance().evaluationDate());
mktAssetSwap.setPricingEngine(swapEngine);
fairCleanPrice = mktAssetSwap.fairCleanPrice();
fairSpread = mktAssetSwap.fairSpread();
assetSwap4 = AssetSwap(payFixedRate,
bond, fairCleanPrice,
overnight, vars.spread,
overnightSchedule,
overnight->dayCounter(),
isPar);
assetSwap4.setPricingEngine(swapEngine);
if (std::fabs(assetSwap4.NPV())>tolerance) {
BOOST_FAIL("\nmarket asset swap fair clean price doesn't zero the NPV: " <<
std::fixed << std::setprecision(4) <<
"\n clean price: " << bondPrice <<
"\n fair clean price: " << fairCleanPrice <<
"\n NPV: " << assetSwap4.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap4.fairCleanPrice() - fairCleanPrice)>tolerance) {
BOOST_FAIL("\nmarket asset swap fair clean price doesn't equal input "
"clean price at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input clean price: " << fairCleanPrice <<
"\n fair clean price: " << assetSwap4.fairCleanPrice() <<
"\n NPV: " << assetSwap4.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap4.fairSpread() - vars.spread)>tolerance) {
BOOST_FAIL("\nmarket asset swap fair spread doesn't equal input spread"
" at zero NPV: " << std::fixed << std::setprecision(4) <<
"\n input spread: " << vars.spread <<
"\n fair spread: " << assetSwap4.fairSpread() <<
"\n NPV: " << assetSwap4.NPV() <<
"\n tolerance: " << tolerance);
}
assetSwap5 = AssetSwap(payFixedRate,
bond, bondPrice,
overnight, fairSpread,
overnightSchedule,
overnight->dayCounter(),
isPar);
assetSwap5.setPricingEngine(swapEngine);
if (std::fabs(assetSwap5.NPV())>tolerance) {
BOOST_FAIL("\nmarket asset swap fair spread doesn't zero the NPV: " <<
std::fixed << std::setprecision(4) <<
"\n spread: " << vars.spread <<
"\n fair spread: " << fairSpread <<
"\n NPV: " << assetSwap5.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap5.fairCleanPrice() - bondPrice)>tolerance) {
BOOST_FAIL("\nmarket asset swap fair clean price doesn't equal input "
"clean price at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input clean price: " << bondPrice <<
"\n fair clean price: " << assetSwap5.fairCleanPrice() <<
"\n NPV: " << assetSwap5.NPV() <<
"\n tolerance: " << tolerance);
}
if (std::fabs(assetSwap5.fairSpread() - fairSpread)>tolerance) {
BOOST_FAIL("\nmarket asset swap fair spread doesn't equal input spread at zero NPV: " <<
std::fixed << std::setprecision(4) <<
"\n input spread: " << fairSpread <<
"\n fair spread: " << assetSwap5.fairSpread() <<
"\n NPV: " << assetSwap5.NPV() <<
"\n tolerance: " << tolerance);
}
}
BOOST_AUTO_TEST_CASE(testImpliedValue) {
BOOST_TEST_MESSAGE("Testing implied bond value against asset-swap fair"
" price with null spread...");
bool usingAtParCoupons = IborCoupon::Settings::instance().usingAtParCoupons();
CommonVars vars;
Calendar bondCalendar = TARGET();
Natural settlementDays = 3;
Natural fixingDays = 2;
bool payFixedRate = true;
bool parAssetSwap = true;
bool inArrears = false;
// Fixed Underlying bond (Isin: DE0001135275 DBR 4 01/04/37)
// maturity doesn't occur on a business day
Schedule fixedBondSchedule1(Date(4,January,2005),
Date(4,January,2037),
Period(Annual), bondCalendar,
Unadjusted, Unadjusted,
DateGeneration::Backward, false);
ext::shared_ptr<Bond> fixedBond1(
new FixedRateBond(settlementDays, vars.faceAmount,
fixedBondSchedule1,
std::vector<Rate>(1, 0.04),
ActualActual(ActualActual::ISDA),
Following,
100.0, Date(4,January,2005)));
ext::shared_ptr<PricingEngine> bondEngine(
new DiscountingBondEngine(vars.termStructure));
ext::shared_ptr<PricingEngine> swapEngine(
new DiscountingSwapEngine(vars.termStructure));
fixedBond1->setPricingEngine(bondEngine);
Real fixedBondPrice1 = fixedBond1->cleanPrice();
AssetSwap fixedBondAssetSwap1(payFixedRate,
fixedBond1, fixedBondPrice1,
vars.iborIndex, vars.spread,
Schedule(),
vars.iborIndex->dayCounter(),
parAssetSwap);
fixedBondAssetSwap1.setPricingEngine(swapEngine);
Real fixedBondAssetSwapPrice1 = fixedBondAssetSwap1.fairCleanPrice();
Real tolerance = 1.0e-13;
// for indexed coupons the float leg will not be par, therefore we
// have to relax the tolerance - note that the fair clean price is
// correct though, only we can not compare it to the bond price
// directly. The same kind of discrepancy will occur for a multi
// curve set up, which we do not test here.
Real tolerance2 = usingAtParCoupons ? 1.0e-13 : 1.0e-2;
Real error1 = std::fabs(fixedBondAssetSwapPrice1-fixedBondPrice1);
if (error1>tolerance2) {
BOOST_FAIL("wrong zero spread asset swap price for fixed bond:" <<
std::fixed << std::setprecision(4) <<
"\n bond's clean price: " << fixedBondPrice1 <<
"\n asset swap fair price: " << fixedBondAssetSwapPrice1 <<
std::scientific << std::setprecision(2) <<
"\n error: " << error1 <<
"\n tolerance: " << tolerance2);
}
// Fixed Underlying bond (Isin: IT0006527060 IBRD 5 02/05/19)
// maturity occurs on a business day
Schedule fixedBondSchedule2(Date(5,February,2005),
Date(5,February,2019),
Period(Annual), bondCalendar,
Unadjusted, Unadjusted,
DateGeneration::Backward, false);
ext::shared_ptr<Bond> fixedBond2(
new FixedRateBond(settlementDays, vars.faceAmount,
fixedBondSchedule2,
std::vector<Rate>(1, 0.05),
Thirty360(Thirty360::BondBasis),
Following,
100.0, Date(5,February,2005)));
fixedBond2->setPricingEngine(bondEngine);
Real fixedBondPrice2 = fixedBond2->cleanPrice();
AssetSwap fixedBondAssetSwap2(payFixedRate,
fixedBond2, fixedBondPrice2,
vars.iborIndex, vars.spread,
Schedule(),
vars.iborIndex->dayCounter(),
parAssetSwap);
fixedBondAssetSwap2.setPricingEngine(swapEngine);
Real fixedBondAssetSwapPrice2 = fixedBondAssetSwap2.fairCleanPrice();
Real error2 = std::fabs(fixedBondAssetSwapPrice2-fixedBondPrice2);
if (error2>tolerance2) {
BOOST_FAIL("wrong zero spread asset swap price for fixed bond:" <<
std::fixed << std::setprecision(4) <<
"\n bond's clean price: " << fixedBondPrice2 <<
"\n asset swap fair price: " << fixedBondAssetSwapPrice2 <<
std::scientific << std::setprecision(2) <<
"\n error: " << error2 <<
"\n tolerance: " << tolerance2);
}
// FRN Underlying bond (Isin: IT0003543847 ISPIM 0 09/29/13)
// maturity doesn't occur on a business day
Schedule floatingBondSchedule1(Date(29,September,2003),
Date(29,September,2013),
Period(Semiannual), bondCalendar,
Unadjusted, Unadjusted,
DateGeneration::Backward, false);
ext::shared_ptr<Bond> floatingBond1(
new FloatingRateBond(settlementDays, vars.faceAmount,
floatingBondSchedule1,
vars.iborIndex, Actual360(),
Following, fixingDays,
std::vector<Real>(1,1),
std::vector<Spread>(1,0.0056),
std::vector<Rate>(),
std::vector<Rate>(),
inArrears,
100.0, Date(29,September,2003)));
floatingBond1->setPricingEngine(bondEngine);
setCouponPricer(floatingBond1->cashflows(), vars.pricer);
vars.iborIndex->addFixing(Date(27,March,2007), 0.0402);
Real floatingBondPrice1 = floatingBond1->cleanPrice();
AssetSwap floatingBondAssetSwap1(payFixedRate,
floatingBond1, floatingBondPrice1,
vars.iborIndex, vars.spread,
Schedule(),
vars.iborIndex->dayCounter(),
parAssetSwap);
floatingBondAssetSwap1.setPricingEngine(swapEngine);
Real floatingBondAssetSwapPrice1 = floatingBondAssetSwap1.fairCleanPrice();
Real error3 = std::fabs(floatingBondAssetSwapPrice1-floatingBondPrice1);
if (error3>tolerance2) {
BOOST_FAIL("wrong zero spread asset swap price for floater:" <<
std::fixed << std::setprecision(4) <<
"\n bond's clean price: " << floatingBondPrice1 <<
"\n asset swap fair price: " << floatingBondAssetSwapPrice1 <<
std::scientific << std::setprecision(2) <<
"\n error: " << error3 <<
"\n tolerance: " << tolerance2);
}
// FRN Underlying bond (Isin: XS0090566539 COE 0 09/24/18)
// maturity occurs on a business day
Schedule floatingBondSchedule2(Date(24,September,2004),
Date(24,September,2018),
Period(Semiannual), bondCalendar,
ModifiedFollowing, ModifiedFollowing,
DateGeneration::Backward, false);
ext::shared_ptr<Bond> floatingBond2(
new FloatingRateBond(settlementDays, vars.faceAmount,
floatingBondSchedule2,
vars.iborIndex, Actual360(),
ModifiedFollowing, fixingDays,
std::vector<Real>(1,1),
std::vector<Spread>(1,0.0025),
std::vector<Rate>(),
std::vector<Rate>(),
inArrears,
100.0, Date(24,September,2004)));
floatingBond2->setPricingEngine(bondEngine);
setCouponPricer(floatingBond2->cashflows(), vars.pricer);
vars.iborIndex->addFixing(Date(22,March,2007), 0.04013);
Real currentCoupon=0.04013+0.0025;
Rate floatingCurrentCoupon= floatingBond2->nextCouponRate();
Real error4= std::fabs(floatingCurrentCoupon-currentCoupon);
if (error4>tolerance) {
BOOST_FAIL("wrong current coupon is returned for floater bond:" <<
std::fixed << std::setprecision(4) <<
"\n bond's calculated current coupon: " <<
currentCoupon <<
"\n current coupon asked to the bond: " <<
floatingCurrentCoupon <<
std::scientific << std::setprecision(2) <<
"\n error: " << error4 <<
"\n tolerance: " << tolerance);
}
Real floatingBondPrice2 = floatingBond2->cleanPrice();
AssetSwap floatingBondAssetSwap2(payFixedRate,
floatingBond2, floatingBondPrice2,
vars.iborIndex, vars.spread,
Schedule(),
vars.iborIndex->dayCounter(),
parAssetSwap);
floatingBondAssetSwap2.setPricingEngine(swapEngine);
Real floatingBondAssetSwapPrice2 = floatingBondAssetSwap2.fairCleanPrice();
Real error5 = std::fabs(floatingBondAssetSwapPrice2-floatingBondPrice2);
if (error5>tolerance2) {
BOOST_FAIL("wrong zero spread asset swap price for floater:" <<
std::fixed << std::setprecision(4) <<
"\n bond's clean price: " << floatingBondPrice2 <<
"\n asset swap fair price: " << floatingBondAssetSwapPrice2 <<
std::scientific << std::setprecision(2) <<
"\n error: " << error5 <<
"\n tolerance: " << tolerance2);
}
// CMS Underlying bond (Isin: XS0228052402 CRDIT 0 8/22/20)
// maturity doesn't occur on a business day
Schedule cmsBondSchedule1(Date(22,August,2005),
Date(22,August,2020),
Period(Annual), bondCalendar,
Unadjusted, Unadjusted,
DateGeneration::Backward, false);
ext::shared_ptr<Bond> cmsBond1(
new CmsRateBond(settlementDays, vars.faceAmount,
cmsBondSchedule1,
vars.swapIndex, Thirty360(Thirty360::BondBasis),
Following, fixingDays,
std::vector<Real>(1,1.0),
std::vector<Spread>(1,0.0),
std::vector<Rate>(1,0.055),
std::vector<Rate>(1,0.025),
inArrears,
100.0, Date(22,August,2005)));
cmsBond1->setPricingEngine(bondEngine);
setCouponPricer(cmsBond1->cashflows(), vars.cmspricer);
vars.swapIndex->addFixing(Date(18,August,2006), 0.04158);
Real cmsBondPrice1 = cmsBond1->cleanPrice();
AssetSwap cmsBondAssetSwap1(payFixedRate,
cmsBond1, cmsBondPrice1,
vars.iborIndex, vars.spread,
Schedule(),
vars.iborIndex->dayCounter(),
parAssetSwap);
cmsBondAssetSwap1.setPricingEngine(swapEngine);
Real cmsBondAssetSwapPrice1 = cmsBondAssetSwap1.fairCleanPrice();
Real error6 = std::fabs(cmsBondAssetSwapPrice1-cmsBondPrice1);
if (error6>tolerance2) {
BOOST_FAIL("wrong zero spread asset swap price for cms bond:" <<
std::fixed << std::setprecision(4) <<
"\n bond's clean price: " << cmsBondPrice1 <<
"\n asset swap fair price: " << cmsBondAssetSwapPrice1 <<
std::scientific << std::setprecision(2) <<
"\n error: " << error6 <<
"\n tolerance: " << tolerance2);
}
// CMS Underlying bond (Isin: XS0218766664 ISPIM 0 5/6/15)
// maturity occurs on a business day
Schedule cmsBondSchedule2(Date(06,May,2005),
Date(06,May,2015),
Period(Annual), bondCalendar,
Unadjusted, Unadjusted,
DateGeneration::Backward, false);
ext::shared_ptr<Bond> cmsBond2(new
CmsRateBond(settlementDays, vars.faceAmount, cmsBondSchedule2,
vars.swapIndex, Thirty360(Thirty360::BondBasis),
Following, fixingDays,
std::vector<Real>(1,0.84), std::vector<Spread>(1,0.0),
std::vector<Rate>(), std::vector<Rate>(),
inArrears,
100.0, Date(06,May,2005)));
cmsBond2->setPricingEngine(bondEngine);
setCouponPricer(cmsBond2->cashflows(), vars.cmspricer);
vars.swapIndex->addFixing(Date(04,May,2006), 0.04217);
Real cmsBondPrice2 = cmsBond2->cleanPrice();
AssetSwap cmsBondAssetSwap2(payFixedRate,
cmsBond2, cmsBondPrice2,
vars.iborIndex, vars.spread,
Schedule(),
vars.iborIndex->dayCounter(),
parAssetSwap);
cmsBondAssetSwap2.setPricingEngine(swapEngine);
Real cmsBondAssetSwapPrice2 = cmsBondAssetSwap2.fairCleanPrice();
Real error7 = std::fabs(cmsBondAssetSwapPrice2-cmsBondPrice2);
if (error7>tolerance2) {
BOOST_FAIL("wrong zero spread asset swap price for cms bond:" <<
std::fixed << std::setprecision(4) <<
"\n bond's clean price: " << cmsBondPrice2 <<
"\n asset swap fair price: " << cmsBondAssetSwapPrice2 <<
std::scientific << std::setprecision(2) <<
"\n error: " << error7 <<
"\n tolerance: " << tolerance2);
}
// Zero Coupon bond (Isin: DE0004771662 IBRD 0 12/20/15)
// maturity doesn't occur on a business day
ext::shared_ptr<Bond> zeroCpnBond1(new
ZeroCouponBond(settlementDays, bondCalendar, vars.faceAmount,
Date(20,December,2015),
Following,
100.0, Date(19,December,1985)));
zeroCpnBond1->setPricingEngine(bondEngine);
Real zeroCpnBondPrice1 = zeroCpnBond1->cleanPrice();
AssetSwap zeroCpnAssetSwap1(payFixedRate,
zeroCpnBond1, zeroCpnBondPrice1,
vars.iborIndex, vars.spread,
Schedule(),
vars.iborIndex->dayCounter(),
parAssetSwap);
zeroCpnAssetSwap1.setPricingEngine(swapEngine);
Real zeroCpnBondAssetSwapPrice1 = zeroCpnAssetSwap1.fairCleanPrice();
Real error8 = std::fabs(cmsBondAssetSwapPrice1-cmsBondPrice1);
if (error8>tolerance2) {
BOOST_FAIL("wrong zero spread asset swap price for zero cpn bond:" <<
std::fixed << std::setprecision(4) <<