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

Class for reading historical scenarios from a csv file. More...

#include <orea/scenario/historicalscenariofilereader.hpp>

+ Inheritance diagram for HistoricalScenarioFileReader:
+ Collaboration diagram for HistoricalScenarioFileReader:

Public Member Functions

 HistoricalScenarioFileReader (const std::string &fileName, const QuantLib::ext::shared_ptr< ScenarioFactory > &scenarioFactory)
 
 ~HistoricalScenarioFileReader () override
 Destructor. More...
 
bool next () override
 Return true if there is another Scenario to read and move to it. More...
 
QuantLib::Date date () const override
 Return the current scenario's date if reader is still valid and Null<Date>() otherwise. More...
 
QuantLib::ext::shared_ptr< Scenarioscenario () const override
 Return the current scenario if reader is still valid and nullptr otherwise. More...
 
- Public Member Functions inherited from HistoricalScenarioReader
virtual ~HistoricalScenarioReader ()
 Destructor. More...
 
virtual bool next ()=0
 Return true if there is another Scenario to read and move to it. More...
 
virtual QuantLib::Date date () const =0
 Return the current scenario's date if reader is still valid and Null<Date>() otherwise. More...
 
virtual QuantLib::ext::shared_ptr< ore::analytics::Scenarioscenario () const =0
 Return the current scenario if reader is still valid and nullptr otherwise. More...
 
virtual void load (const QuantLib::ext::shared_ptr< ore::analytics::ScenarioSimMarketParameters > &simParams, const QuantLib::ext::shared_ptr< ore::data::TodaysMarketParameters > &marketParams)
 

Private Attributes

QuantLib::ext::shared_ptr< ScenarioFactoryscenarioFactory_
 Scenario factory. More...
 
ore::data::CSVFileReader file_
 Handle on the csv file. More...
 
std::vector< RiskFactorKeykeys_
 The risk factor keys of the scenarios in the file. More...
 
bool finished_
 Flag indicating if the reader has no more scenarios to read. More...
 

Detailed Description

Class for reading historical scenarios from a csv file.

Definition at line 37 of file historicalscenariofilereader.hpp.

Constructor & Destructor Documentation

◆ HistoricalScenarioFileReader()

HistoricalScenarioFileReader ( const std::string &  fileName,
const QuantLib::ext::shared_ptr< ScenarioFactory > &  scenarioFactory 
)

Constructor where filename gives the path to the file from which to read the scenarios and scenarioFactory is a factory for building Scenarios

Definition at line 34 of file historicalscenariofilereader.cpp.

36 : scenarioFactory_(scenarioFactory), file_(fileName, true), finished_(false) {
37
38 // Do some checks
39 QL_REQUIRE(file_.fields().size() >= 4, "Need at least 4 columns in the file " << fileName);
40 QL_REQUIRE(file_.fields()[0] == "Date", "First column must be 'Date' in the file " << fileName);
41 QL_REQUIRE(file_.fields()[1] == "Scenario", "Second column should be 'Scenario' in the file " << fileName);
42 QL_REQUIRE(file_.fields()[2] == "Numeraire", "Third column should be 'Numeraire' in the file " << fileName);
43
44 // Populate the risk factor keys vector
45 keys_.reserve(file_.fields().size() - 3);
46 for (Size k = 3; k < file_.fields().size(); ++k) {
47 keys_.push_back(parseRiskFactorKey(file_.fields()[k]));
48 }
49}
ore::data::CSVFileReader file_
Handle on the csv file.
bool finished_
Flag indicating if the reader has no more scenarios to read.
std::vector< RiskFactorKey > keys_
The risk factor keys of the scenarios in the file.
QuantLib::ext::shared_ptr< ScenarioFactory > scenarioFactory_
Scenario factory.
const std::vector< std::string > & fields() const
RiskFactorKey parseRiskFactorKey(const string &str)
Definition: scenario.cpp:183
Size size(const ValueType &v)
+ Here is the call graph for this function:

◆ ~HistoricalScenarioFileReader()

Destructor.

Definition at line 51 of file historicalscenariofilereader.cpp.

51 {
52 // Close the file
53 file_.close();
54 LOG("The file has been closed");
55}
void close() override
#define LOG(text)
+ Here is the call graph for this function:

Member Function Documentation

◆ next()

bool next ( )
overridevirtual

Return true if there is another Scenario to read and move to it.

Implements HistoricalScenarioReader.

Definition at line 57 of file historicalscenariofilereader.cpp.

57 {
58 finished_ = file_.next() ? false : true;
59 return !finished_;
60}
+ Here is the call graph for this function:

◆ date()

Date date ( ) const
overridevirtual

Return the current scenario's date if reader is still valid and Null<Date>() otherwise.

Implements HistoricalScenarioReader.

Definition at line 62 of file historicalscenariofilereader.cpp.

62 {
63 if (finished_) {
64 return Null<Date>();
65 } else {
66 return parseDate(file_.get("Date"));
67 }
68}
std::string get(const std::string &field) const
Date parseDate(const string &s)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ scenario()

QuantLib::ext::shared_ptr< ore::analytics::Scenario > scenario ( ) const
overridevirtual

Return the current scenario if reader is still valid and nullptr otherwise.

Implements HistoricalScenarioReader.

Definition at line 70 of file historicalscenariofilereader.cpp.

70 {
71 if (finished_) {
72 return nullptr;
73 } else {
74 Date date = parseDate(file_.get("Date"));
75 Real numeraire = parseReal(file_.get("Numeraire"));
76 TLOG("Creating scenario for date " << io::iso_date(date));
77 QuantLib::ext::shared_ptr<Scenario> scenario =
78 scenarioFactory_->buildScenario(date, true, std::string(), numeraire);
79 Real value;
80 for (Size k = 0; k < keys_.size(); ++k) {
81 if (ore::data::tryParseReal(file_.get(k + 3), value))
82 scenario->add(keys_[k], value);
83 }
84 return scenario;
85 }
86}
QuantLib::ext::shared_ptr< Scenario > scenario() const override
Return the current scenario if reader is still valid and nullptr otherwise.
QuantLib::Date date() const override
Return the current scenario's date if reader is still valid and Null<Date>() otherwise.
SafeStack< ValueType > value
bool tryParseReal(const string &s, QuantLib::Real &result)
Real parseReal(const string &s)
#define TLOG(text)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ scenarioFactory_

QuantLib::ext::shared_ptr<ScenarioFactory> scenarioFactory_
private

Scenario factory.

Definition at line 55 of file historicalscenariofilereader.hpp.

◆ file_

ore::data::CSVFileReader file_
private

Handle on the csv file.

Definition at line 57 of file historicalscenariofilereader.hpp.

◆ keys_

std::vector<RiskFactorKey> keys_
private

The risk factor keys of the scenarios in the file.

Definition at line 59 of file historicalscenariofilereader.hpp.

◆ finished_

bool finished_
private

Flag indicating if the reader has no more scenarios to read.

Definition at line 61 of file historicalscenariofilereader.hpp.