如何在彭博社获取实时价格快照?

发布于 2024-12-03 03:49:41 字数 1032 浏览 2 评论 0原文

我希望使用 C# 从 Bloomberg .Net API 3 获取实时价格快照。

我可以从示例中看到如何获取历史价格或订阅数据,但我找不到获取订单簿快照的正确请求,即买价/卖价/最后交易价格和数量。

对于日内报价,我会做这样的事情:

Service refDataService = d_session.GetService("//blp/refdata");
// create intraday tick request
Request request = refDataService.CreateRequest("IntradayTickRequest");
// set request parameters
request.Set("includeConditionCodes", checkBoxIncludeConditionCode.Checked);
request.Set("includeExchangeCodes", checkBoxIncludeExchangeCode.Checked);
Element eventTypes = request.GetElement("eventTypes");
eventTypes.AppendValue("TRADE");
eventTypes.AppendValue("BID");
eventTypes.AppendValue("ASK");
request.Set("security", d_requestSecurity);
request.Set("startDateTime", new BDateTime(startDate.Year, startDate.Month,
             startDate.Day,startDate.Hour, startDate.Minute, startDate.Second, 0));
request.Set("endDateTime", new BDateTime(endDate.Year, endDate.Month, endDate.Day,
             endDate.Hour, endDate.Minute, endDate.Second, 0));

是否有不同的实时快照请求?

I am looking to get a live price snapshot from Bloomberg .Net API 3 with C#.

I can see from the samples how to get historical prices or subscribe to data, but I cannot find the correct request to get an order book snapshot, ie Bid/Ask/Last trade Price and Quantities.

For an intraday tick I would do something like this:

Service refDataService = d_session.GetService("//blp/refdata");
// create intraday tick request
Request request = refDataService.CreateRequest("IntradayTickRequest");
// set request parameters
request.Set("includeConditionCodes", checkBoxIncludeConditionCode.Checked);
request.Set("includeExchangeCodes", checkBoxIncludeExchangeCode.Checked);
Element eventTypes = request.GetElement("eventTypes");
eventTypes.AppendValue("TRADE");
eventTypes.AppendValue("BID");
eventTypes.AppendValue("ASK");
request.Set("security", d_requestSecurity);
request.Set("startDateTime", new BDateTime(startDate.Year, startDate.Month,
             startDate.Day,startDate.Hour, startDate.Minute, startDate.Second, 0));
request.Set("endDateTime", new BDateTime(endDate.Year, endDate.Month, endDate.Day,
             endDate.Hour, endDate.Minute, endDate.Second, 0));

Is there a different, live snapshot request?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

莫相离 2024-12-10 03:49:41

最低程度改编自 API 附带的示例:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Bloomberglp.Blpapi;

namespace BbServerApiTool
{
    public class GetFields : GetBloombergFields
    {
        private static readonly Name EXCEPTIONS = new Name("exceptions");
        private static readonly Name FIELD_ID = new Name("fieldId");
        private static readonly Name REASON = new Name("reason");
        private static readonly Name CATEGORY = new Name("category");
        private static readonly Name DESCRIPTION = new Name("description");
        private static readonly Name ERROR_CODE = new Name("errorCode");
        private static readonly Name SOURCE = new Name("source");
        private static readonly Name SECURITY_ERROR = new Name("securityError");
        private static readonly Name MESSAGE = new Name("message");
        private static readonly Name RESPONSE_ERROR = new Name("responseError");
        private static readonly Name SECURITY_DATA = new Name("securityData");
        private static readonly Name FIELD_EXCEPTIONS = new Name("fieldExceptions");
        private static readonly Name ERROR_INFO = new Name("errorInfo");

        public override List<List<string>> GetBbFields(string[] tickers, string[] fieldsParam)
        {
            string serverHost = System.Configuration.ConfigurationManager.AppSettings["Host"];
            int serverPort = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["Port"]);

            var sessionOptions = new SessionOptions {ServerHost = serverHost, ServerPort = serverPort};

