Пример #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            securityList1 = ConfigurationManager.AppSettings["securities1"].Split(',').Select(s => s.Trim()).ToArray();
            securityList2 = ConfigurationManager.AppSettings["securities2"].Split(',').Select(s => s.Trim()).ToArray();
            currencyList1 = ConfigurationManager.AppSettings["currencies1"].Split(',').Select(s => s.Trim()).ToArray();
            currencyList2 = ConfigurationManager.AppSettings["currencies2"].Split(',').Select(s => s.Trim()).ToArray();
            baseCurrency = ConfigurationManager.AppSettings["baseCurrency"];
            sendTransactions = Convert.ToBoolean(ConfigurationManager.AppSettings["sendTransaction"]);
            tc = new TransactionComputer
                (securityList1, securityList2, currencyList1, currencyList2, baseCurrency);
            ts = new TransactionSender();
            SessionOptions sessionOptions = new SessionOptions();
            sessionOptions.ServerHost = "localhost";
            sessionOptions.ServerPort = 8194;
            Session session = new Session(sessionOptions, new EventHandler(ProcessEvent));
            session.StartAsync();

            //Invoke(new Action(() => richTextBox1.AppendText("moi\n");
            Invoke(new Action(() => richTextBox1.AppendText("end\n")));
        }
Пример #2
0
        private void ProcessEvent(Event evt, Session session)
        {
            Invoke(new Action(() => richTextBox1.AppendText("ProcessEvent(Event, Session)\n")));
            List<string> _fields;
            _fields = new List<String>();
            _fields.Add("LAST_PRICE");
            _fields.Add("BEST_BID1");
            _fields.Add("BEST_ASK1");
            _fields.Add("BEST_BID1_SZ");
            _fields.Add("BEST_ASK1_SZ");

            switch (evt.Type)
            {
                case Event.EventType.SESSION_STATUS: //use this to open the service
                    foreach (Bloomberglp.Blpapi.Message message in evt.GetMessages())
                    {
                        if (message.MessageType.Equals("SessionStarted"))
                        {
                            try
                            {
                                session.OpenServiceAsync("//blp/mktdata", new CorrelationID(-9999));
                                Invoke(new Action(() => richTextBox1.AppendText("Session opened\n")));
                            }
                            catch (Exception)
                            {
                                Invoke(new Action(() => richTextBox1.AppendText("Could not open //blp/mktdata for async\n")));
                            }
                        }
                    }
                    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.

                    //uncomment the following line to see what a request for a nonexistent security looks like
                    //slist.Add(new Subscription("ZYZZ US EQUITY", MarketDataRequest._fields, options));
                    //  My code treats all securities that start with a 'Z' as a nonexistent security

                    foreach (string security in securityList1)
                    {
                        Invoke(new Action(() =>
                            richTextBox1.AppendText
                            (string.Format("ADDING {0} TO SUBSCRIPTIONS\n",
                            security))));
                        slist.Add(new Subscription(security, _fields, options));
                    }
                    foreach (string security in securityList2)
                    {
                        Invoke(new Action(() =>
                            richTextBox1.AppendText
                            (string.Format("ADDING {0} TO SUBSCRIPTIONS\n",
                            security))));
                        slist.Add(new Subscription(security, _fields, options));
                    }
                    Invoke(new Action(() => richTextBox1.AppendText("added securities\n")));

                    foreach (string currency in currencyList1)
                    {
                        if (currency == baseCurrency) { continue; }
                        string currencyID = currency + baseCurrency + " Curncy";
                        Invoke(new Action(() =>
                            richTextBox1.AppendText
                            (string.Format("ADDING {0} TO SUBSCRIPTIONS\n",
                            currencyID))));
                        slist.Add(new Subscription(currencyID, _fields, options));
                    }
                    foreach (string currency in currencyList2)
                    {
                        if (currency == baseCurrency) { continue; }
                        string currencyID = currency + baseCurrency + " Curncy";
                        Invoke(new Action(() =>
                            richTextBox1.AppendText
                            (string.Format("ADDING {0} TO SUBSCRIPTIONS\n",
                            currencyID))));
                        slist.Add(new Subscription(currencyID, _fields, options));
                    }
                    Invoke(new Action(() => richTextBox1.AppendText("added currencies\n")));

                    //slist.Add(new Subscription("SPY US EQUITY", _fields, options));
                    //slist.Add(new Subscription("AAPL 150117C00600000 EQUITY", _fields, options));

                    session.Subscribe(slist);
                    break;

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

                case Event.EventType.SUBSCRIPTION_STATUS:
                    foreach (var msg in evt.GetMessages())
                    {
                        bool fieldExceptionsExist = msg.MessageType.ToString() == "SubscriptionStarted" && msg.HasElement("exceptions", true);
                        bool securityError = msg.MessageType.ToString() == "SubscriptionFailure" && msg.HasElement("reason", 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");
                                Invoke(new Action(() => richTextBox1.AppendText("field error: ")));
                                Invoke(new Action(() => richTextBox1.AppendText(string.Format("\tfieldId = {0}", fieldId))));
                                Invoke(new Action(() => richTextBox1.AppendText(string.Format("\tsource = {0}", source))));
                                //Invoke(new Action(() => richTextBox1.AppendText(string.Format("\terrorCode = {0}", errorCode))));
                                Invoke(new Action(() => richTextBox1.AppendText(string.Format("\tcategory = {0}", category))));
                                Invoke(new Action(() => richTextBox1.AppendText(string.Format("\tdescription = {0}", description))));
                            }
                        }
                        else if (securityError)
                        {
                            string security = msg.TopicName;
                            Element elmReason = msg["reason"];
                            string source = elmReason.GetElementAsString("source");
                            int errorCode = elmReason.GetElementAsInt32("errorCode");
                            string category = elmReason.GetElementAsString("category");
                            string description = elmReason.GetElementAsString("description");
                            Invoke(new Action(() => richTextBox1.AppendText("security not found: ")));
                            Invoke(new Action(() => richTextBox1.AppendText(string.Format("\tsecurity = {0}", security))));
                            Invoke(new Action(() => richTextBox1.AppendText(string.Format("\tsource = {0}", source))));
                            Invoke(new Action(() => richTextBox1.AppendText(string.Format("\terrorCode = {0}", errorCode))));
                            Invoke(new Action(() => richTextBox1.AppendText(string.Format("\tcategory = {0}", category))));
                            Invoke(new Action(() => richTextBox1.AppendText(string.Format("\tdescription = {0}", description))));
                        }
                    }
                    break;

                default:
                    foreach (var msg in evt.GetMessages())
                    {
                        Invoke(new Action(() => richTextBox1.AppendText(msg.ToString())));
                    }
                    break;
            }
        }
