Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
loader.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
20
21namespace ore {
22namespace data {
23
24QuantLib::ext::shared_ptr<MarketDatum> Loader::get(const std::string& name, const QuantLib::Date& d) const {
25 for (const auto& md : loadQuotes(d)) {
26 if (md->name() == name)
27 return md;
28 }
29 QL_FAIL("No MarketDatum for name " << name << " and date " << d);
30}
31
32std::set<QuantLib::ext::shared_ptr<MarketDatum>> Loader::get(const std::set<std::string>& names,
33 const QuantLib::Date& asof) const {
34 std::set<QuantLib::ext::shared_ptr<MarketDatum>> result;
35 for (auto const& md : loadQuotes(asof)) {
36 if (names.find(md->name()) != names.end()) {
37 result.insert(md);
38 }
39 }
40 return result;
41}
42
43std::set<QuantLib::ext::shared_ptr<MarketDatum>> Loader::get(const Wildcard& wildcard, const QuantLib::Date& asof) const {
44 std::set<QuantLib::ext::shared_ptr<MarketDatum>> result;
45 for (auto const& md : loadQuotes(asof)) {
46 if (wildcard.matches(md->name())) {
47 result.insert(md);
48 }
49 }
50 return result;
51}
52
53bool Loader::has(const std::string& name, const QuantLib::Date& d) const {
54 try {
55 return get(name, d) != nullptr;
56 } catch (...) {
57 return false;
58 }
59}
60
61bool Loader::hasQuotes(const QuantLib::Date& d) const {
62 try {
63 return !loadQuotes(d).empty();
64 } catch (...) {
65 return false;
66 }
67}
68
69QuantLib::ext::shared_ptr<MarketDatum> Loader::get(const std::pair<std::string, bool>& name, const QuantLib::Date& d) const {
70 if (has(name.first, d)) {
71 return get(name.first, d);
72 } else {
73 const Date& originalDate = actualDate() == Null<Date>() ? d : actualDate();
74 if (name.second) {
75 DLOG("Could not find quote for ID " << name.first << " with as of date " << QuantLib::io::iso_date(originalDate)
76 << ".");
77 return QuantLib::ext::shared_ptr<MarketDatum>();
78 } else {
79 QL_FAIL("Could not find quote for Mandatory ID " << name.first << " with as of date "
80 << QuantLib::io::iso_date(originalDate));
81 }
82 }
83}
84
85std::pair<bool, string> Loader::checkFxDuplicate(const ext::shared_ptr<MarketDatum> md, const QuantLib::Date& d) {
86 string cc1 = ext::dynamic_pointer_cast<FXSpotQuote>(md)->unitCcy();
87 string cc2 = ext::dynamic_pointer_cast<FXSpotQuote>(md)->ccy();
88 string tmp = "FX/RATE/" + cc2 + "/" + cc1;
89 if (Loader::has(tmp, d)) {
90 string dom = fxDominance(cc1, cc2);
91 if (dom == (cc1 + cc2)) {
92 return {true, tmp};
93 } else {
94 return {false, ""};
95 }
96 }
97 return {true, ""};
98}
99
100bool Loader::hasFixing(const string& name, const QuantLib::Date& d) const {
101 try {
102 return !getFixing(name, d).empty();
103 } catch (...) {
104 return false;
105 }
106}
107
108Fixing Loader::getFixing(const string& name, const QuantLib::Date& d) const {
109 Fixing fixing;
110 for (auto const& f : loadFixings()) {
111 if (f.name == name && f.date == d) {
112 fixing = f;
113 }
114 }
115 return fixing;
116}
117
118std::set<QuantExt::Dividend> Loader::loadDividends() const { return {}; }
119
120} // namespace data
121} // namespace ore
virtual std::vector< QuantLib::ext::shared_ptr< MarketDatum > > loadQuotes(const QuantLib::Date &) const =0
get all quotes, TODO change the return value to std::set
virtual Fixing getFixing(const string &name, const QuantLib::Date &d) const
Default implementation for getFixing.
Definition: loader.cpp:108
virtual QuantLib::ext::shared_ptr< MarketDatum > get(const std::string &name, const QuantLib::Date &d) const
get quote by its unique name, throws if not existent, override in derived classes for performance
Definition: loader.cpp:24
virtual bool hasQuotes(const QuantLib::Date &d) const
check if there are quotes for a date
Definition: loader.cpp:61
std::pair< bool, string > checkFxDuplicate(const ext::shared_ptr< MarketDatum >, const QuantLib::Date &)
Definition: loader.cpp:85
virtual std::set< Fixing > loadFixings() const =0
const Date & actualDate() const
Definition: loader.hpp:95
virtual bool has(const std::string &name, const QuantLib::Date &d) const
Default implementation, returns false if get throws or returns a null pointer.
Definition: loader.cpp:53
virtual bool hasFixing(const string &name, const QuantLib::Date &d) const
Definition: loader.cpp:100
virtual std::set< QuantExt::Dividend > loadDividends() const
Optional load dividends method.
Definition: loader.cpp:118
bool matches(const std::string &s) const
Definition: wildcard.cpp:68
string fxDominance(const string &s1, const string &s2)
Convert FX pair to market standard dominance.
Definition: parsers.cpp:1296
Market Datum Loader Interface.
@ data
Definition: log.hpp:77
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Fixing data structure.
Definition: fixings.hpp:44
string name