-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrunExampleCalcs.R
More file actions
173 lines (142 loc) · 7.75 KB
/
Copy pathrunExampleCalcs.R
File metadata and controls
173 lines (142 loc) · 7.75 KB
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
runExampleCalcs <-function(trades, csas, colls, simplified = FALSE, OEM = FALSE, ignore_margin = FALSE)
{
if(length(trades)==length(unlist(lapply(trades,function(x) x$TradeType))))
{
if(all(unlist(lapply(trades,function(x) x$TradeType=='Option'&toupper(x$BuySell)=='SELL'))))
{
cat('All trades are sold options, EAD is zero')
return(0)
}
}
cpties = unique(unlist(lapply(trades,function(x) x$Counterparty)))
if(length(cpties)>1)
{
trade_trees_all = list()
for(cpty_counter in 1:length(cpties))
{
trades_cpty = trades[sapply(trades, function(x) (x$Counterparty %in% cpties[cpty_counter]))]
ext_trade_ids_temp = array()
trade_trees = list()
trades_temp = list()
if(length(csas)!=0)
{
for(i in 1:(length(csas)+1))
{
if(i < (length(csas)+1))
{
if(cpties[cpty_counter] %in% unique(unlist(lapply(csas,function(x) x$Counterparty))))
{
csa_tradegroup = gsub("[']","",unlist(csas[[i]]$TradeGroups))
csa_currency = gsub("[']","",unlist(csas[[i]]$Currency))
trades_temp[[i]] = trades_cpty[sapply(trades_cpty, function(x) x$TradeGroup %in% csa_tradegroup & x$Currency %in% csa_currency & x$Counterparty == csas[[i]]$Counterparty & !(x$external_id %in% ext_trade_ids_temp))]
if(length(trades_temp[[i]])==0)
next
trades_tree = CreateTradeGraph(trades_temp[[i]])
trade_ids = sapply(trades_temp[[i]], function(x) x$external_id)
if(ignore_margin)
{ MF =1
}else {MF = csas[[i]]$CalcMF(simplified = simplified)}
ext_trade_ids_temp = c(ext_trade_ids_temp, trade_ids)
# calculating the add-on
trade_trees[[i]] = CalcAddon(trades_tree, MF, simplified = simplified, OEM = OEM)
trade_trees[[i]]$maturity_factor = MF
} else
{
trades_unmargined = trades_cpty[sapply(trades_cpty, function(x) !(x$external_id %in% ext_trade_ids_temp))]
if(length(trades_unmargined)==0)
next
trades_temp[[i]] = trades_cpty[sapply(trades_cpty, function(x) !(x$external_id %in% ext_trade_ids_temp))]
trades_tree = CreateTradeGraph(trades_temp[[i]])
trade_trees[[i]] = CalcAddon(trades_tree, simplified = simplified, OEM = OEM)
}
}
}
}else
{
trades_tree = CreateTradeGraph(trades_cpty)
trade_trees[[1]] = CalcAddon(trades_tree, simplified = simplified, OEM = OEM)
trades_temp[[1]] = trades_cpty
}
for(i in 1:length(trade_trees))
{
# calculating the RC and the V-c amount
if(i>length(csas))
{
trade_trees[[i]]$`Replacement Cost` <- CalcRC(trades_temp[[i]],simplified = simplified, ignore_margin = ignore_margin)
}else
{ trade_trees[[i]]$`Replacement Cost` <- CalcRC(trades_temp[[i]], csas[[i]], colls,simplified = simplified, ignore_margin = ignore_margin) }
# calculating the PFE after multiplying the addon with a factor if V-C<0
trade_trees[[i]]$PFE <- CalcPFE(trade_trees[[i]]$`Replacement Cost`$V_C,trade_trees[[i]]$`Replacement Cost`$V, trade_trees[[i]]$addon, simplified = simplified)
# calculating the Exposure-at-Default
trade_trees[[i]]$EAD <- CalcEAD(trade_trees[[i]]$`Replacement Cost`$RC,trade_trees[[i]]$PFE)
trades_tree_unmmargined = CreateTradeGraph(trades_temp[[i]])
trades_tree_unmmargined= CalcAddon(trades_tree_unmmargined, simplified = simplified, OEM = OEM)
trades_tree_unmmargined$`Replacement Cost` <- CalcRC(trades_temp[[i]],simplified = simplified, ignore_margin = ignore_margin)
trades_tree_unmmargined$PFE <- CalcPFE(trades_tree_unmmargined$`Replacement Cost`$V_C,trades_tree_unmmargined$`Replacement Cost`$V, trades_tree_unmmargined$addon, simplified = simplified)
trades_tree_unmmargined$EAD <- CalcEAD(trades_tree_unmmargined$`Replacement Cost`$RC,trades_tree_unmmargined$PFE)
trade_trees[[i]]$EAD = min(trades_tree_unmmargined$EAD,trade_trees[[i]]$EAD)
}
trade_trees_all[[cpty_counter]] = trade_trees
}
return(trade_trees_all)
}else
{
ext_trade_ids_temp = array()
trade_trees = list()
trades_temp = list()
if(length(csas)!=0)
{
for(i in 1:(length(csas)+1))
{
if(i < (length(csas)+1))
{
csa_tradegroup = gsub("[']","",unlist(csas[[i]]$TradeGroups))
csa_currency = gsub("[']","",unlist(csas[[i]]$Currency))
trades_temp[[i]] = trades[sapply(trades, function(x) x$TradeGroup %in% csa_tradegroup & x$Currency %in% csa_currency & x$Counterparty == csas[[i]]$Counterparty & !(x$external_id %in% ext_trade_ids_temp))]
if(length(trades_temp[[i]])==0)
next
trades_tree = CreateTradeGraph(trades_temp[[i]])
trade_ids = sapply(trades_temp[[i]], function(x) x$external_id)
MF = csas[[i]]$CalcMF(simplified = simplified)
ext_trade_ids_temp = c(ext_trade_ids_temp, trade_ids)
# calculating the add-on
trade_trees[[i]] = CalcAddon(trades_tree, MF, simplified = simplified, OEM = OEM)
trade_trees[[i]]$maturity_factor = MF
} else
{
trades_unmargined = trades[sapply(trades, function(x) !(x$external_id %in% ext_trade_ids_temp))]
if(length(trades_unmargined)==0)
next
trades_temp[[i]] = trades[sapply(trades, function(x) !(x$external_id %in% ext_trade_ids_temp))]
trades_tree = CreateTradeGraph(trades_temp[[i]])
trade_trees[[i]] = CalcAddon(trades_tree, simplified = simplified, OEM = OEM)
}
}
} else
{
trades_tree = CreateTradeGraph(trades)
trade_trees[[1]] = CalcAddon(trades_tree, simplified = simplified, OEM = OEM)
trades_temp[[1]] = trades
}
for(i in 1:length(trade_trees))
{
# calculating the RC and the V-c amount
if(i>length(csas))
{
trade_trees[[i]]$`Replacement Cost` <- CalcRC(trades_temp[[i]],simplified = simplified, ignore_margin = ignore_margin)
}else
{ trade_trees[[i]]$`Replacement Cost` <- CalcRC(trades_temp[[i]], csas[[i]], colls,simplified = simplified, ignore_margin = ignore_margin) }
# calculating the PFE after multiplying the addon with a factor if V-C<0
trade_trees[[i]]$PFE <- CalcPFE(trade_trees[[i]]$`Replacement Cost`$V_C,trade_trees[[i]]$`Replacement Cost`$V, trade_trees[[i]]$addon, simplified = simplified)
# calculating the Exposure-at-Default
trade_trees[[i]]$EAD <- CalcEAD(trade_trees[[i]]$`Replacement Cost`$RC,trade_trees[[i]]$PFE)
trades_tree_unmmargined = CreateTradeGraph(trades_temp[[i]])
trades_tree_unmmargined= CalcAddon(trades_tree_unmmargined, simplified = simplified, OEM = OEM)
trades_tree_unmmargined$`Replacement Cost` <- CalcRC(trades_temp[[i]],simplified = simplified, ignore_margin = ignore_margin)
trades_tree_unmmargined$PFE <- CalcPFE(trades_tree_unmmargined$`Replacement Cost`$V_C,trades_tree_unmmargined$`Replacement Cost`$V, trades_tree_unmmargined$addon, simplified = simplified)
trades_tree_unmmargined$EAD <- CalcEAD(trades_tree_unmmargined$`Replacement Cost`$RC,trades_tree_unmmargined$PFE)
trade_trees[[i]]$EAD = min(trades_tree_unmmargined$EAD,trade_trees[[i]]$EAD)
}
return(trade_trees)
}
}