Пример #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            Lgr.Start();
            int _securities;

            try
            {
                _securities = get_Securities();
            }
            catch (CsvHelper.CsvMissingFieldException)
            {
                Invoke(new Action(() => richTextBox1.AppendText("Malformed securities.csv.\nSee that the header line is security1,security2,currency1,currency2,security1scale,security2scale,arbidir\n")));
                return;
            }
            catch (System.IO.FileNotFoundException)
            {
                Invoke(new Action(() => richTextBox1.AppendText("Could not find securities.csv\n")));
                return;
            }

            Invoke(new Action(() => richTextBox1.AppendText(String.Format("Found {0} securities\n", _securities))));
            if (_securities == 0)
            {
                Invoke(new Action(() => richTextBox1.AppendText("No valid securities found. Not starting.\n")));
                return;
            }

            baseCurrency = ConfigurationManager.AppSettings["baseCurrency"];
            profitThresholdEur = Convert.ToInt32(ConfigurationManager.AppSettings["profitThresholdEur"]);
            sendTransactions = Convert.ToBoolean(ConfigurationManager.AppSettings["sendTransaction"]);
            tc = new TransactionComputer
                (securityList1, securityList2,
                 currencyList1, currencyList2,
                 securityScale1, securityScale2,
                 baseCurrency, profitThresholdEur);
            ts = new TransactionSender();
            if (running == false)
            {
                running = true;

                SessionOptions sessionOptions = new SessionOptions();
                sessionOptions.ServerHost = "localhost";
                sessionOptions.ServerPort = 8194;

                //pullInitial(sessionOptions);

                sessionCurr = new Session(sessionOptions, new EventHandler(ProcessEventCurr));
                sessionCurr.StartAsync();

                sessionSec = new Session(sessionOptions, new EventHandler(ProcessEventSec));
                sessionSec.StartAsync();

                Invoke(new Action(() => richTextBox1.AppendText("started\n")));
                this.button1.Text = "Stop";
            }
            else
            {
                // FIXME: Any danger in stopping, eg. to transactions?
                running = false;
                sessionCurr.Stop();
                sessionSec.Stop();
                Invoke(new Action(() => richTextBox1.AppendText("stopped\n")));
                this.button1.Text = "Start";
            }
        }
