Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
flowanalysis.cpp
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#include <iomanip>
22#include <ostream>
23#include <ql/cashflows/averagebmacoupon.hpp>
24#include <ql/cashflows/coupon.hpp>
25#include <ql/cashflows/cpicoupon.hpp>
26#include <ql/cashflows/floatingratecoupon.hpp>
27#include <ql/cashflows/yoyinflationcoupon.hpp>
28#include <ql/indexes/interestrateindex.hpp>
32using namespace std;
33using QuantLib::Date;
34using QuantLib::Real;
35
36namespace ore {
37namespace data {
38
39class AnalysisGenerator : public QuantLib::AcyclicVisitor,
40 public QuantLib::Visitor<QuantLib::CashFlow>,
41 public QuantLib::Visitor<QuantLib::IndexedCashFlow>,
42 public QuantLib::Visitor<QuantLib::Coupon>,
43 public QuantLib::Visitor<QuantLib::FloatingRateCoupon>,
44 public QuantLib::Visitor<QuantExt::AverageONIndexedCoupon>,
45 public QuantLib::Visitor<QuantLib::AverageBMACoupon>,
46 public QuantLib::Visitor<QuantExt::FXLinkedCashFlow>,
47 public QuantLib::Visitor<QuantExt::AverageFXLinkedCashFlow>,
48 public QuantLib::Visitor<QuantExt::FloatingRateFXLinkedNotionalCoupon>,
49 public QuantLib::Visitor<QuantLib::InflationCoupon> {
50private:
51 vector<vector<string>> flowAnalysis_;
52 static const QuantLib::Size numberOfColumns_ = 5;
53
54public:
55 AnalysisGenerator();
56 void reset();
57 void visit(QuantLib::CashFlow& c) override;
58 void visit(QuantLib::IndexedCashFlow& c) override;
59 void visit(QuantLib::Coupon& c) override;
60 void visit(QuantLib::FloatingRateCoupon& c) override;
61 void visit(QuantExt::AverageONIndexedCoupon& c) override;
62 void visit(QuantLib::AverageBMACoupon& c) override;
63 void visit(QuantExt::FXLinkedCashFlow& c) override;
64 void visit(QuantExt::AverageFXLinkedCashFlow& c) override;
66 void visit(QuantLib::InflationCoupon& c) override;
67 const vector<vector<string>>& analysis() const;
68};
69
70#define PAYMENT_DATE 0
71#define ACCRUAL_START_DATE 1
72#define ACCRUAL_END_DATE 2
73#define FIXING_DATE 3
74#define INDEX 4
75
76AnalysisGenerator::AnalysisGenerator() { reset(); }
77
78void AnalysisGenerator::reset() {
79 flowAnalysis_.clear();
80
81 vector<string> headings(numberOfColumns_);
82 headings[PAYMENT_DATE] = string("Payment Date");
83 headings[ACCRUAL_START_DATE] = string("Accrual Start Date");
84 headings[ACCRUAL_END_DATE] = string("Accrual End Date");
85 headings[FIXING_DATE] = string("Fixing Date");
86 headings[INDEX] = string("Index");
87
88 flowAnalysis_.push_back(headings);
89}
90
91void AnalysisGenerator::visit(QuantLib::CashFlow& c) {
92 vector<string> cf(numberOfColumns_, "#N/A");
93 cf[PAYMENT_DATE] = to_string(c.date());
94 flowAnalysis_.push_back(cf);
95}
96
97void AnalysisGenerator::visit(QuantLib::Coupon& c) {
98 visit(static_cast<QuantLib::CashFlow&>(c));
99 flowAnalysis_.back()[ACCRUAL_START_DATE] = to_string(c.accrualStartDate());
100 flowAnalysis_.back()[ACCRUAL_END_DATE] = to_string(c.accrualEndDate());
101};
102
103void AnalysisGenerator::visit(QuantLib::FloatingRateCoupon& c) {
104 visit(static_cast<QuantLib::Coupon&>(c));
105 flowAnalysis_.back()[FIXING_DATE] = to_string(c.fixingDate());
106 flowAnalysis_.back()[INDEX] = c.index()->name();
107}
108
109void AnalysisGenerator::visit(QuantExt::AverageONIndexedCoupon& c) {
110 for (auto& d : c.fixingDates()) {
111 visit(static_cast<QuantLib::Coupon&>(c));
112 flowAnalysis_.back()[FIXING_DATE] = to_string(d);
113 flowAnalysis_.back()[INDEX] = c.index()->name();
114 }
115}
116
117void AnalysisGenerator::visit(QuantLib::AverageBMACoupon& c) {
118 for (auto& d : c.fixingDates()) {
119 visit(static_cast<QuantLib::Coupon&>(c));
120 flowAnalysis_.back()[FIXING_DATE] = to_string(d);
121 flowAnalysis_.back()[INDEX] = c.index()->name();
122 }
123}
124
125void AnalysisGenerator::visit(QuantExt::FXLinkedCashFlow& c) {
126 visit(static_cast<QuantLib::CashFlow&>(c));
127 flowAnalysis_.back()[FIXING_DATE] = to_string(c.fxFixingDate());
128 flowAnalysis_.back()[INDEX] = c.fxIndex()->name();
129}
130
131void AnalysisGenerator::visit(QuantExt::AverageFXLinkedCashFlow& c) {
132 visit(static_cast<QuantLib::CashFlow&>(c));
133 flowAnalysis_.back()[FIXING_DATE] = to_string(c.fxFixingDates().back());
134 flowAnalysis_.back()[INDEX] = c.fxIndex()->name();
135}
136
137void AnalysisGenerator::visit(QuantExt::FloatingRateFXLinkedNotionalCoupon& c) {
138 // Ibor
139 visit(static_cast<QuantLib::FloatingRateCoupon&>(c));
140 // FX
141 flowAnalysis_.back()[FIXING_DATE] = to_string(c.fxFixingDate());
142 flowAnalysis_.back()[INDEX] = c.fxIndex()->name();
143}
144
145void AnalysisGenerator::visit(QuantLib::InflationCoupon& c) {
146 visit(static_cast<QuantLib::Coupon&>(c));
147 flowAnalysis_.back()[FIXING_DATE] = to_string(c.fixingDate());
148 flowAnalysis_.back()[INDEX] = c.index()->name();
149}
150
151void AnalysisGenerator::visit(QuantLib::IndexedCashFlow& c) {
152 visit(static_cast<QuantLib::CashFlow&>(c));
153 flowAnalysis_.back()[FIXING_DATE] = to_string(c.fixingDate());
154 flowAnalysis_.back()[INDEX] = c.index()->name();
155}
156const vector<vector<string>>& AnalysisGenerator::analysis() const { return flowAnalysis_; }
157
158vector<vector<string>> flowAnalysis(const QuantLib::Leg& leg) {
159 AnalysisGenerator generator;
160 for (QuantLib::Size i = 0; i < leg.size(); ++i)
161 leg[i]->accept(generator);
162 return generator.analysis();
163}
164} // namespace data
165} // namespace ore
const boost::shared_ptr< FxIndex > & fxIndex() const
const std::vector< Date > & fxFixingDates() const
const std::vector< Date > & fixingDates() const
const boost::shared_ptr< FxIndex > & fxIndex() const
Date fxFixingDate() const
#define FIXING_DATE
#define PAYMENT_DATE
#define INDEX
#define ACCRUAL_END_DATE
#define ACCRUAL_START_DATE
Extended QuantLib flow analysis.
vector< vector< string > > flowAnalysis(const QuantLib::Leg &leg)
Flow Analysis.
@ data
Definition: log.hpp:77
Matrix generator(const Matrix &t, const Real horizon)
void reset(const ASTNodePtr root)
Definition: astresetter.cpp:44
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
Serializable Credit Default Swap.
Definition: namespaces.docs:23
string conversion utilities