Graph historical stock prices in C#

The key to this example is the GetStockPrices method shown in the following code.
// Get the prices for this symbol.
private List GetStockPrices(string symbol)
{
// Compose the URL.
string url = "http://www.google.com/finance/historical?output=csv&q=" + symbol;

// Get the result.
// Get the web response.
string result = GetWebResponse(url);

// Get the historical prices.
string[] lines = result.Split(
new char[] { '\r', '\n' },
StringSplitOptions.RemoveEmptyEntries);
List prices = new List();
// Process the lines, skipping the header.
for (int i = 1; i < lines.Length; i++)
{
string line = lines[i];
prices.Add(float.Parse(line.Split(',')[4]));
}

return prices;
}

This method composes a URL of the form www.google.com/finance/historical?output=csv&q=DIS where DIS is a stock ticker symbol. The Google website returns historical stock data for that symbol in the form of a CSV (comma-separated value) file. The program breaks it into lines and saves the prices in a List of float that it returns to the calling code.

For additional information about how the program works, including how it draws the graph and how the GetWebResponse method works, download the example and look at the code or see the example Display a continuously updating graph of stock prices taken from the internet in C#

  

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.