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 "
123
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
146 if (startDate < flow->date() && flow->date() <= endDate)
147 legFlow += flow->amount();
148 }
149 if (legFlow != 0) {
150
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< double > fxRates_
std::vector< std::vector< Size > > tradeAndLegCcyIndex_
Size size(const ValueType &v)