VBA #NA 请求数据尝试从 BBG 提取数据

问题描述 投票:0回答:1

在构建从 BBG 获取数据、取其倒数并输入到单元格 F16 和 G16 的按钮时,我遇到了 BBG 插件“NA 请求数据”问题。

Sub CommandButton1_Click()
    Dim inversePairTicker As String
    Dim bidValue As Double
    Dim askValue As Double
    Dim invBidValue As Double
    Dim invAskValue As Double
    inversePairTicker = Range("E13").Value

    If inversePairTicker = "" Then
          Range("F16").Formula  = "=BDP(E9 & "" BGN Curncy"", ""RQ002"")"
          Range("G16").Formula = "=BDP(E9 & "" BGN Curncy"", ""RQ004"")"
    Else
          With Worksheets("Inv Pair Calc")
               .Range("E4").Formula = "=BDP(""" & inversePairTicker & " BGN Curncy"", ""RQ002"")"
               .Range("F4").Formula = "=BDP(""" & inversePairTicker & " BGN Curncy"", ""RQ004"")"

               bidValue = .Range("E4").Value
               askValue = .Range("F4").Value

               invBidValue = 1 / askValue
               invAskValue = 1 / bidValue

           End With

           Range("F16").Value = invBidValue
           Range("G16").Value = invAskValue
    End If
End Sub

Inv Pair Calc 中的 E4 和 F4 单元有时会产生 #NA 请求数据,从而停止代码流。我必须退出错误页面,单击 Bloomberg 插件上的刷新按钮才能实际从 BBG 获取数据并将其填充到 E4 和 F4 中(删除 NA 错误)。我再次单击此命令按钮以填充我的目标单元格 F16 和 G16。知道如何解决吗?

我已向 BBG 确认该插件没有任何问题。谢谢!

excel vba finance bloomberg
1个回答
0
投票

根据 @assylias 使用 VBA API 的建议,我提供了一些同步 BDP 的基本示例代码,因为这似乎是一个常见问题。

这是按原样交付的:有最少的错误检查和异常处理(供读者练习)。该函数返回一种证券的一个字段。底层 API 将接受多种证券和多个字段,这些可以从响应中解析(再次为读者练习)。

相同的基本结构对于 BDH 和 BDS 调用有效,但它们需要不同的请求和响应处理。

Option Explicit

'Use Tools | References to include "Bloomberg API COM 3.5 Type Library"
'
'C# Reference: https://bloomberg.github.io/blpapi-docs/dotnet/3.24.4/html/N_Bloomberglp_Blpapi.htm

'Global variables
Private g_session As blpapicomLib2.Session
Private g_servRefData As blpapicomLib2.Service

Private Function bbgConnect() As Boolean
    bbgConnect = False

    If g_session Is Nothing Then
        'Initialize global session
        Set g_session = New blpapicomLib2.Session 'Usually the default Session options work
        
        'Try to start the Bloomberg session
        If g_session.Start Then
            'Acquire the global //blp/refdata service
            bbgConnect = g_session.OpenService("//blp/refdata")
            Set g_servRefData = g_session.GetService("//blp/refdata")
        End If
    Else
        bbgConnect = True
    End If
End Function

Public Function syncBDP(strTicker As String, strField As String) As Variant
    If bbgConnect() Then
        syncBDP = CVErr(xlErrNA)
        
        'Create a request
        Dim req As blpapicomLib2.REQUEST
        Set req = g_servRefData.CreateRequest("ReferenceDataRequest")
        
        'Add the request parameters
        req.Append "securities", strTicker
        req.Append "fields", strField
        
        'Send the request to Bloomberg
        g_session.SendRequest req
        
        Dim bContinue As Boolean
        bContinue = True
        
        Dim evt As blpapicomLib2.Event
        Dim iter As blpapicomLib2.MessageIterator
        Dim eltSecurity As blpapicomLib2.Element
        Dim eltData As blpapicomLib2.Element
        
        'Process the returned events
        While bContinue
            Set evt = g_session.NextEvent
           
            'Only care about RESPONSE events
            If evt.EventType = RESPONSE Then
                Set iter = evt.CreateMessageIterator

                'Iterate through the message, though only expecting one
                While iter.Next
                    'Extract the field result, again only expecting one
                    Set eltSecurity = iter.Message.GetElement("securityData").GetValueAsElement(0)
                    Set eltData = eltSecurity.GetElement("fieldData").GetElement(0)
                    syncBDP = eltData.GetValue(0)
                Wend
            
                'Have the result so break out of loop
                bContinue = False
            End If
        Wend
    Else
        syncBDP = "# No connection to Bloomberg!"
    End If
End Function

Public Sub testBDH()
    Dim v As Variant
    v = syncBDP("EUR Curncy", "PX_LAST")
    Debug.Print v
End Sub

在电子表格中:

enter image description here

Bloomberg API 的参考文档位于此处

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.