-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEMSXModifyOrder.py
More file actions
189 lines (132 loc) · 6.91 KB
/
Copy pathEMSXModifyOrder.py
File metadata and controls
189 lines (132 loc) · 6.91 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
182
183
184
185
186
187
188
189
# EMSXModifyOrder.py
import blpapi
import sys
SESSION_STARTED = blpapi.Name("SessionStarted")
SESSION_STARTUP_FAILURE = blpapi.Name("SessionStartupFailure")
SERVICE_OPENED = blpapi.Name("ServiceOpened")
SERVICE_OPEN_FAILURE = blpapi.Name("ServiceOpenFailure")
ERROR_INFO = blpapi.Name("ErrorInfo")
MODIFY_ORDER_EX = blpapi.Name("ModifyOrderEx")
d_service="//blp/emapisvc_beta"
d_host="localhost"
d_port=8194
bEnd=False
class SessionEventHandler():
def processEvent(self, event, session):
try:
if event.eventType() == blpapi.Event.SESSION_STATUS:
self.processSessionStatusEvent(event,session)
elif event.eventType() == blpapi.Event.SERVICE_STATUS:
self.processServiceStatusEvent(event,session)
elif event.eventType() == blpapi.Event.RESPONSE:
self.processResponseEvent(event)
else:
self.processMiscEvents(event)
except:
print ("Exception: %s" % sys.exc_info()[0])
return False
def processSessionStatusEvent(self,event,session):
print ("Processing SESSION_STATUS event")
for msg in event:
if msg.messageType() == SESSION_STARTED:
print ("Session started...")
session.openServiceAsync(d_service)
elif msg.messageType() == SESSION_STARTUP_FAILURE:
print("Error: Session startup failed", file=sys.stderr)
else:
print (msg)
def processServiceStatusEvent(self,event,session):
print ("Processing SERVICE_STATUS event")
for msg in event:
if msg.messageType() == SERVICE_OPENED:
print ("Service opened...")
service = session.getService(d_service)
request = service.createRequest("ModifyOrderEx")
# The fields below are mandatory
request.set("EMSX_SEQUENCE", 4116143)
request.set("EMSX_AMOUNT", 500)
request.set("EMSX_ORDER_TYPE", "MKT")
request.set("EMSX_TIF", "DAY")
request.set("EMSX_TICKER", "CVX US Equity")
# The fields below are optional
#request.set("EMSX_HAND_INSTRUCTION", "ANY")
#request.set("EMSX_ACCOUNT","TestAccount")
#request.set("EMSX_CFD_FLAG", "1")
#request.set("EMSX_EXEC_INSTRUCTION", "Drop down values from EMSX Ticket")
#request.set("EMSX_GET_WARNINGS", "0")
#request.set("EMSX_GTD_DATE", "20170105")
#request.set("EMSX_INVESTOR_ID", "InvID")
#request.set("EMSX_LIMIT_PRICE", 123.45)
#request.set("EMSX_NOTES", "Some notes")
#request.set("EMSX_REQUEST_SEQ", 1001)
#request.set("EMSX_STOP_PRICE", 123.5)
# Note: When changing order type to a LMT order, you will need to provide the EMSX_LIMIT_PRICE value.
# When changing order type away from LMT order, you will need to reset the EMSX_LIMIT_PRICE value
# by setting the content to -99999
# Note: To clear down the stop price, set the content to -1
# If modifying on behalf of another trader, set the order owner's UUID
#request.set("EMSX_TRADER_UUID", 1234567)
print ("Request: %s" % request.toString())
self.requestID = blpapi.CorrelationId()
session.sendRequest(request, correlationId=self.requestID )
elif msg.messageType() == SERVICE_OPEN_FAILURE:
print("Error: Service failed to open", file=sys.stderr)
def processResponseEvent(self, event):
print ("Processing RESPONSE event")
for msg in event:
print ("MESSAGE: %s" % msg.toString())
print ("CORRELATION ID: %d" % msg.correlationIds()[0].value())
if msg.correlationIds()[0].value() == self.requestID.value():
print ("MESSAGE TYPE: %s" % msg.messageType())
if msg.messageType() == ERROR_INFO:
errorCode = msg.getElementAsInteger("ERROR_CODE")
errorMessage = msg.getElementAsString("ERROR_MESSAGE")
print ("ERROR CODE: %d\tERROR MESSAGE: %s" % (errorCode,errorMessage))
elif msg.messageType() == MODIFY_ORDER_EX:
emsx_sequence = msg.getElementAsInteger("EMSX_SEQUENCE")
message = msg.getElementAsString("MESSAGE")
print ("EMSX_SEQUENCE: %d\tMESSAGE: %s" % (emsx_sequence,message))
global bEnd
bEnd = True
def processMiscEvents(self, event):
print ("Processing " + event.eventType() + " event")
for msg in event:
print ("MESSAGE: %s" % (msg.tostring()))
def main():
sessionOptions = blpapi.SessionOptions()
sessionOptions.setServerHost(d_host)
sessionOptions.setServerPort(d_port)
print ("Connecting to %s:%d" % (d_host,d_port))
eventHandler = SessionEventHandler()
session = blpapi.Session(sessionOptions, eventHandler.processEvent)
if not session.startAsync():
print ("Failed to start session.")
return
global bEnd
while bEnd==False:
pass
session.stop()
if __name__ == "__main__":
print ("Bloomberg - EMSX API Example - ModifyOrderEx")
try:
main()
except KeyboardInterrupt:
print ("Ctrl+C pressed. Stopping...")
__copyright__ = """
Copyright 2024. 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.
"""