aboutsummaryrefslogtreecommitdiffstats
path: root/vba/corp cdo prices.vba
blob: 08ac023be06a97ca8b978c93eb4b4a00dce6bd76 (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
Option Explicit

Function questionmarks(size) As String
    Dim qarray() As String
    ReDim qarray(1 To size)
    Dim i As Integer
    For i = 1 To size
        qarray(i) = "?"
    Next i
    questionmarks = Join(qarray, ",")
End Function

Sub getdata()
    'cusipRange = Range(ActiveCell, ActiveCell.End(xlDown))
    Dim cn As New ADODB.Connection
    Dim cmd As New ADODB.Command

    Dim rs As New ADODB.Recordset

    Dim Notional As Variant
    Dim DisableReinv As Variant
    Dim Cusip As Object

    Notional = Selection.Offset(0, 4).Value
    DisableReinv = Selection.Offset(0, 7).Value
    Set cn = DBConn()

    Application.EnableEvents = False

    With cmd
        .ActiveConnection = cn
        .CommandType = adCmdText
        For Each Cusip In Selection
            .Parameters.Append .CreateParameter(, adChar, adParamInput, 13, Trim(Cusip))
        Next Cusip
        .CommandText = "SELECT * FROM et_cusip_details(" & questionmarks(Selection.Count) & ")"
    End With

    Set rs = cmd.Execute
    ActiveCell.Offset(0, 2).CopyFromRecordset rs
    rs.Close
    cn.Close
    Selection.Offset(0, 4).Value = Notional
    Selection.Offset(0, 7).Value = DisableReinv
    Application.EnableEvents = True
End Sub
Sub savecolor()
    'cusipRange = Range(ActiveCell, ActiveCell.End(xlDown))
    Dim cn As New ADODB.Connection
    Dim cmd As New ADODB.Command

    Dim Cusip As Object
    Set cn = DBConn()

    Application.EnableEvents = False

    With cmd
        .ActiveConnection = cn
        .CommandType = adCmdText
        .Parameters.Append .CreateParameter("ListDate", adDate, adParamInput)
        .Parameters.Append .CreateParameter("ListInfo", adVarChar, adParamInput, 20)
        .Parameters.Append .CreateParameter("Cusip", adChar, adParamInput, 9)
        .Parameters.Append .CreateParameter("Notional", adDouble, adParamInput)
        .Parameters.Append .CreateParameter("Indications", adVarChar, adParamInput, 100)
        .Parameters.Append .CreateParameter("Cover", adVarChar, adParamInput, 100)
        .Parameters.Append .CreateParameter("ListColor", adVarChar, adParamInput, 100)
        .Parameters.Append .CreateParameter("Bid", adVarChar, adParamInput, 100)
        .Parameters.Append .CreateParameter("BidNote", adVarChar, adParamInput, 100)
        For Each Cusip In Selection
            .Parameters("ListDate") = Cusip.Offset(0, -2)
            .Parameters("ListInfo") = Cusip.Offset(0, -1)
            .Parameters("Cusip") = Trim(Cusip)
            .Parameters("Notional") = Cusip.Offset(0, 4)
            .Parameters("Indications") = Cusip.Offset(0, 31)
            .Parameters("Cover") = Cusip.Offset(0, 32)
            .Parameters("ListColor") = Cusip.Offset(0, 33)
            .Parameters("Bid") = Cusip.Offset(0, 29)
            .Parameters("BidNote") = Cusip.Offset(0, 30)
            .CommandText = "INSERT INTO color VALUES(" & questionmarks(cmd.Parameters.Count) & ")"
            .Execute
        Next Cusip
    End With
    cn.Close
    Application.EnableEvents = True
End Sub
Sub getcolor()
    'cusipRange = Range(ActiveCell, ActiveCell.End(xlDown))
    Dim cn As New ADODB.Connection
    Dim cmd As New ADODB.Command

    Dim rs As New ADODB.Recordset

    Dim Cusip As Object

    Set cn = DBConn()

    Application.EnableEvents = False

    With cmd
        .ActiveConnection = cn
        .CommandType = adCmdText
        .Parameters.Append .CreateParameter("Cusip", adChar, adParamInput, 9)
        For Each Cusip In Selection
            .Parameters("Cusip") = Trim(Cusip)
            .CommandText = "select max(listdate), string_agg(listinfo, ',') as listinfo, string_agg(bid,',') as bid," & _
                            "string_agg(bid_note,',') as bid_note, sum(notional) as notional," & _
                            "string_agg(indications,',') as indications, string_agg(cover,',') as cover, " & _
                            "string_agg(listcolor,',') as listcolor from latest_color where cusip=?"
            Set rs = cmd.Execute
            Cusip.Offset(0, 37).CopyFromRecordset rs
        Next Cusip
    End With
    rs.Close
    cn.Close
    Application.EnableEvents = True
End Sub

Sub getintexdealnames()
    Dim cn As New ADODB.Connection
    Dim cmd As New ADODB.Command

    Dim rs As New ADODB.Recordset
    Dim Clip As New DataObject

    Dim firsttime As Boolean
    Set cn = DBConn()
    Dim dealnameArray As Variant
    Dim cliptext As String
    Dim Cusip As Object
    Dim i As Integer

    Application.EnableEvents = False
    With cmd
        .ActiveConnection = cn
        .CommandType = adCmdText
        For Each Cusip In Selection
            .Parameters.Append .CreateParameter(, adChar, adParamInput, 10, Trim(Cusip))
        Next Cusip
        .CommandText = "SELECT * FROM dealname_from_cusip(" & questionmarks(Selection.Count) & ")"
    End With

    Set rs = cmd.Execute
    dealnameArray = rs.GetRows()

    For i = 0 To Selection.Count - 1
        cliptext = cliptext & dealnameArray(0, i) & vbNewLine
    Next
    Clip.SetText cliptext
    Clip.PutInClipboard
    rs.Close
    cn.Close
    Application.EnableEvents = True
End Sub

Sub getallcusips()
    Dim cn As New ADODB.Connection
    Dim cmd As New ADODB.Command

    Dim rs As New ADODB.Recordset
    Dim Clip As New DataObject

    Set cn = DBConn()
    Dim cusipArray, dealnameArray As Variant
    Dim cliptext As String
    Dim Cusip As Object
    Dim i As Integer

    Application.EnableEvents = False
    With cmd
        .ActiveConnection = cn
        .CommandType = adCmdText
        For Each Cusip In Selection
            .Parameters.Append .CreateParameter(, adChar, adParamInput, 10, Trim(Cusip))
        Next Cusip
        .CommandText = "SELECT * FROM dealname_from_cusip(" & questionmarks(Selection.Count) & ")"
    End With

    Set rs = cmd.Execute
    dealnameArray = rs.GetRows()

    Set cmd = New ADODB.Command
    With cmd
        .ActiveConnection = cn
        .CommandType = adCmdText
        For i = 0 To Selection.Count - 1
            .Parameters.Append .CreateParameter(, adChar, adParamInput, 10, dealnameArray(0, i))
        Next i
        .CommandText = "SELECT DISTINCT cusip FROM latest_cusip_universe where dealname IN (" & questionmarks(Selection.Count) & ")"
    End With

    Set rs = cmd.Execute
    cusipArray = rs.GetRows()

    For i = 0 To UBound(cusipArray, 2)
        cliptext = cliptext & cusipArray(0, i) & vbNewLine
    Next
    Clip.SetText cliptext
    Clip.PutInClipboard
    rs.Close
    cn.Close
    Application.EnableEvents = True

End Sub

Sub savedeals_to_price()

    'Just showing where the input data are
    Dim cn As New ADODB.Connection
    Dim cmd As New ADODB.Command
    Dim rs As New ADODB.Recordset
    Set cn = DBConn()
    Dim cusipArray, dealnameArray, dealnameArrayuniq As Variant
    Dim sqlstr, Data As String
    Dim dealname As Variant
    Dim i As Integer
    Dim key As Variant

    Dim filename As String
    Dim fh As Integer
    Dim Cusip As Object
    Dim noreinvindex As New Scripting.Dictionary
    Dim noreinvdeal As New Scripting.Dictionary
    Application.EnableEvents = False

    filename = "\\WDSENTINEL\share\CorpCDOs\scripts\deals_to_price.txt"

    'Get an unused file number
    fh = FreeFile
    'Create a new file (or overwrite an existing one)
    Open filename For Output As fh

    key = 0
    With cmd
        .ActiveConnection = cn
        .CommandType = adCmdText
        For Each Cusip In Selection
            .Parameters.Append .CreateParameter(, adChar, adParamInput, 10, Trim(Cusip))
            If Cusip.Offset(0, 7) = "Y" Then
                noreinvindex.Add key, 1
            End If
            key = key + 1
        Next Cusip
        .CommandText = "SELECT * FROM dealname_from_cusip(" & questionmarks(Selection.Count) & ")"
    End With

    Set rs = cmd.Execute
    dealnameArray = rs.GetRows()

    For Each key In noreinvindex.Keys()
        noreinvdeal.Add dealnameArray(0, key), 1
    Next

    cmd.CommandText = "SELECT distinct * FROM dealname_from_cusip(" & questionmarks(Selection.Count) & ")"
    Set rs = cmd.Execute
    dealnameArrayuniq = rs.GetRows()
    For Each dealname In dealnameArrayuniq
        If Not IsNull(dealname) Then
            If Not noreinvdeal.Exists(dealname) Then
                sqlstr = "Select ""Reinv End Date"" from latest_clo_universe where dealname = '" & dealname & "'"
                Set rs = cn.Execute(sqlstr)
                Data = rs.GetString()
                If Len(Data) > 1 Then
                    Print #fh, dealname & vbTab & "TRUE"
                Else
                    Print #fh, dealname & vbTab & "FALSE"
                End If
            Else
                Print #fh, dealname & vbTab & "FALSE"
            End If
        End If
    Next dealname

    'Close the file
    Close #fh
End Sub

Sub generate_intex_portfolio()
    Dim cn As New ADODB.Connection
    Dim cmd As New ADODB.Command

    Dim rs As New ADODB.Recordset
    Dim Clip As New DataObject

    Set cn = DBConn()
    Dim cusipArray, dealnameArray, dealnameArrayuniq As Variant
    Dim cliptext As String
    Dim Cusip As Object
    Dim sqlstr, Data As String
    Dim dealname As Variant
    Dim i As Integer
    Dim key As Variant

    Dim noreinvindex As New Scripting.Dictionary
    Dim noreinvdeal As New Scripting.Dictionary
    Application.EnableEvents = False

    key = 0
    With cmd
        .ActiveConnection = cn
        .CommandType = adCmdText
        For Each Cusip In Selection
            .Parameters.Append .CreateParameter(, adChar, adParamInput, 10, Trim(Cusip))
            If Cusip.Offset(0, 7) = "Y" Then
                noreinvindex.Add key, 1
            End If
            key = key + 1
        Next Cusip
        .CommandText = "SELECT * FROM dealname_from_cusip(" & questionmarks(Selection.Count) & ")"
    End With

    Set rs = cmd.Execute
    dealnameArray = rs.GetRows()

    For Each key In noreinvindex.Keys()
        noreinvdeal.Add dealnameArray(0, key), 1
    Next

    cmd.CommandText = "SELECT distinct * FROM dealname_from_cusip(" & questionmarks(Selection.Count) & ")"
    Set rs = cmd.Execute
    dealnameArrayuniq = rs.GetRows()
    For Each dealname In dealnameArrayuniq
        If Not IsNull(dealname) Then
            If Not noreinvdeal.Exists(dealname) Then
                sqlstr = "Select ""Reinv End Date"" from latest_clo_universe where dealname = '" & dealname & "'"
                Set rs = cn.Execute(sqlstr)
                Data = rs.GetString()
                If Len(Data) > 1 Then
                    cliptext = cliptext & UCase(dealname) & "," & "COLLAT_INITIAL" & vbTab
                    cliptext = cliptext & dealname & ".sss" & vbNewLine
                    cliptext = cliptext & UCase(dealname) & "," & "COLLAT_REINVEST" & vbTab
                    cliptext = cliptext & dealname & ".sss" & vbNewLine
                Else
                    cliptext = cliptext & UCase(dealname) & "," & "COLLAT" & vbTab
                    cliptext = cliptext & dealname & ".sss" & vbNewLine
                End If
            Else
                cliptext = cliptext & UCase(dealname) & "," & "COLLAT" & vbTab
                cliptext = cliptext & dealname & ".sss" & vbNewLine
            End If
        End If
    Next dealname

    i = 1
    For Each Cusip In Selection
        If Not IsNull(dealnameArray(0, i - 1)) Then
            cliptext = cliptext & Cusip & vbTab & dealnameArray(0, i - 1) & ".sss" & vbNewLine
        End If
        i = i + 1
    Next Cusip

    Clip.SetText cliptext
    Clip.PutInClipboard
    rs.Close
    cn.Close
    Application.EnableEvents = True

End Sub