使用彭博API的酒吧数据(即OHLCV)

本文关键字:OHLCV 数据 酒吧 彭博 API | 更新日期: 2023-09-27 17:51:17

我试图终止我旧的数据来源过程(在一个工作表上使用Excel BDH公式),而不是试图编写一个非常简单的c#控制台应用程序到彭博c#桌面API。

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

= BDH(security,fields,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 ?下面我提供的代码返回错误:

"类型为' bloomberg . blpapi '的未处理异常。在bloomberg . blpapi .dll中发生NotFoundException。附加信息:bloomberg glp. blpapi . notfoundexception: security not found in: IntradayBarResponse;

我的直觉是,开始和结束时间不能正确设置(不幸的是,我不能想出正确的方法来定义开始和结束日期)。这就是为什么没有结果从服务器返回。

额外的来源:

  • 一个可能与这个问题相关的链接:每小时数据使用Bloomberg .Net API

  • 另一个直接来自bloomberg示例的链接在这里!:

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

Aykut Saribiyik

使用彭博API的酒吧数据(即OHLCV)

在彭博终端上仔细搜索后:

    开放WAPI
  • 选择DESKTOP API

  • 下载zip文件,解压缩,然后转到examples

  • 选择IntraDayExample.cs

这个例子正是我所需要的。

谢谢