Get stock prices from the internet in C#

There are many web sites that can return stock prices. This program uses quote.yahoo.com. It builds a URL of the following form to get prices for MCI, DIS, COKE, and PEP:

    quote.yahoo.com/download/quotes.csv?format=sl&ext=.csv&symbols=MCI,DIS,COKE,PEP

The web site returns a result similar to the following:

    "MCI”,30.50,”10/26/2010”,”2:05pm”,0.00,N/A,N/A,N/A,0
“DIS”,35.96,”10/26/2010”,”4:00pm”,0.00,N/A,N/A,N/A,6652
“COKE”,54.46,”10/26/2010”,”4:00pm”,0.00,N/A,N/A,N/A,0
“PEP”,64.79,”10/26/2010”,”4:01pm”,0.00,N/A,N/A,N/A,10725

The GetWebResponse method shown in the following code gets a web response and returns it in a string.

// Get a web response.
private string GetWebResponse(string url)
{
// Make a WebClient.
WebClient web_client = new WebClient();

// Get the indicated URL.
Stream response = web_client.OpenRead(url);

// Read the result.
using (StreamReader stream_reader = new StreamReader(response))
{
// Get the results.
string result = stream_reader.ReadToEnd();

// Close the stream reader and its underlying stream.
stream_reader.Close();

// Return the result.
return result;
}
}

The code creates a WebClient and uses its OpenRead method to get a StreamReader that can read the result of the URL. The code reads the stream to its end and returns the resulting string.

The following code shows how the program uses GetWebResponse.
// Get the stock prices.
private void btnGetPrices_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
Application.DoEvents();

// Build the URL.
string url = "";
if (txtSymbol1.Text != "") url += txtSymbol1.Text + ",";
if (txtSymbol2.Text != "") url += txtSymbol2.Text + ",";
if (txtSymbol3.Text != "") url += txtSymbol3.Text + ",";
if (txtSymbol4.Text != "") url += txtSymbol4.Text + ",";
if (url != "")
{
// Remove the trailing comma.
url=url.Substring(0, url.Length - 1);

// Prepend the base URL.
const string base_url = "http://quote.yahoo.com/download/quotes.csv?format=sl&ext=.csv&symbols=";
url = base_url + url;

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

// Pull out the current prices.
string[] lines = result.Split(
new char[] { '\r', '\n' },
StringSplitOptions.RemoveEmptyEntries);
txtPrice1.Text = lines[0].Split(',')[1];
txtPrice2.Text = lines[1].Split(',')[1];
txtPrice3.Text = lines[2].Split(',')[1];
txtPrice4.Text = lines[3].Split(',')[1];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Read Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}

this.Cursor = Cursors.Default;
}
The code builds the URL from the symbols in its text boxes. It calls GetWebResponse and parses the result to find the stock prices.

Note that this web site returns a string with the following format if you try to look up a stock symbol that doesn't exist.

    "MCIX",0.00,"N/A","N/A",N/A,N/A,N/A,N/A,N/A

In that case, the program displays 0.00 for the price instead of crashing.

   

 

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.