/
GetBrokerStrategiesWithAssetClass.cs
233 lines (185 loc) · 8.61 KB
/
GetBrokerStrategiesWithAssetClass.cs
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
/* Copyright 2017. 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.
*/
using Name = Bloomberglp.Blpapi.Name;
using SessionOptions = Bloomberglp.Blpapi.SessionOptions;
using Session = Bloomberglp.Blpapi.Session;
using Service = Bloomberglp.Blpapi.Service;
using Request = Bloomberglp.Blpapi.Request;
using Element = Bloomberglp.Blpapi.Element;
using CorrelationID = Bloomberglp.Blpapi.CorrelationID;
using Event = Bloomberglp.Blpapi.Event;
using Message = Bloomberglp.Blpapi.Message;
using EventHandler = Bloomberglp.Blpapi.EventHandler;
using System;
namespace com.bloomberg.emsx.samples
{
public class GetBrokerStrategiesWithAssetClass
{
private static readonly Name SESSION_STARTED = new Name("SessionStarted");
private static readonly Name SESSION_STARTUP_FAILURE = new Name("SessionStartupFailure");
private static readonly Name SERVICE_OPENED = new Name("ServiceOpened");
private static readonly Name SERVICE_OPEN_FAILURE = new Name("ServiceOpenFailure");
private static readonly Name ERROR_INFO = new Name("ErrorInfo");
private static readonly Name GET_BROKER_STRATEGIES_WITH_ASSET_CLASS = new Name("GetBrokerStrategiesWithAssetClass");
private string d_service;
private string d_host;
private int d_port;
private static bool quit = false;
private CorrelationID requestID;
public static void Main(String[] args)
{
System.Console.WriteLine("Bloomberg - EMSX API Example - GetBrokerStrategiesWithAssetClass\n");
GetBrokerStrategiesWithAssetClass example = new GetBrokerStrategiesWithAssetClass();
example.run(args);
while (!quit) { };
System.Console.WriteLine("Press ENTER to terminate...");
System.Console.ReadKey();
}
public GetBrokerStrategiesWithAssetClass()
{
// Define the service required, in this case the beta service,
// and the values to be used by the SessionOptions object
// to identify IP/port of the back-end process.
d_service = "//blp/emapisvc_beta";
d_host = "localhost";
d_port = 8194;
}
private void run(String[] args)
{
SessionOptions d_sessionOptions = new SessionOptions();
d_sessionOptions.ServerHost = d_host;
d_sessionOptions.ServerPort = d_port;
Session session = new Session(d_sessionOptions, new EventHandler(processEvent));
session.StartAsync();
}
public void processEvent(Event evt, Session session)
{
try
{
switch (evt.Type)
{
case Event.EventType.SESSION_STATUS:
processSessionEvent(evt, session);
break;
case Event.EventType.SERVICE_STATUS:
processServiceEvent(evt, session);
break;
case Event.EventType.RESPONSE:
processResponseEvent(evt, session);
break;
default:
processMiscEvents(evt, session);
break;
}
}
catch (Exception e)
{
System.Console.Error.WriteLine(e);
}
}
private void processSessionEvent(Event evt, Session session)
{
System.Console.WriteLine("\nProcessing " + evt.Type);
foreach (Message msg in evt)
{
if (msg.MessageType.Equals(SESSION_STARTED))
{
System.Console.WriteLine("Session started...");
session.OpenServiceAsync(d_service);
}
else if (msg.MessageType.Equals(SESSION_STARTUP_FAILURE))
{
System.Console.Error.WriteLine("Error: Session startup failed");
}
}
}
private void processServiceEvent(Event evt, Session session)
{
System.Console.WriteLine("\nProcessing " + evt.Type);
foreach (Message msg in evt)
{
if (msg.MessageType.Equals(SERVICE_OPENED))
{
System.Console.WriteLine("Service opened...");
Service service = session.GetService(d_service);
Request request = service.CreateRequest("GetBrokerStrategiesWithAssetClass");
//request.set("EMSX_REQUEST_SEQ", 1);
request.Set("EMSX_ASSET_CLASS", "EQTY"); //one of EQTY, OPT, FUT or MULTILEG_OPT
request.Set("EMSX_BROKER", "BMTB");
System.Console.WriteLine("Request: " + request.ToString());
requestID = new CorrelationID();
// Submit the request
try
{
session.SendRequest(request, requestID);
}
catch (Exception ex)
{
System.Console.Error.WriteLine("Failed to send the request: " + ex.Message);
}
}
else if (msg.MessageType.Equals(SERVICE_OPEN_FAILURE))
{
System.Console.Error.WriteLine("Error: Service failed to open");
}
}
}
private void processResponseEvent(Event evt, Session session)
{
System.Console.WriteLine("Received Event: " + evt.Type);
foreach (Message msg in evt)
{
System.Console.WriteLine("MESSAGE: " + msg.ToString());
System.Console.WriteLine("CORRELATION ID: " + msg.CorrelationID);
if (evt.Type == Event.EventType.RESPONSE && msg.CorrelationID == requestID)
{
System.Console.WriteLine("Message Type: " + msg.MessageType);
if (msg.MessageType.Equals(ERROR_INFO))
{
int errorCode = msg.GetElementAsInt32("ERROR_CODE");
String errorMessage = msg.GetElementAsString("ERROR_MESSAGE");
System.Console.WriteLine("ERROR CODE: " + errorCode + "\tERROR MESSAGE: " + errorMessage);
}
else if (msg.MessageType.Equals(GET_BROKER_STRATEGIES_WITH_ASSET_CLASS))
{
Element strategies = msg.GetElement("EMSX_STRATEGIES");
int numValues = strategies.NumValues;
System.Console.WriteLine("Number of Strategies: " + numValues);
for (int i = 0; i < numValues; i++)
{
String strategy = strategies.GetValueAsString(i);
System.Console.WriteLine("EMSX_STRATEGIES: " + strategy);
}
}
quit = true;
session.Stop();
}
}
}
private void processMiscEvents(Event evt, Session session)
{
System.Console.WriteLine("Processing " + evt.Type);
foreach (Message msg in evt)
{
System.Console.WriteLine("MESSAGE: " + msg);
}
}
}
}