How to retrieve the price units for different countries ?

Sep 5, 2013 at 10:26 AM
Hi, As am new to this emulator, i could not get the things fast; especially FIELDS.. And my target is to retrieve the price units of different countries. I've used ReferenceDataRequest and Curncy as the yellow key in the security. But am not being able to display those values properly. And that shows same for all the countries.. Help me out..
Coordinator
Sep 9, 2013 at 3:24 PM
mattavidhyasagarr,

Hello. To confirm, you would like to know how to receive prices from the API in some currency other than USD? Maybe Euros or Yen? I cannot figure out how to do this unfortunately. This may be some setting in the Bloomberg Terminal itself rather than a field that you request as a part of some larger request. I'm not familiar with the localization options the API may or may not provide.

That said, I have a work around that could help you. You can simply request the conversion rate as a part of your larger request, and multiply your responses by the conversion rate as necessary. This will work for values that are returned as decimals. I have some sample code for you that includes a class called PriceConverter that runs in the background and provides a way to convert your values from USD to your local currency using the most up-to-date conversion rate. It is thread-safe and lock-free. Give it a try:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BEmu;
using EventHandler = BEmu.EventHandler;

//using Bloomberglp.Blpapi;
//using EventHandler = Bloomberglp.Blpapi.EventHandler;

namespace SimpleTest
{
    public class Program2
    {
        public static void Main(string[] args)
        {
            PriceConverter conv = new PriceConverter("USDEUR Curncy"); //euros
            //PriceConverter conv = new PriceConverter("USDJPY Curncy"); //yen
            //PriceConverter conv = new PriceConverter("USDCAD Curncy"); //Canadian dollars

            conv.Start();

            while (true)
            {
                System.Threading.Thread.Sleep(1000);
                double dollars = 1; //this can be a value in dollars that you receive from a separate API request
                double euros = conv.ConvertFromUSD(dollars); //this is the value converted to euros
                Console.WriteLine(euros);
            }

            conv.Dispose();
        }

        private class PriceConverter : IDisposable
        {
            private double _convertFromUSD;
            private readonly string _currencyKey;
            private readonly Session _session;
            private const string LAST_FIELD = "LAST_PRICE";

            public PriceConverter(string currencyKey)
            {
                this._currencyKey = currencyKey;

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

                this._session = new Session(sessionOptions, new EventHandler(this.ProcessEvent));
            }

            public double ConvertFromUSD(double valueInUSD)
            {
                //read a double atomically
                double converter = System.Threading.Interlocked.CompareExchange(
                    ref this._convertFromUSD, this._convertFromUSD, -this._convertFromUSD);
                return valueInUSD * converter;
            }

            private void SetMultiplier(double convertFromUSD)
            {
                //write a double atomically
                System.Threading.Interlocked.Exchange(ref this._convertFromUSD, convertFromUSD);
            }

            public void Start()
            {
                this._session.StartAsync();
            }

            public void Dispose()
            {
                if (this._session != null)
                {
                    this._session.Stop();
                    this._session.Dispose();
                }
            }

            private 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");
                                }
                                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>();
                        slist.Add(new Subscription(this._currencyKey, new string[] { PriceConverter.LAST_FIELD }.ToList()));
                        session.Subscribe(slist);
                        break;

                    case Event.EventType.SUBSCRIPTION_DATA:
                    case Event.EventType.RESPONSE:
                    case Event.EventType.PARTIAL_RESPONSE:
                        foreach (Message message in evt.GetMessages())
                        {
                            string security = message.TopicName;
                            if (message.HasElement(PriceConverter.LAST_FIELD, true))
                            {
                                Element elmField = message[PriceConverter.LAST_FIELD];
                                double converter = elmField.GetValueAsFloat64();
                                this.SetMultiplier(converter);
                            }
                        }
                        break;
                }
            }

        }
    }
}