示例#1
0
        private static void ProcessEvent(Event evt, Session session)
        {
            switch (evt.Type)
            {
                case Event.EventType.SESSION_STATUS: //use this to open the service
                    foreach (Message message in evt.GetMessages())
                    {
                        if (message.MessageType.Equals("SessionStarted"))
                        {
                            try
                            {
                                session.OpenServiceAsync("//blp/mktdata", new CorrelationID(-9999));
                            }
                            catch (Exception)
                            {
                                System.Console.Error.WriteLine("Could not open //blp/mktdata for async");
                            }
                        }
                    }
                    break;

                case Event.EventType.SERVICE_STATUS: //use this to subscribe to ticker feeds
                    List<Subscription> slist = new List<Subscription>();

                    //Conflate the data to show every two seconds.
                    //  Please note that the Bloomberg API Emulator code does not treat this exactly correct: individual subscriptions should each have their own interval setting.
                    //  I have not coded that in the emulator.
                    List<string> options = new string[] { "interval=2" }.ToList(); //2 seconds.  //Comment this line to receive a subscription data event whenever it happens in the market.

                    //slist.Add(new Subscription("ZYZZ US EQUITY", MarketDataRequest._fields, options)); //the code treats securities that start with a "Z" as non-existent
                    slist.Add(new Subscription("SPY US EQUITY", MarketDataRequest._fields, options));
                    slist.Add(new Subscription("AAPL 150117C00600000 EQUITY", MarketDataRequest._fields, options));

                    session.Subscribe(slist);
                    break;

                case Event.EventType.SUBSCRIPTION_DATA:
                case Event.EventType.RESPONSE:
                case Event.EventType.PARTIAL_RESPONSE:
                    MarketDataRequest.ProcessEvent(evt);
                    break;

                case Event.EventType.SUBSCRIPTION_STATUS:
                    foreach (var msg in evt.GetMessages())
                    {
                        bool fieldExceptionsExist = msg.HasElement("exceptions", true);
                        if (fieldExceptionsExist)
                        {
                            Element elmExceptions = msg["exceptions"];
                            for (int i = 0; i < elmExceptions.NumValues; i++)
                            {
                                Element elmException = elmExceptions.GetValueAsElement(i);
                                string fieldId = elmException.GetElementAsString("fieldId");

                                Element elmReason = elmException["reason"];
                                string source = elmReason.GetElementAsString("source");
                                int errorCode = elmReason.GetElementAsInt32("errorCode");
                                string category = elmReason.GetElementAsString("category");
                                string description = elmReason.GetElementAsString("description");

                                Console.WriteLine("field error: ");
                                Console.WriteLine(string.Format("\tfieldId = {0}", fieldId));
                                Console.WriteLine(string.Format("\tsource = {0}", source));
                                Console.WriteLine(string.Format("\terrorCode = {0}", errorCode));
                                Console.WriteLine(string.Format("\tcategory = {0}", category));
                                Console.WriteLine(string.Format("\tdescription = {0}", description));
                            }
                        }

                    }
                    break;
            }
        }
        private static void ProcessResponse(Event evt, string security)
        {
            //Note that the IntradayBarResponse does not include the name of the requested security anywhere
            Console.WriteLine(security);

            foreach (var msg in evt.GetMessages())
            {
                bool isSecurityError = msg.HasElement("responseError", true);

                if (isSecurityError)
                {
                    Element secError = msg["responseError"];
                    string source = secError.GetElementAsString("source");
                    int code = secError.GetElementAsInt32("code");
                    string category = secError.GetElementAsString("category");
                    string errorMessage = secError.GetElementAsString("message");
                    string subCategory = secError.GetElementAsString("subcategory");

                    Console.WriteLine("response error");
                    Console.WriteLine(string.Format("source = {0}", source));
                    Console.WriteLine(string.Format("code = {0}", code));
                    Console.WriteLine(string.Format("category = {0}", category));
                    Console.WriteLine(string.Format("errorMessage = {0}", errorMessage));
                    Console.WriteLine(string.Format("subCategory = {0}", subCategory));
                }
                else
                {
                    Element elmBarTickDataArray = msg["barData"]["barTickData"];
                    for (int valueIndex = 0; valueIndex < elmBarTickDataArray.NumValues; valueIndex++)
                    {
                        Element elmBarTickData = elmBarTickDataArray.GetValueAsElement(valueIndex);

                        DateTime dtTick = elmBarTickData.GetElementAsDatetime("time").ToSystemDateTime();

                        double open = elmBarTickData.GetElementAsFloat64("open");
                        double high = elmBarTickData.GetElementAsFloat64("high");
                        double low = elmBarTickData.GetElementAsFloat64("low");
                        double close = elmBarTickData.GetElementAsFloat64("close");

                        int numEvents = elmBarTickData.GetElementAsInt32("numEvents");
                        long volume = elmBarTickData.GetElementAsInt64("volume");
                        double value = elmBarTickData.GetElementAsFloat64("value");

                        Console.WriteLine(dtTick.ToString("HH:mm:ss"));
                        Console.WriteLine(string.Format("\t open = {0:c2}", open));
                        Console.WriteLine(string.Format("\t high = {0:c2}", high));
                        Console.WriteLine(string.Format("\t low = {0:c2}", low));
                        Console.WriteLine(string.Format("\t close = {0:c2}", close));

                        Console.WriteLine(string.Format("\t numEvents = {0:n0}", numEvents));
                        Console.WriteLine(string.Format("\t volume = {0:n0}", volume));
                        Console.WriteLine(string.Format("\t value = {0:n0}", value));

                        Console.WriteLine();
                    }
                }
            }
        }
示例#3
0
        private static void ProcessEvent(Event evt)
        {
            const bool excludeNullElements = true;
            foreach (Message message in evt.GetMessages())
            {
                string security = message.TopicName;
                foreach (var field in MarketDataRequest._fields)
                {
                    //This ignores the extraneous fields in the response
                    if (message.HasElement(field, excludeNullElements)) //be careful, excludeNullElements is false by default
                    {
                        Element elmField = message[field];

                        Console.WriteLine(string.Format("{0:HH:mm:ss}: {1}, {2}",
                            DateTime.Now,
                            security,
                            elmField.ToString().Trim()));
                    }
                }
            }
            Console.WriteLine();
        }
 private static void handleOtherEvent(Event eventObj)
 {
     Console.WriteLine("EventType=" + eventObj.Type);
     foreach (Message message in eventObj.GetMessages())
     {
         Console.WriteLine("correlationID=" + message.CorrelationID);
         Console.WriteLine("messageType=" + message.MessageType);
         Console.WriteLine(message.ToString());
         if (Event.EventType.SESSION_STATUS == eventObj.Type && message.MessageType.Equals("SessionTerminated"))
         {
             Console.WriteLine("Terminating: " + message.MessageType);
         }
     }
 }
        private static void handleResponseEvent(Event eventObj)
        {
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("EventType =" + eventObj.Type);
            foreach (Message message in eventObj.GetMessages())
            {
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("correlationID=" + message.CorrelationID);
                Console.WriteLine("messageType =" + message.MessageType);

                Element elmSecurityDataArray = message["securityData"];

                for (int valueIndex = 0; valueIndex < elmSecurityDataArray.NumValues; valueIndex++)
                {
                    Element elmSecurityData = elmSecurityDataArray.GetValueAsElement(valueIndex);

                    string security = elmSecurityData.GetElementAsString("security");
                    Console.WriteLine(security);

                    bool hasFieldErrors = elmSecurityData.HasElement("fieldExceptions", true);
                    if (hasFieldErrors)
                    {
                        Element elmFieldErrors = elmSecurityData["fieldExceptions"];
                        for (int errorIndex = 0; errorIndex < elmFieldErrors.NumValues; errorIndex++)
                        {
                            Element fieldError = elmFieldErrors.GetValueAsElement(errorIndex);
                            string fieldId = fieldError.GetElementAsString("fieldId");

                            Element errorInfo = fieldError["errorInfo"];
                            string source = errorInfo.GetElementAsString("source");
                            int code = errorInfo.GetElementAsInt32("code");
                            string category = errorInfo.GetElementAsString("category");
                            string strMessage = errorInfo.GetElementAsString("message");
                            string subCategory = errorInfo.GetElementAsString("subcategory");

                            Console.WriteLine();
                            Console.WriteLine();
                            Console.WriteLine("\tfield error");
                            Console.WriteLine(string.Format("\tfieldId = {0}", fieldId));
                            Console.WriteLine(string.Format("\tsource = {0}", source));
                            Console.WriteLine(string.Format("\tcode = {0}", code));
                            Console.WriteLine(string.Format("\tcategory = {0}", category));
                            Console.WriteLine(string.Format("\terrorMessage = {0}", strMessage));
                            Console.WriteLine(string.Format("\tsubCategory = {0}", subCategory));
                        }
                    }

                    bool isSecurityError = elmSecurityData.HasElement("securityError", true);
                    if (isSecurityError)
                    {
                        Element secError = elmSecurityData["securityError"];
                        string source = secError.GetElementAsString("source");
                        int code = secError.GetElementAsInt32("code");
                        string category = secError.GetElementAsString("category");
                        string errorMessage = secError.GetElementAsString("message");
                        string subCategory = secError.GetElementAsString("subcategory");

                        Console.WriteLine();
                        Console.WriteLine();
                        Console.WriteLine("\tsecurity error");
                        Console.WriteLine(string.Format("\tsource = {0}", source));
                        Console.WriteLine(string.Format("\tcode = {0}", code));
                        Console.WriteLine(string.Format("\tcategory = {0}", category));
                        Console.WriteLine(string.Format("\terrorMessage = {0}", errorMessage));
                        Console.WriteLine(string.Format("\tsubCategory = {0}", subCategory));
                    }
                    else
                    {
                        Element elmFieldData = elmSecurityData["fieldData"];

                        double pxLast = elmFieldData.GetElementAsFloat64("PX_LAST");
                        double bid = elmFieldData.GetElementAsFloat64("BID");
                        double ask = elmFieldData.GetElementAsFloat64("ASK");
                        string ticker = elmFieldData.GetElementAsString("TICKER");

                        Console.WriteLine();
                        Console.WriteLine();
                        Console.WriteLine("\tPX_LAST = " + pxLast.ToString());
                        Console.WriteLine("\tBID = " + bid.ToString());
                        Console.WriteLine("\tASK = " + ask.ToString());
                        Console.WriteLine("\tTICKER = " + ticker.ToString());

                        bool excludeNullElements = true;
                        if (elmFieldData.HasElement("CHAIN_TICKERS", excludeNullElements)) //be careful, excludeNullElements is false by default
                        {
                            Element chainTickers = elmFieldData["CHAIN_TICKERS"];
                            for (int chainTickerValueIndex = 0; chainTickerValueIndex < chainTickers.NumValues; chainTickerValueIndex++)
                            {
                                Element chainTicker = chainTickers.GetValueAsElement(chainTickerValueIndex);
                                string strChainTicker = chainTicker.GetElementAsString("Ticker");

                                Console.WriteLine("\tCHAIN_TICKER = " + strChainTicker.ToString());
                            }
                        }
                        else
                        {
                            Console.WriteLine("\tNo CHAIN_TICKER information");
                        }
                    }

                }
            }
        }
        private static void ProcessResponse(Event evt, string security)
        {
            //Note that the IntradayTickResponse does not include the name of the requested security anywhere
            Console.WriteLine(security);

            foreach (var msg in evt.GetMessages())
            {
                bool isSecurityError = msg.HasElement("responseError", true);

                if (isSecurityError)
                {
                    Element secError = msg["responseError"];
                    string source = secError.GetElementAsString("source");
                    int code = secError.GetElementAsInt32("code");
                    string category = secError.GetElementAsString("category");
                    string errorMessage = secError.GetElementAsString("message");
                    string subCategory = secError.GetElementAsString("subcategory");

                    Console.WriteLine("response error");
                    Console.WriteLine(string.Format("source = {0}", source));
                    Console.WriteLine(string.Format("code = {0}", code));
                    Console.WriteLine(string.Format("category = {0}", category));
                    Console.WriteLine(string.Format("errorMessage = {0}", errorMessage));
                    Console.WriteLine(string.Format("subCategory = {0}", subCategory));
                }
                else
                {
                    Element elmTickDataArr = msg["tickData"];
                    Element elmTickData = elmTickDataArr["tickData"];

                    for (int valueCount = 0; valueCount < elmTickData.NumValues; valueCount++)
                    {
                        Element elmTickDataValue = elmTickData.GetValueAsElement(valueCount);

                        DateTime time = elmTickDataValue.GetElementAsTime("time").ToSystemDateTime();
                        string type = elmTickDataValue.GetElementAsString("type");
                        double value = elmTickDataValue.GetElementAsFloat64("value");
                        int size = elmTickDataValue.GetElementAsInt32("size");

                        Console.WriteLine(string.Format("{0:HH:mm:ss}: {1}, {2} @ {3}", time, type, size, value));
                    }
                }

            }
        }
        private static void handleResponseEvent(Event eventObj)
        {
            Console.WriteLine("EventType = " + eventObj.Type);
            foreach (Message message in eventObj.GetMessages())
            {
                Console.WriteLine();
                Console.WriteLine("correlationID= " + message.CorrelationID);
                Console.WriteLine("messageType = " + message.MessageType);

                Element elmSecurityData = message["securityData"];

                Element elmSecurity = elmSecurityData["security"];
                string security = elmSecurity.GetValueAsString();
                Console.WriteLine(security);

                if (elmSecurityData.HasElement("securityError", true))
                {
                    Element elmSecError = elmSecurityData["securityError"];
                    string source = elmSecError.GetElementAsString("source");
                    int code = elmSecError.GetElementAsInt32("code");
                    string category = elmSecError.GetElementAsString("category");
                    string errorMessage = elmSecError.GetElementAsString("message");
                    string subCategory = elmSecError.GetElementAsString("subcategory");

                    Console.WriteLine("security error");
                    Console.WriteLine(string.Format("source = {0}", source));
                    Console.WriteLine(string.Format("code = {0}", code));
                    Console.WriteLine(string.Format("category = {0}", category));
                    Console.WriteLine(string.Format("errorMessage = {0}", errorMessage));
                    Console.WriteLine(string.Format("subCategory = {0}", subCategory));
                }
                else
                {
                    bool hasFieldErrors = elmSecurityData.HasElement("fieldExceptions", true);
                    if (hasFieldErrors)
                    {
                        Element elmFieldErrors = elmSecurityData["fieldExceptions"];
                        for (int errorIndex = 0; errorIndex < elmFieldErrors.NumValues; errorIndex++)
                        {
                            Element fieldError = elmFieldErrors.GetValueAsElement(errorIndex);
                            string fieldId = fieldError.GetElementAsString("fieldId");

                            Element errorInfo = fieldError["errorInfo"];
                            string source = errorInfo.GetElementAsString("source");
                            int code = errorInfo.GetElementAsInt32("code");
                            string category = errorInfo.GetElementAsString("category");
                            string strMessage = errorInfo.GetElementAsString("message");
                            string subCategory = errorInfo.GetElementAsString("subcategory");

                            Console.WriteLine();
                            Console.WriteLine();
                            Console.WriteLine("field error: ");
                            Console.WriteLine(string.Format("\tfieldId = {0}", fieldId));
                            Console.WriteLine(string.Format("\tsource = {0}", source));
                            Console.WriteLine(string.Format("\tcode = {0}", code));
                            Console.WriteLine(string.Format("\tcategory = {0}", category));
                            Console.WriteLine(string.Format("\terrorMessage = {0}", strMessage));
                            Console.WriteLine(string.Format("\tsubCategory = {0}", subCategory));
                        }
                    }

                    Element elmFieldData = elmSecurityData["fieldData"];
                    for (int valueIndex = 0; valueIndex < elmFieldData.NumValues; valueIndex++)
                    {
                        Element elmValues = elmFieldData.GetValueAsElement(valueIndex);
                        DateTime date = elmValues.GetElementAsDate("date").ToSystemDateTime();
                        double bid = elmValues.GetElementAsFloat64("BID");
                        double ask = elmValues.GetElementAsFloat64("ASK");

                        Console.WriteLine(string.Format("{0:yyyy-MM-dd}: BID = {1}, ASK = {2}", date, bid, ask));
                    }
                }
            }
        }