.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> Windows Application >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Making live stocks ticker in c#

Posted By:Dhiraj Ranka       Posted Date: July 21, 2011    Points: 200    Category: Windows Application    URL: http://www.dotnetspark.com  

In this article we will see how to Make real time stock ticker (share market) application in c#


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. http://download.finance.yahoo.com/d/quotes.csv?s=DELL+BCS+HPQ&f=sl1d1t1c1hgvbap2 Depending on the browser it will either ask for save the file as named "quotes.csv" or it will render following in your browser
DELL 16.98 7/1/2011 4:00pm 0.31 17 16.6 13751028 13.53 20.24 1.86%
BCS 17.09 7/1/2011 4:01pm 0.66 17.19 16.83 4259646 N/A N/A 4.02%
HPQ 37.05 7/1/2011 4:00pm 0.65 37.13 36.34 14855025 N/A N/A 1.79%

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:
a Ask a2 Average Daily Volume a5 Ask Size
b Bid b2 Ask (Real-time) b3 Bid (Real-time)
b4 Book Value b6 Bid Size c Change & Percent Change
c1 Change c3 Commission c6 Change (Real-time)
c8 After Hours Change (Real-time) d Dividend/Share d1 Last Trade Date
d2 Trade Date e Earnings/Share e1 Error Indication (returned for symbol changed / invalid)
e7 EPS Estimate Current Year e8 EPS Estimate Next Year e9 EPS Estimate Next Quarter
f6 Float Shares g Day's Low h Day's High
j 52-week Low k 52-week High g1 Holdings Gain Percent
g3 Annualized Gain g4 Holdings Gain g5 Holdings Gain Percent (Real-time)
g6 Holdings Gain (Real-time) i More Info i5 Order Book (Real-time)
j1 Market Capitalization j3 Market Cap (Real-time) j4 EBITDA
j5 Change From 52-week Low j6 Percent Change From 52-week Low k1 Last Trade (Real-time) With Time
k2 Change Percent (Real-time) k3 Last Trade Size k4 Change From 52-week High
k5 Percebt Change From 52-week High l Last Trade (With Time) l1 Last Trade (Price Only)
l2 High Limit l3 Low Limit m Day's Range
m2 Day's Range (Real-time) m3 50-day Moving Average m4 200-day Moving Average
m5 Change From 200-day Moving Average m6 Percent Change From 200-day Moving Average m7 Change From 50-day Moving Average
m8 Percent Change From 50-day Moving Average n Name n4 Notes
o Open p Previous Close p1 Price Paid
p2 Change in Percent p5 Price/Sales p6 Price/Book
q Ex-Dividend Date r P/E Ratio r1 Dividend Pay Date
r2 P/E Ratio (Real-time) r5 PEG Ratio r6 Price/EPS Estimate Current Year
r7 Price/EPS Estimate Next Year s Symbol s1 Shares Owned
s7 Short Ratio t1 Last Trade Time t6 Trade Links
t7 Ticker Trend t8 1 yr Target Price v Volume/td>
v1 Holdings Value v7 Holdings Value (Real-time)/td> w 52-week Range
w1 Day's Value Change w4 Day's Value Change (Real-time) x Stock Exchange
y Dividend Yield

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 http://download.finance.yahoo.com/d/quotes.csv?s=YHOO+GOOG+MSFT&f=nl1vkj


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

Tiny Charts

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

Bigger Charts
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
max: http://chart.finance.yahoo.com/c/my/dell

Getting started

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:
  1. Open Visual Studio 2010.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog box, under Project Types, click Visual C# Projects. Under Templates, click Windows.
  4. In the Name text box, type StockQuotes.
  5. Click OK.
Step 1 - Creating new project

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.
Step 2 - Form 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)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace StockQuotes
    public partial class StockQuotes : Form
        // Stock symbols seperated by space or comma.
        protected string q_symbol = "";

        public StockQuotes()
            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());
                catch(Exception ex)
                    // 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 Step 3 - Output


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.



 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend