Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
marketdatacsvloader.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2022 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
21
22using namespace ore::data;
24
25namespace ore {
26namespace analytics {
27
28void MarketDataCsvLoaderImpl::loadCorporateActionData(QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader,
29 const map<string, string>& equities) {
30 for (const auto& div : csvLoader_->loadDividends()) {
31 for (const auto& it : equities) {
32 if (div.name == it.second)
33 loader->addDividend(div);
34 }
35 }
36}
37
39 const QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader,
40 const map<Date, set<string>>& quotes,
41 const Date& requestDate) {
42 // load csvLoader quotes and add to loader if valid
43 // doesn't currently handle Lagged or Mpor Date
44 if (inputs_->entireMarket()) {
45 for (const auto& md : csvLoader_->loadQuotes(inputs_->asof()))
46 loader->add(inputs_->asof(), md->name(), md->quote()->value());
47 } else {
48 for (const auto& qd : quotes) {
49 auto d = qd.first;
50 for (const auto& q : qd.second) {
51 Wildcard wc(q);
52 if (!wc.hasWildcard()) {
53 // if we don't have it, it's probably optional, just leave it out for now
54 if (csvLoader_->has(q, d)) {
55 QuantLib::ext::shared_ptr<MarketDatum> datum = csvLoader_->get(q, d);
56 loader->add(d, datum->name(), datum->quote()->value());
57 } else {
58 LOG("Requested quote " << q << " for date " << d
59 << " not in csv file. This is not necessarily an error.");
60 }
61
62 } else {
63 // a bit messy, we loop over all quotes each time
64 for (auto md : csvLoader_->loadQuotes(d)) {
65 if (wc.matches(md->name()))
66 loader->add(d, md->name(), md->quote()->value());
67 }
68 }
69 }
70 }
71 }
72}
73
74void MarketDataCsvLoaderImpl::retrieveFixings(const QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader,
76 map<pair<string, Date>, set<Date>> lastAvailableFixingLookupMap) {
77
78 LOG("MarketDataCsvLoader::retrieveFixings called: all fixings ? " << (inputs_->allFixings() ? "Y" : "N"));
79
80 if (inputs_->allFixings()) {
81 for (const auto& f : csvLoader_->loadFixings()) {
82 loader->addFixing(f.date, f.name, f.fixing);
83 //DLOG("add fixing for " << f.name << " fixing date " << io::iso_date(f.date));
84 }
85 } else {
86 // Filter by required fixing data
87
88 // Loop over the relevant fixings and add only them to the InMemoryLoader
89 for (const auto& [name, fixingDates] : fixings) {
90 // LOG("fixings are required for index " << kv.first << " and " << kv.second.size() << " dates");
91 // map<string, set<Date>>
92 for (const auto& [date, mandatory] : fixingDates) {
93 // lazy loop over all of them...
94 for (const auto& fix : csvLoader_->loadFixings()) {
95 if (fix.name == name && fix.date == date) {
96 // add it to the inMemory
97 loader->addFixing(fix.date, fix.name, fix.fixing);
98 //DLOG("add fixing for " << fix.name << " as of " << io::iso_date(fix.date));
99 }
100 }
101 }
102 }
103 }
104
105 for (const auto& fp : lastAvailableFixingLookupMap) {
106 if (loader->getFixing(fp.first.first, fp.first.second).empty()) {
107 set<Date>::reverse_iterator rit;
108 for (rit = fp.second.rbegin(); rit != fp.second.rend(); rit++) {
109 auto f = loader->getFixing(fp.first.first, *rit);
110 if (!f.empty()) {
111 loader->addFixing(fp.first.second, fp.first.first, f.fixing);
112 break;
113 }
114 }
115 WLOG("MarketDataCsvLoader::retrieveFixings(::load Could not find fixing for id " << fp.first.first << " on date "
116 << fp.first.second << ". ");
117 }
118 }
119}
120
121} // namespace analytics
122} // namespace ore
void retrieveMarketData(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const ore::analytics::QuoteMap &quotes, const QuantLib::Date &requestDate=QuantLib::Date()) override
retrieve market data
void retrieveFixings(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, ore::analytics::FixingMap fixings={}, std::map< std::pair< std::string, QuantLib::Date >, std::set< QuantLib::Date > > lastAvailableFixingLookupMap={}) override
retrieve fixings
void loadCorporateActionData(QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const std::map< std::string, std::string > &equities) override
load corporate action data
QuantLib::ext::shared_ptr< ore::data::CSVLoader > csvLoader_
QuantLib::ext::shared_ptr< InputParameters > inputs_
bool hasWildcard() const
bool matches(const std::string &s) const
#define LOG(text)
#define WLOG(text)
Market Data Loader.
std::map< std::string, RequiredFixings::FixingDates > FixingMap
string name