Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Functions
zerocouponswap.cpp File Reference
#include <boost/make_shared.hpp>
#include <boost/test/unit_test.hpp>
#include <ored/marketdata/marketimpl.hpp>
#include <ored/portfolio/builders/swap.hpp>
#include <ored/portfolio/enginedata.hpp>
#include <ored/portfolio/portfolio.hpp>
#include <ored/portfolio/swap.hpp>
#include <ored/utilities/indexparser.hpp>
#include <ored/utilities/log.hpp>
#include <oret/toplevelfixture.hpp>
#include <ql/cashflows/iborcoupon.hpp>
#include <ql/indexes/inflation/ukrpi.hpp>
#include <ql/termstructures/yield/discountcurve.hpp>
#include <ql/time/calendars/unitedkingdom.hpp>
#include <ql/time/daycounters/actual365fixed.hpp>
#include <ql/time/daycounters/actualactual.hpp>
#include <qle/time/yearcounter.hpp>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (testZeroCouponSwapPrice)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE()

BOOST_AUTO_TEST_CASE ( testZeroCouponSwapPrice  )

Definition at line 114 of file zerocouponswap.cpp.

114 {
115
116 BOOST_TEST_MESSAGE("Testing Zero Coupon Swap Price...");
117
118 // build market
119 Date today(28, August, 2018);
120 Settings::instance().evaluationDate() = today;
121 QuantLib::ext::shared_ptr<TestMarket> market = QuantLib::ext::make_shared<TestMarket>();
122 Date marketDate = market->asofDate();
123 BOOST_CHECK_EQUAL(today, marketDate);
124 Settings::instance().evaluationDate() = marketDate;
125
126 // Test if GBP discount curve is empty
127 Handle<YieldTermStructure> dts = market->discountCurve("GBP");
128 QL_REQUIRE(!dts.empty(), "GBP discount curve not found");
129 BOOST_CHECK_CLOSE(market->discountCurve("GBP")->discount(today + 1 * Years), 0.9914, 0.0001);
130
131 // Test if GBP Libor curve is empty
132 Handle<IborIndex> iis = market->iborIndex("GBP-LIBOR-6M");
133 QL_REQUIRE(!iis.empty(), "GBP LIBOR 6M ibor Index not found");
134 BOOST_TEST_MESSAGE(
135 "CPISwap: Projected Libor fixing: " << market->iborIndex("GBP-LIBOR-6M")->forecastFixing(today + 1 * Years));
136
137 // envelope
138 Envelope env("CP");
139
140 // Start/End date
141 Size years = 5;
142 Date startDate = today;
143 Date endDate = today + years * Years;
144
145 // date 2 string
146 std::ostringstream oss;
147 oss << io::iso_date(startDate);
148 string start(oss.str());
149 oss.str("");
150 oss.clear();
151 oss << io::iso_date(endDate);
152 string end(oss.str());
153
154 // Leg variables
155 vector<Real> notional(1, 1000000);
156 string paymentConvention = "MF";
157 Real rate = 0.02;
158 vector<Real> rates;
159 rates.push_back(rate);
160
161 // Schedule
162 string conv = "MF";
163 string rule = "Zero";
164 ScheduleData scheduleData(ScheduleRules(start, end, "5y", "UK", conv, conv, rule));
165 Schedule schedule = makeSchedule(scheduleData);
166 BOOST_CHECK_EQUAL(schedule.dates().size(), 2);
167
168 // GBP Libor Leg
169 bool isPayerLibor = true;
170 string indexLibor = "GBP-LIBOR-6M";
171 LegData leg(QuantLib::ext::make_shared<ZeroCouponFixedLegData>(rates), isPayerLibor, "GBP", scheduleData, "Year", notional,
172 vector<string>(), paymentConvention);
173
174 // Build swap trades
175 vector<LegData> legData;
176 legData.push_back(leg);
177 QuantLib::ext::shared_ptr<Trade> swap(new ore::data::Swap(env, legData));
178
179 // engine data and factory
180 QuantLib::ext::shared_ptr<EngineData> engineData = QuantLib::ext::make_shared<EngineData>();
181 engineData->model("Swap") = "DiscountedCashflows";
182 engineData->engine("Swap") = "DiscountingSwapEngine";
183 QuantLib::ext::shared_ptr<EngineFactory> engineFactory = QuantLib::ext::make_shared<EngineFactory>(engineData, market);
184
185 // build swaps and portfolio
186 QuantLib::ext::shared_ptr<Portfolio> portfolio(new Portfolio());
187 swap->id() = "Swap";
188
189 portfolio->add(swap);
190 portfolio->build(engineFactory);
191
192 Date maturity = schedule.endDate();
193 Date fixedPayDate = schedule.calendar().adjust(maturity, parseBusinessDayConvention(paymentConvention));
194 Real df = market->discountCurve("GBP")->discount(fixedPayDate);
195 Real expectedNPV = -1 * notional.back() * (pow(1.0 + rate, years) - 1.0) * df;
196 BOOST_CHECK_CLOSE(swap->instrument()->NPV(), expectedNPV, 1E-8); // this is 1E-10 rel diff
197}
Serializable object holding generic trade data, reporting dimensions.
Definition: envelope.hpp:51
Serializable object holding leg data.
Definition: legdata.hpp:844
Serializable portfolio.
Definition: portfolio.hpp:43
Serializable schedule data.
Definition: schedule.hpp:202
Serializable object holding schedule Rules data.
Definition: schedule.hpp:37
Serializable Swap, Single and Cross Currency.
Definition: swap.hpp:36
BusinessDayConvention parseBusinessDayConvention(const string &s)
Convert text to QuantLib::BusinessDayConvention.
Definition: parsers.cpp:173
Time maturity
Definition: utilities.cpp:66
RandomVariable pow(RandomVariable x, const RandomVariable &y)
Schedule makeSchedule(const ScheduleDates &data)
Definition: schedule.cpp:263
+ Here is the call graph for this function: