예제 #1
0
        private static void ProcessResponseEvent(Event eventObj, SecurityToDateToFldNameToFldVal result)
        {
            foreach (Message msg in eventObj)
            {
                Console.WriteLine($"Received response to request {msg.RequestId}");

                if (msg.HasElement(ResponseError))
                {
                    PrintErrorInfo(
                        msg,
                        "REQUEST FAILED: ",
                        msg.GetElement(ResponseError));
                    continue;
                }

                Element secData = msg.GetElement(SecurityData);

                for (int i = 0; i < secData.NumValues; ++i)
                {
                    string  ticker         = secData.GetElement(Security).GetValueAsString();
                    Element fieldDataArray = secData.GetElement(FieldData);

                    if (!result.ContainsKey(ticker))
                    {
                        result[ticker] = new DateToFldNameToFldVal();
                    }
                    for (int j = 0; j < fieldDataArray.NumValues; ++j)
                    {
                        Element  field     = fieldDataArray.GetValueAsElement(j);
                        DateTime dt        = field.GetElementAsDate(Date).ToSystemDateTime();
                        string   fieldName = field.GetElement(1).ElementDefinition.Name.ToString();
                        double   val       = (double)field.GetElement(1).GetValue();

                        if (!result[ticker].ContainsKey(dt))
                        {
                            result[ticker][dt] = new FldNameToFldVal();
                        }

                        result[ticker][dt][fieldName] = val;
                    }
                }
            }
        }
예제 #2
0
        public SecurityToDateToFldNameToFldVal PerformRequest(DateTime fromDate, DateTime toDate, List <string> tickers, List <string> fieldNames, Dictionary <string, string> optionalParams, Dictionary <string, string> overrideParams)
        {
            string serverHost = "localhost";
            int    serverPort = 8194;

            SessionOptions sessionOptions = new SessionOptions();

            sessionOptions.ServerHost = serverHost;
            sessionOptions.ServerPort = serverPort;

            System.Console.WriteLine("Connecting to " + serverHost + ":" + serverPort);
            session = new Session(sessionOptions);
            bool sessionStarted = session.Start();

            if (!sessionStarted)
            {
                System.Console.Error.WriteLine("Failed to start session.");
                CheckFailures(session);
                return(null);
            }
            if (!session.OpenService(BbgStrings.Services.REF_DATA_SERVICE))
            {
                System.Console.Error.WriteLine("Failed to open " + BbgStrings.Services.REF_DATA_SERVICE);
                CheckFailures(session);
                return(null);
            }

            Service refDataService = session.GetService(BbgStrings.Services.REF_DATA_SERVICE);
            Request request        = refDataService.CreateRequest(BbgStrings.Requests.HISTORICAL_DATA);
            Element securities     = request.GetElement(BbgStrings.Elements.SECURITIES);

            foreach (string s in tickers)
            {
                securities.AppendValue(s);
            }

            Element fields = request.GetElement(BbgStrings.Elements.FIELD);

            foreach (string f in fieldNames)
            {
                fields.AppendValue(f);
            }

            Element overrides = request.GetElement(BbgStrings.Elements.OVERRIDES);

            foreach (KeyValuePair <string, string> e in overrideParams)
            {
                Element o = overrides.AppendElement();
                o.SetElement("fieldId", e.Key);
                o.SetElement("value", e.Value);
            }

            foreach (KeyValuePair <string, string> e in optionalParams)
            {
                request.Set(e.Key, e.Value);
            }

            request.Set(BbgStrings.QueryParams.START_DATE, fromDate.ToString("yyyyMMdd"));
            request.Set(BbgStrings.QueryParams.END_DATE, toDate.ToString("yyyyMMdd"));

            System.Console.WriteLine("Sending Request: " + request);

            session.SendRequest(request, null);

            SecurityToDateToFldNameToFldVal result = new SecurityToDateToFldNameToFldVal();

            bool isDone = false;

            while (!isDone)
            {
                Event eventObj = session.NextEvent();
                foreach (Message msg in eventObj)
                {
                    System.Console.WriteLine(msg.AsElement);
                }

                switch (eventObj.Type)
                {
                case Event.EventType.PARTIAL_RESPONSE:
                    ProcessResponseEvent(eventObj, result);
                    break;

                case Event.EventType.RESPONSE:
                    ProcessResponseEvent(eventObj, result);
                    isDone = true;
                    break;

                default:
                    ProcessGenericEvent(eventObj);
                    break;
                }
            }

            session.Stop();

            return(result);
        }