Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
CSVLoader Class Reference

Utility class for loading market quotes and fixings from a file. More...

#include <ored/marketdata/csvloader.hpp>

+ Inheritance diagram for CSVLoader:
+ Collaboration diagram for CSVLoader:

Public Member Functions

 CSVLoader ()
 Constructor. More...
 
 CSVLoader (const string &marketFilename, const string &fixingFilename, bool implyTodaysFixings=false, Date fixingCutOffDate=Date())
 
 CSVLoader (const vector< string > &marketFiles, const vector< string > &fixingFiles, bool implyTodaysFixings=false, Date fixingCutOffDate=Date())
 
 CSVLoader (const string &marketFilename, const string &fixingFilename, const string &dividendFilename, bool implyTodaysFixings=false, Date fixingCutOffDate=Date())
 
 CSVLoader (const vector< string > &marketFiles, const vector< string > &fixingFiles, const vector< string > &dividendFiles, bool implyTodaysFixings=false, Date fixingCutOffDate=Date())
 
std::vector< QuantLib::ext::shared_ptr< MarketDatum > > loadQuotes (const QuantLib::Date &) const override
 get all quotes, TODO change the return value to std::set More...
 
QuantLib::ext::shared_ptr< MarketDatumget (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 More...
 
std::set< QuantLib::ext::shared_ptr< MarketDatum > > get (const std::set< std::string > &names, const QuantLib::Date &asof) const override
 get quotes matching a set of names, this should be overridden in derived classes for performance More...
 
std::set< QuantLib::ext::shared_ptr< MarketDatum > > get (const Wildcard &wildcard, const QuantLib::Date &asof) const override
 get quotes matching a wildcard More...
 
std::set< FixingloadFixings () const override
 Load fixings. More...
 
std::set< QuantExt::DividendloadDividends () const override
 Load dividends. More...
 
- Public Member Functions inherited from Loader
virtual ~Loader ()
 
virtual bool has (const std::string &name, const QuantLib::Date &d) const
 Default implementation, returns false if get throws or returns a null pointer. More...
 
virtual bool hasQuotes (const QuantLib::Date &d) const
 check if there are quotes for a date More...
 
virtual QuantLib::ext::shared_ptr< MarketDatumget (const std::pair< std::string, bool > &name, const QuantLib::Date &d) const
 
virtual bool hasFixing (const string &name, const QuantLib::Date &d) const
 
virtual Fixing getFixing (const string &name, const QuantLib::Date &d) const
 Default implementation for getFixing. More...
 
void setActualDate (const QuantLib::Date &d)
 
const Date & actualDate () const
 
std::pair< bool, string > checkFxDuplicate (const ext::shared_ptr< MarketDatum >, const QuantLib::Date &)
 

Private Types

enum class  DataType { Market , Fixing , Dividend }
 

Private Member Functions

void loadFile (const string &, DataType)
 

Private Attributes

bool implyTodaysFixings_
 
std::map< QuantLib::Date, std::set< QuantLib::ext::shared_ptr< MarketDatum >, SharedPtrMarketDatumComparator > > data_
 
std::set< Fixingfixings_
 
std::set< QuantExt::Dividenddividends_
 
Date fixingCutOffDate_
 

Additional Inherited Members

- Protected Attributes inherited from Loader
Date actualDate_ = Date()
 

Detailed Description

Utility class for loading market quotes and fixings from a file.

Data is loaded with the call to the constructor. Inspectors can be called to then retrieve quotes and fixings.

TODO implementation has large overlap with inmemoryloader.?pp, factor this out

Definition at line 41 of file csvloader.hpp.

Member Enumeration Documentation

◆ DataType

enum class DataType
strongprivate
Enumerator
Market 
Fixing 
Dividend 

Definition at line 101 of file csvloader.hpp.

Constructor & Destructor Documentation

◆ CSVLoader() [1/5]

CSVLoader ( )

Constructor.

Definition at line 44 of file csvloader.hpp.

44{}

◆ CSVLoader() [2/5]

CSVLoader ( const string &  marketFilename,
const string &  fixingFilename,
bool  implyTodaysFixings = false,
Date  fixingCutOffDate = Date() 
)
Parameters
marketFilenameQuote file name
fixingFilenameFixing file name
implyTodaysFixingsEnable/disable implying today's fixings
fixingCutOffDateLoad fixings up to this date

Definition at line 38 of file csvloader.cpp.

40 : CSVLoader(marketFilename, fixingFilename, "", implyTodaysFixings, fixingCutOffDate) {}
CSVLoader()
Constructor.
Definition: csvloader.hpp:44

◆ CSVLoader() [3/5]

CSVLoader ( const vector< string > &  marketFiles,
const vector< string > &  fixingFiles,
bool  implyTodaysFixings = false,
Date  fixingCutOffDate = Date() 
)
Parameters
marketFilesQuote file name
fixingFilesFixing file name
implyTodaysFixingsEnable/disable implying today's fixings
fixingCutOffDateLoad fixings up to this date

Definition at line 42 of file csvloader.cpp.

43 : CSVLoader(marketFiles, fixingFiles, {}, implyTodaysFixings, fixingCutOffDate) {}

◆ CSVLoader() [4/5]

CSVLoader ( const string &  marketFilename,
const string &  fixingFilename,
const string &  dividendFilename,
bool  implyTodaysFixings = false,
Date  fixingCutOffDate = Date() 
)
Parameters
marketFilenameQuote file name
fixingFilenameFixing file name
dividendFilenameDividend file name
implyTodaysFixingsEnable/disable implying today's fixings
fixingCutOffDateLoad fixings up to this date

Definition at line 45 of file csvloader.cpp.

47 : implyTodaysFixings_(implyTodaysFixings), fixingCutOffDate_(fixingCutOffDate) {
48
49 // load market data
50 loadFile(marketFilename, DataType::Market);
51 // log
52 for (auto it : data_) {
53 LOG("CSVLoader loaded " << it.second.size() << " market data points for " << it.first);
54 }
55
56 // load fixings
57 loadFile(fixingFilename, DataType::Fixing);
58 LOG("CSVLoader loaded " << fixings_.size() << " fixings");
59
60 // load dividends
61 if (dividendFilename != "") {
62 loadFile(dividendFilename, DataType::Dividend);
63 LOG("CSVLoader loaded " << dividends_.size() << " dividends");
64 }
65
66 LOG("CSVLoader complete.");
67}
std::set< QuantExt::Dividend > dividends_
Definition: csvloader.hpp:107
std::set< Fixing > fixings_
Definition: csvloader.hpp:106
std::map< QuantLib::Date, std::set< QuantLib::ext::shared_ptr< MarketDatum >, SharedPtrMarketDatumComparator > > data_
Definition: csvloader.hpp:105
void loadFile(const string &, DataType)
Definition: csvloader.cpp:100
#define LOG(text)
Logging Macro (Level = Notice)
Definition: log.hpp:552
+ Here is the call graph for this function:

◆ CSVLoader() [5/5]

CSVLoader ( const vector< string > &  marketFiles,
const vector< string > &  fixingFiles,
const vector< string > &  dividendFiles,
bool  implyTodaysFixings = false,
Date  fixingCutOffDate = Date() 
)
Parameters
marketFilesQuote file name
fixingFilesFixing file name
dividendFilesDividend file name
implyTodaysFixingsEnable/disable implying today's fixings
fixingCutOffDateLoad fixings up to this date

Definition at line 69 of file csvloader.cpp.

72 : implyTodaysFixings_(implyTodaysFixings), fixingCutOffDate_(fixingCutOffDate) {
73
74 for (auto marketFile : marketFiles)
75 // load market data
76 loadFile(marketFile, DataType::Market);
77
78 // log
79 for (auto it : data_)
80 LOG("CSVLoader loaded " << it.second.size() << " market data points for " << it.first);
81
82 for (auto fixingFile : fixingFiles)
83 // load fixings
84 loadFile(fixingFile, DataType::Fixing);
85 LOG("CSVLoader loaded " << fixings_.size() << " fixings");
86
87 for (auto dividendFile : dividendFiles)
88 // load dividends
89 loadFile(dividendFile, DataType::Dividend);
90 LOG("CSVLoader loaded " << dividends_.size() << " dividends");
91
92 LOG("CSVLoader complete.");
93}
+ Here is the call graph for this function:

Member Function Documentation

◆ loadQuotes()

vector< QuantLib::ext::shared_ptr< MarketDatum > > loadQuotes ( const QuantLib::Date &  ) const
overridevirtual

get all quotes, TODO change the return value to std::set

Implements Loader.

Definition at line 192 of file csvloader.cpp.

192 {
193 auto it = data_.find(d);
194 if (it == data_.end())
195 return {};
196 return std::vector<QuantLib::ext::shared_ptr<MarketDatum>>(it->second.begin(), it->second.end());
197}

◆ get() [1/3]

QuantLib::ext::shared_ptr< MarketDatum > get ( const string &  name,
const QuantLib::Date &  d 
) const
overridevirtual

get quote by its unique name, throws if not existent, override in derived classes for performance

Reimplemented from Loader.

Definition at line 199 of file csvloader.cpp.

199 {
200 auto it = data_.find(d);
201 QL_REQUIRE(it != data_.end(), "No datum for " << name << " on date " << d);
202 auto it2 = it->second.find(makeDummyMarketDatum(d, name));
203 QL_REQUIRE(it2 != it->second.end(), "No datum for " << name << " on date " << d);
204 return *it2;
205}
QuantLib::ext::shared_ptr< MarketDatum > makeDummyMarketDatum(const Date &d, const std::string &name)
Definition: csvloader.cpp:95
string name
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get() [2/3]

std::set< QuantLib::ext::shared_ptr< MarketDatum > > get ( const std::set< std::string > &  names,
const QuantLib::Date &  asof 
) const
overridevirtual

get quotes matching a set of names, this should be overridden in derived classes for performance

Reimplemented from Loader.

Definition at line 207 of file csvloader.cpp.

208 {
209 auto it = data_.find(asof);
210 if(it == data_.end())
211 return {};
212 std::set<QuantLib::ext::shared_ptr<MarketDatum>> result;
213 for (auto const& n : names) {
214 auto it2 = it->second.find(makeDummyMarketDatum(asof, n));
215 if (it2 != it->second.end())
216 result.insert(*it2);
217 }
218 return result;
219}
+ Here is the call graph for this function:

◆ get() [3/3]

std::set< QuantLib::ext::shared_ptr< MarketDatum > > get ( const Wildcard wildcard,
const QuantLib::Date &  asof 
) const
overridevirtual

get quotes matching a wildcard

Reimplemented from Loader.

Definition at line 221 of file csvloader.cpp.

222 {
223 if (!wildcard.hasWildcard()) {
224 // no wildcard => use get by name function
225 try {
226 return {get(wildcard.pattern(), asof)};
227 } catch (...) {
228 }
229 return {};
230 }
231 auto it = data_.find(asof);
232 if (it == data_.end())
233 return {};
234 std::set<QuantLib::ext::shared_ptr<MarketDatum>> result;
235 std::set<QuantLib::ext::shared_ptr<MarketDatum>>::iterator it1, it2;
236 if (wildcard.wildcardPos() == 0) {
237 // wildcard at first position => we have to search all of the data
238 it1 = it->second.begin();
239 it2 = it->second.end();
240 } else {
241 // search the range matching the substring of the pattern until the wildcard
242 std::string prefix = wildcard.pattern().substr(0, wildcard.wildcardPos());
243 it1 = it->second.lower_bound(makeDummyMarketDatum(asof, prefix));
244 it2 = it->second.upper_bound(makeDummyMarketDatum(asof, prefix + "\xFF"));
245 }
246 for (auto it = it1; it != it2; ++it) {
247 if (wildcard.isPrefix() || wildcard.matches((*it)->name()))
248 result.insert(*it);
249 }
250 return result;
251}
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
Definition: csvloader.cpp:199
+ Here is the call graph for this function:

◆ loadFixings()

std::set< Fixing > loadFixings ( ) const
overridevirtual

Load fixings.

Implements Loader.

Definition at line 95 of file csvloader.hpp.

95{ return fixings_; }

◆ loadDividends()

std::set< QuantExt::Dividend > loadDividends ( ) const
overridevirtual

Load dividends.

Reimplemented from Loader.

Definition at line 97 of file csvloader.hpp.

97{ return dividends_; }

◆ loadFile()

void loadFile ( const string &  filename,
DataType  dataType 
)
private

Definition at line 100 of file csvloader.cpp.

100 {
101 LOG("CSVLoader loading from " << filename);
102
103 Date today = QuantLib::Settings::instance().evaluationDate();
104
105 ifstream file;
106 file.open(filename.c_str());
107 QL_REQUIRE(file.is_open(), "error opening file " << filename);
108
109 while (!file.eof()) {
110 string line;
111 getline(file, line);
112 boost::trim(line);
113 // skip blank and comment lines
114 if (line.size() > 0 && line[0] != '#') {
115
116 vector<string> tokens;
117 boost::trim(line);
118 boost::split(tokens, line, boost::is_any_of(",;\t "), boost::token_compress_on);
119
120 // TODO: should we try, catch and log any invalid lines?
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");
124 Date date = parseDate(tokens[0]);
125 const string& key = tokens[1];
126 Real value = parseReal(tokens[2]);
127
128 if (dataType == DataType::Market) {
129 // process market
130 // build market datum and add to map
131 try {
132 QuantLib::ext::shared_ptr<MarketDatum> md;
133 try {
134 md = parseMarketDatum(date, key, value);
135 } catch (std::exception& e) {
136 WLOG("Failed to parse MarketDatum " << key << ": " << e.what());
137 }
138 if (md != nullptr) {
139 std::pair<bool, string> addFX = {true, ""};
140 if (md->instrumentType() == MarketDatum::InstrumentType::FX_SPOT &&
141 md->quoteType() == MarketDatum::QuoteType::RATE) {
142 addFX = checkFxDuplicate(md, date);
143 if (!addFX.second.empty()) {
144 auto it2 = data_[date].find(makeDummyMarketDatum(date, addFX.second));
145 TLOG("Replacing MarketDatum " << addFX.second << " with " << key
146 << " due to FX Dominance.");
147 if (it2 != data_[date].end())
148 data_[date].erase(it2);
149 }
150 }
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.")
155 }
156 else {
157 LOG("Skipped MarketDatum " << key << " - this is already present.");
158 }
159 }
160 } catch (std::exception& e) {
161 WLOG("Failed to parse MarketDatum " << key << ": " << e.what());
162 }
163 } else if (dataType == DataType::Fixing) {
164 // process fixings
165 if (date < today || (date == today && !implyTodaysFixings_)
166 || (fixingCutOffDate_ != Date() && date <= fixingCutOffDate_)) {
167 if(!fixings_.insert(Fixing(date, key, value)).second) {
168 WLOG("Skipped Fixing " << key << "@" << QuantLib::io::iso_date(date)
169 << " - this is already present.");
170 }
171 }
172 } else if (dataType == DataType::Dividend) {
173 Date payDate = date;
174 if (tokens.size() == 4)
175 payDate = parseDate(tokens[3]);
176 // process dividends
177 if (date <= today) {
178 if (!dividends_.insert(QuantExt::Dividend(date, key, value, payDate)).second) {
179 WLOG("Skipped Dividend " << key << "@" << QuantLib::io::iso_date(date)
180 << " - this is already present.");
181 }
182 }
183 } else {
184 QL_FAIL("unknown data type");
185 }
186 }
187 }
188 file.close();
189 LOG("CSVLoader completed processing " << filename);
190}
std::pair< bool, string > checkFxDuplicate(const ext::shared_ptr< MarketDatum >, const QuantLib::Date &)
Definition: loader.cpp:85
SafeStack< ValueType > value
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.
Definition: parsers.cpp:51
Real parseReal(const string &s)
Convert text to Real.
Definition: parsers.cpp:112
#define WLOG(text)
Logging Macro (Level = Warning)
Definition: log.hpp:550
#define TLOG(text)
Logging Macro (Level = Data)
Definition: log.hpp:556
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ implyTodaysFixings_

bool implyTodaysFixings_
private

Definition at line 104 of file csvloader.hpp.

◆ data_

std::map<QuantLib::Date, std::set<QuantLib::ext::shared_ptr<MarketDatum>, SharedPtrMarketDatumComparator> > data_
private

Definition at line 105 of file csvloader.hpp.

◆ fixings_

std::set<Fixing> fixings_
private

Definition at line 106 of file csvloader.hpp.

◆ dividends_

std::set<QuantExt::Dividend> dividends_
private

Definition at line 107 of file csvloader.hpp.

◆ fixingCutOffDate_

Date fixingCutOffDate_
private

Definition at line 108 of file csvloader.hpp.