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=.csFor 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=.csExtending 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:
In order to add currency matrix information, we will first do 2 things:ILS USD GBPILS 0.247 0.170USD 4.044 0.686GBP 5.893 1.457
- 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 combinationsint 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 informationconvert(currencyPairs);// Build matrixCurrencyPair[][] 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 ILSUSD 0.733 0.686 96.940 1.125 1.223 1.419 14.275 4.041EUR 1.363 0.936 132.178 1.534 1.668 1.935 19.464 5.510GBP 1.457 1.069 141.246 1.639 1.782 2.068 20.799 5.888JPY 0.010 0.008 0.007 0.012 0.013 0.015 0.147 0.042CHF 0.889 0.652 0.610 86.177 1.087 1.262 12.690 3.592CAD 0.817 0.600 0.561 79.245 0.920 1.160 11.669 3.303AUD 0.705 0.517 0.484 68.309 0.793 0.862 10.059 2.848MXN 0.070 0.051 0.048 6.791 0.079 0.086 0.099 0.283ILS 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.