Пример #4
0
        private void pullInitial(SessionOptions sessionOptions)
        {
            Session session = new Session(sessionOptions);
            if (!session.Start())
            {
                Invoke(new Action(() => richTextBox1.AppendText("Could not start session 2.")));
            }
            if (!session.OpenService("//blp/refdata"))
            {
                Invoke(new Action(() => richTextBox1.AppendText("Could not open service " + "//blp/refdata")));
            }
            CorrelationID requestID = new CorrelationID(1);

            Service refDataSvc = session.GetService("//blp/refdata");

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

            foreach (string security in securityList1)
            {
                request.Append("securities", security);
            }
            foreach (string security in securityList2)
            {
                request.Append("securities", security);
            }
            foreach (string currency in currencyList1)
            {
                if (currency == baseCurrency) { continue; }
                string currencyID = currency + baseCurrency + " Curncy";
                request.Append("securities", currencyID);
            }
            foreach (string currency in currencyList2)
            {
                if (currency == baseCurrency) { continue; }
                string currencyID = currency + baseCurrency + " Curncy";
                request.Append("securities", currencyID);
            }
            Invoke(new Action(() => richTextBox1.AppendText("added securities\n")));

            List<string> _fields;
            _fields = new List<String>();
            _fields.Add("LAST_PRICE");
            _fields.Add("BEST_BID");
            _fields.Add("BEST_ASK");
            _fields.Add("BEST_BID1_SZ");
            _fields.Add("BEST_ASK1_SZ");

            request.Append("fields", "LAST_PRICE");
            request.Append("fields", "BEST_BID");
            request.Append("fields", "BEST_ASK");
            request.Append("fields", "BEST_BID1_SZ");
            request.Append("fields", "BEST_ASK1_SZ");

            session.SendRequest(request, requestID);

            bool continueToLoop = true;

            while (continueToLoop)
            {
                Bloomberglp.Blpapi.Event evt = session.NextEvent();

                switch (evt.Type)
                {
                    case Event.EventType.PARTIAL_RESPONSE:
                        Invoke(new Action(() => richTextBox1.AppendText("partial response\n")));
                        ProcessEvent(evt, _fields);
                        break;
                    case Event.EventType.RESPONSE: // final event
                        Invoke(new Action(() => richTextBox1.AppendText("final response\n")));
                        continueToLoop = false; // fall through
                        break;
                }
            }
            Invoke(new Action(() => richTextBox1.AppendText("-------------- FINISHED INITIAL PULL -------------")));
            Invoke(new Action(() => richTextBox1.AppendText("-------------- FINISHED INITIAL PULL -------------")));
            Invoke(new Action(() => richTextBox1.AppendText("-------------- FINISHED INITIAL PULL -------------")));
        }
Пример #5
0
 private void ProcessEventSec(Event evt, Session session)
 {
     ProcessEvent(evt, session, "security");
 }
Пример #6
0
 private void ProcessEventCurr(Event evt, Session session)
 {
     ProcessEvent(evt, session, "currency");
 }