-
Notifications
You must be signed in to change notification settings - Fork 0
/
xDealCashflowFixed.cs
212 lines (186 loc) · 6.99 KB
/
xDealCashflowFixed.cs
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
204
205
206
207
208
209
210
211
212
using System;
using System.ComponentModel;
using System.Drawing.Design;
using SunGard.Adaptiv.Analytics.Framework;
namespace SunGard.Adaptiv.Analytics.Models
{
/// <summary>
/// Base class for fixed cashflow deals.
/// </summary>
[Serializable]
public abstract class FixedCashflowBaseDeal : IRDeal
{
/// <summary>
/// Gets or sets the list of holiday calendar names.
/// </summary>
[NonMandatory]
[LocationStringsFormAttribute]
[Editor(typeof(ModalEditor), typeof(UITypeEditor))]
public string Calendars
{
get { return GetCalendarNames(0); }
set { SetCalendarNames(0, value); }
}
/// <summary>
/// Gets or sets the cashflow payment date.
/// </summary>
public TDate Payment_Date
{
get;
set;
}
/// <summary>
/// Deal end date.
/// </summary>
public override double EndDate()
{
return Payment_Date;
}
}
/// <summary>
/// Fixed cashlfow amount deal.
/// </summary>
[Serializable]
[System.ComponentModel.DisplayName("Cashflow Fixed")]
public class FixedCashflowDeal : FixedCashflowBaseDeal
{
/// <summary>
/// Gets or sets the cashflow amount.
/// </summary>
public double Amount
{
get;
set;
}
/// <summary>
/// Deal description constructed from main deal properties.
/// </summary>
public override string Summary()
{
return string.Format("{0} {1:N} {2}", fCurrency, Amount, Payment_Date);
}
/// <summary>
/// Try to get the notional of the deal at the given date.
/// </summary>
protected override bool DoTryGetNotional(PriceFactorList priceFactors, out double notional)
{
notional = Amount * priceFactors.GetInterface<IFxRate>(Currency).BaseCurrencySpotPrice();
return true;
}
}
/// <summary>
/// Base valuation class for fixed cashflow deals.
/// </summary>
[Serializable]
public abstract class CashflowFixedBaseValuation : IRValuation, ISingleDateValuation
{
[NonSerialized]
protected double fAmount = 0.0;
/// <summary>
/// Type of deals that this class can value.
/// </summary>
public override Type DealType()
{
return typeof(FixedCashflowBaseDeal);
}
/// <summary>
/// Prepare for valuation anything that will be shared between scenarios.
/// </summary>
public override void PreCloneInitialize(PriceFactorList factors, BaseTimeGrid baseTimes, RequiredResults requiredResults)
{
base.PreCloneInitialize(factors, baseTimes, requiredResults);
FixedCashflowBaseDeal deal = (FixedCashflowBaseDeal)fDeal;
// Add to valuation time grid
fT.AddPayDate(deal.Payment_Date);
}
/// <summary>
/// Calculate valuation profiles.
/// </summary>
public override void Value(ValuationResults valuationResults, PriceFactorList factors, BaseTimeGrid baseTimes)
{
PreValue(factors);
TimeGridIterator tgi = new TimeGridIterator(fT);
PVProfiles result = valuationResults.Profile;
CashAccumulators cashAccumulators = valuationResults.Cash;
FixedCashflowBaseDeal deal = (FixedCashflowBaseDeal)fDeal;
double payDate = deal.Payment_Date;
double tPay = CalcUtils.DaysToYears(payDate - factors.BaseDate);
VectorEngine.For(tgi, () =>
{
if (tgi.Date == payDate)
{
result.AppendVector(tgi.Date, fFxRate.Get(tgi.T) * fAmount);
}
else
{
result.AppendVector(tgi.Date, fFxRate.Get(tgi.T) * fDiscountRate.Get(tgi.T, tPay) * fAmount);
}
});
if (!cashAccumulators.Ignore && factors.BaseDate <= payDate && payDate <= fT.fHorizon)
cashAccumulators.Accumulate(fFxRate, payDate, fAmount);
result.Complete(fT);
}
/// <summary>
/// Single date valuation function.
/// </summary>
public void Value(Vector pv, Vector cash, double baseDate, double valueDate, Vector settlementDate, IInterestRate discount,
IInterestRate forecast, IInterestRate repo, IInterestRateVol interestRateVol, IInterestYieldVol interestYieldVol,
ISurvivalProb survivalProb, ISACCRResult saccrResult, IIntraValuationDiagnosticsWriter intraValuationDiagnosticsWriter)
{
FixedCashflowBaseDeal deal = (FixedCashflowBaseDeal)fDeal;
double payDate = deal.Payment_Date;
if (payDate < valueDate)
return;
using (var cache = Vector.CacheLike(pv))
{
Vector amount = cache.Get(fAmount);
if (settlementDate != null)
amount.MultiplyBy(settlementDate < payDate);
if (payDate == valueDate)
{
pv.Assign(amount);
if (cash != null)
cash.Assign(amount);
}
else
{
double t = CalcUtils.DaysToYears(valueDate - baseDate);
double tPay = CalcUtils.DaysToYears(payDate - baseDate);
if (survivalProb != null)
pv.Assign(amount * discount.Get(t, tPay) * survivalProb.Get(t, tPay));
else
pv.Assign(amount * discount.Get(t, tPay));
}
}
}
/// <inheritdoc />
protected override bool DoIsTracingVectorCompatible()
{
return true;
}
}
/// <summary>
/// Valuation class for fixed cashflow deals.
/// </summary>
[Serializable]
[System.ComponentModel.DisplayName("Cashflow Fixed Valuation")]
public class CashflowFixedValuation : CashflowFixedBaseValuation
{
/// <summary>
/// Type of deals that this class can value.
/// </summary>
public override Type DealType()
{
return typeof(FixedCashflowDeal);
}
/// <summary>
/// Prepare for valuation anything that will be shared between scenarios.
/// </summary>
public override void PreCloneInitialize(PriceFactorList factors, BaseTimeGrid baseTimes, RequiredResults requiredResults)
{
base.PreCloneInitialize(factors, baseTimes, requiredResults);
FixedCashflowDeal deal = (FixedCashflowDeal)fDeal;
fAmount = deal.Amount;
}
}
}