Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/indexes/interestrateindex.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2006, 2011 Ferdinando Ametrano
5
 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
6
 Copyright (C) 2003, 2004, 2005, 2006, 2007 StatPro Italia srl
7
8
 This file is part of QuantLib, a free-software/open-source library
9
 for financial quantitative analysts and developers - http://quantlib.org/
10
11
 QuantLib is free software: you can redistribute it and/or modify it
12
 under the terms of the QuantLib license.  You should have received a
13
 copy of the license along with this program; if not, please email
14
 <quantlib-dev@lists.sf.net>. The license is also available online at
15
 <http://quantlib.org/license.shtml>.
16
17
 This program is distributed in the hope that it will be useful, but WITHOUT
18
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 FOR A PARTICULAR PURPOSE.  See the license for more details.
20
*/
21
22
#include <ql/indexes/interestrateindex.hpp>
23
#include <ql/settings.hpp>
24
#include <sstream>
25
#include <utility>
26
27
namespace QuantLib {
28
29
    InterestRateIndex::InterestRateIndex(std::string familyName,
30
                                         const Period& tenor,
31
                                         Natural fixingDays,
32
                                         Currency currency,
33
                                         Calendar fixingCalendar,
34
                                         DayCounter dayCounter)
35
0
    : familyName_(std::move(familyName)), tenor_(tenor), fixingDays_(fixingDays),
36
0
      currency_(std::move(currency)), dayCounter_(std::move(dayCounter)),
37
0
      fixingCalendar_(std::move(fixingCalendar)) {
38
        // tenor_.normalize() does too much; we want to leave days alone
39
0
        if (tenor.units() == Months && tenor.length() % 12 == 0)
40
0
            tenor_ = Period(tenor.length() / 12, Years);
41
42
0
        std::ostringstream out;
43
0
        out << familyName_;
44
0
        if (tenor_ == 1*Days) {
45
0
            if (fixingDays_==0)
46
0
                out << "ON";
47
0
            else if (fixingDays_==1)
48
0
                out << "TN";
49
0
            else if (fixingDays_==2)
50
0
                out << "SN";
51
0
            else
52
0
                out << io::short_period(tenor_);
53
0
        } else {
54
0
            out << io::short_period(tenor_);
55
0
        }
56
0
        out << " " << dayCounter_.name();
57
0
        name_ = out.str();
58
59
0
        registerWith(Settings::instance().evaluationDate());
60
0
        registerWith(notifier());
61
0
    }
62
63
    Rate InterestRateIndex::fixing(const Date& fixingDate,
64
0
                                   bool forecastTodaysFixing) const {
65
66
0
        QL_REQUIRE(isValidFixingDate(fixingDate),
67
0
                   "Fixing date " << fixingDate << " is not valid");
68
69
0
        Date today = Settings::instance().evaluationDate();
70
71
0
        if (fixingDate>today ||
72
0
            (fixingDate==today && forecastTodaysFixing))
73
0
            return forecastFixing(fixingDate);
74
75
0
        if (fixingDate<today ||
76
0
            Settings::instance().enforcesTodaysHistoricFixings()) {
77
            // must have been fixed
78
            // do not catch exceptions
79
0
            Rate result = pastFixing(fixingDate);
80
0
            QL_REQUIRE(result != Null<Real>(),
81
0
                       "Missing " << name() << " fixing for " << fixingDate);
82
0
            return result;
83
0
        }
84
85
0
        try {
86
            // might have been fixed
87
0
            Rate result = pastFixing(fixingDate);
88
0
            if (result!=Null<Real>())
89
0
                return result;
90
0
            else
91
0
                ;   // fall through and forecast
92
0
        } catch (Error&) {
93
0
                ;   // fall through and forecast
94
0
        }
95
0
        return forecastFixing(fixingDate);
96
0
    }
97
98
}