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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="robots" content="none">
<title>QuantLib: Repo.cpp</title>
<link rel="stylesheet" href="quantlib.css" type="text/css">
<link rel="stylesheet" href="print.css" type="text/css" media="print">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="icon" href="favicon.ico" type="image/x-icon">
</head>
<body>
<div id="container">
<div id="header">
<img class="titleimage"
src="QL-title.jpg" width="212" height="47" border="0"
alt="QuantLib">
<br>
<h3 class="subtitle">A free/open-source library for quantitative finance</h3>
</div>
<div id="menu">
<h3 class="navbartitle">Version 0.9.0</h3>
<hr>
<h3 class="navbartitle">Getting started</h3>
<ul class="navbarlist">
<li class="navlink"><a href="index.html">Introduction</a></li>
<li class="navlink"><a href="overview.html">Project overview</a></li>
<li class="navlink"><a href="where.html">Where to get QuantLib</a></li>
<li class="navlink"><a href="install.html">Installation</a></li>
<li class="navlink"><a href="config.html">Configuration</a></li>
<li class="navlink"><a href="usage.html">Usage</a></li>
<li class="navlink"><a href="history.html">Version history</a></li>
<li class="navlink"><a href="resources.html">Additional resources</a></li>
<li class="navlink"><a href="group.html">The QuantLib group</a></li>
<li class="navlink"><a href="license.html">Copyright and license</a></li>
</ul>
<hr>
<h3 class="navbartitle">Reference manual</h3>
<ul class="navbarlist">
<li class="navlink"><a href="modules.html">Modules</a></li>
<li class="navlink"><a href="hierarchy.html">Class Hierarchy</a></li>
<li class="navlink"><a href="annotated.html">Compound List</a></li>
<li class="navlink"><a href="files.html">File List</a></li>
<li class="navlink"><a href="functions.html">Compound Members</a></li>
<li class="navlink"><a href="globals.html">File Members</a></li>
<li class="navlink"><a href="bug.html">Known Bugs</a></li>
<li class="navlink"><a href="caveats.html">Caveats</a></li>
<li class="navlink"><a href="test.html">Test Suite</a></li>
<li class="navlink"><a href="examples.html">Examples</a></li>
</ul>
</div>
<div id="content">
<!--Doxygen-generated content-->
<!-- Generated by Doxygen 1.5.4 -->
<h1>Repo.cpp</h1>This example evaluates a repo on a fixed-coupon bond.<p>
<div class="fragment"><pre class="fragment"><span class="comment">/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */</span>
<span class="comment">/* a Repo calculation done using the FixedRateBondForward class</span>
<span class="comment"> cf. aaBondFwd() repo example at</span>
<span class="comment"> http://www.fincad.com/support/developerFunc/mathref/BFWD.htm</span>
<span class="comment"></span>
<span class="comment"> This repo is set up to use the repo rate to do all discounting</span>
<span class="comment"> (including the underlying bond income). Forward delivery price is</span>
<span class="comment"> also obtained using this repo rate. All this is done by supplying</span>
<span class="comment"> the FixedRateBondForward constructor with a flat repo</span>
<span class="comment"> YieldTermStructure.</span>
<span class="comment">*/</span>
<span class="comment">// the only header you need to use QuantLib</span>
<span class="preprocessor">#include <ql/quantlib.hpp></span>
<span class="preprocessor">#ifdef BOOST_MSVC</span>
<span class="preprocessor"></span><span class="comment">/* Uncomment the following lines to unmask floating-point</span>
<span class="comment"> exceptions. Warning: unpredictable results can arise...</span>
<span class="comment"></span>
<span class="comment"> See http://www.wilmott.com/messageview.cfm?catid=10&threadid=9481</span>
<span class="comment"> Is there anyone with a definitive word about this?</span>
<span class="comment">*/</span>
<span class="comment">// #include <float.h></span>
<span class="comment">// namespace { unsigned int u = _controlfp(_EM_INEXACT, _MCW_EM); }</span>
<span class="preprocessor">#endif</span>
<span class="preprocessor"></span>
<span class="preprocessor">#include <boost/timer.hpp></span>
<span class="preprocessor">#include <iostream></span>
<span class="keyword">using namespace </span>std;
<span class="keyword">using namespace </span>QuantLib;
<span class="preprocessor">#if defined(QL_ENABLE_SESSIONS)</span>
<span class="preprocessor"></span><span class="keyword">namespace </span>QuantLib {
<a name="a0"></a><a class="code" href="group__types.html#gb9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> sessionId() { <span class="keywordflow">return</span> 0; }
}
<span class="preprocessor">#endif</span>
<span class="preprocessor"></span>
<span class="keywordtype">int</span> main(<span class="keywordtype">int</span>, <span class="keywordtype">char</span>* []) {
<span class="keywordflow">try</span> {
boost::timer timer;
std::cout << std::endl;
<a name="_a1"></a><a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> repoSettlementDate(14,February,2000);;
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> repoDeliveryDate(15,August,2000);
<a name="a2"></a><a class="code" href="group__types.html#gede435af51236692b1107d7639581d39" title="interest rates">Rate</a> repoRate = 0.05;
<a name="_a3"></a><a class="code" href="class_quant_lib_1_1_day_counter.html" title="day counter class">DayCounter</a> repoDayCountConvention = <a name="_a4"></a><a class="code" href="class_quant_lib_1_1_actual360.html" title="Actual/360 day count convention.">Actual360</a>();
<a class="code" href="group__types.html#gb9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> repoSettlementDays = 0;
<a class="code" href="namespace_quant_lib.html#2779d04b4839fd386b5c85bbb96aaf73" title="Interest rate coumpounding rule.">Compounding</a> repoCompounding = <a name="a5"></a><a class="code" href="namespace_quant_lib.html#2779d04b4839fd386b5c85bbb96aaf73ebfbf7dc5cde0772efb1aa49712bd76b">Simple</a>;
<a class="code" href="group__datetime.html#g6d41db8ba0ea90d22df35889df452ada" title="Frequency of events.">Frequency</a> repoCompoundFreq = <a name="a6"></a><a class="code" href="group__datetime.html#gg6d41db8ba0ea90d22df35889df452ada508b18014c96e2d466c12b39d7f4e426" title="once a year">Annual</a>;
<span class="comment">// assume a ten year bond- this is irrelevant</span>
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> bondIssueDate(15,September,1995);
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> bondDatedDate(15,September,1995);
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> bondMaturityDate(15,September,2005);
<a name="a7"></a><a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> bondCoupon = 0.08;
<a class="code" href="group__datetime.html#g6d41db8ba0ea90d22df35889df452ada" title="Frequency of events.">Frequency</a> bondCouponFrequency = <a name="a8"></a><a class="code" href="group__datetime.html#gg6d41db8ba0ea90d22df35889df452adad1a5868a1c314bb7f6ab68e2fa182b2d" title="twice a year">Semiannual</a>;
<span class="comment">// unknown what calendar fincad is using</span>
<a name="_a9"></a><a class="code" href="class_quant_lib_1_1_calendar.html" title="calendar class">Calendar</a> bondCalendar = <a name="_a10"></a><a class="code" href="class_quant_lib_1_1_null_calendar.html" title="Calendar for reproducing theoretical calculations.">NullCalendar</a>();
<a class="code" href="class_quant_lib_1_1_day_counter.html" title="day counter class">DayCounter</a> bondDayCountConvention = <a name="_a11"></a><a class="code" href="class_quant_lib_1_1_thirty360.html" title="30/360 day count convention">Thirty360</a>(Thirty360::BondBasis);
<span class="comment">// unknown what fincad is using. this may affect accrued calculation</span>
<a class="code" href="group__types.html#gb9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> bondSettlementDays = 0;
<a class="code" href="group__datetime.html#gff46c5ae9385d20709bedade86edd368" title="Business Day conventions.">BusinessDayConvention</a> bondBusinessDayConvention = <a name="a12"></a><a class="code" href="group__datetime.html#ggff46c5ae9385d20709bedade86edd3685fb8b90b7fdf1a3a228fbbc083e64547">Unadjusted</a>;
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> bondCleanPrice = 89.97693786;
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> bondRedemption = 100.0;
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> faceAmount = 100.0;
Settings::instance().evaluationDate() = repoSettlementDate;
<a name="_a13"></a><a class="code" href="class_quant_lib_1_1_relinkable_handle.html" title="Relinkable handle to an observable.">RelinkableHandle<YieldTermStructure></a> bondCurve;
bondCurve.<a name="a14"></a><a class="code" href="class_quant_lib_1_1_relinkable_handle.html#7eec88ccb240dcc59ac81e7347c335bd">linkTo</a>(boost::shared_ptr<YieldTermStructure>(
<span class="keyword">new</span> <a name="_a15"></a><a class="code" href="class_quant_lib_1_1_flat_forward.html" title="Flat interest-rate curve.">FlatForward</a>(repoSettlementDate,
.01, <span class="comment">// dummy rate</span>
bondDayCountConvention,
<a name="a16"></a><a class="code" href="namespace_quant_lib.html#2779d04b4839fd386b5c85bbb96aaf737f3e11143ca01a7d20f92e11ad445e0c">Compounded</a>,
bondCouponFrequency)));
<span class="comment">/*</span>
<span class="comment"> boost::shared_ptr<FixedRateBond> bond(</span>
<span class="comment"> new FixedRateBond(faceAmount,</span>
<span class="comment"> bondIssueDate,</span>
<span class="comment"> bondDatedDate,</span>
<span class="comment"> bondMaturityDate,</span>
<span class="comment"> bondSettlementDays,</span>
<span class="comment"> std::vector<Rate>(1,bondCoupon),</span>
<span class="comment"> bondCouponFrequency,</span>
<span class="comment"> bondCalendar,</span>
<span class="comment"> bondDayCountConvention,</span>
<span class="comment"> bondBusinessDayConvention,</span>
<span class="comment"> bondBusinessDayConvention,</span>
<span class="comment"> bondRedemption,</span>
<span class="comment"> bondCurve));</span>
<span class="comment"> */</span>
<a name="_a17"></a><a class="code" href="class_quant_lib_1_1_schedule.html" title="Payment schedule.">Schedule</a> bondSchedule(bondDatedDate, bondMaturityDate,
<a name="_a18"></a><a class="code" href="class_quant_lib_1_1_period.html">Period</a>(bondCouponFrequency),
bondCalendar,bondBusinessDayConvention,
bondBusinessDayConvention,
DateGeneration::Backward,<span class="keyword">false</span>);
boost::shared_ptr<FixedRateBond> bond(
<span class="keyword">new</span> <a name="_a19"></a><a class="code" href="class_quant_lib_1_1_fixed_rate_bond.html" title="fixed-rate bond">FixedRateBond</a>(bondSettlementDays,
faceAmount,
bondSchedule,
std::vector<Rate>(1,bondCoupon),
bondDayCountConvention,
bondBusinessDayConvention,
bondRedemption,
bondIssueDate));
bond->setPricingEngine(boost::shared_ptr<PricingEngine>(
<span class="keyword">new</span> DiscountingBondEngine(bondCurve)));
bondCurve.<a class="code" href="class_quant_lib_1_1_relinkable_handle.html#7eec88ccb240dcc59ac81e7347c335bd">linkTo</a>(boost::shared_ptr<YieldTermStructure> (
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_flat_forward.html" title="Flat interest-rate curve.">FlatForward</a>(repoSettlementDate,
bond->yield(bondCleanPrice,
bondDayCountConvention,
<a class="code" href="namespace_quant_lib.html#2779d04b4839fd386b5c85bbb96aaf737f3e11143ca01a7d20f92e11ad445e0c">Compounded</a>,
bondCouponFrequency),
bondDayCountConvention,
<a class="code" href="namespace_quant_lib.html#2779d04b4839fd386b5c85bbb96aaf737f3e11143ca01a7d20f92e11ad445e0c">Compounded</a>,
bondCouponFrequency)));
Position::Type fwdType = Position::Long;
<span class="keywordtype">double</span> dummyStrike = 91.5745;
<a class="code" href="class_quant_lib_1_1_relinkable_handle.html" title="Relinkable handle to an observable.">RelinkableHandle<YieldTermStructure></a> repoCurve;
repoCurve.<a class="code" href="class_quant_lib_1_1_relinkable_handle.html#7eec88ccb240dcc59ac81e7347c335bd">linkTo</a>(boost::shared_ptr<YieldTermStructure> (
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_flat_forward.html" title="Flat interest-rate curve.">FlatForward</a>(repoSettlementDate,
repoRate,
repoDayCountConvention,
repoCompounding,
repoCompoundFreq)));
<a name="_a20"></a><a class="code" href="class_quant_lib_1_1_fixed_rate_bond_forward.html" title="Forward contract on a fixed-rate bond">FixedRateBondForward</a> bondFwd(repoSettlementDate,
repoDeliveryDate,
fwdType,
dummyStrike,
repoSettlementDays,
repoDayCountConvention,
bondCalendar,
bondBusinessDayConvention,
bond,
repoCurve,
repoCurve);
cout << <span class="stringliteral">"Underlying bond clean price: "</span>
<< bond->cleanPrice()
<< endl;
cout << <span class="stringliteral">"Underlying bond dirty price: "</span>
<< bond->dirtyPrice()
<< endl;
cout << <span class="stringliteral">"Underlying bond accrued at settlement: "</span>
<< bond->accruedAmount(repoSettlementDate)
<< endl;
cout << <span class="stringliteral">"Underlying bond accrued at delivery: "</span>
<< bond->accruedAmount(repoDeliveryDate)
<< endl;
cout << <span class="stringliteral">"Underlying bond spot income: "</span>
<< bondFwd.spotIncome(repoCurve)
<< endl;
cout << <span class="stringliteral">"Underlying bond fwd income: "</span>
<< bondFwd.spotIncome(repoCurve)/
repoCurve->discount(repoDeliveryDate)
<< endl;
cout << <span class="stringliteral">"Repo strike: "</span>
<< dummyStrike
<< endl;
cout << <span class="stringliteral">"Repo NPV: "</span>
<< bondFwd.NPV()
<< endl;
cout << <span class="stringliteral">"Repo clean forward price: "</span>
<< bondFwd.cleanForwardPrice()
<< endl;
cout << <span class="stringliteral">"Repo dirty forward price: "</span>
<< bondFwd.forwardPrice()
<< endl;
cout << <span class="stringliteral">"Repo implied yield: "</span>
<< bondFwd.impliedYield(bond->dirtyPrice(),
dummyStrike,
repoSettlementDate,
repoCompounding,
repoDayCountConvention)
<< endl;
cout << <span class="stringliteral">"Market repo rate: "</span>
<< repoCurve->zeroRate(repoDeliveryDate,
repoDayCountConvention,
repoCompounding,
repoCompoundFreq)
<< endl
<< endl;
cout << <span class="stringliteral">"Compare with example given at \n"</span>
<< <span class="stringliteral">"http://www.fincad.com/support/developerFunc/mathref/BFWD.htm"</span>
<< endl;
cout << <span class="stringliteral">"Clean forward price = 88.2408"</span>
<< endl
<< endl;
cout << <span class="stringliteral">"In that example, it is unknown what bond calendar they are\n"</span>
<< <span class="stringliteral">"using, as well as settlement Days. For that reason, I have\n"</span>
<< <span class="stringliteral">"made the simplest possible assumptions here: NullCalendar\n"</span>
<< <span class="stringliteral">"and 0 settlement days."</span>
<< endl;
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> seconds = timer.elapsed();
<a class="code" href="group__types.html#gb9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> hours = int(seconds/3600);
seconds -= hours * 3600;
<a class="code" href="group__types.html#gb9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> minutes = int(seconds/60);
seconds -= minutes * 60;
cout << <span class="stringliteral">" \nRun completed in "</span>;
<span class="keywordflow">if</span> (hours > 0)
cout << hours << <span class="stringliteral">" h "</span>;
<span class="keywordflow">if</span> (hours > 0 || minutes > 0)
cout << minutes << <span class="stringliteral">" m "</span>;
cout << fixed << setprecision(0)
<< seconds << <span class="stringliteral">" s\n"</span> << endl;
<span class="keywordflow">return</span> 0;
} <span class="keywordflow">catch</span> (exception& e) {
cout << e.what() << endl;
<span class="keywordflow">return</span> 1;
} <span class="keywordflow">catch</span> (...) {
cout << <span class="stringliteral">"unknown error"</span> << endl;
<span class="keywordflow">return</span> 1;
}
}
</pre></div>
</div>
<div class="footer">
<div class="endmatter">
Documentation generated by
<a href="http://www.doxygen.org">Doxygen</a> 1.5.4
</div>
</div>
</div>
</body>
</html>
|