Click here to Skip to main content
15,891,674 members
Articles / Web Development / HTML
Tip/Trick

Bloomberg API End of Day Closing Quotes

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
24 Jun 2015CPOL 14.4K   144   3  
Get closing quotes for multiple securities with asynchronous call to Bloomberg API using VB.

Introduction

The Per Security WSDL allows to send a request for a financial information to Bloomberg. The request can be shaped in many ways. This example shows how to query closing quotes, volume, and high52weeks for multiple securities in a single call.

Background

Each call needs to have a header, instruments, and fields. It also needs to have an access information - path to the security certificate file on your local machine, and the password (the file and the password are provided by Bloomberg once you sign the service contract).

Using the Code

Login information is placed in PerSecurity.vb file:

VB.NET
Imports System
Imports System.IO
Imports System.Net
Imports System.Configuration
Imports System.Xml
Imports System.Security.Cryptography.X509Certificates
Imports BloomVB
Imports BloomVB.PerSecurityWSDL

Module PerSecurity

    Public Const DATA_NOT_AVAILABLE As Integer = 100
    Public Const SUCCESS As Integer = 0
    Public Const REQUEST_ERROR As Integer = 200
    Public Const POLL_INTERVAL As Integer = 5000

    Sub Main()

        Try
            '-- provide your access information
            Dim clientCert As New X509Certificate2("c:\Cert\DLWSCert.p12", "PASSWORD")
            Dim ps As BloomVB.PerSecurityWSDL.PerSecurityWSClient = _
            		New PerSecurityWSClient("PerSecurityWSPort")
            ps.ClientCredentials.ClientCertificate.Certificate = clientCert

            '-- run this to get end-of-day quotes (in EOD.vb file)
            Dim eodReq As New EOD()
            eodReq.run(ps)

        Catch ex As Exception
            WriteToFile(LogFile.ToString, ex.Message & ex.StackTrace)
        End Try

    End Sub

End Module

The API call is placed in EOD.vb file:

VB.NET
Imports System
Imports System.IO
Imports System.Net
Imports System.Configuration
Imports System.Xml
Imports System.Security.Cryptography.X509Certificates
Imports BloomVB
Imports BloomVB.PerSecurityWSDL

