QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
actual365fixed.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) 2013 BGC Partners L.P.
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 <cmath>
23
24namespace QuantLib {
25
26 ext::shared_ptr<DayCounter::Impl>
28 switch (c) {
29 case Standard:
30 return ext::shared_ptr<DayCounter::Impl>(new Impl);
31 case Canadian:
32 return ext::shared_ptr<DayCounter::Impl>(new CA_Impl);
33 case NoLeap:
34 return ext::shared_ptr<DayCounter::Impl>(new NL_Impl);
35 default:
36 QL_FAIL("unknown Actual/365 (Fixed) convention");
37 }
38 }
39
41 const Date& d2,
42 const Date& refPeriodStart,
43 const Date& refPeriodEnd) const {
44 if (d1 == d2)
45 return 0.0;
46
47 // We need the period to calculate the frequency
48 QL_REQUIRE(refPeriodStart != Date(), "invalid refPeriodStart");
49 QL_REQUIRE(refPeriodEnd != Date(), "invalid refPeriodEnd");
50
51 Time dcs = daysBetween(d1,d2);
52 Time dcc = daysBetween(refPeriodStart,refPeriodEnd);
53 auto months = Integer(std::lround(12 * dcc / 365));
54 QL_REQUIRE(months != 0,
55 "invalid reference period for Act/365 Canadian; "
56 "must be longer than a month");
57 auto frequency = Integer(12 / months);
58
59 if (dcs < Integer(365/frequency))
60 return dcs/365.0;
61
62 return 1./frequency - (dcc-dcs)/365.0;
63
64 }
65
67 const Date& d2) const {
68
69 static const Integer MonthOffset[] = {
70 0, 31, 59, 90, 120, 151, // Jan - Jun
71 181, 212, 243, 273, 304, 334 // Jun - Dec
72 };
73
75 + MonthOffset[d1.month()-1] + (d1.year() * 365);
77 + MonthOffset[d2.month()-1] + (d2.year() * 365);
78
79 if (d1.month() == Feb && d1.dayOfMonth() == 29) {
80 --s1;
81 }
82
83 if (d2.month() == Feb && d2.dayOfMonth() == 29) {
84 --s2;
85 }
86
87 return s2 - s1;
88 }
89
91 const Date& d2,
92 const Date& d3,
93 const Date& d4) const {
94 return dayCount(d1, d2)/365.0;
95 }
96
97}
98
Actual/365 (Fixed) day counter.
Time yearFraction(const Date &d1, const Date &d2, const Date &refPeriodStart, const Date &refPeriodEnd) const override
Date::serial_type dayCount(const Date &d1, const Date &d2) const override
to be overloaded by more complex day counters
Time yearFraction(const Date &d1, const Date &d2, const Date &refPeriodStart, const Date &refPeriodEnd) const override
static ext::shared_ptr< DayCounter::Impl > implementation(Convention)
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
Date::serial_type dayCount(const Date &, const Date &) const
Returns the number of days between two dates.
Definition: daycounter.hpp:122
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
@ Feb
Definition: date.hpp:70
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Definition: any.hpp:35
Real months(const Period &p)
Definition: period.cpp:296
Time daysBetween(const Date &d1, const Date &d2)
Definition: date.hpp:442