forked from msitt/blpapi-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRefDataTableOverrideExample.py
More file actions
181 lines (146 loc) · 6.22 KB
/
Copy pathRefDataTableOverrideExample.py
File metadata and controls
181 lines (146 loc) · 6.22 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
174
175
176
177
178
179
180
181
# RefDataTableOverrideExample.py
from __future__ import print_function
from __future__ import absolute_import
import blpapi
from optparse import OptionParser
SECURITY_DATA = blpapi.Name("securityData")
SECURITY = blpapi.Name("security")
FIELD_DATA = blpapi.Name("fieldData")
FIELD_EXCEPTIONS = blpapi.Name("fieldExceptions")
FIELD_ID = blpapi.Name("fieldId")
ERROR_INFO = blpapi.Name("errorInfo")
def parseCmdLine():
parser = OptionParser(description="Retrieve reference data.")
parser.add_option("-a",
"--ip",
dest="host",
help="server name or IP (default: %default)",
metavar="ipAddress",
default="localhost")
parser.add_option("-p",
dest="port",
type="int",
help="server port (default: %default)",
metavar="tcpPort",
default=8194)
(options, args) = parser.parse_args()
return options
def processMessage(msg):
securityDataArray = msg.getElement(SECURITY_DATA)
for securityData in securityDataArray.values():
print(securityData.getElementAsString(SECURITY))
fieldData = securityData.getElement(FIELD_DATA)
for field in fieldData.elements():
if not field.isValid():
print(field.name(), "is NULL.")
elif field.isArray():
# The following illustrates how to iterate over complex
# data returns.
for i, row in enumerate(field.values()):
print("Row %d: %s" % (i, row))
else:
print("%s = %s" % (field.name(), field.getValueAsString()))
fieldExceptionArray = securityData.getElement(FIELD_EXCEPTIONS)
for fieldException in fieldExceptionArray.values():
errorInfo = fieldException.getElement(ERROR_INFO)
print("%s: %s" % (errorInfo.getElementAsString("category"),
fieldException.getElementAsString(FIELD_ID)))
def main():
global options
options = parseCmdLine()
# Fill SessionOptions
sessionOptions = blpapi.SessionOptions()
sessionOptions.setServerHost(options.host)
sessionOptions.setServerPort(options.port)
print("Connecting to %s:%d" % (options.host, options.port))
# Create a Session
session = blpapi.Session(sessionOptions)
# Start a Session
if not session.start():
print("Failed to start session.")
return
if not session.openService("//blp/refdata"):
print("Failed to open //blp/refdata")
return
refDataService = session.getService("//blp/refdata")
request = refDataService.createRequest("ReferenceDataRequest")
# Add securities to request.
request.append("securities", "CWHL 2006-20 1A1 Mtge")
# ...
# Add fields to request. Cash flow is a table (data set) field.
request.append("fields", "MTG_CASH_FLOW")
request.append("fields", "SETTLE_DT")
# Add scalar overrides to request.
overrides = request.getElement("overrides")
override1 = overrides.appendElement()
override1.setElement("fieldId", "ALLOW_DYNAMIC_CASHFLOW_CALCS")
override1.setElement("value", "Y")
override2 = overrides.appendElement()
override2.setElement("fieldId", "LOSS_SEVERITY")
override2.setElement("value", 31)
# Add table overrides to request.
tableOverrides = request.getElement("tableOverrides")
tableOverride = tableOverrides.appendElement()
tableOverride.setElement("fieldId", "DEFAULT_VECTOR")
rows = tableOverride.getElement("row")
# Layout of input table is specified by the definition of
# 'DEFAULT_VECTOR'. Attributes are specified in the first rows.
# Subsequent rows include rate, duration, and transition.
row = rows.appendElement()
cols = row.getElement("value")
cols.appendValue("Anchor") # Anchor type
cols.appendValue("PROJ") # PROJ = Projected
row = rows.appendElement()
cols = row.getElement("value")
cols.appendValue("Type") # Type of default
cols.appendValue("CDR") # CDR = Conditional Default Rate
rateVectors = [
[1.0, 12, "S"], # S = Step
[2.0, 12, "R"] # R = Ramp
]
for rateVector in rateVectors:
row = rows.appendElement()
cols = row.getElement("value")
for value in rateVector:
cols.appendValue(value)
print("Sending Request:", request)
cid = session.sendRequest(request)
try:
# Process received events
while(True):
# We provide timeout to give the chance to Ctrl+C handling:
ev = session.nextEvent(500)
for msg in ev:
if cid in msg.correlationIds():
# Process the response generically.
processMessage(msg)
# Response completly received, so we could exit
if ev.eventType() == blpapi.Event.RESPONSE:
break
finally:
# Stop the session
session.stop()
if __name__ == "__main__":
print("RefDataTableOverrideExample")
try:
main()
except KeyboardInterrupt:
print("Ctrl+C pressed. Stopping...")
__copyright__ = """
Copyright 2012. Bloomberg Finance L.P.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: The above
copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
"""