Public Class EOD
    Public Sub run(ByVal ps As PerSecurityWSClient, ByVal logfilename As String)

        Try
            '-- pass a comma-separated list of tickers (hard-coded here)
            Dim tickerslist As String = "MYO.AU,1530.HK,JPOST.RC,DTEA,VSI,GNC"
			
            '-- place tickers in an array
            Dim tickersymbols As String() = tickerslist.Split(",")
			
            '-- each ticker becomes an instrument
            Dim uBound As Integer = tickersymbols.Count
            Dim ticker As New Instrument()

            Dim tickers As Instrument()
            tickers = New Instrument() {ticker}

            Dim u As Integer

            For u = 0 To uBound - 1
                ticker = New Instrument()
                ticker.id = tickersymbols(u)
                ticker.yellowkey = MarketSector.Equity
                ticker.yellowkeySpecified = True
                Array.Resize(tickers, tickers.Length + 1)
                tickers(u) = ticker
            Next

            '-- array of instruments
            Dim instr As Instrument() = tickers.ToArray()
            Dim instrs As New Instruments()
            instrs.instrument = instr

            '-- Setting request header information	
            Dim getDataHeaders As New GetDataHeaders()
            getDataHeaders.secmaster = True
            getDataHeaders.secmasterSpecified = True
            getDataHeaders.closingvalues = True
            getDataHeaders.closingvaluesSpecified = True
            getDataHeaders.derived = True
            getDataHeaders.derivedSpecified = True
            Dim sbmtGtDtReq As New SubmitGetDataRequest()
            sbmtGtDtReq.headers = getDataHeaders
			
            '-- list fields you need to query
            sbmtGtDtReq.fields = New String() {"LAST_UPDATE_DATE_EOD", _
            "PX_LAST_EOD", "PX_VOLUME_EOD", "HIGH_52WEEK_EOD"}

            sbmtGtDtReq.instruments = instrs
			
            '-- send your request, and retrieve the response
            Dim sbmtGtDtResp As SubmitGetDataResponse = ps.submitGetDataRequest(sbmtGtDtReq)
            Dim rtrvGtDrReq As New RetrieveGetDataRequest()
            rtrvGtDrReq.responseId = sbmtGtDtResp.responseId
            Dim rtrvGtDrResp As RetrieveGetDataResponse

            '-- loop your thread while the response is not available
            Do
                System.Threading.Thread.Sleep(PerSecurity.POLL_INTERVAL)
                rtrvGtDrResp = ps.retrieveGetDataResponse(rtrvGtDrReq)
            Loop While rtrvGtDrResp.statusCode.code = PerSecurity.DATA_NOT_AVAILABLE

            '-- process the response once you've got it
            If rtrvGtDrResp.statusCode.code = PerSecurity.SUCCESS Then
			
                '-- helpful to have the responseID, 
                '-- in case you need to troubleshoot it with the support
                WriteToFile(LogFile.ToString, "Response ID: " & rtrvGtDrResp.responseId)
				
                'Displaying the RetrieveGetDataResponse
                For i As Integer = 0 To rtrvGtDrResp.instrumentDatas.Length - 1

                    WriteToFile(LogFile.ToString, _
                    rtrvGtDrResp.instrumentDatas(i).instrument.id.ToString)
                    For j As Integer = 0 To rtrvGtDrResp.instrumentDatas(i).data.Length - 1
                        If rtrvGtDrResp.instrumentDatas(i).data(j).isArray = True Then
                            'In case this is a bulk field request
                            For k As Integer = 0 _
                            To rtrvGtDrResp.instrumentDatas(i).data(j).bulkarray.Length - 1
                                WriteToFile(LogFile.ToString, _
                                "-------------------------")
                                For l As Integer = 0 _
                                To rtrvGtDrResp.instrumentDatas(i).data(j).bulkarray(k).data.Length - 1
                                    WriteToFile(LogFile.ToString, _
                                    rtrvGtDrResp.instrumentDatas(i).data(j).bulkarray(k).data(l).value)
                                Next
                            Next
                        Else
                            WriteToFile(LogFile.ToString, vbTab + rtrvGtDrResp.fields(j) + _
                            ": " + rtrvGtDrResp.instrumentDatas(i).data(j).value)
                        End If
                    Next
                Next
            End If

        Catch ex As Exception
            '--
        End Try
    End Sub
End Class

This is a log file produced as a result of running the call:

VB.NET
Response ID: XXXXXXXXXX-XXXXXXXXX
MYO.AU
	LAST_UPDATE_DATE_EOD: 06/09/2015
	PX_LAST_EOD: 3.480000
	PX_VOLUME_EOD: 221871
	HIGH_52WEEK_EOD: 3.920000
1530.HK
	LAST_UPDATE_DATE_EOD: N.A.
	PX_LAST_EOD: N.A.
	PX_VOLUME_EOD: 0
	HIGH_52WEEK_EOD: N.A.
JPOST.RC
	LAST_UPDATE_DATE_EOD: 
	PX_LAST_EOD: 
	PX_VOLUME_EOD: 
	HIGH_52WEEK_EOD: 
DTEA
	LAST_UPDATE_DATE_EOD: 06/08/2015
	PX_LAST_EOD: 28.810000
	PX_VOLUME_EOD: 1797455
	HIGH_52WEEK_EOD: 29.700000
VSI
	LAST_UPDATE_DATE_EOD: 06/08/2015
	PX_LAST_EOD: 39.180000
	PX_VOLUME_EOD: 391528
	HIGH_52WEEK_EOD: 49.040000
GNC
	LAST_UPDATE_DATE_EOD: 06/08/2015
	PX_LAST_EOD: 44.230000
	PX_VOLUME_EOD: 737081
	HIGH_52WEEK_EOD: 49.660000

Points of Interest

The LAST_UPDATE_DATE_EOD in the log file is different for different tickers, because these are international securities, and for some of them, the exchange can close as early as 2am EDT.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --