Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Quotes from Google Finance / Yahoo Finance

Tags:

c#

finance

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

like image 639
user1243565 Avatar asked Jun 25 '12 00:06

user1243565


People also ask

How do you get quotes on Yahoo Finance?

To obtain a real-time quote on Yahoo! Finance, users simply need to select the "Real-time ECN" option from the quotes drop-down menu on the Yahoo! Finance main page. An alternate way to display real-time quotes is to obtain a basic quote, or view a portfolio, and then click the "Real-time ECN" View link.

Does Yahoo Finance give real time quotes?

Yahoo Finance provides real-time streaming quotes for many exchanges. Real-time data is available during an exchange's market hours, and in some cases during pre-market and post-market hours.

Can Google Sheets pull data from Yahoo Finance?

The YAHOOFINANCE formula extends the functionality of GOOGLEFINANCE as it offers a much more comprehensive data set for stock tickers. You can easily pull Yahoo Finance data into your Google Sheets automatically using the Google Apps Script.


2 Answers

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.

like image 192
Walter Avatar answered Oct 13 '22 01:10

Walter


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.

like image 29
kusnaditjung tjung Avatar answered Oct 13 '22 00:10

kusnaditjung tjung