25#include <boost/algorithm/string.hpp>
39 Date fixingCutOffDate)
40 :
CSVLoader(marketFilename, fixingFilename,
"", implyTodaysFixings, fixingCutOffDate) {}
42CSVLoader::CSVLoader(
const vector<string>& marketFiles,
const vector<string>& fixingFiles,
bool implyTodaysFixings, Date fixingCutOffDate)
43 :
CSVLoader(marketFiles, fixingFiles, {}, implyTodaysFixings, fixingCutOffDate) {}
45CSVLoader::CSVLoader(
const string& marketFilename,
const string& fixingFilename,
const string& dividendFilename,
46 bool implyTodaysFixings, Date fixingCutOffDate)
47 : implyTodaysFixings_(implyTodaysFixings), fixingCutOffDate_(fixingCutOffDate) {
52 for (
auto it :
data_) {
53 LOG(
"CSVLoader loaded " << it.second.size() <<
" market data points for " << it.first);
58 LOG(
"CSVLoader loaded " <<
fixings_.size() <<
" fixings");
61 if (dividendFilename !=
"") {
63 LOG(
"CSVLoader loaded " <<
dividends_.size() <<
" dividends");
66 LOG(
"CSVLoader complete.");
70 const vector<string>& dividendFiles,
bool implyTodaysFixings,
71 Date fixingCutOffDate)
72 : implyTodaysFixings_(implyTodaysFixings), fixingCutOffDate_(fixingCutOffDate) {
74 for (
auto marketFile : marketFiles)
80 LOG(
"CSVLoader loaded " << it.second.size() <<
" market data points for " << it.first);
82 for (
auto fixingFile : fixingFiles)
85 LOG(
"CSVLoader loaded " <<
fixings_.size() <<
" fixings");
87 for (
auto dividendFile : dividendFiles)
90 LOG(
"CSVLoader loaded " <<
dividends_.size() <<
" dividends");
92 LOG(
"CSVLoader complete.");
101 LOG(
"CSVLoader loading from " << filename);
103 Date today = QuantLib::Settings::instance().evaluationDate();
106 file.open(filename.c_str());
107 QL_REQUIRE(file.is_open(),
"error opening file " << filename);
109 while (!file.eof()) {
114 if (line.size() > 0 && line[0] !=
'#') {
116 vector<string> tokens;
118 boost::split(tokens, line, boost::is_any_of(
",;\t "), boost::token_compress_on);
121 QL_REQUIRE(tokens.size() == 3 || tokens.size() == 4,
"Invalid CSVLoader line, 3 tokens expected " << line);
122 if (tokens.size() == 4)
123 QL_REQUIRE(dataType ==
DataType::Dividend,
"CSVLoader, dataType must be of type Dividend");
125 const string& key = tokens[1];
132 QuantLib::ext::shared_ptr<MarketDatum> md;
135 }
catch (std::exception& e) {
136 WLOG(
"Failed to parse MarketDatum " << key <<
": " << e.what());
139 std::pair<bool, string> addFX = {
true,
""};
143 if (!addFX.second.empty()) {
145 TLOG(
"Replacing MarketDatum " << addFX.second <<
" with " << key
146 <<
" due to FX Dominance.");
147 if (it2 !=
data_[date].end())
148 data_[date].erase(it2);
151 if (addFX.first &&
data_[date].insert(md).second) {
152 LOG(
"Added MarketDatum " << key);
153 }
else if (!addFX.first) {
154 LOG(
"Skipped MarketDatum " << key <<
" - dominant FX already present.")
157 LOG(
"Skipped MarketDatum " << key <<
" - this is already present.");
160 }
catch (std::exception& e) {
161 WLOG(
"Failed to parse MarketDatum " << key <<
": " << e.what());
168 WLOG(
"Skipped Fixing " << key <<
"@" << QuantLib::io::iso_date(date)
169 <<
" - this is already present.");
174 if (tokens.size() == 4)
179 WLOG(
"Skipped Dividend " << key <<
"@" << QuantLib::io::iso_date(date)
180 <<
" - this is already present.");
184 QL_FAIL(
"unknown data type");
189 LOG(
"CSVLoader completed processing " << filename);
193 auto it =
data_.find(d);
194 if (it ==
data_.end())
196 return std::vector<QuantLib::ext::shared_ptr<MarketDatum>>(it->second.begin(), it->second.end());
199QuantLib::ext::shared_ptr<MarketDatum>
CSVLoader::get(
const string&
name,
const QuantLib::Date& d)
const {
200 auto it =
data_.find(d);
201 QL_REQUIRE(it !=
data_.end(),
"No datum for " <<
name <<
" on date " << d);
203 QL_REQUIRE(it2 != it->second.end(),
"No datum for " <<
name <<
" on date " << d);
207std::set<QuantLib::ext::shared_ptr<MarketDatum>>
CSVLoader::get(
const std::set<std::string>& names,
208 const QuantLib::Date& asof)
const {
209 auto it =
data_.find(asof);
210 if(it ==
data_.end())
212 std::set<QuantLib::ext::shared_ptr<MarketDatum>> result;
213 for (
auto const& n : names) {
215 if (it2 != it->second.end())
222 const QuantLib::Date& asof)
const {
231 auto it =
data_.find(asof);
232 if (it ==
data_.end())
234 std::set<QuantLib::ext::shared_ptr<MarketDatum>> result;
235 std::set<QuantLib::ext::shared_ptr<MarketDatum>>::iterator it1, it2;
238 it1 = it->second.begin();
239 it2 = it->second.end();
246 for (
auto it = it1; it != it2; ++it) {
Utility class for loading market quotes and fixings from a file.
std::set< QuantExt::Dividend > dividends_
std::set< Fixing > fixings_
QuantLib::ext::shared_ptr< MarketDatum > get(const string &name, const QuantLib::Date &d) const override
get quote by its unique name, throws if not existent, override in derived classes for performance
std::vector< QuantLib::ext::shared_ptr< MarketDatum > > loadQuotes(const QuantLib::Date &) const override
get all quotes, TODO change the return value to std::set
std::map< QuantLib::Date, std::set< QuantLib::ext::shared_ptr< MarketDatum >, SharedPtrMarketDatumComparator > > data_
void loadFile(const string &, DataType)
std::pair< bool, string > checkFxDuplicate(const ext::shared_ptr< MarketDatum >, const QuantLib::Date &)
const std::string & pattern() const
bool matches(const std::string &s) const
std::size_t wildcardPos() const
SafeStack< ValueType > value
Market Datum Loader Implementation.
QuantLib::ext::shared_ptr< MarketDatum > parseMarketDatum(const Date &asof, const string &datumName, const Real &value)
Function to parse a market datum.
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Real parseReal(const string &s)
Convert text to Real.
Classes and functions for log message handling.
#define LOG(text)
Logging Macro (Level = Notice)
#define WLOG(text)
Logging Macro (Level = Warning)
#define TLOG(text)
Logging Macro (Level = Data)
QuantLib::ext::shared_ptr< MarketDatum > makeDummyMarketDatum(const Date &d, const std::string &name)
Serializable Credit Default Swap.
Map text representations to QuantLib/QuantExt types.