Question

I am trying to calculate the stock index Day’s Value Change as explained here: http://www.codeproject.com/Articles/37550/Stock-quote-and-chart-from-Yahoo-in-C

How does one calculate this in C#?

Was it helpful?

Solution

Reworking the GetQuote method from the article... if you pass in the symbol to this method it will return the Days Value Change.

public string GetDaysValueChange(string symbol)
{
    // Set the return string to null.
    string result = null;            
    try
    {
        // Use Yahoo finance service to download stock data from Yahoo
        //  Note that f=w1 tells the service we just want the Days Value Change
        string yahooURL = @"http://download.finance.yahoo.com/d/quotes.csv?s=" + 
                          symbol + "&f=w1";

        // Initialize a new WebRequest.
        HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(yahooURL);
        // Get the response from the Internet resource.
        HttpWebResponse webresp = (HttpWebResponse)webreq.GetResponse();
        // Read the body of the response from the server.
        StreamReader strm = 
          new StreamReader(webresp.GetResponseStream(), Encoding.ASCII);

        result = strm.ReadLine().Replace("\"", "");
        strm.Close();
    }
    catch
    {
        // Handle exceptions.
    }
    // Return the stock quote data in XML format.
    return result;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top