Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
impliedbondspread.cpp
Go to the documentation of this file.
1/*
2Copyright (C) 2018 Quaternion Risk Management Ltd
3All rights reserved.
4
5This file is part of ORE, a free-software/open-source library
6for transparent pricing and risk analysis - http://opensourcerisk.org
7
8ORE is free software: you can redistribute it and/or modify it
9under the terms of the Modified BSD License. You should have received a
10copy of the license along with this program.
11The license is also available online at <http://opensourcerisk.org>
12
13This program is distributed on the basis that it will form a useful
14contribution to risk analytics and model standardisation, but WITHOUT
15ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19#include <boost/make_shared.hpp>
20#include <ql/math/solvers1d/brent.hpp>
21#include <ql/termstructures/yieldtermstructure.hpp>
23
24using namespace QuantLib;
25
26namespace QuantExt {
27
28namespace {
29
30class PriceError {
31public:
32 PriceError(const Bond& engine, SimpleQuote& spread, Real targetValue, bool isCleanPrice);
33 Real operator()(Real spread) const;
34
35private:
36 const Bond& bond_;
37 SimpleQuote& spread_;
40};
41
42PriceError::PriceError(const Bond& bond, SimpleQuote& spread, Real targetValue, bool isCleanPrice)
43 : bond_(bond), spread_(spread), targetValue_(targetValue), isCleanPrice_(isCleanPrice) {}
44
45Real PriceError::operator()(Real spread) const {
46 spread_.setValue(spread);
47 Real guessValue = isCleanPrice_ ? bond_.cleanPrice() : bond_.dirtyPrice();
48 return guessValue - targetValue_;
49}
50
51} // namespace
52
53namespace detail {
54
55Real ImpliedBondSpreadHelper::calculate(const QuantLib::ext::shared_ptr<Bond>& bond,
56 const QuantLib::ext::shared_ptr<PricingEngine>& engine,
57 const QuantLib::ext::shared_ptr<SimpleQuote>& spreadQuote, Real targetValue,
58 bool isCleanPrice, Real accuracy, Natural maxEvaluations, Real minSpread,
59 Real maxSpread) {
60
61 Bond clonedBond = *bond;
62 clonedBond.setPricingEngine(engine);
63 clonedBond.recalculate();
64 spreadQuote->setValue(0.005);
65
66 PriceError f(clonedBond, *spreadQuote, targetValue, isCleanPrice);
67 Brent solver;
68 solver.setMaxEvaluations(maxEvaluations);
69 Real guess = (minSpread + maxSpread) / 2.0;
70 Real result = solver.solve(f, accuracy, guess, minSpread, maxSpread);
71 return result;
72}
73
74} // namespace detail
75
76} // namespace QuantExt
Real targetValue_
Definition: cdsoption.cpp:79
SimpleQuote & spread_
bool isCleanPrice_
const Bond & bond_
Real targetValue_
utilities for implied bond credit spread calculation