在Bloomberg C#API中实现覆盖

时间:2016-06-14 07:24:55

标签: c# bloomberg overrides

查询" YAS_YLD_SPREAD"对于多个证券,我试图为" YAS_BOND_PX"分配不同的覆盖值。覆盖每个(根据Excel快照)

enter image description here

但是,我在分配覆盖时做错了,因为输出没有返回单个" YAS_YLD_SPREAD"对于每个安全性(它也虚假地返回" YAS_YLD_SPREAD 1")并且返回的值不是我期望/需要的100.21,645.06值

enter image description here

感谢你的帮助,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ArrayList = System.Collections.ArrayList;
using Event = Bloomberglp.Blpapi.Event;
using Element = Bloomberglp.Blpapi.Element;
using Message = Bloomberglp.Blpapi.Message;
using Name = Bloomberglp.Blpapi.Name;
using Request = Bloomberglp.Blpapi.Request;
using Service = Bloomberglp.Blpapi.Service;
using Session = Bloomberglp.Blpapi.Session;
using SessionOptions = Bloomberglp.Blpapi.SessionOptions;
using InvalidRequestException =
       Bloomberglp.Blpapi.InvalidRequestException;
using Subscription = Bloomberglp.Blpapi.Subscription;  

namespace COATI
{
    public class BloombergFetch
    {
        public string[] IsinArray;

        private const String APIREFDATA_SVC = "//blp/refdata";
        private static readonly Name SECURITY_DATA =
                                        Name.GetName("securityData");
        private static readonly Name SECURITY = Name.GetName("security");
        private static readonly Name FIELD_DATA = Name.GetName("fieldData");
        private static readonly Name RESPONSE_ERROR =
                                          Name.GetName("responseError");

        private static readonly Name SECURITY_ERROR =
                                         Name.GetName("securityError");
        private static readonly Name FIELD_EXCEPTIONS =
                                          Name.GetName("fieldExceptions");
        private static readonly Name FIELD_ID = Name.GetName("fieldId");
        private static readonly Name ERROR_INFO = Name.GetName("errorInfo");
        private static readonly Name CATEGORY = Name.GetName("category");
        private static readonly Name MESSAGE = Name.GetName("message");

        private ArrayList d_securities = new ArrayList();
        private ArrayList d_fields = new ArrayList();
        private ArrayList d_overrides = new ArrayList();
        private ArrayList d_overridevalues = new ArrayList();

        static void Main()
        {
            BloombergFetch example = new BloombergFetch();
            example.run();
            System.Console.WriteLine("Press ENTER to quit");
            System.Console.Read();
        }


        public void run()
        {

            string serverHost = "localhost";
            int serverPort = 8194;

            SessionOptions sessionOptions = new SessionOptions();
            sessionOptions.ServerHost = serverHost;
            sessionOptions.ServerPort = serverPort;

            System.Console.WriteLine("Connecting to " + serverHost + ":" +
            serverPort);

            Session session = new Session(sessionOptions);
            bool sessionStarted = session.Start();

            if (!sessionStarted)
            {
                System.Console.Error.WriteLine("Failed to start session.");
                return;
            }

            d_securities.Add("XS0975256685 Corp");
            d_securities.Add("XS1207058733 Corp");

            d_fields.Add("YAS_YLD_SPREAD");
            d_fields.Add("YAS_YLD_SPREAD");

            d_overrides.Add("YAS_BOND_PX"); d_overridevalues.Add(116);
            d_overrides.Add("YAS_BOND_PX"); d_overridevalues.Add(88);


            try
            {
                sendRefDataRequest(session);
            }

            catch (InvalidRequestException e)
            {
                System.Console.WriteLine(e.ToString());
            }

            // wait for events from session.
            eventLoop(session);
            session.Stop();

            */
        }

