File: cashflowvectors.cpp

package info (click to toggle)
quantlib 0.2.1.cvs20020322-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,716 kB
  • ctags: 4,614
  • sloc: cpp: 19,601; sh: 7,389; makefile: 796; ansic: 22
file content (203 lines) | stat: -rw-r--r-- 8,635 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203


/*
 Copyright (C) 2000, 2001, 2002 RiskMap srl

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it under the
 terms of the QuantLib license.  You should have received a copy of the
 license along with this program; if not, please email ferdinando@ametrano.net
 The license is also available online at http://quantlib.org/html/license.html

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/
/*! \file cashflowvectors.cpp
    \brief Cash flow vector builders

    \fullpath
    ql/CashFlows/%cashflowvectors.cpp
*/

// $Id: cashflowvectors.cpp,v 1.11 2002/01/28 12:09:46 lballabio Exp $

#include <ql/CashFlows/cashflowvectors.hpp>
#include <ql/CashFlows/fixedratecoupon.hpp>
#include <ql/CashFlows/floatingratecoupon.hpp>
#include <ql/CashFlows/shortfloatingcoupon.hpp>
#include <ql/scheduler.hpp>

namespace QuantLib {

    using Indexes::Xibor;

    namespace CashFlows {

        FixedRateCouponVector::FixedRateCouponVector(
          const std::vector<double>& nominals,
          const std::vector<Rate>& couponRates,
          const Date& startDate, const Date& endDate,
          int frequency, const Calendar& calendar,
          RollingConvention rollingConvention, bool isAdjusted,
          const DayCounter& dayCount, const DayCounter& firstPeriodDayCount,
          const Date& stubDate) {
            QL_REQUIRE(couponRates.size() != 0, "unspecified coupon rates");
            QL_REQUIRE(nominals.size() != 0, "unspecified nominals");
            Scheduler scheduler(calendar, startDate, endDate, frequency,
                rollingConvention, isAdjusted, stubDate);
            // first period might be short or long
            Date start = scheduler.date(0), end = scheduler.date(1);
            Rate rate = couponRates[0];
            double nominal = nominals[0];
            if (scheduler.isRegular(1)) {
                QL_REQUIRE(dayCount == firstPeriodDayCount,
                    "regular first bond coupon "
                    "does not allow a first period day count");
                push_back(Handle<CashFlow>(
                    new FixedRateCoupon(nominal, rate, calendar,
                        rollingConvention, dayCount,
                        start, end, start, end)));
            } else {
                Date reference = end.plusMonths(-12/frequency);
                if (isAdjusted)
                    reference =
                        calendar.roll(reference,rollingConvention);
                push_back(Handle<CashFlow>(
                    new FixedRateCoupon(nominal, rate, calendar,
                        rollingConvention, firstPeriodDayCount,
                        start, end, reference, end)));
            }
            // regular periods
            for (Size i=2; i<scheduler.size()-1; i++) {
                start = end; end = scheduler.date(i);
                if ((i-1) < couponRates.size())
                    rate = couponRates[i-1];
                else
                    rate = couponRates.back();
                if ((i-1) < nominals.size())
                    nominal = nominals[i-1];
                else
                    nominal = nominals.back();
                push_back(Handle<CashFlow>(
                    new FixedRateCoupon(nominal, rate, calendar,
                        rollingConvention, dayCount, start, end,
                        start, end)));
            }
            if (scheduler.size() > 2) {
                // last period might be short or long
                Size N = scheduler.size();
                start = end; end = scheduler.date(N-1);
                if ((N-2) < couponRates.size())
                    rate = couponRates[N-2];
                else
                    rate = couponRates.back();
                if ((N-2) < nominals.size())
                    nominal = nominals[N-2];
                else
                    nominal = nominals.back();
                if (scheduler.isRegular(N-1)) {
                    push_back(Handle<CashFlow>(
                        new FixedRateCoupon(nominal, rate, calendar,
                            rollingConvention, dayCount, start, end,
                            start, end)));
                } else {
                    Date reference = start.plusMonths(12/frequency);
                    if (isAdjusted)
                        reference =
                            calendar.roll(reference,rollingConvention);
                    push_back(Handle<CashFlow>(
                        new FixedRateCoupon(nominal, rate, calendar,
                            rollingConvention, dayCount, start, end,
                            start, reference)));
                }
            }
        }


        FloatingRateCouponVector::FloatingRateCouponVector(
          const std::vector<double>& nominals,
          const Date& startDate, const Date& endDate,
          int frequency, const Calendar& calendar,
          RollingConvention rollingConvention,
          const RelinkableHandle<TermStructure>& termStructure,
          const Handle<Xibor>& index, int fixingDays,
          const std::vector<Spread>& spreads,
          const Date& stubDate) {
            QL_REQUIRE(nominals.size() != 0, "unspecified nominals");

            Scheduler scheduler(calendar, startDate, endDate, frequency,
                rollingConvention, true, stubDate);
            // first period might be short or long
            Date start = scheduler.date(0), end = scheduler.date(1);
            Spread spread;
            if (spreads.size() > 0)
                spread = spreads[0];
            else
                spread = 0.0;
            double nominal = nominals[0];
            if (scheduler.isRegular(1)) {
                push_back(Handle<CashFlow>(
                    new FloatingRateCoupon(nominal, index, termStructure,
                        start, end, fixingDays, spread, start, end)));
            } else {
                Date reference = end.plusMonths(-12/frequency);
                reference =
                    calendar.roll(reference,rollingConvention);
                push_back(Handle<CashFlow>(
                    new ShortFloatingRateCoupon(nominal, index, termStructure,
                        start, end, fixingDays, spread, reference, end)));
            }
            // regular periods
            for (Size i=2; i<scheduler.size()-1; i++) {
                start = end; end = scheduler.date(i);
                if ((i-1) < spreads.size())
                    spread = spreads[i-1];
                else if (spreads.size() > 0)
                    spread = spreads.back();
                else
                    spread = 0.0;
                if ((i-1) < nominals.size())
                    nominal = nominals[i-1];
                else
                    nominal = nominals.back();
                push_back(Handle<CashFlow>(
                    new FloatingRateCoupon(nominal, index, termStructure,
                        start, end, fixingDays, spread, start, end)));
            }
            if (scheduler.size() > 2) {
                // last period might be short or long
                Size N = scheduler.size();
                start = end; end = scheduler.date(N-1);
                if ((N-2) < spreads.size())
                    spread = spreads[N-2];
                else if (spreads.size() > 0)
                    spread = spreads.back();
                else
                    spread = 0.0;
                if ((N-2) < nominals.size())
                    nominal = nominals[N-2];
                else
                    nominal = nominals.back();
                if (scheduler.isRegular(N-1)) {
                    push_back(Handle<CashFlow>(
                        new FloatingRateCoupon(nominal, index, termStructure,
                            start, end, fixingDays, spread, start, end)));
                } else {
                    Date reference = start.plusMonths(12/frequency);
                    reference =
                        calendar.roll(reference,rollingConvention);
                    push_back(Handle<CashFlow>(
                        new ShortFloatingRateCoupon(nominal, index, 
                            termStructure, start, end, fixingDays, spread, 
                            start, reference)));
                }
            }
        }

    }

}