Skip to content

Instantly share code, notes, and snippets.

@kocsenc
Last active July 31, 2019 19:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kocsenc/fd7febfda2f6eb8dffb4 to your computer and use it in GitHub Desktop.
Save kocsenc/fd7febfda2f6eb8dffb4 to your computer and use it in GitHub Desktop.
SWEN 262 - Interfacing with Yahoo's Finance API

Using Yahoo's Finance API

It was surprisingly hard to find a simple straight forward stock API; this community built API hosted by Yahoo does the job.

Let's get to it.

Below is a complete URL example if you want to play on your own and figure it out.

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AAPL%22)&env=store://datatables.org/alltableswithkeys

Interactive Dashboard

Theres an awesome interactive dashboard that you should play with. If you still need a breakdown on what's happening, then keep reading.

Let's digest the url.

All urls have a BASE and then PARAMETERS. The Parameter sequence starts with a ? and are delimited with an &.

So you would have something like: http://baseURL.com/path?name1=value1&name2=value2....

This is the BASE URL

http://query.yahooapis.com/v1/public/yql

Then comes the first parameter, the QUERY. This query is not directly SQL instead its something Yahoo calls Yahoo Query Language or YQL. For the purposes of the activity; they are very similar and hence straightforward.

?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AAPL%22)

The above query translates into SELECT * FROM yahoo.finance.quotes WHERE SYMBOL IN ("AAPL"). The reason the %20's are there is because to send the space character through a browser it needs to be URL Encoded, but you probably won't be needing to do that.

Note: We are selecting everything with SELECT * ... but we can also select specific field(s). For the project we only really want LastTradePriceOnly so we can say SELECT LastTradePriceOnly ... and its way cleaner.

The final parameter is &env=store://datatables.org/alltablewithkeys. So long as you include this you will be ok. The reason this is needed is because it's a community kept database so it needs a specific env or environment.

Parsing Yahoo's API with JAVA

We need two main components to make this happen in Java:

  1. A HTTP Library for Java that will allow us to make request to the internet
  2. XML Parser to read in the information.

HTTP Java Library

Java has a built in (library)[https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html] to make HTTP Requests called java.net.HttpURLConnection. There are other ones like (apache)[http://hc.apache.org/httpclient-3.x/tutorial.html] but that's beyond the scope.

See the file RequestAPIExample.java as an example of how to make the GET request for the API.

XML Parsing

The data returned is in XML format; it is literally like getting a file written in XML and then parsing it. In Java there are a few ways to parse XML:

  • DOM (RECOMMENDED)
  • SAX
  • JAXP

Click on the (following tutorial)[http://www.javacodegeeks.com/2013/05/parsing-xml-using-dom-sax-and-stax-parser-in-java.html] on how to parse XML.

Do you like JSON Better?

The data returns in XML format by default, if you want JSON then add the following parameter to the url:

&format=json

Note: Java has more robust standard libraries for parsing XML than JSON.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class RequestAPIExample {
public static void main(String[] args) throws IOException {
String url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AAPL%22)&env=store://datatables.org/alltableswithkeys";
// Create a URL and open a connection
URL YahooURL = new URL(url);
HttpURLConnection con = (HttpURLConnection) YahooURL.openConnection();
// Set the HTTP Request type method to GET (Default: GET)
con.setRequestMethod("GET");
// Created a BufferedReader to read the contents of the request.
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
// MAKE SURE TO CLOSE YOUR CONNECTION!
in.close();
// response is the contents of the XML
System.out.println(response.toString());
}
}
@jcdesimp
Copy link

jcdesimp commented Oct 8, 2015

:shipit:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment