C# = Retrieving yahoo historical prices

Discussion in 'Trading Software' started by Batman28, Nov 17, 2006.

  1. does anyone know of any code or component that allows easily to download yahoo historical quotes directly?

    I prefer a C# code but delphi is fine too.

    if any rightedge or other developer reads this, can u maybe share how u download historical quotes from yahoo?
  2. Have you tried Google? There are dozens of tools for downloading data from Yahoo! Finance.

    If you want to program something yourself then you'll need to have at least basic C# programming skills, specifically networking and file IO, as that is all it really boils down to.

    Firstly, you need to be familar with constructing a URL that will return your desired data from Yahoo! Finance.

    If you go to the site manually and enter information you should be able to work out the correct URL parameters. It's not rocket science.

    Next, you will need to connect and make a request to the URL and then read in the response to get the content e.g.

    HttpWebRequest req = (HttpWebRequest)WebRequestFactory.Create(yahooURL);
    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
    StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.ASCII);
    Then you read in from the stream and parse using .Split with a comma delimiter if you want to work with the data somehow, otherwise you can just write out to a local file.

    Since retrieving Yahoo! data has been done about a trillion times before I'm sure you'll find some more sample code online and some assistance on the Yahoo! URL format etc. by Googling or going to Yahoo! itself.
  3. traderob


  4. thanks for the comments.

    im not exactly looking for price quotes. Im looking for retriving historical quotes over selected periods e.g. 5 yrs daily prices: open, high, low, close, vol. i know of macros that allow u to do this directly in excel. but how can this be done in c# into a table?
  5. Did you follow my suggestions? I don't think you did. Can you even code in C#?

    I'll make it really easy for you:

    1) Got to Yahoo! Finance by typing in your browser: http://finance.yahoo.com

    2) In the "Get Quotes" input box, enter the symbol of the stock you are interested in e.g. MSFT. Click on the button that says "GO".

    3) You should now be presented with a page displaying information for Microsoft.

    4) On the left you should see a column with links in it. Click on the link that says "Historical Prices"

    5) You should now be presented with a page displaying historical price information for Microsoft. In addition, you should be shown a form to allow you to specify the date range and data to retrieve.

    6) Enter the dates and data you want to download e.g. Start Date: Jan 01 2006, End Date: Nov 17 2006, Select the Daily data.

    7) Click on the button that says "Get Prices"

    8) The page should now reload and display the prices for the dates you selected.

    9) At the bottom of the page there should be a link that says "Download to Spreadsheet". Copy the link URL to dissect it for your program e.g.:


    As I said before, it's not rocket science. I'm going to make a wild assumption that you know what a URL is.

    s=TICKER e.g. MSFT
    a=BEGIN MONTH NUMBER -1 e.g. For January, use 0. For February, use 1
    b=BEGIN MONTH DAY e.g. for Jan 01, use 01 here.
    c=BEGIN YEAR e.g. 1989
    d=END MONTH NUMBER -1 e.g. for November, use 10 etc.
    e=END MONTH DAY e.g. for Nov 17, use 17.
    f=END YEAR e.g. 2006
    g=DATA TYPE, e.g. D for Daily, M for Monthly etc.
    ignore=The format of the file to download e.g. CSV (comma separated values)

    Are you with me so far?

    To summarize:

    1) In your C# program, construct a URL with the desired parameters to match your stock, date range and data type.
    2) Connect to the url and read in the data response using the code I outlined in my earlier post.
    3) Now you have to parse the data. Do you know what that means? It means splitting the data up into meaningful bits. You can achieve this by using .Split on each line that is read in to get the comma separated values.

    You now have the raw data. What you do with it is up to you. If you want to display it, you can do. This is where your C# programming skills come in.
  6. gbos


    Its not hard. Using VBA the code is something like this...

    Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
    "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal _
    szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
    Public Function DownloadFromUrl(ByVal strFullUrl As String, _
                ByVal strSaveFile As String) As Boolean
    Dim RetVal As Long
    RetVal = URLDownloadToFile(0, strFullUrl, strSaveFile, 0, 0)
    'Operation succeeded
    If RetVal = 0 Then DownloadFromUrl = True
    End Function
    Sub yahoodownloadsub()
        Dim a As Boolean
        Dim ofile As String
        Dim qurl As String
        ofile = "yahooquotes.csv"
        qurl = "http://chart.yahoo.com/table.csv?s=" + "MSFT" + "&a=1" + "&b=1" + _
     "&c=2002" + "&d=1" + "&e=1" + "&f=2003" + "&g=d" + "&q=q&y=0&z=" + "MSFT" + "&x=.csv"
        a = DownloadFromUrl(qurl, ofile)
    End Sub
    The code can be runed even from within excel, (calling by a click of a button the yahoodownloadsub() sub), or any basic-like compiler and can be easily translated to C etc. You just have to manipulate the qurl and ofile string into what data range do you want to access and where to store that data respectively.
  7. To further reinforce what others said above about the Yahoo format for historical data. Here is my summary of the format:

    To retrieve multiple years of end-of-day market data the following format is used:
    ichart.finance.yahoo.com/table.csv?s={symbol}&a={begin number of month -1}&b={begin day}&c={begin year}&d={end number of month-1}&e={end day}&f={end year}&g=d&ignore=.csv

    Note that &g=d is for daily data and &ignore=csv gives the results in comma separated format.

    For example, to fetch approx of 5 years of daily data for ANDE from February 20th, 2001 to September 29th, 2006 the following is used:

    (Note: add http:// in front of the links to create the full string. It is not added here due to the automatic url formatting which chops the overal demo link.)

    - Greg
  8. I've never written C# code before so this is most likely not best practice code (I've used an ArrayList which is probably superceded by some generics collection) and it is very rough and ready. There is no validation of input data. Anyway, it works and is something to start with:

    private void button1_Click(object sender, EventArgs e)
                //Using an Array list to store the retrieved data. Probably better alternatives...
                ArrayList historicalData = new ArrayList();
                //Retrieve the data from Yahoo! using the parameters specified...
                String yahooURL = String.Format("http://ichart.finance.yahoo.com/table.csv?s={0}&a={1}&b={2}&c={3}&d={4}&e={5}&f={6}&g=d&ignore=.csv",this.tickerSymbol.Text,this.startDateMonth.Text,this.startDateDay.Text,this.startDateYear.Text,this.endDateMonth.Text,this.endDateDay.Text,this.endDateYear.Text);
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(yahooURL);
                    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                    StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.ASCII);
                    //Discard the first line with the headers
                    String currentLine = reader.ReadLine();
                    //Read the rest of the lines
                    while (reader.Peek() >= 0)
                        currentLine = reader.ReadLine();
                        //Parse the current line with the comma delimiter
                        string[] values = currentLine.Split(',');
                        //If there are 7 values in this line then assume there is valid OHLC data and it is not a dividend line
                        if (values.Length == 7)
                            //Add it to our historical data
                            historicalData.Add(new OHLC(values[0], values[1], values[2], values[3],values[4]));
                catch (Exception ee)
                    //Alert user that unable to retrieve historical data
                //Bind the ArrayList to the datagrid
                resultsGrid.DataSource = historicalData;
    On your form, attach this code to your button.

    You can see that the Yahoo! URL pulls in text information from other fields on the form.

    Lastly, it binds the data to a datagrid control (resultsGrid) on the form to display the data.

    A simple OHLC class was created to act as a container to add to the ArrayList. The OHLC class simply stores OHLC values as strings with appropriate properties that are accessible to the datagrid. I ignored volume and data type conversions etc.

    Thank you for giving me the chance to try out Visual C# Express Edition.
  9. great thanks alot guys.. sorry TraderMojo I didn't get ur first post, but now it makes sense thanks alot very helpful. yes I do know c#, but not much on web requests..

    thanks again guys
  10. Or you could use <a href="http://www.freemarketdata.net">FreeMarketData.net</a> with Visual Studio
    #10     Dec 5, 2006