            var session = new Session(sessionOptions);
            session.Start();
            session.OpenService("//blp/refdata");
            Service refDataService = session.GetService("//blp/refdata");
            Request request = refDataService.CreateRequest("ReferenceDataRequest");
            Element securities = request.GetElement("securities");
            Element fields = request.GetElement("fields");
            request.Set("returnEids", true);

            foreach (var ticker in tickers)
            {
                securities.AppendValue(ticker);
            }

            foreach (var field in fieldsParam)
            {
                fields.AppendValue(field);
            }

            var cID = new CorrelationID(1);
            session.Cancel(cID);
            Results = new List<List<string>>();
            session.SendRequest(request, cID);

            while (true)
            {
                Event eventObj = session.NextEvent();
                processEvent(eventObj, session, fieldsParam);
                if (eventObj.Type == Event.EventType.RESPONSE)
                {
                    return Results;   
                }
            }
        }

        protected override string GetName()
        {
            return "BbServerApiTool";
        }

        private void processEvent(Event eventObj, Session session, string[] fields)
        {
            switch (eventObj.Type)
            {
                case Event.EventType.RESPONSE:
                case Event.EventType.PARTIAL_RESPONSE:
                    processRequestDataEvent(eventObj, session, fields);
                    break;
                default:
                    processMiscEvents(eventObj, session);
                    break;
            }
        }

        private void processMiscEvents(Event eventObj, Session session)
        {
            foreach (Message msg in eventObj.GetMessages())
            {
                switch (msg.MessageType.ToString())
                {
                    case "RequestFailure":
                        Element reason = msg.GetElement(REASON);
                        string message = string.Concat("Error: Source-", reason.GetElementAsString(SOURCE),
                            ", Code-", reason.GetElementAsString(ERROR_CODE), ", category-", reason.GetElementAsString(CATEGORY),
                            ", desc-", reason.GetElementAsString(DESCRIPTION));
                        throw new ArgumentException(message);
                    case "SessionStarted":
                    case "SessionTerminated":
                    case "SessionStopped":
                    case "ServiceOpened":
                    default:
                        break;
                }
            }
        }
        private void processRequestDataEvent(Event eventObj, Session session, string[] fields)
        {
            foreach (Message msg in eventObj.GetMessages())
            {
                if (msg.MessageType.Equals(Name.GetName("ReferenceDataResponse")))
                {
                    Element secDataArray = msg.GetElement(SECURITY_DATA);
                    int numberOfSecurities = secDataArray.NumValues;
                    for (int index = 0; index < numberOfSecurities; index++)
                    {
                        Element secData = secDataArray.GetValueAsElement(index);
                        Element fieldData = secData.GetElement("fieldData");

                        if (secData.HasElement(FIELD_EXCEPTIONS))
                        {
                            // process error
                            Element error = secData.GetElement(FIELD_EXCEPTIONS);
                            if (error.Elements.Count() > 0)
                            {
                                Element errorException = error.GetValueAsElement(0);
                                Element errorInfo = errorException.GetElement(ERROR_INFO);
                                string message = errorInfo.GetElementAsString(MESSAGE);
                                throw new ArgumentException(message);
                            }
                        }

                        var list = new List<string> { secData.GetElement("security").GetValueAsString() };
                        if (secData.HasElement(SECURITY_ERROR))
                        {
                            Element error = secData.GetElement(SECURITY_ERROR);
                            string errorMessage = error.GetElementAsString(MESSAGE);
                            //                            throw new ArgumentException(errorMessage);
                            //TODO Log
                            logger.WriteLine("Couldn't get a value for " + secData.GetElement("security").GetValueAsString());
                            foreach (var field in fields)
                            {
                                list.Add("N/A");
                            }
                        }
                        else
                        {
                            foreach (var field in fields)
                            {
                                Element item = fieldData.GetElement(field);
                                list.Add(item.IsNull ? "N/A" : item.GetValueAsString());
                            }
                        }
                        Results.Add(list);
                    }
                }
            }
        }
    }
}

Minimally adapted from the example that comes with the API:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Bloomberglp.Blpapi;

