Collecting more raw data with the Yahoo Quote Reader

2013-07-28_12-04-26_679In the previous post we have built a Quote Reader for Data from Bitstamp. It provides useful market information regarding Bitcoin trading on the Bitstamp platform. That is a good start, but certainly not enough for prognostic purposes. How shall we proceed?

The Bitcoin market is heavily affected by world events and economic factors. The steep November price hike was arguably induced by Chinese gambling trends that emerged during the decline of the domestic stock and housing market. The all time high of the Bitcoin price was a reaction to the imminent expropriation of Cypriot bank customers.

We conclude, that knowledge about the world beyond the Bitcoin markets could be useful for a price prediction. There is one particular class of information, that is easy to acquire and at the same time reflects relevant political shocks and the macro economic situation very well: stock quotes.


You have a great range of options for feeding stock ticker data to your software. One of the most prominent is Yahoo finance. The following Java class reads quotes for arbitrary ticker symbols from a Yahoo service that returns them as comma separated values (CSV).


package de.hsec.datascience.btctrader;

import java.util.logging.Logger;

* Reads quotes from yahoo finance.
* @author helmut hauschild
public class YahooFinanceQuoteReader implements QuoteReader {

* Yahoo finance API URL for ticker symbol
private URL yahooFinanceApiUrl;

* the quote last read.
private double currentQuote;

static Logger log = Logger.getLogger(YahooFinanceQuoteReader.class

* Constructor
* @param pTickerSymbol
* ticker symbol to read
public YahooFinanceQuoteReader(String pTickerSymbol) {
String lUrl = String.format(
try {
yahooFinanceApiUrl = new URL(lUrl);
} catch (MalformedURLException e) {
throw new RuntimeException(
"Cannot initialize YahooQuoteReader class.", e);

* Read and return a new ticker quote from Yahoo Finance.
public double getCurrentQuote() {
return currentQuote;

// other getters

public double getBid() {
// We don't have this information. Return 0.
return 0;

// ...

* Read current data from the Yahoo Finance and update fields.
private void readNextAndUpdate() {
BufferedReader in = null;
StringBuffer sb = new StringBuffer();
try {
in = new BufferedReader(new InputStreamReader(

String inputLine;
while ((inputLine = in.readLine()) != null)
currentQuote = Double.parseDouble(sb.toString().split(",")[1]);
} catch (Exception e) {
// catch-all because we don't have a surrounding framework (other
// then the
// JRE) to handle unexpected exceptions).
log.severe(String.format("Error reading data from yahoo api: %s",
} finally {
try {
if (in != null)
} catch (IOException e) {
log.severe("Unable to close input stream from yahoo api");

* main method for simple tests
public final static void main(String[] args) {
YahooFinanceQuoteReader rqr = new YahooFinanceQuoteReader("A1YKTG.F");"Next Quote: " + rqr.getCurrentQuote());

A few remarks on this:

  • The BitstampQuoteReader does not need to be initialized with a ticker symbol, because there is only one price to read. The YahooFinanceQuoteReader is a bit different in this respect. For each ticker symbol you are interested in, you create a new dedicated instance.
  • The CSV service returns only the current value and the opening price. This does not match the QuoteReader interface, but you want to implement it anyway, because you want to handle the input data in a unified way as much as possible. As a consequence, you have to provide empty implementations for methods like getBid, getAsk, etc..

The main method is added as an easy way to test the implementation. If you prefer a unit test, it should be fairly easy to move the code there.


The implementation for the GoogleFinanceQuoteReader is very similar to the Yahoo version. The URL can be created like this:

new URL(String.format("",tickerSymbol));

The response comes as JSON. I will skip the rest of the implementation here to prevent redundancy. Also, it is not clear, how much longer the service will be available. The Google finance API is deprecated since 2011.



One thought on “Collecting more raw data with the Yahoo Quote Reader

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s