Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
valuationcalculator.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file engine/valuationcalculator.hpp
20 \brief The cube valuation calculator interface
21 \ingroup simulation
22*/
23
24#pragma once
25
26#include <orea/cube/npvcube.hpp>
30
31namespace ore {
32namespace analytics {
34using QuantLib::Date;
35using QuantLib::Real;
36using QuantLib::Size;
37
38//! ValuationCalculator interface
40public:
42
43 virtual void calculate(
44 //! The trade
45 const QuantLib::ext::shared_ptr<Trade>& trade,
46 //! Trade index for writing to the cube
47 Size tradeIndex,
48 //! The market
49 const QuantLib::ext::shared_ptr<SimMarket>& simMarket,
50 //! The cube for data on trade level
51 QuantLib::ext::shared_ptr<NPVCube>& outputCube,
52 //! The cube for data on netting set level
53 QuantLib::ext::shared_ptr<NPVCube>& outputCubeNettingSet,
54 //! The date
55 const Date& date,
56 //! Date index
57 Size dateIndex,
58 //! Sample
59 Size sample,
60 //! isCloseOut
61 bool isCloseOut = false) = 0;
62
63 virtual void calculateT0(
64 //! The trade
65 const QuantLib::ext::shared_ptr<Trade>& trade,
66 //! Trade index for writing to the cube
67 Size tradeIndex,
68 //! The market
69 const QuantLib::ext::shared_ptr<SimMarket>& simMarket,
70 //! The cube
71 QuantLib::ext::shared_ptr<NPVCube>& outputCube,
72 //! The cube
73 QuantLib::ext::shared_ptr<NPVCube>& outputCubeNettingSet) = 0;
74
75 // called once before the valuation engine run
76 virtual void init(const QuantLib::ext::shared_ptr<Portfolio>& portfolio, const QuantLib::ext::shared_ptr<SimMarket>& simMarket) = 0;
77
78 // called after each scenario update before the calculators are run
79 virtual void initScenario() = 0;
80};
81
82//! NPVCalculator
83/*! Calculate the NPV of the given trade, convert to base currency and divide by the numeraire
84 * If the NPV() call throws, we log an exception and write 0 to the cube
85 *
86 */
88public:
89 //! base ccy and index to write to
90 NPVCalculator(const std::string& baseCcyCode, Size index = 0) : baseCcyCode_(baseCcyCode), index_(index) {}
91
92 virtual void calculate(const QuantLib::ext::shared_ptr<Trade>& trade, Size tradeIndex,
93 const QuantLib::ext::shared_ptr<SimMarket>& simMarket, QuantLib::ext::shared_ptr<NPVCube>& outputCube,
94 QuantLib::ext::shared_ptr<NPVCube>& outputCubeNettingSet, const Date& date, Size dateIndex,
95 Size sample, bool isCloseOut = false) override;
96
97 virtual void calculateT0(const QuantLib::ext::shared_ptr<Trade>& trade, Size tradeIndex,
98 const QuantLib::ext::shared_ptr<SimMarket>& simMarket, QuantLib::ext::shared_ptr<NPVCube>& outputCube,
99 QuantLib::ext::shared_ptr<NPVCube>& outputCubeNettingSet) override;
100
101 virtual Real npv(Size tradeIndex, const QuantLib::ext::shared_ptr<Trade>& trade,
102 const QuantLib::ext::shared_ptr<SimMarket>& simMarket);
103
104 void init(const QuantLib::ext::shared_ptr<Portfolio>& portfolio, const QuantLib::ext::shared_ptr<SimMarket>& simMarket) override;
105 void initScenario() override;
106
107protected:
108 std::string baseCcyCode_;
109 Size index_;
110
111 std::vector<Handle<Quote>> ccyQuotes_;
112 std::vector<double> fxRates_;
113 std::vector<Size> tradeCcyIndex_;
114};
115
116//! CashflowCalculator
117/*! Calculates the cashflow, converted to base ccy, from t to t+1, this interval is defined by the provided dategrid
118 * The interval is (t, t+1], i.e. we exclude todays flows and include flows that fall exactly on t+1.
119 * For t0 we do nothing (and so the cube will have a 0 value)
120 */
122public:
123 //! Constructor takes the base ccy, date grid and index of cube to write to.
124 CashflowCalculator(const std::string& baseCcyCode, const Date& t0Date, const QuantLib::ext::shared_ptr<DateGrid>& dateGrid,
125 Size index)
126 : baseCcyCode_(baseCcyCode), t0Date_(t0Date), dateGrid_(dateGrid), index_(index) {}
127
128 virtual void calculate(const QuantLib::ext::shared_ptr<Trade>& trade, Size tradeIndex,
129 const QuantLib::ext::shared_ptr<SimMarket>& simMarket, QuantLib::ext::shared_ptr<NPVCube>& outputCube,
130 QuantLib::ext::shared_ptr<NPVCube>& outputCubeNettingSet, const Date& date, Size dateIndex,
131 Size sample, bool isCloseOut = false) override;
132
133 virtual void calculateT0(const QuantLib::ext::shared_ptr<Trade>& trade, Size tradeIndex,
134 const QuantLib::ext::shared_ptr<SimMarket>& simMarket, QuantLib::ext::shared_ptr<NPVCube>& outputCube,
135 QuantLib::ext::shared_ptr<NPVCube>& outputCubeNettingSet) override {}
136
137 void init(const QuantLib::ext::shared_ptr<Portfolio>& portfolio, const QuantLib::ext::shared_ptr<SimMarket>& simMarket) override;
138 void initScenario() override;
139
140private:
141 std::string baseCcyCode_;
143 QuantLib::ext::shared_ptr<DateGrid> dateGrid_;
144 Size index_;
145
146 std::vector<Handle<Quote>> ccyQuotes_;
147 std::vector<double> fxRates_;
148 std::vector<std::vector<Size>> tradeAndLegCcyIndex_;
149};
150
151//! NPVCalculatorFXT0
152/*! Calculate the NPV of the given trade, convert to base currency USING T0 RATES and divide by the numeraire
153 * This can sometimes be useful for finite difference ("bump-revalue") sensitivities
154 * (for FX spot sensis, if we wish to bump the spot in the pricing model, but still convert to base using static FX)
155 * If the NPV() call throws, we log an exception and write 0 to the cube
156 *
157 */
159public:
160 //! base ccy and index to write to
161 NPVCalculatorFXT0(const std::string& baseCcyCode, const QuantLib::ext::shared_ptr<Market>& t0Market, Size index = 0)
162 : baseCcyCode_(baseCcyCode), t0Market_(t0Market), index_(index) {}
163
164 virtual void calculate(const QuantLib::ext::shared_ptr<Trade>& trade, Size tradeIndex,
165 const QuantLib::ext::shared_ptr<SimMarket>& simMarket, QuantLib::ext::shared_ptr<NPVCube>& outputCube,
166 QuantLib::ext::shared_ptr<NPVCube>& outputCubeNettingSet, const Date& date, Size dateIndex,
167 Size sample, bool isCloseOut = false) override;
168
169 virtual void calculateT0(const QuantLib::ext::shared_ptr<Trade>& trade, Size tradeIndex,
170 const QuantLib::ext::shared_ptr<SimMarket>& simMarket, QuantLib::ext::shared_ptr<NPVCube>& outputCube,
171 QuantLib::ext::shared_ptr<NPVCube>& outputCubeNettingSet) override;
172
173 Real npv(Size tradeIndex, const QuantLib::ext::shared_ptr<Trade>& trade, const QuantLib::ext::shared_ptr<SimMarket>& simMarket);
174
175 void init(const QuantLib::ext::shared_ptr<Portfolio>& portfolio, const QuantLib::ext::shared_ptr<SimMarket>& simMarket) override;
176 void initScenario() override {}
177
178private:
179 std::string baseCcyCode_;
180 QuantLib::ext::shared_ptr<Market> t0Market_;
181 Size index_;
182
183 std::vector<double> fxRates_;
184 std::vector<Size> tradeCcyIndex_;
185};
186
187} // namespace analytics
188} // namespace ore
virtual void calculateT0(const QuantLib::ext::shared_ptr< Trade > &trade, Size tradeIndex, const QuantLib::ext::shared_ptr< SimMarket > &simMarket, QuantLib::ext::shared_ptr< NPVCube > &outputCube, QuantLib::ext::shared_ptr< NPVCube > &outputCubeNettingSet) override
void init(const QuantLib::ext::shared_ptr< Portfolio > &portfolio, const QuantLib::ext::shared_ptr< SimMarket > &simMarket) override
virtual void calculate(const QuantLib::ext::shared_ptr< Trade > &trade, Size tradeIndex, const QuantLib::ext::shared_ptr< SimMarket > &simMarket, QuantLib::ext::shared_ptr< NPVCube > &outputCube, QuantLib::ext::shared_ptr< NPVCube > &outputCubeNettingSet, const Date &date, Size dateIndex, Size sample, bool isCloseOut=false) override
std::vector< std::vector< Size > > tradeAndLegCcyIndex_
QuantLib::ext::shared_ptr< DateGrid > dateGrid_
CashflowCalculator(const std::string &baseCcyCode, const Date &t0Date, const QuantLib::ext::shared_ptr< DateGrid > &dateGrid, Size index)
Constructor takes the base ccy, date grid and index of cube to write to.
std::vector< Handle< Quote > > ccyQuotes_
NPVCalculatorFXT0(const std::string &baseCcyCode, const QuantLib::ext::shared_ptr< Market > &t0Market, Size index=0)
base ccy and index to write to
virtual void calculateT0(const QuantLib::ext::shared_ptr< Trade > &trade, Size tradeIndex, const QuantLib::ext::shared_ptr< SimMarket > &simMarket, QuantLib::ext::shared_ptr< NPVCube > &outputCube, QuantLib::ext::shared_ptr< NPVCube > &outputCubeNettingSet) override
QuantLib::ext::shared_ptr< Market > t0Market_
Real npv(Size tradeIndex, const QuantLib::ext::shared_ptr< Trade > &trade, const QuantLib::ext::shared_ptr< SimMarket > &simMarket)
void init(const QuantLib::ext::shared_ptr< Portfolio > &portfolio, const QuantLib::ext::shared_ptr< SimMarket > &simMarket) override
virtual void calculate(const QuantLib::ext::shared_ptr< Trade > &trade, Size tradeIndex, const QuantLib::ext::shared_ptr< SimMarket > &simMarket, QuantLib::ext::shared_ptr< NPVCube > &outputCube, QuantLib::ext::shared_ptr< NPVCube > &outputCubeNettingSet, const Date &date, Size dateIndex, Size sample, bool isCloseOut=false) override
virtual void calculateT0(const QuantLib::ext::shared_ptr< Trade > &trade, Size tradeIndex, const QuantLib::ext::shared_ptr< SimMarket > &simMarket, QuantLib::ext::shared_ptr< NPVCube > &outputCube, QuantLib::ext::shared_ptr< NPVCube > &outputCubeNettingSet) override
virtual Real npv(Size tradeIndex, const QuantLib::ext::shared_ptr< Trade > &trade, const QuantLib::ext::shared_ptr< SimMarket > &simMarket)
void init(const QuantLib::ext::shared_ptr< Portfolio > &portfolio, const QuantLib::ext::shared_ptr< SimMarket > &simMarket) override
virtual void calculate(const QuantLib::ext::shared_ptr< Trade > &trade, Size tradeIndex, const QuantLib::ext::shared_ptr< SimMarket > &simMarket, QuantLib::ext::shared_ptr< NPVCube > &outputCube, QuantLib::ext::shared_ptr< NPVCube > &outputCubeNettingSet, const Date &date, Size dateIndex, Size sample, bool isCloseOut=false) override
NPVCalculator(const std::string &baseCcyCode, Size index=0)
base ccy and index to write to
std::vector< Handle< Quote > > ccyQuotes_
ValuationCalculator interface.
virtual void init(const QuantLib::ext::shared_ptr< Portfolio > &portfolio, const QuantLib::ext::shared_ptr< SimMarket > &simMarket)=0
virtual void calculate(const QuantLib::ext::shared_ptr< Trade > &trade, Size tradeIndex, const QuantLib::ext::shared_ptr< SimMarket > &simMarket, QuantLib::ext::shared_ptr< NPVCube > &outputCube, QuantLib::ext::shared_ptr< NPVCube > &outputCubeNettingSet, const Date &date, Size dateIndex, Size sample, bool isCloseOut=false)=0
virtual void calculateT0(const QuantLib::ext::shared_ptr< Trade > &trade, Size tradeIndex, const QuantLib::ext::shared_ptr< SimMarket > &simMarket, QuantLib::ext::shared_ptr< NPVCube > &outputCube, QuantLib::ext::shared_ptr< NPVCube > &outputCubeNettingSet)=0
The base NPV cube class.
A Market class that can be Simulated.