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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2006, 2011 Ferdinando Ametrano
Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
Copyright (C) 2003, 2004, 2005, 2007, 2008 StatPro Italia srl
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
<https://www.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 <ql/instruments/swap.hpp>
#include <ql/cashflows/cashflows.hpp>
#include <ql/cashflows/floatingratecoupon.hpp>
#include <ql/termstructures/yieldtermstructure.hpp>
#include <ostream>
namespace QuantLib {
Swap::Swap(const Leg& firstLeg,
const Leg& secondLeg)
: legs_(2), payer_(2),
legNPV_(2, 0.0), legBPS_(2, 0.0),
startDiscounts_(2, 0.0), endDiscounts_(2, 0.0),
npvDateDiscount_(0.0) {
legs_[0] = firstLeg;
legs_[1] = secondLeg;
payer_[0] = -1.0;
payer_[1] = 1.0;
for (auto& i : legs_[0])
registerWith(i);
for (auto& i : legs_[1])
registerWith(i);
}
Swap::Swap(const std::vector<Leg>& legs,
const std::vector<bool>& payer)
: legs_(legs), payer_(legs.size(), 1.0),
legNPV_(legs.size(), 0.0), legBPS_(legs.size(), 0.0),
startDiscounts_(legs.size(), 0.0), endDiscounts_(legs.size(), 0.0),
npvDateDiscount_(0.0) {
QL_REQUIRE(payer.size()==legs_.size(),
"size mismatch between payer (" << payer.size() <<
") and legs (" << legs_.size() << ")");
for (Size j=0; j<legs_.size(); ++j) {
if (payer[j]) payer_[j]=-1.0;
for (auto& i : legs_[j])
registerWith(i);
}
}
Swap::Swap(Size legs)
: legs_(legs), payer_(legs),
legNPV_(legs, 0.0), legBPS_(legs, 0.0),
startDiscounts_(legs, 0.0), endDiscounts_(legs, 0.0),
npvDateDiscount_(0.0) {}
bool Swap::isExpired() const {
for (const auto& leg : legs_) {
Leg::const_iterator i;
for (i = leg.begin(); i != leg.end(); ++i)
if (!(*i)->hasOccurred())
return false;
}
return true;
}
void Swap::setupExpired() const {
Instrument::setupExpired();
std::fill(legBPS_.begin(), legBPS_.end(), 0.0);
std::fill(legNPV_.begin(), legNPV_.end(), 0.0);
std::fill(startDiscounts_.begin(), startDiscounts_.end(), 0.0);
std::fill(endDiscounts_.begin(), endDiscounts_.end(), 0.0);
npvDateDiscount_ = 0.0;
}
void Swap::setupArguments(PricingEngine::arguments* args) const {
auto* arguments = dynamic_cast<Swap::arguments*>(args);
QL_REQUIRE(arguments != nullptr, "wrong argument type");
arguments->legs = legs_;
arguments->payer = payer_;
}
void Swap::fetchResults(const PricingEngine::results* r) const {
Instrument::fetchResults(r);
const auto* results = dynamic_cast<const Swap::results*>(r);
QL_REQUIRE(results != nullptr, "wrong result type");
if (!results->legNPV.empty()) {
QL_REQUIRE(results->legNPV.size() == legNPV_.size(),
"wrong number of leg NPV returned");
legNPV_ = results->legNPV;
} else {
std::fill(legNPV_.begin(), legNPV_.end(), Null<Real>());
}
if (!results->legBPS.empty()) {
QL_REQUIRE(results->legBPS.size() == legBPS_.size(),
"wrong number of leg BPS returned");
legBPS_ = results->legBPS;
} else {
std::fill(legBPS_.begin(), legBPS_.end(), Null<Real>());
}
if (!results->startDiscounts.empty()) {
QL_REQUIRE(results->startDiscounts.size() == startDiscounts_.size(),
"wrong number of leg start discounts returned");
startDiscounts_ = results->startDiscounts;
} else {
std::fill(startDiscounts_.begin(), startDiscounts_.end(),
Null<DiscountFactor>());
}
if (!results->endDiscounts.empty()) {
QL_REQUIRE(results->endDiscounts.size() == endDiscounts_.size(),
"wrong number of leg end discounts returned");
endDiscounts_ = results->endDiscounts;
} else {
std::fill(endDiscounts_.begin(), endDiscounts_.end(),
Null<DiscountFactor>());
}
if (results->npvDateDiscount != Null<DiscountFactor>()) {
npvDateDiscount_ = results->npvDateDiscount;
} else {
npvDateDiscount_ = Null<DiscountFactor>();
}
}
Size Swap::numberOfLegs() const { return legs_.size(); }
const std::vector<Leg>& Swap::legs() const { return legs_; }
Date Swap::startDate() const {
QL_REQUIRE(!legs_.empty(), "no legs given");
Date d = CashFlows::startDate(legs_[0]);
for (Size j=1; j<legs_.size(); ++j)
d = std::min(d, CashFlows::startDate(legs_[j]));
return d;
}
Date Swap::maturityDate() const {
QL_REQUIRE(!legs_.empty(), "no legs given");
Date d = CashFlows::maturityDate(legs_[0]);
for (Size j=1; j<legs_.size(); ++j)
d = std::max(d, CashFlows::maturityDate(legs_[j]));
return d;
}
void Swap::deepUpdate() {
for (auto& leg : legs_) {
for (auto& k : leg) {
k->deepUpdate();
}
}
update();
}
void Swap::arguments::validate() const {
QL_REQUIRE(legs.size() == payer.size(),
"number of legs and multipliers differ");
}
void Swap::results::reset() {
Instrument::results::reset();
legNPV.clear();
legBPS.clear();
startDiscounts.clear();
endDiscounts.clear();
npvDateDiscount = Null<DiscountFactor>();
}
std::ostream& operator<<(std::ostream& out, Swap::Type t) {
switch (t) {
case Swap::Payer:
return out << "Payer";
case Swap::Receiver:
return out << "Receiver";
default:
QL_FAIL("unknown Swap::Type(" << Integer(t) << ")");
}
}
}
|