31void NPVCalculator::init(
const QuantLib::ext::shared_ptr<Portfolio>& portfolio,
const QuantLib::ext::shared_ptr<SimMarket>& simMarket) {
32 DLOG(
"init NPVCalculator");
34 std::set<std::string> ccys;
35 for (
auto const& [tradeId, trade] : portfolio->trades())
36 ccys.insert(trade->npvCurrency());
39 for (
auto const& [tradeId, trade] : portfolio->trades()) {
40 tradeCcyIndex_[i] = std::distance(ccys.begin(), ccys.find(trade->npvCurrency()));
45 for (Size i = 0; i < ccys.size(); ++i)
56 const QuantLib::ext::shared_ptr<SimMarket>& simMarket, QuantLib::ext::shared_ptr<NPVCube>& outputCube,
57 QuantLib::ext::shared_ptr<NPVCube>& outputCubeNettingSet,
const Date& date, Size dateIndex,
58 Size sample,
bool isCloseOut) {
60 outputCube->set(
npv(tradeIndex, trade, simMarket), tradeIndex, dateIndex, sample,
index_);
64 const QuantLib::ext::shared_ptr<SimMarket>& simMarket, QuantLib::ext::shared_ptr<NPVCube>& outputCube,
65 QuantLib::ext::shared_ptr<NPVCube>& outputCubeNettingSet) {
66 outputCube->setT0(
npv(tradeIndex, trade, simMarket), tradeIndex,
index_);
70 const QuantLib::ext::shared_ptr<SimMarket>& simMarket) {
71 Real
npv = trade->instrument()->NPV();
75 Real numeraire = simMarket->numeraire();
76 return npv * fx / numeraire;
80 const QuantLib::ext::shared_ptr<SimMarket>& simMarket) {
81 DLOG(
"init CashflowCalculator");
83 std::set<std::string> ccys;
84 for (
auto const& [tradeId,trade] : portfolio->trades()) {
86 for (
auto const& l : trade->legCurrencies()) {
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]));
99 for (Size i = 0; i < ccys.size(); ++i)
110 const QuantLib::ext::shared_ptr<SimMarket>& simMarket,
111 QuantLib::ext::shared_ptr<NPVCube>& outputCube,
112 QuantLib::ext::shared_ptr<NPVCube>& outputCubeNettingSet,
const Date& date, Size dateIndex,
113 Size sample,
bool isCloseOut) {
117 Real netPositiveFlow = 0;
118 Real netNegativeFlow = 0;
120 QL_REQUIRE(
dateGrid_->valuationDates()[dateIndex] == date,
"Date mixup, date is " << date <<
" but grid index is "
121 << dateIndex <<
", grid(dateIndex) is "
124 Date startDate = date;
125 Date endDate = date ==
dateGrid_->dates().back() ? date :
dateGrid_->dates()[dateIndex + 1];
127 bool isOption = trade->instrument()->isOption();
128 bool isExercised =
false;
129 bool isPhysical =
false;
130 Real longShort = 1.0;
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();
140 if (!isOption || (isExercised && isPhysical)) {
141 for (Size i = 0; i < trade->legs().
size(); i++) {
142 const Leg& leg = trade->legs()[i];
144 for (
auto flow : leg) {
146 if (startDate < flow->date() && flow->date() <= endDate)
147 legFlow += flow->amount();
152 Real direction = trade->legPayers()[i] ? -1.0 : 1.0;
153 legFlow *= direction * longShort * fx;
155 netPositiveFlow += legFlow;
157 netNegativeFlow += legFlow;
161 }
catch (std::exception& e) {
162 ALOG(
"Failed to calculate cashflows for trade " << trade->id() <<
" : " << e.what());
166 ALOG(
"Failed to calculate cashflows for trade " << trade->id() <<
" : Unhandled Exception");
171 Real numeraire = simMarket->numeraire();
173 outputCube->set(netPositiveFlow / numeraire, tradeIndex, dateIndex, sample,
index_);
174 outputCube->set(netNegativeFlow / numeraire, tradeIndex, dateIndex, sample,
index_+1);
178 const QuantLib::ext::shared_ptr<SimMarket>& simMarket) {
179 DLOG(
"init NPVCalculatorFXT0");
181 std::set<std::string> ccys;
182 for (
auto const& [tradeId, trade]: portfolio->trades())
183 ccys.insert(trade->npvCurrency());
185 for (
auto const& [tradeId, trade] : portfolio->trades()){
186 tradeCcyIndex_[i] = std::distance(ccys.begin(), ccys.find(trade->npvCurrency()));
190 for (Size i = 0; i < ccys.size(); ++i)
195 const QuantLib::ext::shared_ptr<SimMarket>& simMarket, QuantLib::ext::shared_ptr<NPVCube>& outputCube,
196 QuantLib::ext::shared_ptr<NPVCube>& outputCubeNettingSet,
const Date& date, Size dateIndex,
197 Size sample,
bool isCloseOut) {
199 outputCube->set(
npv(tradeIndex, trade, simMarket), tradeIndex, dateIndex, sample,
index_);
203 const QuantLib::ext::shared_ptr<SimMarket>& simMarket,
204 QuantLib::ext::shared_ptr<NPVCube>& outputCube,
205 QuantLib::ext::shared_ptr<NPVCube>& outputCubeNettingSet) {
206 outputCube->setT0(
npv(tradeIndex, trade, simMarket), tradeIndex,
index_);
210 const QuantLib::ext::shared_ptr<SimMarket>& simMarket) {
211 Real
npv = trade->instrument()->NPV();
215 Real numeraire = simMarket->numeraire();
216 return npv * fx / numeraire;
std::vector< double > fxRates_
void init(const QuantLib::ext::shared_ptr< Portfolio > &portfolio, const QuantLib::ext::shared_ptr< SimMarket > &simMarket) override
void initScenario() 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_
std::vector< Handle< Quote > > ccyQuotes_
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
std::vector< Size > tradeCcyIndex_
QuantLib::ext::shared_ptr< Market > t0Market_
std::vector< double > fxRates_
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
std::vector< Size > tradeCcyIndex_
std::vector< double > fxRates_
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
void initScenario() 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< Handle< Quote > > ccyQuotes_
SafeStack< ValueType > value
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
Size size(const ValueType &v)
The counterparty cube calculator interface.