Monday, March 23, 2009

Java Currency Converter using Yahoo Finance API – Currency Matrix

On the post Basic Java Currency converter using Yahoo Finance API I showed how currency exchange rate information can be easily acquired.

Getting a single exchange rate between two currencies is a nice thing, but it can be inefficient, if one would like to get several currencies exchange rate information. Since the Acquiring the information requires a request to Yahoo servers, making multiple requests in order to get several exchange rates is an expensive operation. Luckily, Yahoo API allows getting more than one exchange rate information on a single request.

The structure for getting more than one currency information at a single request is pretty straight forward and can be easily deduced from the basic request I was showing on the previous post:

http://download.finance.yahoo.com/d/quotes.csv?s=[From Currency][To Currency]=X&...&s=[From Currency][To Currency]=X&f=l1&e=.cs

For example, getting 3 currencies information on a single request for the following currency pairs:

  • USD, ILS
  • USD, JPY
  • USD, GBP

Looks like:

http://download.finance.yahoo.com/d/quotes.csv?s=USDILS=X&s=USDJPY=X&s=USDGBP=X&f=l1&e=.cs

Extending our YahooCurrencyConverter from the previous post, to get currency information of more than one currency pair, is quite easy and therefore, I will jump directly to the main goal of this post: Getting currencies matrix information. Currency matrix information is a neat way of showing currency exchange rates of several currencies on a single table. Currency matrix can be illustrated easily by example. Currency matrix for the currencies: ILS, USD, GBP looks like:

         ILS     USD     GBP
ILS            0.247   0.170
USD    4.044           0.686
GBP    5.893   1.457        
In order to add currency matrix information, we will first do 2 things:
  • Add a new class named: CurrencyPair. This class represents the relation between 2 currencies.
  • Add a new method to the interface CurrencyConverter. This method defines a way of getting exchange rate information for several currencies.

Note, that the code in this post relies on the code from the post: Basic Java Currency converter using Yahoo Finance API.

This is the code of CurrencyPair class:
public class CurrencyPair {
    private String from;
    private String to;
    float price;
    public CurrencyPair(String from, String to)
    {
      this.from = from;
      this.to = to;
    }
    public String getFrom() {
        return from;
    }
    public void setFrom(String from) {
        this.from = from;
    }
    public String getTo() {
        return to;
    }
    public void setTo(String to) {
        this.to = to;
    }
    public float getPrice()
    {
        return price;
    }
}

This is the code of CurrencyConverter interface:
public interface CurrencyConverter {
    public float convert(String currencyFrom, String currencyTo) throws Exception;
    public void convert(CurrencyPair[] currencyPairs) throws Exception;
}


Now, we will add a new abstract class that will do the currency matrix calculations. Note that the currency matrix calculations relays on the new convert method added to CurrencyConverter interface:

public abstract class BaseCurrencyConverter implements CurrencyConverter {
    public CurrencyPair[][] getConversionMatrix(String... currencies) throws Exception {
        // Build pair combinations
        int size = currencies.length;
        CurrencyPair[] currencyPairs = new CurrencyPair[size * size];
        int index = 0;
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                currencyPairs[index++] = new CurrencyPair(currencies[i], currencies[j]);
            }
        }
        // Get currencies information
        convert(currencyPairs);
        // Build matrix
        CurrencyPair[][] matrix = new CurrencyPair[size][size];
        index = 0;
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                matrix[i][j] = i != j ? currencyPairs[index] : null;
                index++;
            }
        }
        return matrix;
    }
}

The method getConversionMatrix in the abstract class BaseCurrencyConverter builds currency matrix information for any class implementing the convert method. It does not care about how the convert method is implemented. The method receives a list of currencies for which we would like to build the matrix. From the list it builds all combinations between any 2 currencies. After building list of combinations, the abstract convert method is activated, to get the actual currency information. The final part of the class arranges the results in a 2 dimensional array, for easier data access.


