假设我们使用 .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?();
}
}
}
}