Stock quote class in C#

by Mads Kristensen 31. January 2007 03:27

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)

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Tags:

Server-side

Comments

1/31/2007 4:33:04 AM #

Peter

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.

Peter |

1/31/2007 4:36:04 AM #

Mads Kristensen

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

Mads Kristensen |

1/31/2007 8:07:17 AM #

NinjaCross

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

NinjaCross |

1/31/2007 6:44:57 PM #

 Tiernan OToole

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

Tiernan OToole |

1/31/2007 6:53:56 PM #

Mads Kristensen

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.

Mads Kristensen |

2/1/2007 12:17:05 AM #

 NinjaCross

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 |

2/1/2007 12:31:54 AM #

 NinjaCross

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

NinjaCross |

2/1/2007 2:12:01 AM #

Mads Kristensen

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

Mads Kristensen |

2/1/2007 5:39:35 AM #

 Nathan

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

Nathan |

2/1/2007 11:04:08 PM #

 Tiernan OToole

Cool beans man! Glad to help! Smile

Tiernan OToole |

2/9/2007 4:02:08 PM #

 TimSpence


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

Thanks!

TimSpence |

3/25/2008 5:54:20 AM #

Anon

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

Anon United States |

1/13/2009 4:21:15 AM #

pingback

Pingback from theparkisvast.com

Building a stock market ticker in VastPark : The Park is Vast

theparkisvast.com |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008