QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
Repo.cpp

This example values a fixed-coupon bond repurchase (repo). The repurchase agreement example is set up to use the repo rate to do all discounting (including the underlying bond income). Forward delivery price is also obtained using this repo rate. All this is done by supplying the FixedCouponBondForward constructor with a flat repo YieldTermStructure.

/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* a Repo calculation done using the FixedRateBondForward class
cf. aaBondFwd() repo example at
http://www.fincad.com/support/developerFunc/mathref/BFWD.htm
This repo is set up to use the repo rate to do all discounting
(including the underlying bond income). Forward delivery price is
also obtained using this repo rate. All this is done by supplying
the FixedRateBondForward constructor with a flat repo
YieldTermStructure.
*/
#include <ql/qldefines.hpp>
#if !defined(BOOST_ALL_NO_LIB) && defined(BOOST_MSVC)
# include <ql/auto_link.hpp>
#endif
#include <ql/instruments/bondforward.hpp>
#include <ql/instruments/bonds/fixedratebond.hpp>
#include <ql/pricingengines/bond/discountingbondengine.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/time/schedule.hpp>
#include <ql/time/calendars/nullcalendar.hpp>
#include <ql/time/daycounters/actual360.hpp>
#include <ql/time/daycounters/thirty360.hpp>
#include <iostream>
#include <iomanip>
using namespace std;
using namespace QuantLib;
int main(int, char* []) {
try {
std::cout << std::endl;
Date repoSettlementDate(14,February,2000);;
Date repoDeliveryDate(15,August,2000);
Rate repoRate = 0.05;
DayCounter repoDayCountConvention = Actual360();
Integer repoSettlementDays = 0;
Compounding repoCompounding = Simple;
Frequency repoCompoundFreq = Annual;
// assume a ten year bond- this is irrelevant
Date bondIssueDate(15,September,1995);
Date bondDatedDate(15,September,1995);
Date bondMaturityDate(15,September,2005);
Real bondCoupon = 0.08;
Frequency bondCouponFrequency = Semiannual;
// unknown what calendar fincad is using
Calendar bondCalendar = NullCalendar();
DayCounter bondDayCountConvention = Thirty360(Thirty360::BondBasis);
// unknown what fincad is using. this may affect accrued calculation
Integer bondSettlementDays = 0;
BusinessDayConvention bondBusinessDayConvention = Unadjusted;
Real bondCleanPrice = 89.97693786;
Real bondRedemption = 100.0;
Real faceAmount = 100.0;
Settings::instance().evaluationDate() = repoSettlementDate;
bondCurve.linkTo(ext::make_shared<FlatForward>(repoSettlementDate,
.01, // dummy rate
bondDayCountConvention,
Compounded,
bondCouponFrequency));
/*
auto bond = ext::make_shared<FixedRateBond>(faceAmount,
bondIssueDate,
bondDatedDate,
bondMaturityDate,
bondSettlementDays,
std::vector<Rate>(1,bondCoupon),
bondCouponFrequency,
bondCalendar,
bondDayCountConvention,
bondBusinessDayConvention,
bondBusinessDayConvention,
bondRedemption,
bondCurve);
*/
Schedule bondSchedule(bondDatedDate, bondMaturityDate,
Period(bondCouponFrequency),
bondCalendar,bondBusinessDayConvention,
bondBusinessDayConvention,
DateGeneration::Backward,false);
auto bond = ext::make_shared<FixedRateBond>(bondSettlementDays,
faceAmount,
bondSchedule,
std::vector<Rate>(1,bondCoupon),
bondDayCountConvention,
bondBusinessDayConvention,
bondRedemption,
bondIssueDate);
bond->setPricingEngine(ext::make_shared<DiscountingBondEngine>(bondCurve));
bondCurve.linkTo(ext::make_shared<FlatForward>(repoSettlementDate,
bond->yield(bondCleanPrice,
bondDayCountConvention,
Compounded,
bondCouponFrequency),
bondDayCountConvention,
Compounded,
bondCouponFrequency));
Position::Type fwdType = Position::Long;
double dummyStrike = 91.5745;
repoCurve.linkTo(ext::make_shared<FlatForward>(repoSettlementDate,
repoRate,
repoDayCountConvention,
repoCompounding,
repoCompoundFreq));
BondForward bondFwd(repoSettlementDate, repoDeliveryDate, fwdType, dummyStrike,
repoSettlementDays, repoDayCountConvention, bondCalendar,
bondBusinessDayConvention, bond, repoCurve, repoCurve);
cout << "Underlying bond clean price: "
<< bond->cleanPrice()
<< endl;
cout << "Underlying bond dirty price: "
<< bond->dirtyPrice()
<< endl;
cout << "Underlying bond accrued at settlement: "
<< bond->accruedAmount(repoSettlementDate)
<< endl;
cout << "Underlying bond accrued at delivery: "
<< bond->accruedAmount(repoDeliveryDate)
<< endl;
cout << "Underlying bond spot income: "
<< bondFwd.spotIncome(repoCurve)
<< endl;
cout << "Underlying bond fwd income: "
<< bondFwd.spotIncome(repoCurve)/
repoCurve->discount(repoDeliveryDate)
<< endl;
cout << "Repo strike: "
<< dummyStrike
<< endl;
cout << "Repo NPV: "
<< bondFwd.NPV()
<< endl;
cout << "Repo clean forward price: "
<< bondFwd.cleanForwardPrice()
<< endl;
cout << "Repo dirty forward price: "
<< bondFwd.forwardPrice()
<< endl;
cout << "Repo implied yield: "
<< bondFwd.impliedYield(bond->dirtyPrice(),
dummyStrike,
repoSettlementDate,
repoCompounding,
repoDayCountConvention)
<< endl;
cout << "Market repo rate: "
<< repoCurve->zeroRate(repoDeliveryDate,
repoDayCountConvention,
repoCompounding,
repoCompoundFreq)
<< endl
<< endl;
cout << "Compare with example given at \n"
<< "http://www.fincad.com/support/developerFunc/mathref/BFWD.htm"
<< endl;
cout << "Clean forward price = 88.2408"
<< endl
<< endl;
cout << "In that example, it is unknown what bond calendar they are\n"
<< "using, as well as settlement Days. For that reason, I have\n"
<< "made the simplest possible assumptions here: NullCalendar\n"
<< "and 0 settlement days."
<< endl;
return 0;
} catch (exception& e) {
cerr << e.what() << endl;
return 1;
} catch (...) {
cerr << "unknown error" << endl;
return 1;
}
}
Actual/360 day count convention.
Definition: actual360.hpp:37
Forward contract on a bond
Definition: bondforward.hpp:69
Real forwardPrice() const
(dirty) forward bond price
Definition: bondforward.cpp:54
Real cleanForwardPrice() const
(dirty) forward bond price minus accrued on bond at delivery
Definition: bondforward.cpp:49
Real spotIncome(const Handle< YieldTermStructure > &incomeDiscountCurve) const override
NPV of bond coupons discounted using incomeDiscountCurve.
Definition: bondforward.cpp:59
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
InterestRate impliedYield(Real underlyingSpotValue, Real forwardValue, Date settlementDate, Compounding compoundingConvention, const DayCounter &dayCounter)
Definition: forward.cpp:68
Real NPV() const
returns the net present value of the instrument.
Definition: instrument.hpp:167
Calendar for reproducing theoretical calculations.
Relinkable handle to an observable.
Definition: handle.hpp:112
void linkTo(const ext::shared_ptr< T > &, bool registerAsObserver=true)
Definition: handle.hpp:187
Payment schedule.
Definition: schedule.hpp:40
30/360 day count convention
Definition: thirty360.hpp:76
Frequency
Frequency of events.
Definition: frequency.hpp:37
BusinessDayConvention
Business Day conventions.
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Real Rate
interest rates
Definition: types.hpp:70
Definition: any.hpp:35
Compounding
Interest rate coumpounding rule.
Definition: compounding.hpp:32
STL namespace.