Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
brlcdiswap.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
21#include <ql/cashflows/overnightindexedcoupon.hpp>
22#include <ql/cashflows/simplecashflow.hpp>
23#include <ql/time/daycounters/business252.hpp>
24#include <ql/time/schedule.hpp>
27
28using namespace QuantLib;
29using std::pow;
30using std::vector;
31
32namespace QuantExt {
33
34BRLCdiSwap::BRLCdiSwap(Type type, Real nominal, const Date& startDate, const Date& endDate, Rate fixedRate,
35 const QuantLib::ext::shared_ptr<BRLCdi>& overnightIndex, Spread spread, bool telescopicValueDates)
36 : OvernightIndexedSwap(type, nominal,
37 Schedule({startDate, endDate}, NullCalendar(),
38 QuantLib::Unadjusted, QuantLib::Unadjusted, 100 * Years),
39 fixedRate, overnightIndex->dayCounter(), overnightIndex, spread, 0, ModifiedFollowing,
40 overnightIndex->fixingCalendar(), telescopicValueDates),
41 startDate_(startDate), endDate_(endDate), index_(overnightIndex) {
42
43 // Need to overwrite the fixed leg with the correct fixed leg for a standard BRL CDI swap
44 // Fixed leg is of the form: N [(1 + k) ^ \delta - 1]
45 // where \delta is the number of BRL business days in the period divided by 252 i.e.
46 // the day count fraction for the period on a Business252 basis.
47 Time dcf = index_->dayCounter().yearFraction(startDate_, endDate_);
48 Real fixedLegPayment = nominal * (pow(1.0 + fixedRate, dcf) - 1.0);
49 Date paymentDate = legs_[0].back()->date();
50 QuantLib::ext::shared_ptr<CashFlow> fixedCashflow = QuantLib::ext::make_shared<SimpleCashFlow>(fixedLegPayment, paymentDate);
51 legs_[0].clear();
52 legs_[0].push_back(fixedCashflow);
53 registerWith(fixedCashflow);
54
55 // Set the pricer on the BRL CDI coupon
56 QL_REQUIRE(legs_[1].size() == 1, "BRLCdiSwap expected exactly one overnight coupon");
57 QuantLib::ext::shared_ptr<QuantLib::OvernightIndexedCoupon> coupon =
58 QuantLib::ext::dynamic_pointer_cast<QuantLib::OvernightIndexedCoupon>(legs_[1][0]);
59 QL_REQUIRE(coupon, "BRLCdiSwap: expected QuantLib::OvernightIndexedCoupon");
60 coupon->setPricer(QuantLib::ext::make_shared<BRLCdiCouponPricer>());
61}
62
64
65 calculate();
66
67 const Spread basisPoint = 1.0e-4;
68 if (!close(endDiscounts_[0], 0.0) && endDiscounts_[0] != Null<DiscountFactor>()) {
69 DiscountFactor df = payer_[0] * endDiscounts_[0];
70 Time dcf = index_->dayCounter().yearFraction(startDate_, endDate_);
71 legBPS_[0] = df * nominal() * (pow(1.0 + fixedRate() + basisPoint, dcf) - pow(1.0 + fixedRate(), dcf));
72 return legBPS_[0];
73 }
74
75 QL_FAIL("BRLCdiSwap cannot calculate fixed leg BPS because end discount is not populated");
76}
77
79
80 calculate();
81
82 if (!close(endDiscounts_[0], 0.0) && endDiscounts_[0] != Null<DiscountFactor>()) {
83 DiscountFactor df = -payer_[0] * endDiscounts_[0];
84 Time dcf = index_->dayCounter().yearFraction(startDate_, endDate_);
85 return pow(overnightLegNPV() / (nominal() * df) + 1.0, 1.0 / dcf) - 1.0;
86 }
87
88 QL_FAIL("BRLCdiSwap cannot calculate fair rate because end discount is not populated");
89}
90
91} // namespace QuantExt
Coupon pricer for a BRL CDI coupon.
Standard BRL CDI swap.
QuantLib::Real fairRate() const
Definition: brlcdiswap.cpp:78
QuantLib::Date endDate_
Definition: brlcdiswap.hpp:47
QuantLib::ext::shared_ptr< QuantLib::OvernightIndex > index_
QuantLib does not implement the method OvernightIndexedSwap::overnightIndex() so I need this.
Definition: brlcdiswap.hpp:49
QuantLib::Real fixedLegBPS() const
Definition: brlcdiswap.cpp:63
QuantLib::Date startDate_
Definition: brlcdiswap.hpp:46
BRLCdiSwap(Type type, QuantLib::Real nominal, const QuantLib::Date &startDate, const QuantLib::Date &endDate, QuantLib::Rate fixedRate, const QuantLib::ext::shared_ptr< BRLCdi > &overnightIndex, QuantLib::Spread spread=0.0, bool telescopicValueDates=false)
Definition: brlcdiswap.cpp:34
Utility functions for setting coupon pricers on legs.
CompiledFormula pow(CompiledFormula x, const CompiledFormula &y)