1+ # GetTradeDesks.py
2+
3+ import blpapi
4+ import sys
5+
6+
7+ SESSION_STARTED = blpapi .Name ("SessionStarted" )
8+ SESSION_STARTUP_FAILURE = blpapi .Name ("SessionStartupFailure" )
9+ SERVICE_OPENED = blpapi .Name ("ServiceOpened" )
10+ SERVICE_OPEN_FAILURE = blpapi .Name ("ServiceOpenFailure" )
11+ ERROR_INFO = blpapi .Name ("ErrorInfo" )
12+ GET_TRADE_DESKS = blpapi .Name ("GetTradeDesks" )
13+
14+ # This is an AIM only function and thus there are no valid //blp/emapisvc_beta access.
15+ d_service = "//blp/emapisvc"
16+ d_host = "localhost"
17+ d_port = 8194
18+ bEnd = False
19+
20+ class SessionEventHandler ():
21+
22+ def processEvent (self , event , session ):
23+ try :
24+ if event .eventType () == blpapi .Event .SESSION_STATUS :
25+ self .processSessionStatusEvent (event ,session )
26+
27+ elif event .eventType () == blpapi .Event .SERVICE_STATUS :
28+ self .processServiceStatusEvent (event ,session )
29+
30+ elif event .eventType () == blpapi .Event .RESPONSE :
31+ self .processResponseEvent (event )
32+
33+ else :
34+ self .processMiscEvents (event )
35+
36+ except :
37+ print ("Exception: %s" % sys .exc_info ()[0 ])
38+
39+ return False
40+
41+
42+ def processSessionStatusEvent (self ,event ,session ):
43+ print ("Processing SESSION_STATUS event" )
44+
45+ for msg in event :
46+ if msg .messageType () == SESSION_STARTED :
47+ print ("Session started..." )
48+ session .openServiceAsync (d_service )
49+
50+ elif msg .messageType () == SESSION_STARTUP_FAILURE :
51+ print >> sys .stderr , ("Error: Session startup failed" )
52+
53+ else :
54+ print (msg )
55+
56+
57+ def processServiceStatusEvent (self ,event ,session ):
58+ print ("Processing SERVICE_STATUS event" )
59+
60+ for msg in event :
61+
62+ if msg .messageType () == SERVICE_OPENED :
63+ print ("Service opened..." )
64+
65+ service = session .getService (d_service )
66+
67+ request = service .createRequest ("GetTradeDesks" )
68+
69+ #request.set("EMSX_REQUEST_SEQ", 1)
70+
71+ print ("Request: %s" % request .toString ())
72+
73+ self .requestID = blpapi .CorrelationId ()
74+
75+ session .sendRequest (request , correlationId = self .requestID )
76+
77+ elif msg .messageType () == SERVICE_OPEN_FAILURE :
78+ print >> sys .stderr , ("Error: Service failed to open" )
79+
80+ def processResponseEvent (self , event ):
81+ print ("Processing RESPONSE event" )
82+
83+ for msg in event :
84+
85+ print ("MESSAGE: %s" % msg .toString ())
86+ print ("CORRELATION ID: %d" % msg .correlationIds ()[0 ].value ())
87+
88+
89+ if msg .correlationIds ()[0 ].value () == self .requestID .value ():
90+ print ("MESSAGE TYPE: %s" % msg .messageType ())
91+
92+ if msg .messageType () == ERROR_INFO :
93+ errorCode = msg .getElementAsInteger ("ERROR_CODE" )
94+ errorMessage = msg .getElementAsString ("ERROR_MESSAGE" )
95+ print ("ERROR CODE: %d\t ERROR MESSAGE: %s" % (errorCode ,errorMessage ))
96+ elif msg .messageType () == GET_TRADE_DESKS :
97+
98+ tradeDesks = msg .getElement ("EMSX_TRADE_DESK" )
99+ print (msg )
100+
101+ for t in tradeDesks .values ():
102+ print ("TRADEDESKS: %s" % (t ))
103+
104+ global bEnd
105+ bEnd = True
106+
107+ def processMiscEvents (self , event ):
108+
109+ print ("Processing " + event .eventType () + " event" )
110+
111+ for msg in event :
112+
113+ print ("MESSAGE: %s" % (msg .tostring ()))
114+
115+
116+ def main ():
117+
118+ sessionOptions = blpapi .SessionOptions ()
119+ sessionOptions .setServerHost (d_host )
120+ sessionOptions .setServerPort (d_port )
121+
122+ print ("Connecting to %s:%d" % (d_host ,d_port ))
123+
124+ eventHandler = SessionEventHandler ()
125+
126+ session = blpapi .Session (sessionOptions , eventHandler .processEvent )
127+
128+ if not session .startAsync ():
129+ print ("Failed to start session." )
130+ return
131+
132+ global bEnd
133+ while bEnd == False :
134+ pass
135+
136+ session .stop ()
137+
138+ if __name__ == "__main__" :
139+ print ("Bloomberg - EMSX API Example - GetTraders" )
140+ try :
141+ main ()
142+ except KeyboardInterrupt :
143+ print ("Ctrl+C pressed. Stopping..." )
144+
145+
146+ __copyright__ = """
147+ Copyright 2017. Bloomberg Finance L.P.
148+ Permission is hereby granted, free of charge, to any person obtaining a copy
149+ of this software and associated documentation files (the "Software"), to
150+ deal in the Software without restriction, including without limitation the
151+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
152+ sell copies of the Software, and to permit persons to whom the Software is
153+ furnished to do so, subject to the following conditions: The above
154+ copyright notice and this permission notice shall be included in all copies
155+ or substantial portions of the Software.
156+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
157+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
158+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
159+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
160+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
161+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
162+ IN THE SOFTWARE.
163+ """
0 commit comments