| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2018 Tom Anderson |
| 5 | |
| 6 | This file is part of QuantLib, a free-software/open-source library |
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 8 | |
| 9 | QuantLib is free software: you can redistribute it and/or modify it |
| 10 | under the terms of the QuantLib license. You should have received a |
| 11 | copy of the license along with this program; if not, please email |
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 13 | <http://quantlib.org/license.shtml>. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 18 | */ |
| 19 | |
| 20 | #include "forwardrateagreement.hpp" |
| 21 | #include "utilities.hpp" |
| 22 | #include <ql/handle.hpp> |
| 23 | #include <ql/indexes/ibor/usdlibor.hpp> |
| 24 | #include <ql/instruments/forwardrateagreement.hpp> |
| 25 | #include <ql/quotes/simplequote.hpp> |
| 26 | #include <ql/termstructures/yield/piecewiseyieldcurve.hpp> |
| 27 | #include <ql/termstructures/yield/ratehelpers.hpp> |
| 28 | |
| 29 | |
| 30 | using namespace QuantLib; |
| 31 | using namespace boost::unit_test_framework; |
| 32 | |
| 33 | void ForwardRateAgreementTest::testConstructionWithoutACurve() { |
| 34 | BOOST_TEST_MESSAGE("Testing forward rate agreement construction..." ); |
| 35 | |
| 36 | Date today = QuantLib::Settings::instance().evaluationDate(); |
| 37 | |
| 38 | // set up the index |
| 39 | RelinkableHandle<YieldTermStructure> curveHandle; |
| 40 | ext::shared_ptr<IborIndex> index = ext::make_shared<USDLibor>(args: Period(3, Months), args&: curveHandle); |
| 41 | |
| 42 | // determine the settlement date for a FRA |
| 43 | Date settlementDate = index->fixingCalendar().advance(date: today, period: index->fixingDays() * Days); |
| 44 | |
| 45 | // set up quotes with no values |
| 46 | std::vector<ext::shared_ptr<SimpleQuote> > quotes = { |
| 47 | ext::make_shared<SimpleQuote>(), |
| 48 | ext::make_shared<SimpleQuote>(), |
| 49 | ext::make_shared<SimpleQuote>() |
| 50 | }; |
| 51 | |
| 52 | #ifdef QL_USE_INDEXED_COUPON |
| 53 | bool useIndexedFra = false; |
| 54 | #else |
| 55 | bool useIndexedFra = true; |
| 56 | #endif |
| 57 | |
| 58 | // set up the curve (this bit is a very rough sketch - i'm actually using swaps !) |
| 59 | std::vector<ext::shared_ptr<RateHelper> > helpers; |
| 60 | helpers.push_back(x: ext::make_shared<FraRateHelper>(args: Handle<Quote>(quotes[0]), |
| 61 | args: Period(1, Years), args&: index, |
| 62 | args: Pillar::LastRelevantDate, args: Date(), |
| 63 | args&: useIndexedFra)); |
| 64 | helpers.push_back(x: ext::make_shared<FraRateHelper>(args: Handle<Quote>(quotes[1]), |
| 65 | args: Period(2, Years), args&: index, |
| 66 | args: Pillar::LastRelevantDate, args: Date(), |
| 67 | args&: useIndexedFra)); |
| 68 | helpers.push_back(x: ext::make_shared<FraRateHelper>(args: Handle<Quote>(quotes[2]), |
| 69 | args: Period(3, Years), args&: index, |
| 70 | args: Pillar::LastRelevantDate, args: Date(), |
| 71 | args&: useIndexedFra)); |
| 72 | ext::shared_ptr<PiecewiseYieldCurve<ForwardRate, QuantLib::Cubic> > curve = |
| 73 | ext::make_shared<PiecewiseYieldCurve<ForwardRate, QuantLib::Cubic> >( |
| 74 | args&: today, args&: helpers, args: index->dayCounter()); |
| 75 | |
| 76 | curveHandle.linkTo(h: curve); |
| 77 | |
| 78 | // set up the instrument to price |
| 79 | // check the constructor without maturity date |
| 80 | // inferring maturity date from the index |
| 81 | ForwardRateAgreement fra(index, |
| 82 | settlementDate + Period(12, Months), |
| 83 | Position::Long, |
| 84 | 0, |
| 85 | 1, |
| 86 | curveHandle); |
| 87 | |
| 88 | // finally put values in the quotes |
| 89 | quotes[0]->setValue(0.01); |
| 90 | quotes[1]->setValue(0.02); |
| 91 | quotes[2]->setValue(0.03); |
| 92 | |
| 93 | Real rate = fra.forwardRate(); |
| 94 | if (std::fabs(x: rate - 0.01) > 1e-6) { |
| 95 | BOOST_ERROR("grid creation failed for FRA without maturityDate, got rate " << rate << " expected " << 0.01); |
| 96 | } |
| 97 | |
| 98 | // check the constructor with explicit maturity date |
| 99 | ForwardRateAgreement fra2(index, |
| 100 | settlementDate + Period(12, Months), |
| 101 | settlementDate + Period(15, Months), |
| 102 | Position::Long, |
| 103 | 0, |
| 104 | 1, |
| 105 | curveHandle); |
| 106 | |
| 107 | Real rate2 = fra2.forwardRate(); |
| 108 | if (std::fabs(x: rate2 - 0.01) > 1e-6) { |
| 109 | BOOST_ERROR("grid creation failed for FRA with maturityDate, got rate " << rate << " expected " << 0.01); |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | |
| 114 | test_suite* ForwardRateAgreementTest::suite() { |
| 115 | auto* suite = BOOST_TEST_SUITE("forward rate agreement" ); |
| 116 | suite->add(QUANTLIB_TEST_CASE(&ForwardRateAgreementTest::testConstructionWithoutACurve)); |
| 117 | return suite; |
| 118 | } |
| 119 | |