I would have thought that stock quote API's would be everywhere. It seems like getting a stock quote is among the most obvious 3rd party services out there. Yet, when I needed to find one, I was at a loss.
There's the Google Finance API - but that seems overkill. It will get information about a stock portfolio. But, what if I don't want a portfolio? What if I just want to know what the current price of a stock is?
Yahoo also has a finance developer section. But, theirs is focused on giving you RSS feeds for company information. Interesting, but again, if all I want to know is the current price of a stock - this doesn't help.
After much poking around, I found that Yahoo does offer a solution. It's even a refreshingly simple one. You can get stock data in CSV format by hitting the following URL:
Which in this case, returns:
More generally, the URL has the parameter s which takes in stock symbols (which would be space URL encoded - or + separated), and f is set to a format string. If you visit this page you can see all the format string options.
I'm a little bit confused by Yahoo doesn't advertise this capability more. But, it seems there and just what I need, so I may end up using.
Seeing It In Action
Here's some trivial code to grab back stock quotes using Scheme
(require (planet neil/csv:1:2/csv) net/url) (define-struct sq (symbol price last-closed opened) #:prefab) (define (stock-quote stock-symbol) (let ([url (format "http://download.finance.yahoo.com/d/quotes.csv?s=~a&f=sl1po" stock-symbol)]) (apply make-sq (first (csv->list (port->string (get-pure-port (string->url url))))))))
With the stock-quote function defined, you can say:
(define q1 (stock-quote "MSFT")) (printf "Current price: ~a\n" (sq-price q1))
Definitely not rocket science. But, pulling down a CSV feed from a URL and cramming it into a data structure shouldn't be. It should be easy, and as the above code shows, it is.