Home > Forum Home > Building Market Trading Systems > Downloading Market Data For Free Share

Downloading Market Data For Free

Excel Help for Downloading Market Data For Free in Building Market Trading Systems


Forum TopicPost Reply Login

Downloading Market Data For Free

Rate this:
(3.8/5 from 6 votes)
HappyFinancial 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))

'Real-time Quotes

With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;http://finance.yahoo.com/q/ecn?s=" & symb, Destination:=Range("A5"))
    .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 = """table1"""
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
End With

 

'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.
  • Add a button at the top to launch the macro.
 Excel Business Forums Administrator
 Posted by on
 
Replies - Displaying 21 to 30 of 48Order Replies By: Most Recent | Chronological | Highest Rated
Confused
Rate this:
(3/5 from 1 vote)
The data downloader hangs after it downloads about 10 years worth of daily data. Due to this I cannot populate the spreadsheet with several tickers. I tried clearing the Internet cache, but it does not help. Please advice.
Thank you.
 Yuri
 Yuri
 Posted by on
Oops
Rate this:
(3/5 from 1 vote)
It would be interesting to know the system specifications that forces the download market data program to hang.  The program processes the requested data in chunks so it is unlikely to be due to memory.  If the version of Excel is below 2007, then more than 65,000 rows of data will cause a problem but this should be presented as a run-time error.
 Excel Business Forums Administrator
 Posted by on
Confused
Rate this:
(3/5 from 1 vote)
The version of Excel I am using is 2003, but I don't even get close to the 65,000 rows, since I am getting 10 years worth of daily data.


 Yuri
 Posted by on
Angry
Rate this:
(3/5 from 1 vote)
We tested this in Excel 2003 and it ran fine to get 10 years of daily data. This was run with 2GB of RAM. 

Perhaps you could indicate the symbols, start and end dates that you are running so that we can try and replicate the problem.

 Excel Business Forums Administrator
 Posted by on
Confused
Rate this:
(3/5 from 1 vote)
I need the Excel program data which is display dynamically the price history for the a list of penny stocks.
Can every body help me Sir!
 www.madpennystocks.com
 Posted by on
Confused
Rate this:
(3/5 from 1 vote)
Hi Singapore National Environmental Agency posts its most disaggregated data on a daily basis at this website: [http://app2.nea.gov.sg/psi.aspx]
I see how to do a webquery manually for this, but I want it to do this automatically everyday for the next few years. Even if I don't open Excel everyday. 
 How is this possible? I'm a Stata user, not much of an Excel user. So I don't know Excel code. Can someone help?

 Posted by on
Confused
Rate this:
(3/5 from 1 vote)
Does anybody know whats needs to be added to code so the dividend data is not included in the results? Any help is much appreciated.
 Posted by on
Surprised
Rate this:
(3/5 from 1 vote)
In the Downloaded data, choose "Close" price to get the closing price unadjusted for dividends.  The "Adjusted Closing Price" accounts for share splits, buybacks and dividends.
 Excel Business Forums Administrator
 Posted by on
Confused
Rate this:
(3/5 from 1 vote)
No matter what I set the date range to, my results only show 3 months worth of data. Is there a way to fix this so it shows all the data requested? Thanks for your help, it is much appreciated. 
 Posted by on
Oops
Rate this:
(3/5 from 1 vote)
If you are using the VBA code here by itself then the result will be limited to the number of observations displayed on the web page requested within the web query.

In the Excel program download which uses this code, we have created a loop that gets around this limitation to make multiple requests for longer time periods. This program also has other features facilitate market data downloading into Excel.
 Excel Business Forums Administrator
 Posted by on
 Displaying page 3 of 5 

Excel templates and solutions matched for Downloading Market Data For Free:

Solutions: Download Market Data Download Stock Quotes Complete Trading System Real Time Stock Quotes Options Trading
Categories: Market Data Access