Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
CashflowCalculator Class Reference

CashflowCalculator. More...

#include <orea/engine/valuationcalculator.hpp>

+ Inheritance diagram for CashflowCalculator:
+ Collaboration diagram for CashflowCalculator:

Public Member Functions

 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. More...
 
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
 
void init (const QuantLib::ext::shared_ptr< Portfolio > &portfolio, const QuantLib::ext::shared_ptr< SimMarket > &simMarket) override
 
void initScenario () override
 
- Public Member Functions inherited from ValuationCalculator
virtual ~ValuationCalculator ()
 
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
 
virtual void init (const QuantLib::ext::shared_ptr< Portfolio > &portfolio, const QuantLib::ext::shared_ptr< SimMarket > &simMarket)=0
 
virtual void initScenario ()=0
 

Private Attributes

std::string baseCcyCode_
 
Date t0Date_
 
QuantLib::ext::shared_ptr< DateGriddateGrid_
 
Size index_
 
std::vector< Handle< Quote > > ccyQuotes_
 
std::vector< double > fxRates_
 
std::vector< std::vector< Size > > tradeAndLegCcyIndex_
 

Detailed Description

CashflowCalculator.

Calculates the cashflow, converted to base ccy, from t to t+1, this interval is defined by the provided dategrid The interval is (t, t+1], i.e. we exclude todays flows and include flows that fall exactly on t+1. For t0 we do nothing (and so the cube will have a 0 value)

Definition at line 121 of file valuationcalculator.hpp.

Constructor & Destructor Documentation

◆ CashflowCalculator()

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.

Definition at line 124 of file valuationcalculator.hpp.

Member Function Documentation

◆ calculate()

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 
)
overridevirtual

Implements ValuationCalculator.

Definition at line 109 of file valuationcalculator.cpp.

113 {
114 if (isCloseOut)
115 return;
116
117 Real netPositiveFlow = 0;
118 Real netNegativeFlow = 0;
119
120 QL_REQUIRE(dateGrid_->valuationDates()[dateIndex] == date, "Date mixup, date is " << date << " but grid index is "
121 << dateIndex << ", grid(dateIndex) is "
122 << dateGrid_->dates()[dateIndex]);
123 // Date startDate = dateIndex == 0 ? t0Date_ : dateGrid_->dates()[dateIndex - 1];
124 Date startDate = date;
125 Date endDate = date == dateGrid_->dates().back() ? date : dateGrid_->dates()[dateIndex + 1];
126
127 bool isOption = trade->instrument()->isOption();
128 bool isExercised = false;
129 bool isPhysical = false;
130 Real longShort = 1.0;
131 if (isOption) {
132 QuantLib::ext::shared_ptr<data::OptionWrapper> wrapper =
133 QuantLib::ext::dynamic_pointer_cast<data::OptionWrapper>(trade->instrument());
134 isExercised = wrapper->isExercised();
135 longShort = wrapper->isLong() ? 1.0 : -1.0;
136 isPhysical = wrapper->isPhysicalDelivery();
137 }
138
139 try {
140 if (!isOption || (isExercised && isPhysical)) {
141 for (Size i = 0; i < trade->legs().size(); i++) {
142 const Leg& leg = trade->legs()[i];
143 Real legFlow = 0;
144 for (auto flow : leg) {
145 // Take flows in (t, t+1]
146 if (startDate < flow->date() && flow->date() <= endDate)
147 legFlow += flow->amount();
148 }
149 if (legFlow != 0) {
150 // Do FX conversion and add to netFlow
151 Real fx = fxRates_[tradeAndLegCcyIndex_[tradeIndex][i]];
152 Real direction = trade->legPayers()[i] ? -1.0 : 1.0;
153 legFlow *= direction * longShort * fx;
154 if (legFlow > 0)
155 netPositiveFlow += legFlow;
156 else
157 netNegativeFlow += legFlow;
158 }
159 }
160 }
161 } catch (std::exception& e) {
162 ALOG("Failed to calculate cashflows for trade " << trade->id() << " : " << e.what());
163 netPositiveFlow = 0;
164 netNegativeFlow = 0;
165 } catch (...) {
166 ALOG("Failed to calculate cashflows for trade " << trade->id() << " : Unhandled Exception");
167 netPositiveFlow = 0;
168 netNegativeFlow = 0;
169 }
170
171 Real numeraire = simMarket->numeraire();
172
173 outputCube->set(netPositiveFlow / numeraire, tradeIndex, dateIndex, sample, index_);
174 outputCube->set(netNegativeFlow / numeraire, tradeIndex, dateIndex, sample, index_+1);
175}
std::vector< std::vector< Size > > tradeAndLegCcyIndex_
#define ALOG(text)
Size size(const ValueType &v)
+ Here is the call graph for this function:

◆ calculateT0()

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 
)
overridevirtual

Implements ValuationCalculator.

Definition at line 133 of file valuationcalculator.hpp.

135 {}

◆ init()

void init ( const QuantLib::ext::shared_ptr< Portfolio > &  portfolio,
const QuantLib::ext::shared_ptr< SimMarket > &  simMarket 
)
overridevirtual

Implements ValuationCalculator.

Definition at line 79 of file valuationcalculator.cpp.

80 {
81 DLOG("init CashflowCalculator");
83 std::set<std::string> ccys;
84 for (auto const& [tradeId,trade] : portfolio->trades()) {
85 tradeAndLegCcyIndex_.push_back(std::vector<Size>(trade->legs().size()));
86 for (auto const& l : trade->legCurrencies()) {
87 ccys.insert(l);
88 }
89 }
90 size_t i = 0;
91 for (const auto& [tradeId, trade] : portfolio->trades()) {
92 for (Size j = 0; j < trade->legs().size(); ++j) {
94 std::distance(ccys.begin(), ccys.find(trade->legCurrencies()[j]));
95 }
96 i++;
97 }
98 ccyQuotes_.resize(ccys.size());
99 for (Size i = 0; i < ccys.size(); ++i)
100 ccyQuotes_[i] = (simMarket->fxRate(*std::next(ccys.begin(), i) + baseCcyCode_));
101 fxRates_.resize(ccys.size());
102}
std::vector< Handle< Quote > > ccyQuotes_
#define DLOG(text)
+ Here is the call graph for this function:

◆ initScenario()

void initScenario ( )
overridevirtual

Implements ValuationCalculator.

Definition at line 104 of file valuationcalculator.cpp.

104 {
105 for (Size i = 0; i < ccyQuotes_.size(); ++i)
106 fxRates_[i] = ccyQuotes_[i]->value();
107}
SafeStack< ValueType > value

Member Data Documentation

◆ baseCcyCode_

std::string baseCcyCode_
private

Definition at line 141 of file valuationcalculator.hpp.

◆ t0Date_

Date t0Date_
private

Definition at line 142 of file valuationcalculator.hpp.

◆ dateGrid_

QuantLib::ext::shared_ptr<DateGrid> dateGrid_
private

Definition at line 143 of file valuationcalculator.hpp.

◆ index_

Size index_
private

Definition at line 144 of file valuationcalculator.hpp.

◆ ccyQuotes_

std::vector<Handle<Quote> > ccyQuotes_
private

Definition at line 146 of file valuationcalculator.hpp.

◆ fxRates_

std::vector<double> fxRates_
private

Definition at line 147 of file valuationcalculator.hpp.

◆ tradeAndLegCcyIndex_

std::vector<std::vector<Size> > tradeAndLegCcyIndex_
private

Definition at line 148 of file valuationcalculator.hpp.