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
.