Stock market has always attracted people to invest money, whether it has gone up or down. Many of us made money :-) and few of us must have lost money :-( Now what made me write this article is necessary of understand that how to access stock services with 20 min delay from Yahoo Finance Service. You can download the complete source code from here.
It is very easy to use stock data from yahoo finance service. You can try with the following link by copy pasting it in your browser.
Depending on the browser it will either ask for save the file as named "quotes.csv" or it will render following in your browser
Of course not in a such a formatted way but in comma (,) separated. As we know that the components of URL like base URL and a parameters, so it is not an exception.
http://download.finance.yahoo.com/d/quotes.csv? - The base URL to retrieve information
's=' - query stock symbols separated by "+" after this
& - indicates that there is another parameter
'f=' - special tags without any spaces in between
The following are special tags provided by Yahoo finance:
||Average Daily Volume
||Change & Percent Change
||After Hours Change (Real-time)
||Last Trade Date
||Error Indication (returned for symbol changed / invalid)
||EPS Estimate Current Year
||EPS Estimate Next Year
||EPS Estimate Next Quarter
||Holdings Gain Percent
||Holdings Gain Percent (Real-time)
||Holdings Gain (Real-time)
||Order Book (Real-time)
||Market Cap (Real-time)
||Change From 52-week Low
||Percent Change From 52-week Low
||Last Trade (Real-time) With Time
||Change Percent (Real-time)
||Last Trade Size
||Change From 52-week High
||Percebt Change From 52-week High
||Last Trade (With Time)
||Last Trade (Price Only)
||Day's Range (Real-time)
||50-day Moving Average
||200-day Moving Average
||Change From 200-day Moving Average
||Percent Change From 200-day Moving Average
||Change From 50-day Moving Average
||Percent Change From 50-day Moving Average
||Change in Percent
||Dividend Pay Date
||P/E Ratio (Real-time)
||Price/EPS Estimate Current Year
||Price/EPS Estimate Next Year
||Last Trade Time
||1 yr Target Price
||Holdings Value (Real-time)/td>
||Day's Value Change
||Day's Value Change (Real-time)
Let's assume that we want to get the information for following quotes: DELL, BCS, HPQ. We will get the Name, Last Price, Last traded volume, 52-week high. Using table above you can use other special tags as needed. Following are the tags that we have used
Name - n
Last Price- l1
Last Traded Volume - v
52-Week High - k
52-Week Low - j
We just have to construct the appropriate URL using these special tags, which would look like this
Once we got the row data it is also very important to put it pictorially, that is nothing but charts. As we got data easily from yahoo finance service same goes for charts as well. All we need to do is craft a request specifically for an image to yahoo finance service will respond with required image.
For example this is request for 3 months chart for DELL
This URL can also be modified as we have done while getting the information for getting small and big chart
1 day: http://ichart.yahoo.com/t?s=DELL
5 days: http://ichart.yahoo.com/v?s=DELL
1 year: http://ichart.finance.yahoo.com/c/bb/m/dell
1 day: http://ichart.finance.yahoo.com/b?s=DELL
5 days: http://ichart.finance.yahoo.com/w?s=DELL
3 months: http://chart.finance.yahoo.com/c/3m/dell
6 months: http://chart.finance.yahoo.com/c/6m/dell
1 year: http://chart.finance.yahoo.com/c/1y/dell
2 years: http://chart.finance.yahoo.com/c/2y/dell
5 years: http://chart.finance.yahoo.com/c/5y/dell
In order to start this we have to create and code a bit. I am going to create windows forms application in visual studio using following steps:
- Open Visual Studio 2010.
- On the File menu, point to New, and then click Project.
- In the New Project dialog box, under Project Types, click Visual C# Projects. Under Templates, click Windows.
- In the Name text box, type StockQuotes.
- Click OK.
Next thing we have to do is just put some controls and make our form ready for the action. You can create of your own or else you can use this design.
Now once you have your design ready you are just away from few lines of code. So you can paste the following code in your "StockQuotes.cs" file (Code behind file for this form)
public partial class StockQuotes : Form
// Stock symbols seperated by space or comma.
protected string q_symbol = "";
if (q_symbol == "")
q_symbol = "YHOO";
q_symbol = txtSymbol.Text;
txtSymbol.Text = q_symbol;
private void btnGetQuotes_Click(object sender, EventArgs e)
// Update the textbox value.
q_symbol = txtSymbol.Text;
// This DIV that contains text and DIVs that displays stock quotes and chart from Yahoo.
// Set the innerHTML property to replaces the existing content of the DIV.
if (q_symbol.Trim() != "")
// Return the stock quote data in XML format.
String arg = GetQuote(q_symbol.Trim());
if (arg == null)
// Display stock charts.
String symbols = q_symbol.Replace(",", " ").Split(' ');
// Loop through each stock
for (int i = 0; i < symbols.Length; ++i)
if (symbols[i].Trim() == "")
// If index = -1, the stock symbol is valid.
// Use a random number to defeat cache.
Random random = new Random();
imgChart.Load("http://ichart.finance.yahoo.com/b?s=" + symbols[i].Trim().ToUpper() + "&" + random.Next());
// Handle exceptions
Now you are all set to run this application. So you just fit the F5 and start typing the quotes in the textbox and then hit the "Get quotes" button. For example you can use "YHOO" for Yahoo!, "GOOG" for Google, "MSFT" for Microsoft, "DELL" for Dell, etc. and you will have an output something like this
This article walked you through a tutorial for Yahoo Finance usage and displaying the charts in your application using C# .NET. There is not limit to the creativity, so you can use this services as per your need.