Stock quote class in C#

Jan 30, 2007

A couple of months ago one of my readers asked me to build a stock quote class that would automatically update the quote. I forgot about it, but then I found the e-mail and decided to give it a go. I started looking for some free services that could provide the data in XML format or something similar that can be easy parsed. Apparently that does not exist. All the stock sites charge you for that information and I want something free, so that was not the way to go.

Then I read about the Yahoo financial website and that it could be screen scraped fairly easy, so that’s what the class will use. The class is actually very simple to both use and modify. If the class isn't exactly what you need, you can just use its screen scraping methods to create your own implementation.

Update:
I've got some comments pointing out that Yahoo can give you CSV files, so I took a look at it, and now it does not use screen scraping any more. Thanks for the comments.

Examples of use

To get the quote for a specific stock, you can call the static Execute method. In this example a text box is filled with the quote:

txtStockQuote.Text = StockEngine.Execute("MSFT").Value.ToString();

You can also hook into the automatic update event, so that you can update a text box every time an update occurs. By default it is every minute.

private StockEngine _Engine;

 

private void PrepareStockQuote()

{

  _Engine = new StockEngine("MSFT");

  _Engine.AutoUpdate = true;

  _Engine.UpdateInterval = 60000; // 1 minute

  _Engine.Updated += new EventHandler<EventArgs>(_Engine_Updated);

}

 

private void _Engine_Updated(object sender, EventArgs e)

{

  txtStockQuote.Text = _Engine.Value.ToString();

}

Download

StockEngine.zip (1,92 KB)

* $4.95/month ASP.NET Hosting with FREE SQL 2012 DB! – Click Here!

Comments (12) -

Peter
Peter
1/30/2007 7:33:04 PM #

Hi Mads, been an avid reader for a while now; keep up the good work.  Just a quick note to let you know that ticker symbols can be up to 5 letters long not only 4 like you have in your code.

Mads Kristensen
Mads Kristensen
1/30/2007 7:36:04 PM #

I wasn't aware of that Peter. Thanks for the tip.

NinjaCross
NinjaCross
1/30/2007 11:07:17 PM #

Amazing idea, and really well designed implementation.
Keep up the good work, as everytime Smile

 Tiernan OToole
Tiernan OToole
1/31/2007 9:44:57 AM #

Hi Mads. Just an FYI. Insted of parsing the Yahoo site, they have a CVS report available for each stock symbol. its available here (download.finance.yahoo.com/.../quotes.csv). might be quicker too because its only a few bytes to download, not the whole page Smile

Mads Kristensen
Mads Kristensen
1/31/2007 9:53:56 AM #

Tiernan, the CVS report is not accessible for requests directly. I've tried to change the referrer header of the web request, but that does not work. I think they blog access the the CVS file by cookies. If you find the solution please let me know.

 NinjaCross
NinjaCross
1/31/2007 3:17:05 PM #

Hi Mads.
Tiernan OToole is right and its idea works like a charm.
I've just wrote and successfully tested this code:

        string serverURL = "download.finance.yahoo.com/.../quotes.csv;;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverURL);
        WebResponse response = request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII);
        string retVal = reader.ReadToEnd();
        Response.Write(retVal);

 NinjaCross
NinjaCross
1/31/2007 3:31:54 PM #

Ops, Tiernan sorry for the "**it's*** idea"... hehe, sometimes my english sux ;)

Mads Kristensen
Mads Kristensen
1/31/2007 5:12:01 PM #

Yes he is. I just found out and made the adjustements to the class. Thanks.

 Nathan
Nathan
1/31/2007 8:39:35 PM #

Hi,

I don't mean to sound like a doofus here (I'm pretty new to C#), but how do I integrate this into a webapp?

I have created a Atlas WebApplication and added this class. On my default.aspx I have added a textbox, a button and a label for the Symbol, the execute command and the rate respectively.

I've added this as a variable.
StockEngine stocks = new StockEngine();

Have I missed something?

Thanks

Nathan

 Tiernan OToole
Tiernan OToole
2/1/2007 2:04:08 PM #

Cool beans man! Glad to help! Smile

 TimSpence
TimSpence
2/9/2007 7:02:08 AM #


I'm with Nathan it would be great for us newbs to see this in an actual working page.

Thanks!

Anon
Anon United States
3/24/2008 8:54:20 PM #

Nice class, but tickers can be single digits like "T" or "M".

Pingbacks and trackbacks (1)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.