QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
thirty360.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
5 Copyright (C) 2018 Alexey Indiryakov
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
22#include <algorithm>
23
24namespace QuantLib {
25
26 namespace {
27
28 bool isLastOfFebruary(Day d, Month m, Year y) {
29 return m == 2 && d == 28 + (Date::isLeap(y) ? 1 : 0);
30 }
31
32 }
33
34 ext::shared_ptr<DayCounter::Impl>
36 switch (c) {
37 case USA:
38 return ext::shared_ptr<DayCounter::Impl>(new US_Impl);
39 case European:
40 case EurobondBasis:
41 return ext::shared_ptr<DayCounter::Impl>(new EU_Impl);
42 case Italian:
43 return ext::shared_ptr<DayCounter::Impl>(new IT_Impl);
44 case ISMA:
45 case BondBasis:
46 return ext::shared_ptr<DayCounter::Impl>(new ISMA_Impl);
47 case ISDA:
48 case German:
49 return ext::shared_ptr<DayCounter::Impl>(new ISDA_Impl(terminationDate));
50 case NASD:
51 return ext::shared_ptr<DayCounter::Impl>(new NASD_Impl);
52 default:
53 QL_FAIL("unknown 30/360 convention");
54 }
55 }
56
58 const Date& d2) const {
59 Day dd1 = d1.dayOfMonth(), dd2 = d2.dayOfMonth();
60 Month mm1 = d1.month(), mm2 = d2.month();
61 Year yy1 = d1.year(), yy2 = d2.year();
62
63 if (dd1 == 31) { dd1 = 30; }
64 if (dd2 == 31 && dd1 >= 30) { dd2 = 30; }
65
66 if (isLastOfFebruary(dd2, mm2, yy2) && isLastOfFebruary(dd1, mm1, yy1)) { dd2 = 30; }
67 if (isLastOfFebruary(dd1, mm1, yy1)) { dd1 = 30; }
68
69 return 360*(yy2-yy1) + 30*(mm2-mm1) + (dd2-dd1);
70 }
71
73 const Date& d2) const {
74 Day dd1 = d1.dayOfMonth(), dd2 = d2.dayOfMonth();
75 Month mm1 = d1.month(), mm2 = d2.month();
76 Year yy1 = d1.year(), yy2 = d2.year();
77
78 if (dd1 == 31) { dd1 = 30; }
79 if (dd2 == 31 && dd1 == 30) { dd2 = 30; }
80
81 return 360*(yy2-yy1) + 30*(mm2-mm1) + (dd2-dd1);
82 }
83
85 const Date& d2) const {
86 Day dd1 = d1.dayOfMonth(), dd2 = d2.dayOfMonth();
87 Month mm1 = d1.month(), mm2 = d2.month();
88 Year yy1 = d1.year(), yy2 = d2.year();
89
90 if (dd1 == 31) { dd1 = 30; }
91 if (dd2 == 31) { dd2 = 30; }
92
93 return 360*(yy2-yy1) + 30*(mm2-mm1) + (dd2-dd1);
94 }
95
97 const Date& d2) const {
98 Day dd1 = d1.dayOfMonth(), dd2 = d2.dayOfMonth();
99 Month mm1 = d1.month(), mm2 = d2.month();
100 Year yy1 = d1.year(), yy2 = d2.year();
101
102 if (dd1 == 31) { dd1 = 30; }
103 if (dd2 == 31) { dd2 = 30; }
104
105 if (mm1 == 2 && dd1 > 27) { dd1 = 30; }
106 if (mm2 == 2 && dd2 > 27) { dd2 = 30; }
107
108 return 360*(yy2-yy1) + 30*(mm2-mm1) + (dd2-dd1);
109 }
110
112 const Date& d2) const {
113 Day dd1 = d1.dayOfMonth(), dd2 = d2.dayOfMonth();
114 Month mm1 = d1.month(), mm2 = d2.month();
115 Year yy1 = d1.year(), yy2 = d2.year();
116
117 if (dd1 == 31) { dd1 = 30; }
118 if (dd2 == 31) { dd2 = 30; }
119
120 if (isLastOfFebruary(dd1, mm1, yy1)) { dd1 = 30; }
121
122 if (d2 != terminationDate_ && isLastOfFebruary(dd2, mm2, yy2)) { dd2 = 30; }
123
124 return 360*(yy2-yy1) + 30*(mm2-mm1) + (dd2-dd1);
125 }
126
128 const Date& d2) const {
129 Day dd1 = d1.dayOfMonth(), dd2 = d2.dayOfMonth();
130 Integer mm1 = d1.month(), mm2 = d2.month();
131 Year yy1 = d1.year(), yy2 = d2.year();
132
133 if (dd1 == 31) { dd1 = 30; }
134 if (dd2 == 31 && dd1 >= 30) { dd2 = 30; }
135 if (dd2 == 31 && dd1 < 30) { dd2 = 1; mm2++; }
136
137 return 360*(yy2-yy1) + 30*(mm2-mm1) + (dd2-dd1);
138 }
139
140}
Concrete date class.
Definition: date.hpp:125
Month month() const
Definition: date.cpp:82
Year year() const
Definition: date.cpp:93
Day dayOfMonth() const
Definition: date.hpp:400
std::int_fast32_t serial_type
serial number type
Definition: date.hpp:128
static bool isLeap(Year y)
whether the given year is a leap one
Definition: date.cpp:187
Date::serial_type dayCount(const Date &d1, const Date &d2) const override
to be overloaded by more complex day counters
Definition: thirty360.cpp:84
Date::serial_type dayCount(const Date &d1, const Date &d2) const override
to be overloaded by more complex day counters
Definition: thirty360.cpp:111
Date::serial_type dayCount(const Date &d1, const Date &d2) const override
to be overloaded by more complex day counters
Definition: thirty360.cpp:72
Date::serial_type dayCount(const Date &d1, const Date &d2) const override
to be overloaded by more complex day counters
Definition: thirty360.cpp:96
Date::serial_type dayCount(const Date &d1, const Date &d2) const override
to be overloaded by more complex day counters
Definition: thirty360.cpp:127
Date::serial_type dayCount(const Date &d1, const Date &d2) const override
to be overloaded by more complex day counters
Definition: thirty360.cpp:57
static ext::shared_ptr< DayCounter::Impl > implementation(Convention c, const Date &terminationDate)
Definition: thirty360.cpp:35
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Date d
Integer Year
Year number.
Definition: date.hpp:87
Integer Day
Day number.
Definition: date.hpp:53
Month
Month names.
Definition: date.hpp:57
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Definition: any.hpp:35
30/360 day counters