-
Notifications
You must be signed in to change notification settings - Fork 1
/
SwapLegs.h
187 lines (169 loc) · 5.48 KB
/
SwapLegs.h
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
#pragma once
#include <vector>
#include <iostream>
#include <memory>
#include <random>
#include "Curves.h"
////////////////////////////////////////////////////
//
// FixedLeg
//
// amounts Values of the Cash Flows
// times Payment dates
//
// <mdouble>: double, aadc:idouble, adept::adouble
//
////////////////////////////////////////////////////
template<typename mdouble>
class FixedLeg {
public:
FixedLeg (
const std::vector<double>& amounts, const std::vector<qtime>& times
)
: mAmounts(amounts), mTimes(times)
{}
public:
// Implements Leg initialization
template<class Market>
void initT0 (const Market& m0, const qtime& t0) {
mT0FirstIndx = mFirstCfIndx = moveCFIndx(0, t0);
}
// Make the Leg state in concordance with the market state
void nextT (const qtime& next_t) {
mFirstCfIndx = moveCFIndx(mFirstCfIndx, next_t);
}
// Returns price of the FixedLeg
template<class Market>
mdouble getPrice(const Market& m, const qtime& t) const {
mdouble price(0.);
const typename Market::DiscountCurve& curve=m.getDiscountCurve();
for (int i = mFirstCfIndx; i < mTimes.size(); ++i) {
if (idouble::recording) CAAD_LoopPulse(i - mFirstCfIndx);
price = price + mAmounts[i] * curve(mTimes[i]);
}
return price;
}
template<class Market>
mdouble getDeltaPrice (const Market& delta_m, const qtime& t) const {
return getPrice(delta_m, t);
}
void resetT0 () {
mFirstCfIndx = mT0FirstIndx;
}
private:
int moveCFIndx (const int& curr_indx, const qtime& time) {
int indx(curr_indx);
for (indx = 0; indx < mTimes.size() && mTimes[indx] < time; ++indx) {}
return indx;
}
private:
const std::vector<double> mAmounts;
const std::vector<qtime> mTimes;
int mFirstCfIndx, mT0FirstIndx;
};
////////////////////////////////////////////////////
//
// FloatLeg
//
// notionals Values of the Cash Flows
// start_times
// end_times
// pay_times Payment times
// spread_id Spread curve ID
//
// <mdouble>: double, aadc:idouble, adept::adouble
//
////////////////////////////////////////////////////
template<typename mdouble>
class FloatLeg {
public:
FloatLeg (
const std::vector<double>& notionals
, const std::vector<qtime>& start_times
, const std::vector<qtime>& end_times
, const std::vector<qtime>& pay_times
, const int& spread_id
)
: mNotionals(notionals)
, mStartTimes(start_times)
, mEndTimes(end_times)
, mPayTimes(pay_times)
, mFwds(mPayTimes.size(), 0.)
, m_spread_id(spread_id)
{}
public:
// Implements Leg initialization
template<class Market>
void initT0 (const Market& m0, const qtime& t0) {
mT0FirstIndx = mFirstCfIndx = moveCFIndx(0, t0);
}
// Make the Leg state in concordance with the market state
template<class Market>
void nextT (const Market& next_m, const qtime& next_t) {
mFirstCfIndx = moveCFIndx(mFirstCfIndx, next_t);
}
// Returns price of the FloatLeg
template<class Market>
mdouble getPrice (const Market& m, const qtime& t) {
mdouble price(0.);
const typename Market::DiscountCurve& curve=m.getDiscountCurve();
const typename Market::ProjectCurveT& proj_curve=m.getProjectCurve(m_spread_id);
for (int i = mFirstCfIndx; i < mPayTimes.size(); ++i) {
if (idouble::recording) CAAD_LoopPulse(i - mFirstCfIndx);
mdouble fwd;
if (mStartTimes[i] >= t) {
fwd = (proj_curve(mStartTimes[i]) / proj_curve(mEndTimes[i]) - 1.0)
/ qYearFrac(mStartTimes[i], mEndTimes[i])
;
mFwds[i] = fwd; // store fwd rates for future pricing
} else {
fwd = mFwds[i]; // use previously forcasted rate. TODO: Change to fixing framework
}
price = price + mNotionals[i] * fwd * curve(mPayTimes[i]);
}
return price;
}
void resetT0 () {
mFirstCfIndx = mT0FirstIndx;
}
private:
int moveCFIndx (const int& curr_indx, const qtime& time) {
int indx(curr_indx);
for (indx = 0; indx < mPayTimes.size() && mPayTimes[indx] < time; ++indx) {}
return indx;
}
private:
const std::vector<double> mNotionals;
const std::vector<qtime> mStartTimes, mEndTimes, mPayTimes;
std::vector<mdouble> mFwds;
int mFirstCfIndx, mT0FirstIndx;
int m_spread_id;
};
////////////////////////////////////////////////////
//
// IRMarket
//
// discount Discount curve
// proj Projected curve
//
// <mdouble>: double, aadc:idouble, adept::adouble
//
////////////////////////////////////////////////////
template<typename mdouble>
class IRMarket {
public:
typedef LinearInterpDiscountCurve<mdouble> DiscountCurve;
typedef LinearInterpDiscountCurve<mdouble> ProjectCurveT;
public:
IRMarket (
const LinearInterpDiscountCurve<mdouble>& discount, const LinearInterpDiscountCurve<mdouble>& proj
)
: m_discount(discount), m_proj(proj)
{}
const DiscountCurve& getDiscountCurve () const { return m_discount; }
const ProjectCurveT& getProjectCurve (int spread_id) const { return m_proj; }
ProjectCurveT& getBumpProjectCurve () { return m_proj; }
DiscountCurve& getBumpDiscountCurve () { return m_discount; }
private:
const LinearInterpDiscountCurve<mdouble> m_discount, m_proj;
};