1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*
4  Copyright (C) 2009 Roland Lichters
5  Copyright (C) 2009 Ferdinando Ametrano
6  Copyright (C) 2017 Joseph Jeisman
7  Copyright (C) 2017 Fabrice Lecuyer
8 
9  This file is part of QuantLib, a free-software/open-source library
10  for financial quantitative analysts and developers - http://quantlib.org/
11 
12  QuantLib is free software: you can redistribute it and/or modify it
13  under the terms of the QuantLib license.  You should have received a
14  copy of the license along with this program; if not, please email
15  <quantlib-dev@lists.sf.net>. The license is also available online at
16  <http://quantlib.org/license.shtml>.
17 
18  This program is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  FOR A PARTICULAR PURPOSE.  See the license for more details.
21 */
22 
23 #include <ql/instruments/overnightindexedswap.hpp>
24 #include <ql/cashflows/overnightindexedcoupon.hpp>
25 #include <ql/cashflows/fixedratecoupon.hpp>
26 
27 namespace QuantLib {
28 
OvernightIndexedSwap(Type type,Real nominal,const Schedule & schedule,Rate fixedRate,const DayCounter & fixedDC,const ext::shared_ptr<OvernightIndex> & overnightIndex,Spread spread,Natural paymentLag,BusinessDayConvention paymentAdjustment,const Calendar & paymentCalendar,bool telescopicValueDates)29     OvernightIndexedSwap::OvernightIndexedSwap(
30         Type type,
31         Real nominal,
32         const Schedule& schedule,
33         Rate fixedRate,
34         const DayCounter& fixedDC,
35         const ext::shared_ptr<OvernightIndex>& overnightIndex,
36         Spread spread,
37         Natural paymentLag,
38         BusinessDayConvention paymentAdjustment,
39         const Calendar& paymentCalendar,
40         bool telescopicValueDates)
41     : Swap(2), type_(type), nominals_(std::vector<Real>(1, nominal)),
42       paymentFrequency_(schedule.tenor().frequency()),
43       paymentCalendar_(paymentCalendar.empty() ? schedule.calendar() : paymentCalendar),
44       paymentAdjustment_(paymentAdjustment), paymentLag_(paymentLag), fixedRate_(fixedRate),
45       fixedDC_(fixedDC), overnightIndex_(overnightIndex), spread_(spread),
46       telescopicValueDates_(telescopicValueDates) {
47 
48         initialize(schedule);
49     }
50 
OvernightIndexedSwap(Type type,const std::vector<Real> & nominals,const Schedule & schedule,Rate fixedRate,const DayCounter & fixedDC,const ext::shared_ptr<OvernightIndex> & overnightIndex,Spread spread,Natural paymentLag,BusinessDayConvention paymentAdjustment,const Calendar & paymentCalendar,bool telescopicValueDates)51     OvernightIndexedSwap::OvernightIndexedSwap(
52         Type type,
53         const std::vector<Real>& nominals,
54         const Schedule& schedule,
55         Rate fixedRate,
56         const DayCounter& fixedDC,
57         const ext::shared_ptr<OvernightIndex>& overnightIndex,
58         Spread spread,
59         Natural paymentLag,
60         BusinessDayConvention paymentAdjustment,
61         const Calendar& paymentCalendar,
62         bool telescopicValueDates)
63     : Swap(2), type_(type), nominals_(nominals), paymentFrequency_(schedule.tenor().frequency()),
64       paymentCalendar_(paymentCalendar.empty() ? schedule.calendar() : paymentCalendar),
65       paymentAdjustment_(paymentAdjustment), paymentLag_(paymentLag), fixedRate_(fixedRate),
66       fixedDC_(fixedDC), overnightIndex_(overnightIndex), spread_(spread),
67       telescopicValueDates_(telescopicValueDates) {
68 
69         initialize(schedule);
70     }
71 
initialize(const Schedule & schedule)72     void OvernightIndexedSwap::initialize(const Schedule& schedule) {
73         if (fixedDC_==DayCounter())
74             fixedDC_ = overnightIndex_->dayCounter();
75         legs_[0] = FixedRateLeg(schedule)
76             .withNotionals(nominals_)
77             .withCouponRates(fixedRate_, fixedDC_)
78             .withPaymentLag(paymentLag_)
79             .withPaymentAdjustment(paymentAdjustment_)
80             .withPaymentCalendar(paymentCalendar_);
81 
82 		legs_[1] = OvernightLeg(schedule, overnightIndex_)
83             .withNotionals(nominals_)
84             .withSpreads(spread_)
85             .withTelescopicValueDates(telescopicValueDates_)
86             .withPaymentLag(paymentLag_)
87             .withPaymentAdjustment(paymentAdjustment_)
88             .withPaymentCalendar(paymentCalendar_);
89 
90         for (Size j=0; j<2; ++j) {
91             for (Leg::iterator i = legs_[j].begin(); i!= legs_[j].end(); ++i)
92                 registerWith(*i);
93         }
94 
95         switch (type_) {
96           case Payer:
97             payer_[0] = -1.0;
98             payer_[1] = +1.0;
99             break;
100           case Receiver:
101             payer_[0] = +1.0;
102             payer_[1] = -1.0;
103             break;
104           default:
105             QL_FAIL("Unknown overnight-swap type");
106         }
107     }
108 
fairRate() const109     Real OvernightIndexedSwap::fairRate() const {
110         static Spread basisPoint = 1.0e-4;
111         calculate();
112         return fixedRate_ - NPV_/(fixedLegBPS()/basisPoint);
113     }
114 
fairSpread() const115     Spread OvernightIndexedSwap::fairSpread() const {
116         static Spread basisPoint = 1.0e-4;
117         calculate();
118         return spread_ - NPV_/(overnightLegBPS()/basisPoint);
119     }
120 
fixedLegBPS() const121     Real OvernightIndexedSwap::fixedLegBPS() const {
122         calculate();
123         QL_REQUIRE(legBPS_[0] != Null<Real>(), "result not available");
124         return legBPS_[0];
125     }
126 
overnightLegBPS() const127     Real OvernightIndexedSwap::overnightLegBPS() const {
128         calculate();
129         QL_REQUIRE(legBPS_[1] != Null<Real>(), "result not available");
130         return legBPS_[1];
131     }
132 
fixedLegNPV() const133     Real OvernightIndexedSwap::fixedLegNPV() const {
134         calculate();
135         QL_REQUIRE(legNPV_[0] != Null<Real>(), "result not available");
136         return legNPV_[0];
137     }
138 
overnightLegNPV() const139     Real OvernightIndexedSwap::overnightLegNPV() const {
140         calculate();
141         QL_REQUIRE(legNPV_[1] != Null<Real>(), "result not available");
142         return legNPV_[1];
143     }
144 
145 }
146