使用Bloomberg API的条形数据(即OHLCV)

时间:2014-03-20 23:41:12

标签: c# api bloomberg

我正在尝试终止旧的数据采购流程(在一张纸上使用Excel BDH公式),而是尝试将一个非常简单的C#控制台应用程序编写到Bloomberg C#Desktop API。

所有我需要的是复制这个BDH公式 - 基本上返回给定时间间隔的BAR数据:

= BDH(安全性,字段,startdatetime,enddatetime," BarTp = T"," BarSz = 1")

我尽力复制bloomberg文档中提供的示例,但无法处理我在下面提到的错误。

我检查了openbloomberg.com以及相关示例的stackoverflow问题,但是,简单地说,当我复制代码时,我能找到的最接近的示例返回错误。

以下代码是我到目前为止完成的整体结构:

using System;
using System.Collections.Generic;
using System.Text;


using CorrelationID = Bloomberglp.Blpapi.CorrelationID;
using Element = Bloomberglp.Blpapi.Element;
using Event = Bloomberglp.Blpapi.Event;
using Message = Bloomberglp.Blpapi.Message;
using Request = Bloomberglp.Blpapi.Request;
using Service = Bloomberglp.Blpapi.Service;
using Session = Bloomberglp.Blpapi.Session;
using SessionOptions = Bloomberglp.Blpapi.SessionOptions;
using BDateTime = Bloomberglp.Blpapi.Datetime;




namespace RequestResponseMultiple
{


class RequestResponseMultiple
{

    public static Request request;
    public static Session session;


    static void Main(string[] args)
    {


        open_one_bb_session();


        // Add securities
        request.Set("security", "GARAN TI Equity");
        request.Set("eventType", "TRADE");
        request.Set("interval", 1); // bar interval in minutes
        request.Set("startDateTime", new BDateTime(2014, 03, 21, 13, 30, 0, 0));
        request.Set("endDateTime", new BDateTime(2014, 03, 21, 15, 30, 0, 0));


        // send request and handle response!
        while (true)
        {

            //... SEND THE REQUEST...
            session.SendRequest(request, new CorrelationID(1));

            //... AND, HANDLE THE RESPONSE!
            bool continueToLoop = true;
            while (continueToLoop)
            {
                Event eventObj = session.NextEvent();
                switch (eventObj.Type)
                {
                    case Event.EventType.RESPONSE:
                        continueToLoop = false;
                        handleResponseEvent(eventObj);
                        break;

                    case Event.EventType.PARTIAL_RESPONSE:
                        handleResponseEvent(eventObj);
                        break;

                    default:
                        handleOtherEvent(eventObj);
                        break;
                }
            } // each eventobj

            Console.ReadKey();



        } // time

    } // void









    private static void open_one_bb_session()
    {
        // open session
        SessionOptions sessionOptions = new SessionOptions();
        sessionOptions.ServerHost = "localhost";
        sessionOptions.ServerPort = 8194;
        session = new Session(sessionOptions);
        if (!session.Start())
        {
            System.Console.WriteLine("Could not start session.");
            System.Environment.Exit(1);
        }

        // open server from the session
        if (!session.OpenService("//blp/refdata"))
        {
            System.Console.WriteLine("Could not open service " + "//blp/refdata");
            System.Environment.Exit(1);
        }

        // get server from the session
        Service refDataSvc = session.GetService("//blp/refdata");

        // Create request from this server
        // request = refDataSvc.CreateRequest("ReferenceDataRequest");
        request = refDataSvc.CreateRequest("IntradayBarRequest");

        // create intraday bar request 
        // request = refDataSvc.CreateRequest("IntradayBarRequest");
    }







    private static void handleResponseEvent(Event eventObj)
    {
        foreach (Message message in eventObj.GetMessages())
        {
            Element ReferenceDataResponse = message.AsElement;

            if (ReferenceDataResponse.HasElement("responseError"))
            {System.Environment.Exit(1);}

            Element securityDataArray =ReferenceDataResponse.GetElement("security");

            int numItems = securityDataArray.NumValues;

            for (int i = 0; i < numItems; ++i)
            {
                Element securityData =securityDataArray.GetValueAsElement(i);
                String security =securityData.GetElementAsString("security");
                int sequenceNumber =securityData.GetElementAsInt32("sequenceNumber");

                if (securityData.HasElement("securityError"))
                {
                    Element securityError =securityData.GetElement("securityError");
                    System.Console.WriteLine("* security =" + security);

                    securityError =securityData.GetElement("securityError");
                    securityError.Print(System.Console.Out);
                    return;
                }

                else
                {
                    Element Bars = message.GetElement("barData").GetElement("barTickData");
                     int nBars = Bars.NumValues;

                     for (int nB = 0; nB < nBars; ++nB) 
                    {
                        Element Bar = Bars.GetValueAsElement(nB);
                        BDateTime time = Bar.GetElementAsDate("time");
                        double open = Bar.GetElementAsFloat64("open");
                        double high = Bar.GetElementAsFloat64("high");
                        double low = Bar.GetElementAsFloat64("low");
                        double close = Bar.GetElementAsFloat64("close");
                        int numEvents = Bar.GetElementAsInt32("numEvents");
                        long volume = Bar.GetElementAsInt64("volume");

                        // Individually output each value
                        System.Console.WriteLine(
                            "security =" +security 
                            + " sequenceNumber =" +sequenceNumber
                            + ", " + time
                            + ", " + open
                            + ", " + high
                            + ", " + low
                            + ", " + close
                            + ", " + volume);
                    } // each Bar
                }

            } // num msg items

        } // msg

    } // void





    private static void handleOtherEvent(Event eventObj)
    {
        System.Console.WriteLine("EventType=" + eventObj.Type);

        foreach (Message message in eventObj.GetMessages())
        {
            System.Console.WriteLine("correlationID=" +message.CorrelationID);
            System.Console.WriteLine("messageType=" +message.MessageType);
            message.Print(System.Console.Out);
            if (Event.EventType.SESSION_STATUS == eventObj.Type && message.MessageType.Equals("SessionTerminated"))
            {
                System.Console.WriteLine("Terminating: " + message.MessageType);
                System.Environment.Exit(1);
            }
        } // each msg

    } // void





} // class

}// n

我的问题如下:

Q1-如何为这种tick数据请求设置startdatetime和enddatetime? 我在下面提供的代码返回错误:

&#34;未处理的类型&#39; Bloomberglp.Blpapi.NotFoundException&#39;发生在Bloomberglp.Blpapi.dll中。其他信息:Bloomberglp.Blpapi.NotFoundException:找不到安全性:IntradayBarResponse;

我的预感是,开始和结束时间无法正确设置(不幸的是,我无法用正确的方式来定义开始和结束日期)。这就是没有从服务器返回结果的原因。

其他来源:

提前感谢您的时间,帮助,支持,

Aykut Saribiyik

1 个答案:

答案 0 :(得分:2)

在bloomberg终端进行了不错的搜索后:

  • 打开WAPI

  • 选择DESKTOP API

  • 下载zip文件,解压缩并转到示例

  • 选择IntraDayExample.cs

这个例子正是我的要求。

由于