How-To Obtain Stock Information from Yahoo Finance | Actionscript | Adobe Flex

Yahoo Finance offers an extensive API to obtain an vaste amount of information on a public companie’s stock trading. Their API is accessible via url parameters and can return the information in the form of csv formatted list. Another option is to the YQL (Yahoo Query Language) to obtain stock trading information. The nice thing is that the Yahoo Finance API has made easy to obtain this information using a language like Adobe Actionscript in conjunction with Adobe Flash or more specfically Adobe Flex.

Yahoo Finance uses parameters on an url to obtain and return the stock information. The following table list the available types of information on stocks that is available.

Parameter
Description
Parameter
Description
a
Ask
k5
% Change from 52-W
a2
Average Daily Volume
l
Last Trade with Time
a5
Ask Size
l1
Last Trade (Price Only)
b
Bid
l2
High Limit
b2
Ask (RT)
l3
Low Limit
b3
Bid (RT)
m
Day's Range
b4
Book Value
m2
Day's Range (RT)
b6
Bid Size
m3
50-Day MA
c
% Change
m4
200-D MA
c1
Change
m5
Change from 200-D MA
c3
Commission
m6
% Change from 200-D MA
c6
Change (RT)
m7
Change from 50-D MA
c8
After Hour Change (RT)
m8
% Change from 50-D MA
d
Dividend/Share
n
Name
d1
Last Trade Date
n4
Notes
d2
Trade Date
o
Open
e
Earning/Share
p
Previous Close
e1
Error Indication
p1
Price Paid
e7
EPS Estimate Current Year
p2
Change in %
e8
EPS Estimate Next Year
p5
Price/Sales
e9
EPS Estimate Next Quarter
p6
Price/Book
f6
Float Shares
q
Ex-Dividend Date
g
Day's Low
r
P/E Ratio
h
Day's High
r1
Dividend Pay Date
j
52-Week Low
r2
P/E Ratio (RT)
k
52-Week High
r5
PEG Ratio
g1
Holding Gain %
r6
Price/EPS Estimate Current Year
g3
Annualized Gain
r7
Price/EPS Estimate Next Year
g4
Holding Gain
s
Symbol
g5
Holding Gain % (RT)
s1
Shares Owned
g6
Holding Gain (RT)
s7
Short Ratio
i
More Info
t1
Last Trade Time
i5
Order Book (RT)
t6
Trade Links
j1
Market Capitalization
t7
Ticker Trend
j3
Market Cap (RT)
t8
1 Year Target
j4
EBITDA
v
Volume
j5
Change from 52-W Low
v1
Holding Value
j6
% Change from 52-W Low
v7
Holding Value (RT)
k1
Last Trade with Time (RT)
w
52-W Range
k2
Change % (RT)
w1
Day's Value Change
k3
Last Trade Size
w4
Day's Value Change(RT)
k4
Change from 52-W High
x
Stock Exchange
 
 
y
dividend Yield
Yahoo Finance Parameter List

Yahoo Finance Query URL Format

To obtain the stock information in the csv form, you can use the following url:


http://quote.yahoo.com/d/quotes.csv?s=goog&f=snl1a


The first parameter field “s” indicates which companies the needs to be queried. Stock symbols can be join by using the “+” symbol. The second parameter field “f” is the type of information that needs to be returned. The second parameter contain any combination of query parameters. They are grouped together without any separator.

So the above url is seeking informaiton on Google (GOOG) The types of information requested are : symbol, name, last traded and ask.

protected function GetQutote():void
			{
				httpServ = new HTTPService();
				httpServ.url = 'http://quote.yahoo.com/d/quotes.csv?s=goog&f=snl1a';
				
				httpServ.method = 'GET';
				
				httpServ.addEventListener(ResultEvent.RESULT,yDataResult);
				httpServ.addEventListener(FaultEvent.FAULT,yDataFault);
				httpServ.send();
			}

Actionscript App Code

Using this knowledge we can construct a script in Actionscript to return information for the Yahoo Finance service and store it as an Array, ArrayCollection, or XML. To obtain this information using Actionscript, we will use the HttpService. Here is sample code to demonstrate how this can be done, although in an actual production component, more validation and other types of controls should be included.

Knowing the parameters that we sent, we can build an ArrayCollection, adding labels to the data. To build to the ArrayCollection.

protected function yDataResult(evt:ResultEvent):void
{
var s:String = evt.result as String;
	var a:Array = s.split(",");
	var makeObject:Object = new Object();
				
	//To add to a DataGrid or List or GroupData
	var stockArr:ArrayCollection = new ArrayCollection();
				
	makeObject = ({symbol: a[0],name: a[1],last: a[2],
		ask: a[3].toString().substring(0, a[3].toString().indexOf('\r\n'))})
				stockArr.addItem(makeObject);
}

You could just as easily add the output to Labels or TextInput fields on a form or other visual element like so:

protected function yDataResult(evt:ResultEvent):void
{
var s:String = evt.result as String;
	var a:Array = s.split(",");
					
	//To add to a form or individual fields
	var symbol:Label =  a[0].toString();
	var name:Label =  a[1].toString();
	var last:Label =  a[2].toString();
	var ask:Label =  a[3].toString();
}

Here is the output of the query, and complete code:

Query Output in Adobe Flash Builder
Query Output in Adobe Flash Builder
Complete Code
Complete Code

In Summary

You can download the project, including all the sample code, here at : http://code.google.com/p/kevlangdo-actionscript/

Code: http://code.google.com/p/kevlangdo-actionscript/downloads/list


At this point, you could add this ArrayColleciton to any DataProvider for a DataGrid, a GroupData or a List. Yahoo Finance makes it easy to obtain stock information on any publicly listed company. Actionscript provides a robust API for data manipulation.

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working