-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathrunExampleCalcs.py
More file actions
146 lines (120 loc) · 7.21 KB
/
Copy pathrunExampleCalcs.py
File metadata and controls
146 lines (120 loc) · 7.21 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
from CalcAddon import *
def run_example_calcs(trades, csas, colls, simplified=False, OEM=False, ignore_margin=False):
if len(trades) == len([x['TradeType'] for x in trades]):
if all(x['TradeType'] == 'Option' and x['BuySell'].upper() == 'SELL' for x in trades):
print('All trades are sold options, EAD is zero')
return 0
cpties = list(set(x['Counterparty'] for x in trades))
if len(cpties) > 1:
trade_trees_all = []
for cpty in cpties:
trades_cpty = [x for x in trades if x['Counterparty'] == cpty]
ext_trade_ids_temp = set()
trade_trees = []
trades_temp = []
if csas:
for i in range(len(csas) + 1):
if i < len(csas):
if cpty in [x['Counterparty'] for x in csas]:
csa = csas[i]
csa_tradegroup = [g.replace("'", "") for g in csa['TradeGroups']]
csa_currency = [c.replace("'", "") for c in csa['Currency']]
trades_i = [x for x in trades_cpty if x['TradeGroup'] in csa_tradegroup and x['Currency'] in csa_currency and x['Counterparty'] == csa['Counterparty'] and x['external_id'] not in ext_trade_ids_temp]
if not trades_i:
continue
trades_tree = CreateTradeGraph(trades_i)
trade_ids = [x['external_id'] for x in trades_i]
MF = 1 if ignore_margin else csa['CalcMF'](simplified=simplified)
ext_trade_ids_temp.update(trade_ids)
tree = CalcAddon(trades_tree, MF, simplified=simplified, OEM=OEM)
tree['maturity_factor'] = MF
trade_trees.append(tree)
trades_temp.append(trades_i)
else:
trades_unmargined = [x for x in trades_cpty if x['external_id'] not in ext_trade_ids_temp]
if not trades_unmargined:
continue
trades_tree = CreateTradeGraph(trades_unmargined)
tree = CalcAddon(trades_tree, simplified=simplified, OEM=OEM)
trade_trees.append(tree)
trades_temp.append(trades_unmargined)
else:
trades_tree = CreateTradeGraph(trades_cpty)
trade_trees.append(CalcAddon(trades_tree, simplified=simplified, OEM=OEM))
trades_temp.append(trades_cpty)
for i, tree in enumerate(trade_trees):
trades_i = trades_temp[i]
if i >= len(csas):
tree['Replacement Cost'] = CalcRC(trades_i, simplified=simplified, ignore_margin=ignore_margin)
else:
tree['Replacement Cost'] = CalcRC(trades_i, csas[i], colls, simplified=simplified, ignore_margin=ignore_margin)
rc = tree['Replacement Cost']
tree['PFE'] = CalcPFE(rc['V_C'], rc['V'], tree['addon'], simplified=simplified)
tree['EAD'] = CalcEAD(rc['RC'], tree['PFE'])
trades_tree_unmargined = CreateTradeGraph(trades_i)
trades_tree_unmargined = CalcAddon(trades_tree_unmargined, simplified=simplified, OEM=OEM)
trades_tree_unmargined['Replacement Cost'] = CalcRC(trades_i, simplified=simplified, ignore_margin=ignore_margin)
trades_tree_unmargined['PFE'] = CalcPFE(
trades_tree_unmargined['Replacement Cost']['V_C'],
trades_tree_unmargined['Replacement Cost']['V'],
trades_tree_unmargined['addon'],
simplified=simplified
)
trades_tree_unmargined['EAD'] = CalcEAD(trades_tree_unmargined['Replacement Cost']['RC'], trades_tree_unmargined['PFE'])
tree['EAD'] = min(tree['EAD'], trades_tree_unmargined['EAD'])
trade_trees_all.append(trade_trees)
return trade_trees_all
else:
ext_trade_ids_temp = set()
trade_trees = []
trades_temp = []
if csas:
for i in range(len(csas) + 1):
if i < len(csas):
csa = csas[i]
csa_tradegroup = [g.replace("'", "") for g in csa['TradeGroups']]
csa_currency = [c.replace("'", "") for c in csa['Currency']]
trades_i = [x for x in trades if x['TradeGroup'] in csa_tradegroup and x['Currency'] in csa_currency and x['Counterparty'] == csa['Counterparty'] and x['external_id'] not in ext_trade_ids_temp]
if not trades_i:
continue
trades_tree = CreateTradeGraph(trades_i)
trade_ids = [x['external_id'] for x in trades_i]
MF = csa['CalcMF'](simplified=simplified)
ext_trade_ids_temp.update(trade_ids)
tree = CalcAddon(trades_tree, MF, simplified=simplified, OEM=OEM)
tree['maturity_factor'] = MF
trade_trees.append(tree)
trades_temp.append(trades_i)
else:
trades_unmargined = [x for x in trades if x['external_id'] not in ext_trade_ids_temp]
if not trades_unmargined:
continue
trades_tree = CreateTradeGraph(trades_unmargined)
tree = CalcAddon(trades_tree, simplified=simplified, OEM=OEM)
trade_trees.append(tree)
trades_temp.append(trades_unmargined)
else:
trades_tree = CreateTradeGraph(trades)
trade_trees.append(CalcAddon(trades_tree, simplified=simplified, OEM=OEM))
trades_temp.append(trades)
for i, tree in enumerate(trade_trees):
trades_i = trades_temp[i]
if i >= len(csas):
tree['Replacement Cost'] = CalcRC(trades_i, simplified=simplified, ignore_margin=ignore_margin)
else:
tree['Replacement Cost'] = CalcRC(trades_i, csas[i], colls, simplified=simplified, ignore_margin=ignore_margin)
rc = tree['Replacement Cost']
tree['PFE'] = CalcPFE(rc['V_C'], rc['V'], tree['addon'], simplified=simplified)
tree['EAD'] = CalcEAD(rc['RC'], tree['PFE'])
trades_tree_unmargined = CreateTradeGraph(trades_i)
trades_tree_unmargined = CalcAddon(trades_tree_unmargined, simplified=simplified, OEM=OEM)
trades_tree_unmargined['Replacement Cost'] = CalcRC(trades_i, simplified=simplified, ignore_margin=ignore_margin)
trades_tree_unmargined['PFE'] = CalcPFE(
trades_tree_unmargined['Replacement Cost']['V_C'],
trades_tree_unmargined['Replacement Cost']['V'],
trades_tree_unmargined['addon'],
simplified=simplified
)
trades_tree_unmargined['EAD'] = CalcEAD(trades_tree_unmargined['Replacement Cost']['RC'], trades_tree_unmargined['PFE'])
tree['EAD'] = min(tree['EAD'], trades_tree_unmargined['EAD'])
return trade_trees