5

假设我们使用 .Net API 发送多个证券的彭博历史数据请求,是否有任何优雅的方法来分离/识别每个证券的响应数据?

我知道sequenceNumber响应中有一个字段,但不确定它是如何映射到每个证券的。我的代码片段如下所示。

Service refDataService = session.GetService("//blp/refdata");
Request request = refDataService.CreateRequest("HistoricalDataRequest");
Element securities = request.GetElement("securities");
securities.AppendValue("EURUSD CURNCY");
securities.AppendValue("USDJPY CURNCY");
securities.AppendValue("GBPUSD Curncy");
securities.AppendValue("USDCHF Curncy");

Element fields = request.GetElement("fields");                
fields.AppendValue("OPEN");
fields.AppendValue("HIGH");
fields.AppendValue("LOW");
fields.AppendValue("PX_LAST");

request.Set("periodicityAdjustment", "CALENDAR");  
request.Set("periodicitySelection", "DAILY");
request.Set("startDate", DateTime.Now.AddDays(-30).ToString("yyyyMMdd"));
request.Set("endDate", Convert.ToString(null));

session.SendRequest(request, null);

while (true)
{
Event eventObj = session.NextEvent();
foreach (Message msg in eventObj.GetMessages()) 
{
    if (msg.MessageType.Equals(Name.GetName("HistoricalDataResponse")))
    {
        Element secDataArray = msg.GetElement("securityData");          
        Element securityData = secDataArray.GetElement(3);
        for (int index = 0; index < securityData.NumValues; index++)
        {
            Element fieldData = securityData.GetValueAsElement(index);
            Datetime time = fieldData.GetElementAsDate("date");
            decimal? open = fieldData.HasElement("OPEN") ? Convert.ToDecimal(fieldData.GetElementAsFloat64("OPEN")) : new decimal?();
            decimal? high = fieldData.HasElement("HIGH") ? Convert.ToDecimal(fieldData.GetElementAsFloat64("HIGH")) : new decimal?();
            decimal? low = fieldData.HasElement("LOW") ? Convert.ToDecimal(fieldData.GetElementAsFloat64("LOW")) : new decimal?();
            decimal? close = fieldData.HasElement("PX_LAST") ? Convert.ToDecimal(fieldData.GetElementAsFloat64("PX_LAST")) : new decimal?();
        }
    }
}
}
4

4 回答 4

4

返回的sequenceNumber值对应于您通过调用添加证券的顺序AppendValue

securities.AppendValue("EURUSD CURNCY");  // Sequence number 1
securities.AppendValue("USDJPY CURNCY");  // Sequence number 2
securities.AppendValue("GBPUSD Curncy");  // Sequence number 3
securities.AppendValue("USDCHF Curncy");  // Sequence number 4

根据BLPAPI:开发人员指南

安全序列号,指定安全在请求中的位置。

于 2012-09-07T13:48:54.887 回答
3

您可能还需要一个循环:

foreach (Message msg in eventObj.GetMessages())
{
   Element element = msg.GetElement("securityData");

   for (int i = 0; i < element.NumValues; i++)
   {
      Element security = element.GetValueAsElement(i);
      string ticker = security.GetElementAsString("security");
      Element fields = security.GetElement("fieldData");
      decimal px_last  = Convert.toDecimal(fields.GetElementAsFloat64("PX_LAST"));
   }
}
于 2012-12-05T08:10:52.617 回答
0

在 Java 中(易于转置):

Element securityData = msg.getElement("securityData");
String ticker = securityData.getElementAsString("security");
于 2012-12-05T18:50:13.810 回答
0

您可以从securityDataElement 中提取 Bloomberg 代码,如下所示:

string bloombergCode = secDataArray.GetElement("security").GetValueAsString();
于 2015-09-20T21:02:27.803 回答