StockBag is a desktop utility wherein you can define your stock portfolio/watch-list and view live stock data.
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+
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverUrl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(),
string stockDataString = reader.ReadLine();
string stockDataContents = stockDataString.Split(',');
stockData.Code = stockCode;
stockData.Last = stockDataContents;
stockData.Date = stockDataContents;
stockData.Time = stockDataContents;
stockData.Change = stockDataContents;
stockData.Open = stockDataContents;
stockData.High = stockDataContents;
stockData.Low = stockDataContents;
stockData.Volume = stockDataContents;
stockData.MarketCapital = stockDataContents;
stockData.PreviousClose = stockDataContents;
stockData.PctChange = stockDataContents;
stockData.AnnRange = stockDataContents;
stockData.Earnings = stockDataContents;
stockData.PERatio = stockDataContents;
The tool uses the
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.