Finally, after laying the structure, we can go back to the main implementing class: YahooCurrencyConverter. We don’t really have much more work to do, since the matrix creation is done on the abstract class. We just have to implement the new added convert method, to allow getting currency information for several currencies at a single request. In addition the class contains a small main program showing how the currency matrix can be used:
public class YahooCurrencyConverter extends BaseCurrencyConverter {
    public float convert(String currencyFrom, String currencyTo) throws IOException {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("http://quote.yahoo.com/d/quotes.csv?s=" + currencyFrom + currencyTo + "=X&f=l1&e=.csv");
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpGet, responseHandler);
        httpclient.getConnectionManager().shutdown();
        return Float.parseFloat(responseBody);
    }
    public void convert(CurrencyPair[] currencyPairs) throws IOException {
        HttpClient httpclient = new DefaultHttpClient();
        StringBuffer sb = new StringBuffer();
        for (CurrencyPair currencyPair : currencyPairs) {
            sb.append("s=").append(currencyPair.getFrom()).append(currencyPair.getTo()).append("=X&");
        }
        HttpGet httpGet = new HttpGet("http://quote.yahoo.com/d/quotes.csv?" + sb + "f=l1&e=.csv");
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpGet, responseHandler);
        httpclient.getConnectionManager().shutdown();
        String[] lines = responseBody.split("\n");
        if (lines.length != currencyPairs.length) {
            throw new IllegalStateException("Currency data mismatch");
        }
        int i = 0;
        for (String line : lines) {
            CurrencyPair currencyPair = currencyPairs[i++];
            currencyPair.price = Float.parseFloat(line);
        }
    }
    public static void main(String[] args) {
        YahooCurrencyConverter ycc = new YahooCurrencyConverter();
        try {
            String[] currencies = new String[] { "USD", "EUR", "GBP", "JPY", "CHF", "CAD", "AUD", "MXN", "ILS" };
            CurrencyPair[][] currencyPairs = ycc.getConversionMatrix(currencies);
            System.out.print("    ");
            for (int i = 0; i < currencyPairs.length; i++)
            {
                System.out.print("     " + currencies[i]);
            }
            System.out.println();
            for (int i = 0; i < currencyPairs.length; i++)
            {
                for (int j = 0; j < currencyPairs.length; j++)
                {
                    if (j == 0)
                    {
                        System.out.print(currencies[i] + " ");
                    }
                    CurrencyPair currencyPair = currencyPairs[i][j];
                    if (currencyPair != null)
                    {
                        System.out.printf("%8.3f", currencyPair.price);
                    }
                    else
                    {
                        System.out.print("        ");
                    }
                }
                System.out.println();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This is how the output for the currency matrix looks like:

         USD     EUR     GBP     JPY     CHF     CAD     AUD     MXN     ILS
USD            0.733   0.686  96.940   1.125   1.223   1.419  14.275   4.041
EUR    1.363           0.936 132.178   1.534   1.668   1.935  19.464   5.510
GBP    1.457   1.069         141.246   1.639   1.782   2.068  20.799   5.888
JPY    0.010   0.008   0.007           0.012   0.013   0.015   0.147   0.042
CHF    0.889   0.652   0.610  86.177           1.087   1.262  12.690   3.592
CAD    0.817   0.600   0.561  79.245   0.920           1.160  11.669   3.303
AUD    0.705   0.517   0.484  68.309   0.793   0.862          10.059   2.848
MXN    0.070   0.051   0.048   6.791   0.079   0.086   0.099           0.283
ILS    0.248   0.182   0.170  23.989   0.278   0.303   0.351   3.533        

I hope this code will help you to easily build your own custom currency matrices. This information may be useful to currency traders or applications that need to show the latest exchange rate between currencies.

As stated for the previous post, this class is making use of the Apache open source project: Http Client. You will need to put in your project the proper jar files for this class to work properly.

20 comments:

  1. how to get date in yahoo finance?

    ReplyDelete
  2. X & f=sl1d1t1ba&e=.csv

    i want to show the date in my program.code as above.how do i use

    ReplyDelete
  3. Try something like:
    http://download.finance.yahoo.com/d/quotes.csv?s=USDILS=X&s=USDJPY=X&s=USDGBP=X&f=l1d1t1&e=.cs

    ReplyDelete
  4. We are also getting rates for currencies from Y! using a similar approach to above and it seems to be working fine.

    Just wanted to find out if you happen to know whether it is possible to get currency rates for different currencies in the past. We are having currency values missing for historical dates and I am checking to see if there is a way to retrieve them from Y!.

    Any help from you is appreciated, thanks

    ReplyDelete
  5. As far as I remember, there is no way of taking past data. There are services that can give back past data, but they cost money...

    ReplyDelete
  6. Thank you very much ! It's exactly what i needed !

    ReplyDelete
  7. Thank you very much ! It's exactly what i needed !

    ReplyDelete
  8. This is great! Thanks for your time putting this together

    ReplyDelete
  9. Please provide the link to download the jar file

    Thank you

    ReplyDelete
  10. Hi Sai, sorry for not putting a jar file for this code. As I was telling you on the other post, it is quite easy to copy and paste this code to your Java IDE and things should run and work properly.

    ReplyDelete
  11. Hi Bashan,
    Please provide the link of required jars for this to run.

    I am having problem only with the jars. Please need them. So help me out

    ReplyDelete
  12. Hi Yashwanth,
    Please look the the previous comment, about the JARs.

    ReplyDelete
  13. Hi Bashan,

    Sorry my mistake, but thanks for the url. Its working for me....Really appreciate ur help and response.

    ReplyDelete
  14. Bashan,

    I need a help on the code written above.
    Will this support any number of currencies?
    As I am trying for more than 13 currencies as part of my work,its failing

    Below are the currencies I have been trying for.So please help me out.

    "USD", "EUR", "AUD", "INR", "CNY", "GBP", "HKD", "JPY", "KRW", "MYR", "SGD", "TWD", "CAD"

    When I add "VEF" to the above list its failing.So can u please help me out.

    Thank you in advance.

    ReplyDelete
  15. Hi Yashwanth,
    Did you try adding a currency other than: "VEF"? If other currency works OK I would guess "VEF" currency is simply not supported or the currency code is wrong. If adding another code also doesn't work, so there is probably a limit on the number of currencies and the only way to go around it, is to make several calls to get all the data.

    ReplyDelete
  16. Is there anyway to get all data from on currency pair that ranges from now to the last two or three years?

    ReplyDelete
  17. Hi Bandito,
    I do not think there is a way of getting History from this API. I think that for getting historical currency services you have to pay to some company that collects the info and charge for it, but I am not sure.

    ReplyDelete
  18. I am also looking for historical Fx data for batch download. See the link below for free sources for single downloads to cvs.
    http://computeraidedfinance.com/2012/05/02/comprehensive-list-of-free-historical-market-data-sources/

    ReplyDelete
  19. I am also looking for historical Fx data for batch download. The link below is a pretty comprehensive list for single currency pair download.
    http://computeraidedfinance.com/2012/05/02/comprehensive-list-of-free-historical-market-data-sources/

    ReplyDelete