/src/quantlib/ql/indexes/bmaindex.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2007 Roland Lichters |
5 | | |
6 | | This file is part of QuantLib, a free-software/open-source library |
7 | | for financial quantitative analysts and developers - http://quantlib.org/ |
8 | | |
9 | | QuantLib is free software: you can redistribute it and/or modify it |
10 | | under the terms of the QuantLib license. You should have received a |
11 | | copy of the license along with this program; if not, please email |
12 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
13 | | <https://www.quantlib.org/license.shtml>. |
14 | | |
15 | | This program is distributed in the hope that it will be useful, but WITHOUT |
16 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
17 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
18 | | */ |
19 | | |
20 | | #include <ql/indexes/bmaindex.hpp> |
21 | | #include <ql/currencies/america.hpp> |
22 | | #include <ql/time/calendars/unitedstates.hpp> |
23 | | #include <ql/time/daycounters/actualactual.hpp> |
24 | | |
25 | | namespace QuantLib { |
26 | | |
27 | | namespace { |
28 | | |
29 | 0 | Date previousWednesday(const Date& date) { |
30 | 0 | Weekday w = date.weekday(); |
31 | 0 | if (w >= 4) // roll back w-4 days |
32 | 0 | return date - (w - 4) * Days; |
33 | 0 | else // roll forward 4-w days and back one week |
34 | 0 | return date + (4 - w - 7) * Days; |
35 | 0 | } |
36 | | |
37 | 0 | Date nextWednesday(const Date& date) { |
38 | 0 | return previousWednesday(date+7); |
39 | 0 | } |
40 | | |
41 | | } |
42 | | |
43 | | BMAIndex::BMAIndex(Handle<YieldTermStructure> h) |
44 | 0 | : InterestRateIndex("BMA", |
45 | 0 | 1 * Weeks, |
46 | 0 | 1, |
47 | 0 | USDCurrency(), |
48 | 0 | UnitedStates(UnitedStates::GovernmentBond), |
49 | 0 | ActualActual(ActualActual::ISDA)), |
50 | 0 | termStructure_(std::move(h)) { |
51 | 0 | registerWith(termStructure_); |
52 | 0 | } |
53 | | |
54 | 0 | bool BMAIndex::isValidFixingDate(const Date& date) const { |
55 | 0 | Calendar cal = fixingCalendar(); |
56 | | // either the fixing date is last Wednesday, or all days |
57 | | // between last Wednesday included and the fixing date are |
58 | | // holidays |
59 | 0 | for (Date d = previousWednesday(date); d<date; ++d) { |
60 | 0 | if (cal.isBusinessDay(d)) |
61 | 0 | return false; |
62 | 0 | } |
63 | | // also, the fixing date itself must be a business day |
64 | 0 | return cal.isBusinessDay(date); |
65 | 0 | } |
66 | | |
67 | 0 | Handle<YieldTermStructure> BMAIndex::forwardingTermStructure() const { |
68 | 0 | return termStructure_; |
69 | 0 | } |
70 | | |
71 | 0 | Date BMAIndex::maturityDate(const Date& valueDate) const { |
72 | 0 | Calendar cal = fixingCalendar(); |
73 | 0 | Date fixingDate = cal.advance(valueDate, -1, Days); |
74 | 0 | Date nextWednesday = previousWednesday(fixingDate+7); |
75 | 0 | return cal.advance(nextWednesday, 1, Days); |
76 | 0 | } |
77 | | |
78 | 0 | Schedule BMAIndex::fixingSchedule(const Date& start, const Date& end) { |
79 | 0 | return MakeSchedule().from(previousWednesday(start)) |
80 | 0 | .to(nextWednesday(end)) |
81 | 0 | .withFrequency(Weekly) |
82 | 0 | .withCalendar(fixingCalendar()) |
83 | 0 | .withConvention(Following) |
84 | 0 | .forwards(); |
85 | 0 | } |
86 | | |
87 | 0 | Rate BMAIndex::forecastFixing(const Date& fixingDate) const { |
88 | 0 | QL_REQUIRE(!termStructure_.empty(), |
89 | 0 | "null term structure set to this instance of " << name()); |
90 | 0 | Date start = fixingCalendar().advance(fixingDate, 1, Days); |
91 | 0 | Date end = maturityDate(start); |
92 | 0 | return termStructure_->forwardRate(start, end, |
93 | 0 | dayCounter_, |
94 | 0 | Simple); |
95 | 0 | } |
96 | | |
97 | | } |