namespace BbServerApiTool
{
    public class GetFields : GetBloombergFields
    {
        private static readonly Name EXCEPTIONS = new Name("exceptions");
        private static readonly Name FIELD_ID = new Name("fieldId");
        private static readonly Name REASON = new Name("reason");
        private static readonly Name CATEGORY = new Name("category");
        private static readonly Name DESCRIPTION = new Name("description");
        private static readonly Name ERROR_CODE = new Name("errorCode");
        private static readonly Name SOURCE = new Name("source");
        private static readonly Name SECURITY_ERROR = new Name("securityError");
        private static readonly Name MESSAGE = new Name("message");
        private static readonly Name RESPONSE_ERROR = new Name("responseError");
        private static readonly Name SECURITY_DATA = new Name("securityData");
        private static readonly Name FIELD_EXCEPTIONS = new Name("fieldExceptions");
        private static readonly Name ERROR_INFO = new Name("errorInfo");

        public override List<List<string>> GetBbFields(string[] tickers, string[] fieldsParam)
        {
            string serverHost = System.Configuration.ConfigurationManager.AppSettings["Host"];
            int serverPort = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["Port"]);

            var sessionOptions = new SessionOptions {ServerHost = serverHost, ServerPort = serverPort};

            var session = new Session(sessionOptions);
            session.Start();
            session.OpenService("//blp/refdata");
            Service refDataService = session.GetService("//blp/refdata");
            Request request = refDataService.CreateRequest("ReferenceDataRequest");
            Element securities = request.GetElement("securities");
            Element fields = request.GetElement("fields");
            request.Set("returnEids", true);

            foreach (var ticker in tickers)
            {
                securities.AppendValue(ticker);
            }

            foreach (var field in fieldsParam)
            {
                fields.AppendValue(field);
            }

            var cID = new CorrelationID(1);
            session.Cancel(cID);
            Results = new List<List<string>>();
            session.SendRequest(request, cID);

            while (true)
            {
                Event eventObj = session.NextEvent();
                processEvent(eventObj, session, fieldsParam);
                if (eventObj.Type == Event.EventType.RESPONSE)
                {
                    return Results;   
                }
            }
        }

        protected override string GetName()
        {
            return "BbServerApiTool";
        }

        private void processEvent(Event eventObj, Session session, string[] fields)
        {
            switch (eventObj.Type)
            {
                case Event.EventType.RESPONSE:
                case Event.EventType.PARTIAL_RESPONSE:
                    processRequestDataEvent(eventObj, session, fields);
                    break;
                default:
                    processMiscEvents(eventObj, session);
                    break;
            }
        }

        private void processMiscEvents(Event eventObj, Session session)
        {
            foreach (Message msg in eventObj.GetMessages())
            {
                switch (msg.MessageType.ToString())
                {
                    case "RequestFailure":
                        Element reason = msg.GetElement(REASON);
                        string message = string.Concat("Error: Source-", reason.GetElementAsString(SOURCE),
                            ", Code-", reason.GetElementAsString(ERROR_CODE), ", category-", reason.GetElementAsString(CATEGORY),
                            ", desc-", reason.GetElementAsString(DESCRIPTION));
                        throw new ArgumentException(message);
                    case "SessionStarted":
                    case "SessionTerminated":
                    case "SessionStopped":
                    case "ServiceOpened":
                    default:
                        break;
                }
            }
        }
        private void processRequestDataEvent(Event eventObj, Session session, string[] fields)
        {
            foreach (Message msg in eventObj.GetMessages())
            {
                if (msg.MessageType.Equals(Name.GetName("ReferenceDataResponse")))
                {
                    Element secDataArray = msg.GetElement(SECURITY_DATA);
                    int numberOfSecurities = secDataArray.NumValues;
                    for (int index = 0; index < numberOfSecurities; index++)
                    {
                        Element secData = secDataArray.GetValueAsElement(index);
                        Element fieldData = secData.GetElement("fieldData");

                        if (secData.HasElement(FIELD_EXCEPTIONS))
                        {
                            // process error
                            Element error = secData.GetElement(FIELD_EXCEPTIONS);
                            if (error.Elements.Count() > 0)
                            {
                                Element errorException = error.GetValueAsElement(0);
                                Element errorInfo = errorException.GetElement(ERROR_INFO);
                                string message = errorInfo.GetElementAsString(MESSAGE);
                                throw new ArgumentException(message);
                            }
                        }

                        var list = new List<string> { secData.GetElement("security").GetValueAsString() };
                        if (secData.HasElement(SECURITY_ERROR))
                        {
                            Element error = secData.GetElement(SECURITY_ERROR);
                            string errorMessage = error.GetElementAsString(MESSAGE);
                            //                            throw new ArgumentException(errorMessage);
                            //TODO Log
                            logger.WriteLine("Couldn't get a value for " + secData.GetElement("security").GetValueAsString());
                            foreach (var field in fields)
                            {
                                list.Add("N/A");
                            }
                        }
                        else
                        {
                            foreach (var field in fields)
                            {
                                Element item = fieldData.GetElement(field);
                                list.Add(item.IsNull ? "N/A" : item.GetValueAsString());
                            }
                        }
                        Results.Add(list);
                    }
                }
            }
        }
    }
}
你在我安 2024-12-10 03:49:41

