Click here to Skip to main content
15,884,298 members
Articles / Desktop Programming / Windows Forms
Article

StockBag

Rate me:
Please Sign up or sign in to vote.
4.82/5 (23 votes)
19 Jan 2008CPOL2 min read 105.3K   4.4K   111   16
A tool to retreive live stock/share data from the web.

Image 1

Introduction

StockBag is a desktop utility wherein you can define your stock portfolio/watch-list and view live stock data.

Background

I was trying, for some time, to access live stock market data through a desktop utility, and found that we need to pay for getting live stock data. Then, I found that Yahoo! Finance gives a 15 minutes delayed data through its CSV service. Then, I thought of writing my own little gadget to have a portfolio and watch list. Currently, StockBag can be used to get live stock data from the Indian stock market. However, it can be modified to get live stock data from any stock market over the world which Yahoo! Finance supports.

Using the code

Most of the action happens in the StockBagMainForm class. There are two timers, one for getting live stock data from Yahoo! Finance and the other for refreshing the list. The live stock data timer creates a thread which will access Yahoo! Finance to get a CSV format string with live data.

The key method is GetStockData(), which is used to get stock data.

C#
private void GetStockData(string stockCode, StockData stockData)
{
    string serverUrl = @"http://in.finance.yahoo.com/d/quotes.csv?s="+stockCode+
            "&f=sl1d1t1c1ohgvj1pp2owern&e=.csv";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverUrl);

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    StreamReader reader = new StreamReader(response.GetResponseStream(), 
        Encoding.ASCII);

    string stockDataString = reader.ReadLine();
    string[] stockDataContents = stockDataString.Split(',');

    stockData.Code = stockCode;
    stockData.Last = stockDataContents[1];
    stockData.Date = stockDataContents[2];
    stockData.Time = stockDataContents[3];
    stockData.Change = stockDataContents[4];
    stockData.Open = stockDataContents[5];
    stockData.High = stockDataContents[6];
    stockData.Low = stockDataContents[7];
    stockData.Volume = stockDataContents[8];
    stockData.MarketCapital = stockDataContents[9];
    stockData.PreviousClose = stockDataContents[10];
    stockData.PctChange = stockDataContents[11];
    stockData.AnnRange = stockDataContents[12];
    stockData.Earnings = stockDataContents[13];
    stockData.PERatio = stockDataContents[14];

    response.Close();
}

The tool uses the HttpWebRequest and HttpWebResponse .NET classes, which, by default, use the IE proxy settings. If you don't have IE installed, and proxy is configured via some other browser, this tool might not work. Also, if you have a firewall installed, you need to give access to StockBag to access the Web. If Yahoo! finance is blocked by your firewall, then this tool will not work.

Points of interest

Getting live stock data will be of interest to many to write analysis tools on their own. However, it should be noted that at least for NSE/BSE (Indian stock markets), Yahoo! Finance is giving 15 minutes delayed data.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow do i retrieve Gold rates? Pin
Jαved20-Feb-12 21:14
professionalJαved20-Feb-12 21:14 
Generalproblem while getting live quotes Pin
hardik28200622-Aug-09 21:18
hardik28200622-Aug-09 21:18 
Generalsaving data in csv for Pin
anilk526-Jun-09 7:16
anilk526-Jun-09 7:16 
Questionpls suggest me the code to retrive stock data directly from "nseindia" website? Pin
arun p r20-Mar-09 20:26
arun p r20-Mar-09 20:26 
QuestionGood Project Pin
Doncp27-Feb-09 9:18
Doncp27-Feb-09 9:18 
GeneralStick to desktop Pin
Siavash Mortazavi12-Dec-08 6:28
Siavash Mortazavi12-Dec-08 6:28 
GeneralSimilar gadget Pin
michbex6-Apr-08 5:57
michbex6-Apr-08 5:57 
GeneralRe: Similar gadget Pin
Ravi Bhavnani6-Apr-08 6:36
professionalRavi Bhavnani6-Apr-08 6:36 
GeneralRe: Similar gadget Pin
michbex6-Apr-08 6:51
michbex6-Apr-08 6:51 
GeneralNice article Pin
AKNR Chandra Sekhar26-Jan-08 10:45
AKNR Chandra Sekhar26-Jan-08 10:45 
GeneralI have already build one with actual live data... Pin
Ashutosh Bhawasinka19-Jan-08 20:42
Ashutosh Bhawasinka19-Jan-08 20:42 
GeneralRe: I have already build one with actual live data... Pin
Ashutosh Bhawasinka19-Jan-08 20:45
Ashutosh Bhawasinka19-Jan-08 20:45 
GeneralRe: I have already build one with actual live data... Pin
dharam20-Jan-08 17:12
dharam20-Jan-08 17:12 
GeneralRe: I have already build one with actual live data... Pin
Seetharam22-Jan-08 12:13
Seetharam22-Jan-08 12:13 
GeneralRe: I have already build one with actual live data... Pin
dharam24-Jan-08 4:44
dharam24-Jan-08 4:44 
GeneralRe: I have already build one with actual live data... Pin
Ashutosh Bhawasinka3-Sep-08 8:07
Ashutosh Bhawasinka3-Sep-08 8:07 
GeneralRe: I have already build one with actual live data... Pin
Ashutosh Bhawasinka3-Sep-08 8:08
Ashutosh Bhawasinka3-Sep-08 8:08 
GeneralRe: I have already build one with actual live data... Pin
Ashutosh Bhawasinka3-Sep-08 8:09
Ashutosh Bhawasinka3-Sep-08 8:09 
NewsRe: I have already build one with actual live data... Pin
me_ramkumar17-Feb-08 7:19
me_ramkumar17-Feb-08 7:19 
GeneralRe: I have already build one with actual live data... Pin
Aajaraja20-Feb-08 8:12
Aajaraja20-Feb-08 8:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.