|
| 1 | +VERSION 1.0 CLASS |
| 2 | +BEGIN |
| 3 | + MultiUse = -1 'True |
| 4 | +END |
| 5 | +Attribute VB_Name = "CreateOrderAndRouteWithStrat" |
| 6 | +Attribute VB_GlobalNameSpace = False |
| 7 | +Attribute VB_Creatable = False |
| 8 | +Attribute VB_PredeclaredId = False |
| 9 | +Attribute VB_Exposed = False |
| 10 | +' Copyright 2017. Bloomberg Finance L.P. |
| 11 | +' |
| 12 | +' Permission is hereby granted, free of charge, to any person obtaining a copy |
| 13 | +' of this software and associated documentation files (the "Software"), to |
| 14 | +' deal in the Software without restriction, including without limitation the |
| 15 | +' rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 16 | +' sell copies of the Software, and to permit persons to whom the Software is |
| 17 | +' furnished to do so, subject to the following conditions: The above |
| 18 | +' copyright notice and this permission notice shall be included in all copies |
| 19 | +' or substantial portions of the Software. |
| 20 | +' |
| 21 | +' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | +' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | +' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | +' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | +' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | +' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 27 | +' IN THE SOFTWARE. |
| 28 | + |
| 29 | +Option Explicit |
| 30 | + |
| 31 | +Private WithEvents m_BBG_EMSX As blpapicomLib2.Session |
| 32 | +Attribute m_BBG_EMSX.VB_VarHelpID = -1 |
| 33 | +Public running As Boolean |
| 34 | +Private svc As blpapicomLib2.service |
| 35 | +Private emsxService As String |
| 36 | +Private requestID As blpapicomLib2.CorrelationId |
| 37 | + |
| 38 | +Private Sub Class_Initialize() |
| 39 | + |
| 40 | + log "Bloomberg - EMSX API Example - CreateOrderAndRouteEx" |
| 41 | + |
| 42 | + emsxService = "//blp/emapisvc_beta" |
| 43 | + |
| 44 | + Set m_BBG_EMSX = New blpapicomLib2.Session |
| 45 | + |
| 46 | + running = True |
| 47 | + |
| 48 | + m_BBG_EMSX.QueueEvents = True |
| 49 | + m_BBG_EMSX.Start |
| 50 | + |
| 51 | + |
| 52 | +End Sub |
| 53 | + |
| 54 | +Private Sub Class_Terminate() |
| 55 | + Set m_BBG_EMSX = Nothing |
| 56 | +End Sub |
| 57 | + |
| 58 | +Private Sub m_BBG_EMSX_ProcessEvent(ByVal obj As Object) |
| 59 | + |
| 60 | + On Error GoTo errHandler |
| 61 | + |
| 62 | + Dim eventObj As blpapicomLib2.Event |
| 63 | + |
| 64 | + ' Assign the returned data to a Bloomberg type event |
| 65 | + Set eventObj = obj |
| 66 | + |
| 67 | + If Application.Ready Then |
| 68 | + |
| 69 | + Select Case eventObj.EventType |
| 70 | + |
| 71 | + Case SESSION_STATUS |
| 72 | + processSessionEvent eventObj |
| 73 | + |
| 74 | + Case BLPSERVICE_STATUS |
| 75 | + processServiceEvent eventObj |
| 76 | + |
| 77 | + Case RESPONSE |
| 78 | + processResponseEvent eventObj |
| 79 | + |
| 80 | + End Select |
| 81 | + |
| 82 | + End If |
| 83 | + |
| 84 | + Exit Sub |
| 85 | + |
| 86 | +errHandler: |
| 87 | + Dim errmsg As Variant |
| 88 | + errmsg = Err.Description |
| 89 | + log (errmsg) |
| 90 | + running = False |
| 91 | + |
| 92 | +End Sub |
| 93 | + |
| 94 | + |
| 95 | +Private Sub processSessionEvent(evt As blpapicomLib2.Event) |
| 96 | + |
| 97 | + log "Processing SESSION_STATUS event" |
| 98 | + |
| 99 | + Dim it As blpapicomLib2.MessageIterator |
| 100 | + |
| 101 | + Set it = evt.CreateMessageIterator() |
| 102 | + |
| 103 | + ' Loop while we have messages remaining |
| 104 | + Do While it.Next() |
| 105 | + |
| 106 | + Dim msg As Message |
| 107 | + |
| 108 | + ' Pick up message |
| 109 | + Set msg = it.Message |
| 110 | + |
| 111 | + log "MessageType: " + msg.MessageTypeAsString |
| 112 | + |
| 113 | + If msg.MessageTypeAsString = "SessionStarted" Then |
| 114 | + log "Session started..." |
| 115 | + m_BBG_EMSX.OpenService emsxService |
| 116 | + ElseIf msg.MessageTypeAsString = "SessionStartupFailure" Then |
| 117 | + log "Error: Session startup failed" |
| 118 | + running = False |
| 119 | + End If |
| 120 | + |
| 121 | + Loop |
| 122 | + |
| 123 | +End Sub |
| 124 | + |
| 125 | +Private Sub processServiceEvent(evt As blpapicomLib2.Event) |
| 126 | + |
| 127 | + Dim req As REQUEST |
| 128 | + Dim service As service |
| 129 | + Dim strategy As Element |
| 130 | + Dim indicator As Element |
| 131 | + Dim data As Element |
| 132 | + Dim it As blpapicomLib2.MessageIterator |
| 133 | + |
| 134 | + On Error GoTo failed |
| 135 | + |
| 136 | + log "Processing SERVICE_STATUS event" |
| 137 | + |
| 138 | + Set it = evt.CreateMessageIterator() |
| 139 | + |
| 140 | + ' Loop while we have messages remaining |
| 141 | + Do While it.Next() |
| 142 | + |
| 143 | + Dim msg As Message |
| 144 | + |
| 145 | + ' Pick up message |
| 146 | + Set msg = it.Message |
| 147 | + |
| 148 | + log "MessageType: " + msg.MessageTypeAsString |
| 149 | + |
| 150 | + If msg.MessageTypeAsString = "ServiceOpened" Then |
| 151 | + |
| 152 | + ' Get the service |
| 153 | + Set service = m_BBG_EMSX.GetService(emsxService) |
| 154 | + |
| 155 | + 'First, create our request object |
| 156 | + Set req = service.CreateRequest("CreateOrderAndRouteEx") |
| 157 | + |
| 158 | + 'The fields below are mandatory |
| 159 | + req.Set "EMSX_TICKER", "IBM US Equity" |
| 160 | + req.Set "EMSX_AMOUNT", 1000 |
| 161 | + req.Set "EMSX_ORDER_TYPE", "MKT" |
| 162 | + req.Set "EMSX_TIF", "DAY" |
| 163 | + req.Set "EMSX_HAND_INSTRUCTION", "ANY" |
| 164 | + req.Set "EMSX_SIDE", "BUY" |
| 165 | + req.Set "EMSX_BROKER", "BMTB" |
| 166 | + |
| 167 | + 'The fields below are optional |
| 168 | + 'req.Set "EMSX_ACCOUNT", "TestAccount" |
| 169 | + 'req.Set "EMSX_BOOKNAME", "HedgingBasket" |
| 170 | + 'req.Set "EMSX_BASKET_NAME", "HedgingBasket" |
| 171 | + 'req.Set "EMSX_CFD_FLAG", "1" |
| 172 | + 'req.Set "EMSX_CLEARING_ACCOUNT", "ClrAccName" |
| 173 | + 'req.Set "EMSX_CLEARING_FIRM", "FirmName" |
| 174 | + 'req.Set "EMSX_CUSTOM_NOTE1", "Note1" |
| 175 | + 'req.Set "EMSX_CUSTOM_NOTE2", "Note2" |
| 176 | + 'req.Set "EMSX_CUSTOM_NOTE3", "Note3" |
| 177 | + 'req.Set "EMSX_CUSTOM_NOTE4", "Note4" |
| 178 | + 'req.Set "EMSX_CUSTOM_NOTE5", "Note5" |
| 179 | + 'req.Set "EMSX_EXCHANGE_DESTINATION", "ExchDest" |
| 180 | + 'req.Set "EMSX_EXEC_INSTRUCTIONS", "AnyInst" |
| 181 | + 'req.Set "EMSX_GET_WARNINGS", "0" |
| 182 | + 'req.Set "EMSX_GTD_DATE", "20170105" |
| 183 | + 'req.Set "EMSX_INVESTOR_ID", "InvID" |
| 184 | + 'req.Set "EMSX_LIMIT_PRICE", 123.45 |
| 185 | + 'req.Set "EMSX_LOCATE_BROKER", "BMTB" |
| 186 | + 'req.Set "EMSX_LOCATE_ID", "SomeID" |
| 187 | + 'req.Set "EMSX_LOCATE_REQ", "Y" |
| 188 | + 'req.Set "EMSX_NOTES", "Some notes" |
| 189 | + 'req.Set "EMSX_ODD_LOT", "0" |
| 190 | + 'req.Set "EMSX_ORDER_ORIGIN", "" |
| 191 | + 'req.Set "EMSX_ORDER_REF_ID", "UniqueID" |
| 192 | + 'req.Set "EMSX_P_A", "P" |
| 193 | + 'req.Set "EMSX_RELEASE_TIME", 34341 |
| 194 | + 'req.Set "EMSX_REQUEST_SEQ", 1001 |
| 195 | + 'req.Set "EMSX_ROUTE_REF_ID", "UniqueID" |
| 196 | + 'req.Set "EMSX_SETTLE_CURRENCY", "USD" |
| 197 | + 'req.Set "EMSX_SETTLE_DATE", 20170106 |
| 198 | + 'req.Set "EMSX_SETTLE_TYPE", "T+2" |
| 199 | + 'req.Set "EMSX_STOP_PRICE", 123.5 |
| 200 | + |
| 201 | + 'Below we establish the strategy details |
| 202 | + Set strategy = req.GetElement("EMSX_STRATEGY_PARAMS") |
| 203 | + strategy.SetElement "EMSX_STRATEGY_NAME", "VWAP" |
| 204 | + |
| 205 | + Set indicator = strategy.GetElement("EMSX_STRATEGY_FIELD_INDICATORS") |
| 206 | + Set data = strategy.GetElement("EMSX_STRATEGY_FIELDS") |
| 207 | + |
| 208 | + 'Strategy parameters must be appended in the correct order. See the output |
| 209 | + 'of GetBrokerStrategyInfo request for the order. The indicator value is 0 for |
| 210 | + 'a field that carries a value, and 1 where the field should be ignored |
| 211 | + |
| 212 | + data.AppendElment().SetElement "EMSX_FIELD_DATA", "09:30:00" 'StartTime |
| 213 | + indicator.AppendElment().SetElement "EMSX_FIELD_INDICATOR", 0 |
| 214 | + |
| 215 | + data.AppendElment().SetElement "EMSX_FIELD_DATA", "10:30:00" 'EndTime |
| 216 | + indicator.AppendElment().SetElement "EMSX_FIELD_INDICATOR", 0 |
| 217 | + |
| 218 | + data.AppendElment().SetElement "EMSX_FIELD_DATA", "" 'Max%Volume |
| 219 | + indicator.AppendElment().SetElement "EMSX_FIELD_INDICATOR", 1 |
| 220 | + |
| 221 | + data.AppendElment().SetElement "EMSX_FIELD_DATA", "" '%AMSession |
| 222 | + indicator.AppendElment().SetElement "EMSX_FIELD_INDICATOR", 1 |
| 223 | + |
| 224 | + data.AppendElment().SetElement "EMSX_FIELD_DATA", "" 'OPG |
| 225 | + indicator.AppendElment().SetElement "EMSX_FIELD_INDICATOR", 1 |
| 226 | + |
| 227 | + data.AppendElment().SetElement "EMSX_FIELD_DATA", "" 'MOC |
| 228 | + indicator.AppendElment().SetElement "EMSX_FIELD_INDICATOR", 1 |
| 229 | + |
| 230 | + data.AppendElment().SetElement "EMSX_FIELD_DATA", "" 'CompletePX |
| 231 | + indicator.AppendElment().SetElement "EMSX_FIELD_INDICATOR", 1 |
| 232 | + |
| 233 | + data.AppendElment().SetElement "EMSX_FIELD_DATA", "" 'TriggerPX |
| 234 | + indicator.AppendElment().SetElement "EMSX_FIELD_INDICATOR", 1 |
| 235 | + |
| 236 | + data.AppendElment().SetElement "EMSX_FIELD_DATA", "" 'DarkComplete |
| 237 | + indicator.AppendElment().SetElement "EMSX_FIELD_INDICATOR", 1 |
| 238 | + |
| 239 | + data.AppendElment().SetElement "EMSX_FIELD_DATA", "" 'DarkCompPX |
| 240 | + indicator.AppendElment().SetElement "EMSX_FIELD_INDICATOR", 1 |
| 241 | + |
| 242 | + data.AppendElment().SetElement "EMSX_FIELD_DATA", "" 'RefIndex |
| 243 | + indicator.AppendElment().SetElement "EMSX_FIELD_INDICATOR", 1 |
| 244 | + |
| 245 | + data.AppendElment().SetElement "EMSX_FIELD_DATA", "" 'Discretion |
| 246 | + indicator.AppendElment().SetElement "EMSX_FIELD_INDICATOR", 1 |
| 247 | + |
| 248 | + log "Request: " & req.Print |
| 249 | + |
| 250 | + ' Send the request |
| 251 | + Set requestID = m_BBG_EMSX.SendRequest(req) |
| 252 | + |
| 253 | + ElseIf msg.MessageTypeAsString = "ServiceOpenFailure" Then |
| 254 | + |
| 255 | + log "Error: Service failed to open" |
| 256 | + running = False |
| 257 | + |
| 258 | + End If |
| 259 | + |
| 260 | + Loop |
| 261 | + |
| 262 | + Exit Sub |
| 263 | + |
| 264 | +failed: |
| 265 | + |
| 266 | + log "Failed to send the request: " + Err.Description |
| 267 | + |
| 268 | + running = False |
| 269 | + Exit Sub |
| 270 | + |
| 271 | +End Sub |
| 272 | + |
| 273 | +Private Sub processResponseEvent(evt As blpapicomLib2.Event) |
| 274 | + |
| 275 | + log "Processing RESPONSE event" |
| 276 | + |
| 277 | + Dim it As blpapicomLib2.MessageIterator |
| 278 | + Dim i As Integer |
| 279 | + Dim errorCode As Long |
| 280 | + Dim errorMessage As String |
| 281 | + |
| 282 | + Set it = evt.CreateMessageIterator() |
| 283 | + |
| 284 | + ' Loop while we have messages remaining |
| 285 | + Do While it.Next() |
| 286 | + |
| 287 | + Dim msg As Message |
| 288 | + |
| 289 | + ' Pick up message |
| 290 | + Set msg = it.Message |
| 291 | + |
| 292 | + log "MessageType: " + msg.MessageTypeAsString |
| 293 | + |
| 294 | + If evt.EventType = RESPONSE And msg.CorrelationId.Value = requestID.Value Then |
| 295 | + |
| 296 | + If msg.MessageTypeAsString = "ErrorInfo" Then |
| 297 | + |
| 298 | + errorCode = msg.GetElement("ERROR_CODE") |
| 299 | + errorMessage = msg.GetElement("ERROR_MESSAGE") |
| 300 | + |
| 301 | + log "ERROR CODE: " & errorCode & " ERROR DESCRIPTION: " & errorMessage |
| 302 | + |
| 303 | + running = False |
| 304 | + |
| 305 | + ElseIf msg.MessageTypeAsString = "CreateOrderAndRouteEx" Then |
| 306 | + |
| 307 | + Dim emsxSequence As Long |
| 308 | + Dim emsxRouteId As Long |
| 309 | + Dim msgdesc As String |
| 310 | + |
| 311 | + emsxSequence = msg.GetElement("EMSX_SEQUENCE") |
| 312 | + emsxRouteId = msg.GetElement("EMSX_ROUTE_ID") |
| 313 | + msgdesc = msg.GetElement("MESSAGE") |
| 314 | + |
| 315 | + log "EMSX_SEQUENCE: " & emsxSequence & " EMSX_ROUTE_ID: " & emsxRouteId & " MESSAGE: " & msgdesc |
| 316 | + |
| 317 | + m_BBG_EMSX.Stop |
| 318 | + running = False |
| 319 | + |
| 320 | + End If |
| 321 | + End If |
| 322 | + Loop |
| 323 | + |
| 324 | +End Sub |
| 325 | + |
| 326 | + |
| 327 | + |
| 328 | + |
0 commit comments