6

How would I receive a a stock quote onto C#? Google Finance API isn't very helpful

slugster
  • 49,403
  • 14
  • 95
  • 145
user1243565
  • 91
  • 1
  • 2
  • 3

3 Answers3

2

Google Finance API Alternative. A free, excellent alternative to Google's Finance API is AlphaVantage. You can sign up for a free API key to start retrieving live & historical stock market quotes.

How to retrieve AlphaVantage Stock Market Data using C#? Here is some sample code to retrieve monthly stock market prices in C#. You will need to install ServiceStack.Text - a free, open-source, high performance .NET text utility to run the below (Install-Package ServiceStack.Text).

public class AlphaVantageData
{
      public DateTime Timestamp { get; set; }
      public decimal Open { get; set; }
      public decimal High { get; set; }
      public decimal Low { get; set; }
      public decimal Close { get; set; }
      public decimal Volume { get; set; }
}

// retrieve monthly prices for Microsoft
var symbol = "MSFT";
var apiKey = "demo"; // retrieve your api key from https://www.alphavantage.co/support/#api-key
var monthlyPrices = $"https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol={symbol}&apikey={apiKey}&datatype=csv"
                .GetStringFromUrl().FromCsv<List<AlphaVantageData>>();

monthlyPrices.PrintDump();

You can run above sample code in gistlyn here. I have written a full article "AlphaVantage and C#" here.

Mark
  • 2,175
  • 20
  • 23
1

One of the quickest way is to using yahoo http request (some details can be found in see http://www.gummy-stuff.org/Yahoo-data.htm )

Then using the following code to retrieve the result programmatically instead of manual download or using spreadsheet.

public static string Extract(string yahooHttpRequestString)
{
      //if need to pass proxy using local configuration  
      System.Net.WebClient webClient = new WebClient();  
      webClient.Proxy = HttpWebRequest.GetSystemWebProxy();  
      webClient.Proxy.Credentials = CredentialCache.DefaultCredentials;  

      Stream strm = webClient.OpenRead(yahooHttpRequestString);  
      StreamReader sr = new StreamReader(strm);  
      string result = sr.ReadToEnd();            
      strm.Close();             
      return result;  
}  

then you can process the returned string further, or modify the above code to parse the string for each segment of the quote, into a more elaborated data structure.

0

I would recommend you to go through Stock quote from Yahoo! in C# article to access stock quote from Yahoo......

Akash KC
  • 16,057
  • 6
  • 39
  • 59