Yahoo stock quote from ticker

| | No TrackBacks
using System.Net;
using System.IO;
public class TickerQuote
{
  private string ticker;
  private DateTime effective_date;
  public TickerQuote(string t, DateTime dt)
  {
    ticker = t;
    effective_date = dt;
  }
  private string GetTickerAddress()
  {
    DateTime yesterday = effective_date.AddDays(-1);
    return String.Format(
      "http://ichart.finance.yahoo.com/table.csv?" +
      "s={0}&" +
      "d={1}&e={2}&f={3}&" +
      "g=d&a={4}&b={5}&c={6}" +
      "&ignore=.csv",
      ticker,
      effective_date.Month - 1,
      effective_date.Day,
      effective_date.Year,
      yesterday.Month - 1, yesterday.Day, yesterday.Year);
  }
  private string GetFileContents(string fileName)
  {
    using (StreamReader sr = new StreamReader(fileName))
      return sr.ReadToEnd().ToString();
  }
  public double Price
  {
    get
    {
      string fileName = @"C:\temp\table.csv";
      string addr = GetTickerAddress();
      using (WebClient req = new WebClient())
      {
        req.DownloadFile(addr, fileName);
        string result = GetFileContents(fileName);
        System.IO.File.Delete(fileName);
        //  Get the first line
        char[] separator1 = new char[] { '\n' };
        string[] a = result.Split(separator1);
        string b = a[1];
        //  Get the last value on the line
        char[] separator2 = new char[] { ',' };
        string[] bb = b.Split(separator2);
        string price = bb[bb.Length - 1];
        //  Convert to double
        return double.Parse(price);
      }
    }
  }
}

No TrackBacks

TrackBack URL: http://www.iwebthereforeiam.com/cgi-bin/mt/mt-tb.cgi/436

Leave a comment

Verification (needed to reduce spam):

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.32-en

About this Entry

This page contains a single entry by Hugh Brown published on September 27, 2006 5:35 AM.

How to Avoid Investing Mistakes was the previous entry in this blog.

WinSnap is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.