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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.20"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>QuantLib: Repo.cpp</title>
<link href='https://fonts.googleapis.com/css?family=Merriweather+Sans:800' rel='stylesheet' type='text/css'>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="quantlibextra.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname"><a href="http://quantlib.org">
<img alt="QuantLib" src="QL-title.jpg"></a>
<div id="projectbrief">A free/open-source library for quantitative finance</div>
<div id="projectnumber">Reference manual - version 1.20</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.20 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="headertitle">
<div class="title">Repo.cpp</div> </div>
</div><!--header-->
<div class="contents">
<p>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.</p>
<div class="fragment"><div class="line"><span class="comment">/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* a Repo calculation done using the FixedRateBondForward class</span></div>
<div class="line"><span class="comment"> cf. aaBondFwd() repo example at</span></div>
<div class="line"><span class="comment"> http://www.fincad.com/support/developerFunc/mathref/BFWD.htm</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment"> This repo is set up to use the repo rate to do all discounting</span></div>
<div class="line"><span class="comment"> (including the underlying bond income). Forward delivery price is</span></div>
<div class="line"><span class="comment"> also obtained using this repo rate. All this is done by supplying</span></div>
<div class="line"><span class="comment"> the FixedRateBondForward constructor with a flat repo</span></div>
<div class="line"><span class="comment"> YieldTermStructure.</span></div>
<div class="line"><span class="comment">*/</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include <ql/qldefines.hpp></span></div>
<div class="line"><span class="preprocessor">#ifdef BOOST_MSVC</span></div>
<div class="line"><span class="preprocessor"># include <ql/auto_link.hpp></span></div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"><span class="preprocessor">#include <ql/instruments/fixedratebondforward.hpp></span></div>
<div class="line"><span class="preprocessor">#include <ql/pricingengines/bond/discountingbondengine.hpp></span></div>
<div class="line"><span class="preprocessor">#include <ql/termstructures/yield/flatforward.hpp></span></div>
<div class="line"><span class="preprocessor">#include <ql/time/schedule.hpp></span></div>
<div class="line"><span class="preprocessor">#include <ql/time/calendars/nullcalendar.hpp></span></div>
<div class="line"><span class="preprocessor">#include <ql/time/daycounters/actual360.hpp></span></div>
<div class="line"><span class="preprocessor">#include <ql/time/daycounters/thirty360.hpp></span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include <iostream></span></div>
<div class="line"><span class="preprocessor">#include <iomanip></span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">using namespace </span>std;</div>
<div class="line"><span class="keyword">using namespace </span><a class="code" href="namespace_quant_lib.html">QuantLib</a>;</div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#if defined(QL_ENABLE_SESSIONS)</span></div>
<div class="line"><span class="keyword">namespace </span><a class="code" href="namespace_quant_lib.html">QuantLib</a> {</div>
<div class="line"> </div>
<div class="line"> ThreadKey sessionId() { <span class="keywordflow">return</span> 0; }</div>
<div class="line"> </div>
<div class="line">}</div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span>, <span class="keywordtype">char</span>* []) {</div>
<div class="line"> </div>
<div class="line"> <span class="keywordflow">try</span> {</div>
<div class="line"> </div>
<div class="line"> std::cout << std::endl;</div>
<div class="line"> </div>
<div class="line"> <a name="_a0"></a><a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> repoSettlementDate(14,February,2000);;</div>
<div class="line"> <a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> repoDeliveryDate(15,August,2000);</div>
<div class="line"> <a name="a1"></a><a class="code" href="group__types.html#gaede435af51236692b1107d7639581d39" title="interest rates">Rate</a> repoRate = 0.05;</div>
<div class="line"> <a name="_a2"></a><a class="code" href="class_quant_lib_1_1_day_counter.html" title="day counter class">DayCounter</a> repoDayCountConvention = <a name="_a3"></a><a class="code" href="class_quant_lib_1_1_actual360.html" title="Actual/360 day count convention.">Actual360</a>();</div>
<div class="line"> <a name="a4"></a><a class="code" href="group__types.html#gab9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> repoSettlementDays = 0;</div>
<div class="line"> <a class="code" href="namespace_quant_lib.html#a2779d04b4839fd386b5c85bbb96aaf73" title="Interest rate coumpounding rule.">Compounding</a> repoCompounding = <a class="code" href="namespace_quant_lib.html#a2779d04b4839fd386b5c85bbb96aaf73aebfbf7dc5cde0772efb1aa49712bd76b">Simple</a>;</div>
<div class="line"> <a class="code" href="group__datetime.html#ga6d41db8ba0ea90d22df35889df452ada" title="Frequency of events.">Frequency</a> repoCompoundFreq = <a name="a5"></a><a class="code" href="group__datetime.html#gga6d41db8ba0ea90d22df35889df452adaa508b18014c96e2d466c12b39d7f4e426" title="once a year">Annual</a>;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// assume a ten year bond- this is irrelevant</span></div>
<div class="line"> <a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> bondIssueDate(15,September,1995);</div>
<div class="line"> <a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> bondDatedDate(15,September,1995);</div>
<div class="line"> <a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> bondMaturityDate(15,September,2005);</div>
<div class="line"> <a name="a6"></a><a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> bondCoupon = 0.08;</div>
<div class="line"> <a class="code" href="group__datetime.html#ga6d41db8ba0ea90d22df35889df452ada" title="Frequency of events.">Frequency</a> bondCouponFrequency = <a name="a7"></a><a class="code" href="group__datetime.html#gga6d41db8ba0ea90d22df35889df452adaad1a5868a1c314bb7f6ab68e2fa182b2d" title="twice a year">Semiannual</a>;</div>
<div class="line"> <span class="comment">// unknown what calendar fincad is using</span></div>
<div class="line"> <a name="_a8"></a><a class="code" href="class_quant_lib_1_1_calendar.html" title="calendar class">Calendar</a> bondCalendar = <a name="_a9"></a><a class="code" href="class_quant_lib_1_1_null_calendar.html" title="Calendar for reproducing theoretical calculations.">NullCalendar</a>();</div>
<div class="line"> <a class="code" href="class_quant_lib_1_1_day_counter.html" title="day counter class">DayCounter</a> bondDayCountConvention = <a name="_a10"></a><a class="code" href="class_quant_lib_1_1_thirty360.html" title="30/360 day count convention">Thirty360</a>(Thirty360::BondBasis);</div>
<div class="line"> <span class="comment">// unknown what fincad is using. this may affect accrued calculation</span></div>
<div class="line"> <a class="code" href="group__types.html#gab9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> bondSettlementDays = 0;</div>
<div class="line"> <a class="code" href="group__datetime.html#gaff46c5ae9385d20709bedade86edd368" title="Business Day conventions.">BusinessDayConvention</a> bondBusinessDayConvention = <a name="a11"></a><a class="code" href="group__datetime.html#ggaff46c5ae9385d20709bedade86edd368a5fb8b90b7fdf1a3a228fbbc083e64547">Unadjusted</a>;</div>
<div class="line"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> bondCleanPrice = 89.97693786;</div>
<div class="line"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> bondRedemption = 100.0;</div>
<div class="line"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> faceAmount = 100.0;</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"> Settings::instance().evaluationDate() = repoSettlementDate;</div>
<div class="line"> </div>
<div class="line"> <a name="_a12"></a><a class="code" href="class_quant_lib_1_1_relinkable_handle.html" title="Relinkable handle to an observable.">RelinkableHandle<YieldTermStructure></a> bondCurve;</div>
<div class="line"> bondCurve.<a name="a13"></a>linkTo(ext::shared_ptr<YieldTermStructure>(</div>
<div class="line"> <span class="keyword">new</span> <a name="_a14"></a><a class="code" href="class_quant_lib_1_1_flat_forward.html" title="Flat interest-rate curve.">FlatForward</a>(repoSettlementDate,</div>
<div class="line"> .01, <span class="comment">// dummy rate</span></div>
<div class="line"> bondDayCountConvention,</div>
<div class="line"> Compounded,</div>
<div class="line"> bondCouponFrequency)));</div>
<div class="line"> </div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> ext::shared_ptr<FixedRateBond> bond(</span></div>
<div class="line"><span class="comment"> new FixedRateBond(faceAmount,</span></div>
<div class="line"><span class="comment"> bondIssueDate,</span></div>
<div class="line"><span class="comment"> bondDatedDate,</span></div>
<div class="line"><span class="comment"> bondMaturityDate,</span></div>
<div class="line"><span class="comment"> bondSettlementDays,</span></div>
<div class="line"><span class="comment"> std::vector<Rate>(1,bondCoupon),</span></div>
<div class="line"><span class="comment"> bondCouponFrequency,</span></div>
<div class="line"><span class="comment"> bondCalendar,</span></div>
<div class="line"><span class="comment"> bondDayCountConvention,</span></div>
<div class="line"><span class="comment"> bondBusinessDayConvention,</span></div>
<div class="line"><span class="comment"> bondBusinessDayConvention,</span></div>
<div class="line"><span class="comment"> bondRedemption,</span></div>
<div class="line"><span class="comment"> bondCurve));</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> </div>
<div class="line"> <a name="_a15"></a><a class="code" href="class_quant_lib_1_1_schedule.html" title="Payment schedule.">Schedule</a> bondSchedule(bondDatedDate, bondMaturityDate,</div>
<div class="line"> <a name="_a16"></a><a class="code" href="class_quant_lib_1_1_period.html">Period</a>(bondCouponFrequency),</div>
<div class="line"> bondCalendar,bondBusinessDayConvention,</div>
<div class="line"> bondBusinessDayConvention,</div>
<div class="line"> DateGeneration::Backward,<span class="keyword">false</span>);</div>
<div class="line"> ext::shared_ptr<FixedRateBond> bond(</div>
<div class="line"> <span class="keyword">new</span> <a name="_a17"></a><a class="code" href="class_quant_lib_1_1_fixed_rate_bond.html" title="fixed-rate bond">FixedRateBond</a>(bondSettlementDays,</div>
<div class="line"> faceAmount,</div>
<div class="line"> bondSchedule,</div>
<div class="line"> std::vector<Rate>(1,bondCoupon),</div>
<div class="line"> bondDayCountConvention,</div>
<div class="line"> bondBusinessDayConvention,</div>
<div class="line"> bondRedemption,</div>
<div class="line"> bondIssueDate));</div>
<div class="line"> bond->setPricingEngine(ext::shared_ptr<PricingEngine>(</div>
<div class="line"> <span class="keyword">new</span> DiscountingBondEngine(bondCurve)));</div>
<div class="line"> </div>
<div class="line"> bondCurve.linkTo(ext::shared_ptr<YieldTermStructure> (</div>
<div class="line"> <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,</div>
<div class="line"> bond->yield(bondCleanPrice,</div>
<div class="line"> bondDayCountConvention,</div>
<div class="line"> Compounded,</div>
<div class="line"> bondCouponFrequency),</div>
<div class="line"> bondDayCountConvention,</div>
<div class="line"> Compounded,</div>
<div class="line"> bondCouponFrequency)));</div>
<div class="line"> </div>
<div class="line"> Position::Type fwdType = Position::Long;</div>
<div class="line"> <span class="keywordtype">double</span> dummyStrike = 91.5745;</div>
<div class="line"> </div>
<div class="line"> <a class="code" href="class_quant_lib_1_1_relinkable_handle.html" title="Relinkable handle to an observable.">RelinkableHandle<YieldTermStructure></a> repoCurve;</div>
<div class="line"> repoCurve.linkTo(ext::shared_ptr<YieldTermStructure> (</div>
<div class="line"> <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,</div>
<div class="line"> repoRate,</div>
<div class="line"> repoDayCountConvention,</div>
<div class="line"> repoCompounding,</div>
<div class="line"> repoCompoundFreq)));</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"> <a name="_a18"></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,</div>
<div class="line"> repoDeliveryDate,</div>
<div class="line"> fwdType,</div>
<div class="line"> dummyStrike,</div>
<div class="line"> repoSettlementDays,</div>
<div class="line"> repoDayCountConvention,</div>
<div class="line"> bondCalendar,</div>
<div class="line"> bondBusinessDayConvention,</div>
<div class="line"> bond,</div>
<div class="line"> repoCurve,</div>
<div class="line"> repoCurve);</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"> cout << <span class="stringliteral">"Underlying bond clean price: "</span></div>
<div class="line"> << bond->cleanPrice()</div>
<div class="line"> << endl;</div>
<div class="line"> cout << <span class="stringliteral">"Underlying bond dirty price: "</span></div>
<div class="line"> << bond->dirtyPrice()</div>
<div class="line"> << endl;</div>
<div class="line"> cout << <span class="stringliteral">"Underlying bond accrued at settlement: "</span></div>
<div class="line"> << bond->accruedAmount(repoSettlementDate)</div>
<div class="line"> << endl;</div>
<div class="line"> cout << <span class="stringliteral">"Underlying bond accrued at delivery: "</span></div>
<div class="line"> << bond->accruedAmount(repoDeliveryDate)</div>
<div class="line"> << endl;</div>
<div class="line"> cout << <span class="stringliteral">"Underlying bond spot income: "</span></div>
<div class="line"> << bondFwd.spotIncome(repoCurve)</div>
<div class="line"> << endl;</div>
<div class="line"> cout << <span class="stringliteral">"Underlying bond fwd income: "</span></div>
<div class="line"> << bondFwd.spotIncome(repoCurve)/</div>
<div class="line"> repoCurve->discount(repoDeliveryDate)</div>
<div class="line"> << endl;</div>
<div class="line"> cout << <span class="stringliteral">"Repo strike: "</span></div>
<div class="line"> << dummyStrike</div>
<div class="line"> << endl;</div>
<div class="line"> cout << <span class="stringliteral">"Repo NPV: "</span></div>
<div class="line"> << bondFwd.NPV()</div>
<div class="line"> << endl;</div>
<div class="line"> cout << <span class="stringliteral">"Repo clean forward price: "</span></div>
<div class="line"> << bondFwd.cleanForwardPrice()</div>
<div class="line"> << endl;</div>
<div class="line"> cout << <span class="stringliteral">"Repo dirty forward price: "</span></div>
<div class="line"> << bondFwd.forwardPrice()</div>
<div class="line"> << endl;</div>
<div class="line"> cout << <span class="stringliteral">"Repo implied yield: "</span></div>
<div class="line"> << bondFwd.impliedYield(bond->dirtyPrice(),</div>
<div class="line"> dummyStrike,</div>
<div class="line"> repoSettlementDate,</div>
<div class="line"> repoCompounding,</div>
<div class="line"> repoDayCountConvention)</div>
<div class="line"> << endl;</div>
<div class="line"> cout << <span class="stringliteral">"Market repo rate: "</span></div>
<div class="line"> << repoCurve->zeroRate(repoDeliveryDate,</div>
<div class="line"> repoDayCountConvention,</div>
<div class="line"> repoCompounding,</div>
<div class="line"> repoCompoundFreq)</div>
<div class="line"> << endl</div>
<div class="line"> << endl;</div>
<div class="line"> </div>
<div class="line"> cout << <span class="stringliteral">"Compare with example given at \n"</span></div>
<div class="line"> << <span class="stringliteral">"http://www.fincad.com/support/developerFunc/mathref/BFWD.htm"</span></div>
<div class="line"> << endl;</div>
<div class="line"> cout << <span class="stringliteral">"Clean forward price = 88.2408"</span></div>
<div class="line"> << endl</div>
<div class="line"> << endl;</div>
<div class="line"> cout << <span class="stringliteral">"In that example, it is unknown what bond calendar they are\n"</span></div>
<div class="line"> << <span class="stringliteral">"using, as well as settlement Days. For that reason, I have\n"</span></div>
<div class="line"> << <span class="stringliteral">"made the simplest possible assumptions here: NullCalendar\n"</span></div>
<div class="line"> << <span class="stringliteral">"and 0 settlement days."</span></div>
<div class="line"> << endl;</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"> <span class="keywordflow">return</span> 0;</div>
<div class="line"> </div>
<div class="line"> } <span class="keywordflow">catch</span> (exception& e) {</div>
<div class="line"> cerr << e.what() << endl;</div>
<div class="line"> <span class="keywordflow">return</span> 1;</div>
<div class="line"> } <span class="keywordflow">catch</span> (...) {</div>
<div class="line"> cerr << <span class="stringliteral">"unknown error"</span> << endl;</div>
<div class="line"> <span class="keywordflow">return</span> 1;</div>
<div class="line"> }</div>
<div class="line">}</div>
<div class="line"> </div>
</div><!-- fragment --> </div><!-- contents -->
<!-- HTML footer for doxygen 1.8.9.1-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by <a href="http://www.doxygen.org/index.html">Doxygen</a>
1.8.20
</small></address>
</body>
</html>
|