Put your message here! Contact me for more information
 
 








 

Stock QuoteI’m back from a period of hibernation. Lots of exciting stuff and new things, just barely have enough time to sleep and catch up with all the pending projects that I’ve been crazily taking on. Anyway, today I’d like to share something that maybe useful for someone out there: how to retrieve live stock quote data using Yahoo quote service with ColdFusion.

I was working on a project which originally utilizes the WebServiceX.net’s [[http://www.webservicex.net/stockquote.asmx/GetQuote|StockQuote web service]]. Things were working fine until WebServicex.net is down, as of today, March 22nd, 2007, with a big fat “Service Unavailable”

“Crap!” was what I said to myself. Luckily my work buddy forwarded a [[http://aspalliance.com/articleViewer.aspx?aId=112&pId=#Page2|link]] to an article on how to access the Yahoo Stock Quote service using ASP.NET (C#). Bingo! This was exactly what I needed. Based on the article, I was able to hack out the code using ColdFusion to retrieve stock info in just a few lines of code (or markup since I don’t really consider ColdFusion a real “language”)

===== Yahoo Stock quote CSV service =====
First of all, I didn’t even know if this service ever exists. I don’t really know where the official Yahoo documentations for the Stock Quote CSV service is (anybody knows the link please let me know). More ironically, I used Google to search for tutorials on how to use the Yahoo Stock Quote API. Anyway, this is the URL to request a CSV (comma-separated values) file containing stock information for a particular symbol, in this case, YHOO (I think I do have to give Yahoo some credits for the free service)http://quote.yahoo.com/d/quotes.csv?s=YHOO&f=l1c1v
with
* **s=SYMBOL**: with SYMBOL is the actual stock symbol
* **f=REQUESTED_FORMAT_STRING** where the REQUESTED_FORMAT_STRING is a string of letters representing what fields to be requested. The oder of the letters will be used as the order of the fields in the returned CSV.\\ I messed around and found out about the meaning of the letters used in the **f** parameter. If you want the full information about the symbol, you can use **f=sl1d1t1c1ohgvj1pp2wern** instead.http://quote.yahoo.com/d/quotes.csv?s=YHOO&f=sl1d1t1c1ohgvj1pp2wern Don’t sweat, here comes the explanation for each letter (or letter-digit pair)
* s: the symbol name
* l: last value (or current price). If you use l alone, you will get back a string similar to Mar 22 - 31.26
* l1: (letter l and the number 1) to just get the last value of the symbol
* d: d alone will give you 0, while d1 will return the current date (e.g. 3/22/2007)
* t: t by itself will request the yahoo-generated chart. However, you will get back the chart image with a whole bunch of other HTML garbage, e.g. src=http://chart.yahoo.com/c//y/yhoo.gif" alt="Chart">


* t1: the time of last update, for example 4:00pm.
* c: the change amount. Can either be used as c or c1.
* o: opening value
* h: high value
* g: low value
* v: volume
* j: 52-week low.
* j1: the market cap. This is string like “42.405B” for $42 billion. Man… that can buy **a lot** of hamburger or bowls of phở
* p: after hour price (?)
* p1: (?)
* p2: change percentage e.g. “-0.10%”
* w: 52-week range
* e: EPS (Earning per share)
* r: P/E (Prince/Earning) ratio
* n: Company name

(This list is hand-compiled by me based on my own interpretation. If you find any explanation being inaccurate, please let me know)

===== The Code =====
ColdFusionThe code is pretty simple: we will read from the result the Yahoo CSV service using the tag into a variable. This variable will then contain the comma-separated values list of what we put in for the f parameter. Back to our example, the URL we are requesting is http://quote.yahoo.com/d/quotes.csv?s=YHOO&f=l1c1v, which will return to us a string 31.26,-0.03,13249801. Since the response is in CSV format, we can utilize the ColdFusion List functions like ListLen(), ListGetAt() to access the values.

Here is the CFML code snippet





#stockInfo#




* First, we construct the Yahoo Stock Quote service URL. We are requesting for last price, change amount, and the stock volume.
* Next we use the cfhttp tag to request the url, then grab the content into the cfhttp.FileContent variable and assign to the stockInfo variable. stockInfo will contain something similar to 31.26,-0.03,13249801
* To get the different values in the list, we use ListGetAt() with the appropriate index. And remember, indices of Array and List in ColdFusion start at 1, not 0.

That’s it!

===== Final Remarks =====
I hope you will find this useful. Thanks to Yahoo for providing a free stock quote service. I know I can **abuse** this CSV service as much as possible before getting banned because it’s Yahoo. Any services other than those of Yahoo, Google, Microsoft, I will probably have some guilty thoughts before doing anthing. But it’s Yahoo so I really don’t have no guilt abusing it. Just kidding though. I do appreciate the free (as in free beer) stock quote service.

===== References =====
* Bromberg, Peter, PhD. [[http://www.eggheadcafe.com/articles/20010404.asp|Build a C# Stock Quote WebService and Client using the WebService Behavior HTC (Part I)]]
* Perry, Jason. [[http://aspalliance.com/articleViewer.aspx?aId=112&pId=#Page2|Building a Yahoo stock quote ticker]]


 

10 Responses to “Coldfusion and Yahoo CSV Stock Quote service



10:18 am
March 23, 2007
#19647

My work buddy found this site

http://www.gummy-stuff.org/Yahoo-data.htm

It has really explanation for the formatting tags




Mike Bonner
12:08 pm
May 22, 2007
#31188

Hey, heres a few more codes.
k 52 week high
a ask
b bid
d dividend
f Full name, ticker definition
w 52 week range
x which exchange
y yield




Mike
12:09 pm
May 22, 2007
#31189

er. nvm. The link the other guy sent is great.




Sam
8:06 pm
July 30, 2007
#47812

Does anyone know a way to wrap this into a Java program? I’ve read about cold fusion and C but nothing on Java.




po
7:18 pm
January 24, 2008
#106122

Hi,
How difficult would it be to put this on my site at stockquotes.hk ? I am currently using an apache webserver with php.




Viraj More
5:43 am
February 13, 2008
#115695

hello everybody….
I am here,jst to say thank U very much to
Alex Le for posting the link http://www.gummy-stuff.org/Yahoo-data.htm
it is really helpfull & simple to understand sotck maraket’s tags information
once again Thank u Very much




8:36 am
February 13, 2008
#115738

Thanks for all the info,

But the problem ,
can anybody give me a list of Yahoo stock symbols which can be used




11:29 am
March 10, 2008
#127376

Rahul, you just need to enter a company name and it will show you the ticker symbol.

If you enter APPLE for example, you quickly see:

http://finance.yahoo.com/q?s=AAPL

Good luck,
Jonas




Robert
8:46 am
March 13, 2008
#128549

Are there similar sites, such as MSNBC or others? I don’t want to abuse, er annoy Yahoo by hitting them too much.




9:19 am
March 13, 2008
#128556

@Robert,
So far I think Yahoo is the most generous one that we can use for now. I do know that MSN finance also provides historical data since I am using MSN historical data for a client to do charting and analysis. However, I have never actually work directly with their API since I got the data back from a package written for R (http://www.r-project.org/).




 

Leave a Reply