This project has moved and is read-only. For the latest updates, please go here.

BEmu .NET and EventQueue

Feb 28, 2014 at 5:52 PM
Hi
First off, many many thanks for building up BEmu. I've written about 1/2 dozen tutorials directly using the Bloomberg API (http://holowczak.com/category/bloomberg/) and now I want to go back and put in the tips and suggestions for using BEmu.
One thing I came across from my C# Request/Reply example is the use of the EventQueue
For example:
        // Set up an event Queue to receive events
        EventQueue eventQueue = new EventQueue();
        // Submit the request
        session.SendRequest(request, eventQueue, null);
Bloomberg API has this EventQueue but BEmu does not. Is there a work around for this? Or is it something you may support in the future?
If it helps my example code is here:
http://holowczak.com/bloomberg-api-csharp-rr/

Cheers,

Rich H.
Coordinator
Feb 28, 2014 at 7:04 PM
Rich,

Thank you for your kind words. Concerning the EventQueue object, honestly I haven't used it before. I dug through the documentation but I can't find any useful information on what an EventQueue does. Looking at the sample code you provided, I see this:
            EventQueue eventQueue = new EventQueue();
            session.SendRequest(request, eventQueue, null);

            done = false;
            while (!done)
            {
                Event eventObject = eventQueue.NextEvent();
If I replace that with this (removing the EventQueue entirely):
            session.SendRequest(request, null);

            done = false;
            while (!done)
            {
                Event eventObject = session.NextEvent();
the code runs exactly the same as far as I can tell. I think this is two ways of doing the same thing: the EventQueue seems redundant. Can I ask why you used an EventQueue? Does it provide some unique functionality? It should be relatively easy to emulate this behavior in my code. I can't see any reason not to do so.

Thanks again,

-Jordan
Feb 28, 2014 at 7:34 PM
Edited Feb 28, 2014 at 7:37 PM
My apologies - I must be losing my mind :) I could have sworn I got that example of using EventQueue from one of the Bloomberg API code examples. Maybe I had a older version of the API and they have since deprecated its use for actually fetching messages. In any case, it looks from the documentation that you can use the EventQueue to gain more control over the events coming in. For example, suppose your program sent out a ton of data requests but then the user clicks a "Cancel" button, you might call the .Purge() method on your event queue to clean out any extra data on its way in that has not yet been processed.

In any case, I'll modify (simplify) my code to eliminate the EventQueue bit. I am finding some other things that can be cleaned up as well. For example, the Bloomberg API is perfectly happy doing this:
        String security = "IBM US Equity";
        String fields = "LAST_PRICE";
        // Define a List of subscriptions
        List<Subscription> subscriptions;
        // Instantiate the new list of subscriptions
        subscriptions = new List<Subscription>();
        // Create and Add a subscription. Create a new CorrelationID on the fly
        subscriptions.Add(new Subscription(security, fields, "",
                          new CorrelationID(security)));
But Bemu gets upset that "fields" and options ("" in my example) are not Lists. So I'll be working through the rest of the code to smooth things over between them.

My goal here is that with one comment/uncomment line, programmers can switch between Bloomberg API and Bemu for all of my C# code samples. The C++ (C plus plus) stuff is another matter to explore though...

Cheers,

Rich H.
Coordinator
Feb 28, 2014 at 9:23 PM
Rich,

No need to apologize. This type of redundancy in the BB API is something that I run into again and again. I've found that many pieces of BB API code can be rewritten differently and still work exactly the same. That makes my job difficult for sure. I noticed another example of this with the help of your code. In one line you have:
request.GetElement("securities").AppendValue("IBM US Equity"); //first method
That's equivalent to:
request.Append("securities", "IBM US EQUITY"); //second method
Which is also equivalent to:
request.["securities"].AppendValue("IBM US Equity"); //third method
I implemented the second method, but not the first or the third. That is simply an oversight and I'll include those implementations in the next version of BEmu.

It's funny that you mention using Lists for subscriptions. I was working on that recently in fact. That will also be int he next version of the code. You can take a look at my C# examples to see what calls work for sure. My code is only a subset of the whole BB API.

As for switching between BB API and BEmu, another user had the idea of setting a #define in the Debug configuration that toggles between the two APIs. In Visual Studio, you can go to your project's properties > Build > Conditional compilation symbols to set this. That way you can use BEmu in Debug mode and BB API in Release mode. For my C++ examples check out headers/api_selector.h. In that file, I have a single line that a user can comment to toggle between the two APIs.
Mar 3, 2014 at 5:46 PM
Edited Mar 3, 2014 at 7:09 PM
Here's one more for you - In Bloomberg API a Session object will have a "Unsubscribe" method to shut down a specific subscription. I don't think BEmu has Unsubscribe defined. In my C# code I give an example of:
        Session session = new Session(sessionOptions, new BEmu.EventHandler(processBBEvent));
        // Start the Session
        result = session.Start();
        // Open up the Market data Service
        result = session.OpenService("//blp/mktdata");
        // Instantiate the new list of subscriptions
        subscriptions = new List<Subscription>();
        // Add a subscription. Create a new CorrelationID on the fly
        subscriptions.Add(new Subscription(security, fields, options,
                          correlationId));
        // Kick off the subscriptions
        session.Subscribe(subscriptions);

        // Pause so we can view the output
        Console.ReadKey();

 __       // Turn off the subscription
        session.Unsubscribe(subscriptions);__

        // Close the session
        session.Stop();

p.s. I've now updated two of my C# tutorials with the Bemu .NET code in place. With the exception of Unsubscribe, the switch-off between Bloomberg API and BEmu is seamless. See
http://holowczak.com/bloomberg-api-csharp-subscriptions/ and http://holowczak.com/bloomberg-api-csharp-rr/
Coordinator
Mar 4, 2014 at 7:37 PM
Rich,

It shouldn't be a problem to implement the public void Unsubscribe(IList<Subscription> subscriptionList) call. In the meantime, you can replace that with a call to this:
session.Cancel(subscriptions.Select(s => s.CorrelationID).ToList());
I've already implemented two Cancel functions, one that takes a CorrelationId object and another that takes a list of CorrelationId objects. In fact this is how I will probably implement the Unsubscribe function for a list of CorrelationIds. Note that the Unsubscribe function for a single CorrelationId is marked as obsolete ("Deprecated as of 3.2.2 and use Cancel(CorrelationID) instead").
Coordinator
Mar 6, 2014 at 9:02 PM
Rich,

I just uploaded some changes to the C# and C++ versions of the code. These changes include the issues you mentioned in this conversation. I've implemented EventQueue, expanded the Subscription functionality, and implemented the Unsubscribe functions. I think that's every issue you brought up. Check it out.

Thank you for pointing all of this out. Please let me know if you find any other issues.

-Jordan
Mar 6, 2014 at 9:10 PM
Wow - you are fast! Will be checking this out very soon. Thanks for all of your efforts. Am attempting to pay a tiny bit back :)