如果您想确保绝对实时定价,您可能会使用订阅服务 API (//blp/mktdata),它还将返回带有标记的最后一次交易的确切时间的价格。

在附录 C.2(订阅范例一)中通过 Bloomberg 终端提供的开发人员指南中有一个很好的例子。

If you want to ensure absolutely live pricing, you would probably use the subscription service api (//blp/mktdata), which will also return the price with the exact time of the last trade tagged to it.

There is a good example of this in the Developer's Guide available via a Bloomberg Terminal in appendix C.2 (The subscription paradigm one).

み青杉依旧 2024-12-10 03:49:41

彭博社似乎没有要求提供订单簿的“实时快照”。示例中显然记录了其他方法,但 Bloomberg 似乎没有在其 .Net API 中给出这一点。

有一些参考数据请求似乎最接近快照类型的查询,但没有关于更新这些变量的延迟的文档。 “参考”这个名字并不能激发人们对听起来像实时请求的东西的极大信心。

订阅是快照类型请求的替代方法,在许多应用程序中可能更优越。通过订阅,订单簿的更新将实时传输到您的套接字。这种方法的缺点是您需要内部架构来支持它,而且您可能需要等待一段不确定的时间才能看到某些市场的任何活动。

考虑到这一点,我认为尝试和错误的方法是最好的,并且与其他数据提供商合作可能会更加富有成果。

It appears there is no specific Bloomberg request for a 'Live Snapshot' of the order book. The other methods are obviously documented in the examples, but it seems Bloomberg do not give this in their .Net API.

There are reference data requests which seem to be the closest to a snapshot type of query, but there is no documentation on the latency in updating these variables. The name 'reference' does not inspire great confidence in something sounding like a real time request.

A subscription is the alternative method to a snapshot type of request and can be superior in many applications. With a subscription, updates to the order book are streamed live to your socket. The drawback with this approach is that you need the internal architecture to support it and also you may have to wait for an indeterminate length of time to see any activity in some markets.

With this in mind, I think a trial and error approach is best, and working with another data provider could be more fruitful.

指尖上的星空 2024-12-10 03:49:41

如果您需要获取实时价格而不是静态价格,您仍然可以通过 ReferenceDataRequest 来完成。
唯一的区别是使用什么字段。
PX_LAST 为您提供计入参考数据每月限额的最后价格。
LAST_PRICE 为您提供实时最新价格,该价格计入您的实时数据每月限额。

PS:我从我们的彭博销售代表那里得到了这个信息。

If you need to get real time prices and not the static ones, u can still do it through the ReferenceDataRequest.
The only difference what field to use.
PX_LAST gives you the last price that counts towards your reference data monthly limit.
LAST_PRICE gives you the real-time last price that counts towards your real-time data monthly limit.

PS: I got this info from our Bloomberg Sales Rep.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文