Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
expiry.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 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
22
23#include <boost/archive/binary_iarchive.hpp>
24#include <boost/archive/binary_oarchive.hpp>
25
26using namespace QuantLib;
27using std::ostream;
28using std::string;
29
30namespace ore {
31namespace data {
32
33bool operator==(const Expiry& lhs, const Expiry& rhs) { return lhs.equal_to(rhs); }
34
35template <class Archive> void Expiry::serialize(Archive& ar, const unsigned int version) {}
36
38
39ExpiryDate::ExpiryDate(const Date& expiryDate) : expiryDate_(expiryDate) {}
40
41const Date& ExpiryDate::expiryDate() const { return expiryDate_; }
42
43void ExpiryDate::fromString(const string& strExpiryDate) { expiryDate_ = parseDate(strExpiryDate); }
44
45string ExpiryDate::toString() const { return to_string(expiryDate_); }
46
47bool ExpiryDate::equal_to(const Expiry& other) const {
48 if (const ExpiryDate* p = dynamic_cast<const ExpiryDate*>(&other)) {
49 return expiryDate_ == p->expiryDate();
50 } else {
51 return false;
52 }
53}
54
55template <class Archive> void ExpiryDate::serialize(Archive& ar, const unsigned int version) {
56 ar& boost::serialization::base_object<Expiry>(*this);
57 ar& expiryDate_;
58}
59
61
62ExpiryPeriod::ExpiryPeriod(const Period& expiryPeriod) : expiryPeriod_(expiryPeriod) {}
63
64const Period& ExpiryPeriod::expiryPeriod() const { return expiryPeriod_; }
65
66void ExpiryPeriod::fromString(const string& strExpiryPeriod) { expiryPeriod_ = parsePeriod(strExpiryPeriod); }
67
69
70bool ExpiryPeriod::equal_to(const Expiry& other) const {
71 if (const ExpiryPeriod* p = dynamic_cast<const ExpiryPeriod*>(&other)) {
72 return expiryPeriod_ == p->expiryPeriod();
73 } else {
74 return false;
75 }
76}
77
78template <class Archive> void ExpiryPeriod::serialize(Archive& ar, const unsigned int version) {
79 ar& boost::serialization::base_object<Expiry>(*this);
80 ar& expiryPeriod_;
81}
82
83FutureContinuationExpiry::FutureContinuationExpiry(QuantLib::Natural expiryIndex) : expiryIndex_(expiryIndex) {}
84
85QuantLib::Natural FutureContinuationExpiry::expiryIndex() const { return expiryIndex_; }
86
87void FutureContinuationExpiry::fromString(const string& strIndex) {
88 QL_REQUIRE(strIndex.size() > 1, "Future continuation expiry must have at least 2 characters");
89 QL_REQUIRE(strIndex.at(0) == 'c', "Future continuation expiry string must start with a 'c'");
90 expiryIndex_ = parseInteger(strIndex.substr(1));
91}
92
94
96 if (const FutureContinuationExpiry* p = dynamic_cast<const FutureContinuationExpiry*>(&other)) {
97 return expiryIndex_ == p->expiryIndex();
98 } else {
99 return false;
100 }
101}
102
103template <class Archive> void FutureContinuationExpiry::serialize(Archive& ar, const unsigned int version) {
104 ar& boost::serialization::base_object<Expiry>(*this);
105 ar& expiryIndex_;
106}
107
108ostream& operator<<(ostream& os, const Expiry& expiry) { return os << expiry.toString(); }
109
110QuantLib::ext::shared_ptr<Expiry> parseExpiry(const string& strExpiry) {
111
112 QL_REQUIRE(strExpiry.size() > 1, "Expiry string must have at least 2 characters");
113
114 if (strExpiry.at(0) == 'c') {
115 auto expiry = QuantLib::ext::make_shared<FutureContinuationExpiry>();
116 expiry->fromString(strExpiry);
117 return expiry;
118 } else {
119 Date date;
120 Period period;
121 bool isDate;
122 parseDateOrPeriod(strExpiry, date, period, isDate);
123 if (isDate) {
124 return QuantLib::ext::make_shared<ExpiryDate>(date);
125 } else {
126 return QuantLib::ext::make_shared<ExpiryPeriod>(period);
127 }
128 }
129}
130
131template void Expiry::serialize(boost::archive::binary_oarchive& ar, const unsigned int version);
132template void Expiry::serialize(boost::archive::binary_iarchive& ar, const unsigned int version);
133template void ExpiryDate::serialize(boost::archive::binary_oarchive& ar, const unsigned int version);
134template void ExpiryDate::serialize(boost::archive::binary_iarchive& ar, const unsigned int version);
135template void ExpiryPeriod::serialize(boost::archive::binary_oarchive& ar, const unsigned int version);
136template void ExpiryPeriod::serialize(boost::archive::binary_iarchive& ar, const unsigned int version);
137template void FutureContinuationExpiry::serialize(boost::archive::binary_oarchive& ar, const unsigned int version);
138template void FutureContinuationExpiry::serialize(boost::archive::binary_iarchive& ar, const unsigned int version);
139
140} // namespace data
141} // namespace ore
142
QuantLib::Date expiryDate_
Definition: expiry.hpp:91
const QuantLib::Date & expiryDate() const
Return the expiry date.
Definition: expiry.cpp:41
ExpiryDate()
Default constructor.
Definition: expiry.cpp:37
std::string toString() const override
Definition: expiry.cpp:45
bool equal_to(const Expiry &other) const override
Override in derived classes to compare specific expiries.
Definition: expiry.cpp:47
void fromString(const std::string &strExpiryDate) override
Definition: expiry.cpp:43
void serialize(Archive &ar, const unsigned int version)
Definition: expiry.cpp:55
virtual std::string toString() const =0
Write the Expiry object to string.
virtual bool equal_to(const Expiry &other) const =0
Override in derived classes to compare specific expiries.
void serialize(Archive &ar, const unsigned int version)
Definition: expiry.cpp:35
const QuantLib::Period & expiryPeriod() const
Return the expiry period.
Definition: expiry.cpp:64
QuantLib::Period expiryPeriod_
Definition: expiry.hpp:124
std::string toString() const override
Definition: expiry.cpp:68
bool equal_to(const Expiry &other) const override
Override in derived classes to compare specific expiries.
Definition: expiry.cpp:70
void fromString(const std::string &strExpiryPeriod) override
Definition: expiry.cpp:66
void serialize(Archive &ar, const unsigned int version)
Definition: expiry.cpp:78
ExpiryPeriod()
Default constructor.
Definition: expiry.cpp:60
void fromString(const std::string &strIndex) override
Definition: expiry.cpp:87
QuantLib::Natural expiryIndex() const
Return the future continuation expiry index.
Definition: expiry.cpp:85
QuantLib::Natural expiryIndex_
Definition: expiry.hpp:154
std::string toString() const override
Definition: expiry.cpp:93
bool equal_to(const Expiry &other) const override
Override in derived classes to compare specific expiries.
Definition: expiry.cpp:95
FutureContinuationExpiry(QuantLib::Natural expiryIndex=1)
Constructor with optional explicit future continuation index.
Definition: expiry.cpp:83
void serialize(Archive &ar, const unsigned int version)
Definition: expiry.cpp:103
Classes for representing an expiry for use in market quotes.
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Definition: parsers.cpp:51
boost::variant< QuantLib::Date, QuantLib::Period > parseDateOrPeriod(const string &s)
Convert text to QuantLib::Period or QuantLib::Date.
Definition: parsers.cpp:493
Period parsePeriod(const string &s)
Convert text to QuantLib::Period.
Definition: parsers.cpp:171
Integer parseInteger(const string &s)
Convert text to QuantLib::Integer.
Definition: parsers.cpp:136
@ data
Definition: log.hpp:77
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
bool operator==(const Dividend &d1, const Dividend &d)
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
QuantLib::ext::shared_ptr< Expiry > parseExpiry(const string &strExpiry)
Parse an Expiry from its string representation, strExpiry.
Definition: expiry.cpp:110
Serializable Credit Default Swap.
Definition: namespaces.docs:23
BOOST_CLASS_EXPORT_IMPLEMENT(ore::data::ExpiryDate)
Map text representations to QuantLib/QuantExt types.
string conversion utilities