Financial market data can be downloaded into Excel for free by using Excel web queries. The market data can be automatically extracted from financial web sites that provide such data for free. This example uses Yahoo Finance; however similar approaches could be made with MSN Finance, Google Finance or other such web sites.
Download a free Excel program that uses a variation of the code below here.
Update - 07 January 2012: Given the high popularity of this solution, we have developed an updated version that resolves the issues raised in the posts here and adds the following features:
Works with all versions of Excel and OS including Excel 2011 for Mac.
Symbol look up with click to add to list of securities and indices for data download.
Progress bar shows status of data downloading.
Technical analysis sheet allows charting and calculation of return metrics for selected downloaded data.
Optimized open source code with detailed comment documentation.
Some have asked why this is free. For several reasons but the most important is that we believe that market data is public information and should be freely available to all.
This example will download both real-time quotes and historical price data for a specified stock or index ticker symbol. Suppose we have the following Excel spreadsheet.
Excel Spreadsheet:
A
B
C
D
E
1
2
Start Date:
01/01/10
Symbol:
MSFT
3
End Date:
01/01/12
Frequency:
Daily
4
Cells B2 and B3 specify the start and end dates for the historical data. Cell E2 specifies the stock or index ticker symbol. Cell E3 specifies the historical data frequency. This can be either Daily, Weekly, or Monthly or even D, W or M as only the first letter is used in the web query.
The following VBA macro code can then be loaded through the VBA editor and executed from the spreadsheet to return the data.
VBA Code:
Sub Download_Data() Application.ScreenUpdating = False Dim symb As String Dim startdate As Date Dim enddate As Date Dim a, b, c, d, e, f As Integer Dim g As String
symb = Range("E2").Value startdate = Range("B2").Value enddate = Range("B3").Value a = Month(startdate) - 1 b = Day(startdate) c = Year(startdate) d = Month(enddate) - 1 e = Day(enddate) f = Year(enddate) g = Lcase(Left(Range("E3").Value, 1))
'Historical Data With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://finance.yahoo.com/q/hp?s=" & symb & "&a=" & a & "&b=" & b & "&c=" & c & "&d=" & d & "&e=" & e & "&f=" & f & "&g=" & g _ , Destination:=Range("A12")) .Name = "Quote: " & symb .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlOverwriteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = False .RefreshPeriod = 0 .WebSelectionType = xlSpecifiedTables .WebFormatting = xlWebFormattingNone .WebTables = "15" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With
Application.ScreenUpdating = True End Sub
By simply changing the input cells, the same macro can be run to extract new data. This is a basic example and there are several enhancements which could be made to suit requirements such as:
Loop through the historical data query to page through data on Yahoo back to the start date. This could be done by going to the bottom observation date, resetting the end date to this date, rerun and append new data to the bottom until the reset end date is less than or equal to the start date.
Add extra code at the end to extract data and insert into other trading system or analytical applications in Excel.
Format and clean up the data such as removing dividends from the historical data or removing the date text from the last price real-time quote.
Indeed, I replicated the same behavior. On debugging the code I noticed that the first call returns data starting at the 3 Nov 2010 even through the request is for starting at 1 Nov 2010. As a workaround I chose 10/1/2010 as the start date to make sure that data was retrieved for the beginning of November. In any case, we will look into whether the source page has changed in any way (as this happens from time to time) and update the code accordingly.
From time to time the code posted here fails to work due to changes to the page layout on Yahoo Finance. This has now been updated by replacing .WebTables = "16" with .WebTables = "15".
The best solution is the download file as it does not rely on the layout but rather finds the price data wherever it resides on the page. There are also many other enhancements and advantages with this solution.
The Query tab is a holding space for data downloaded for both historical prices and real time quotes. This sheet downloads the entire page for each URL required and then extracts the data required for the Data sheet. The reason that the entire page is downloaded is to make this solution compatible with all versions of Excel and HTML rendering including those on the Macintosh operating system.
Focus should be made on the Data sheet which is provides the downloaded data in a structured and continuous format.
We've just tested the download and it is working fine for us. In the Query sheet, there are two buttons for testing the web query. Test these first to ensure that data is downloading. If not, then the issue is with the internet connection. If data is there then the issue is with the date matching.
Thanks Admintrators and those who contributed making it happen. The macro works fine and does just what I wanted. Yes it takes little while going through looping each stock at the time, but hey, it is awsome to sit back with your arms folded behind your kneck and watch macro at work.
You guys rock!
I got another problem bit more complex than this, I will let you know next time if it is even doable.
How do you pass more than one stock as argument to above code. Otherwise, the code is working fine. Also I am seeing no matter daily monthly or yearly. I can only get 3 months worth of data. Do i need to change my excel setting? I read the code no where to modify. Thanks Dalmar