        private void sendRefDataRequest(Session session)
        {
            if (!session.OpenService(APIREFDATA_SVC))
            {
                System.Console.Error.WriteLine("Failed to open service: " +
                     APIREFDATA_SVC);
                return;
            }
            Service refDataService = session.GetService(APIREFDATA_SVC);

            Request request = refDataService.CreateRequest("ReferenceDataRequest");

            Element securities = request.GetElement("securities");

            Element overrides = request.GetElement("overrides");

            for (int i = 0; i < d_securities.Count; ++i)
            {
                securities.AppendValue((string)d_securities[i]);
            }

            Element fields = request.GetElement("fields");

            for (int i = 0; i < d_fields.Count; ++i)
            {
                fields.AppendValue((string)d_fields[i]);
            }

            for (int i = 0; i < d_overrides.Count; ++i)
            {
                Element bboverride = overrides.AppendElement();
                bboverride.SetElement("fieldId",d_overrides[i].ToString());
                //bboverride.SetElement("fieldId", "YAS_BOND_PX");
                bboverride.SetElement("value", Convert.ToBoolean(d_overridevalues[i]));
                //bboverride.SetElement("value", 100);
            }

            System.Console.WriteLine("Sending Request: " + request);
            session.SendRequest(request, null);

        }

        private void eventLoop(Session session)
        {
            bool done = false;

            while (!done)
            {
                Event eventObj = session.NextEvent();
                if (eventObj.Type == Event.EventType.PARTIAL_RESPONSE)
                {
                    System.Console.WriteLine("Processing Partial Response");
                    processResponseEvent(eventObj);
                }
                else if (eventObj.Type == Event.EventType.RESPONSE)
                {
                    System.Console.WriteLine("Processing Response");
                    processResponseEvent(eventObj);
                    done = true;
                }
                else
                {
                    foreach (Message msg in eventObj.GetMessages())
                    {
                        System.Console.WriteLine(msg.AsElement);
                        if (eventObj.Type == Event.EventType.SESSION_STATUS)
                        {
                            if (msg.MessageType.Equals("SessionTerminated"))
                            {
                                done = true;
                            }
                        }
                    }
                }
            }

        }

        private void processResponseEvent(Event eventObj)
        {
            foreach (Message msg in eventObj.GetMessages())
            {
                if (msg.HasElement(RESPONSE_ERROR))
                {
                    System.Console.WriteLine("REQUEST FAILED: " +
                              msg.GetElement(RESPONSE_ERROR));
                    continue;
                }

                Element securities = msg.GetElement(SECURITY_DATA);
                int numSecurities = securities.NumValues;
                System.Console.WriteLine("Processing " + numSecurities + " securities:");

                for (int i = 0; i < numSecurities; ++i)
                {
                    Element security = securities.GetValueAsElement(i);
                    string ticker = security.GetElementAsString(SECURITY);
                    System.Console.WriteLine("\nTicker: " + ticker);

                    if (security.HasElement("securityError"))
                    {
                        System.Console.WriteLine("\tSECURITY FAILED: " +
                           security.GetElement(SECURITY_ERROR));
                        continue;
                    }

                    Element fields = security.GetElement(FIELD_DATA);

                    if (fields.NumElements > 0)
                    {
                        System.Console.WriteLine("FIELD\t\tVALUE");
                        System.Console.WriteLine("-----\t\t-----");
                        int numElements = fields.NumElements;

                        for (int j = 0; j < numElements; ++j)
                        {
                            Element field = fields.GetElement(j);
                            System.Console.WriteLine(field.Name + "\t\t" +
                                field.GetValueAsString());
                        }
                    }
                    else System.Console.WriteLine("No fields");


                }

            }
        }

    }
}

1 个答案:

答案 0 :(得分:1)

我没有详细阅读您的代码,但一个明显的问题是,如果您想对两个证券使用不同的覆盖值,则需要提交两个单独的请求。

如果您仅在XS0975256685覆盖设置为YAS_BOND_PX的情况下提交116的一个请求,那么您应该会得到所需的结果(我在一分钟前得到101.8739,这与Excel的结果相符同一时刻)。

另请注意,返回的值会随市场而变化,因此您可能会在每次运行时获得不同的值:如果要验证数据是否匹配,请同时刷新Excel电子表格。