Click here to Skip to main content
Click here to Skip to main content

StockBag

, 19 Jan 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A tool to retreive live stock/share data from the web.

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.

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)

Share

About the Author

No Biography provided

Comments and Discussions

 
QuestionHow do i retrieve Gold rates? PinmemberJαved20-Feb-12 22:14 
Generalproblem while getting live quotes Pinmemberhardik28200622-Aug-09 22:18 
Generalsaving data in csv for Pinmemberanilk526-Jun-09 8:16 
Questionpls suggest me the code to retrive stock data directly from "nseindia" website? Pinmemberarun p r20-Mar-09 21:26 
QuestionGood Project PinmemberDoncp27-Feb-09 10:18 
GeneralStick to desktop PinmemberSiavash Mortazavi12-Dec-08 7:28 
GeneralSimilar gadget Pinmembermichbex16-Apr-08 6:57 
GeneralRe: Similar gadget PinmemberRavi Bhavnani6-Apr-08 7:36 
GeneralRe: Similar gadget Pinmembermichbex16-Apr-08 7:51 
GeneralNice article PinmemberAKNR Chandra Sekhar26-Jan-08 11:45 
GeneralI have already build one with actual live data... PinmemberAshutosh Bhawasinka19-Jan-08 21:42 
GeneralRe: I have already build one with actual live data... PinmemberAshutosh Bhawasinka19-Jan-08 21:45 
GeneralRe: I have already build one with actual live data... Pinmemberdharam20-Jan-08 18:12 
GeneralRe: I have already build one with actual live data... PinmemberSeetharam22-Jan-08 13:13 
GeneralRe: I have already build one with actual live data... Pinmemberdharam24-Jan-08 5:44 
GeneralRe: I have already build one with actual live data... PinmemberAshutosh Bhawasinka3-Sep-08 9:07 
GeneralRe: I have already build one with actual live data... PinmemberAshutosh Bhawasinka3-Sep-08 9:08 
GeneralRe: I have already build one with actual live data... PinmemberAshutosh Bhawasinka3-Sep-08 9:09 
NewsRe: I have already build one with actual live data... Pinmemberme_ramkumar17-Feb-08 8:19 
GeneralRe: I have already build one with actual live data... PinmemberAajaraja20-Feb-08 9:12 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411023.1 | Last Updated 19 Jan 2008
Article Copyright 2008 by Prashanth Uppunda
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid