:: Subscribe to our newsletter
 
Articles / Directory
06/17/2013
Latest version of TraderCode(v5.6) includes new Technical Analysis indicators, Point-and-Figure Charting and Strategy Backtesting.



06/17/2013
InvestmentCode, a comprehensive suite of
Financial calculators and models for Excel is now available.



09/01/2009
Launch of
Free Investment and Financial Calculator for Excel.



09/08/2007
Launch of
TinyGraphs - open source add-in for creating sparklines and tiny charts in Excel.



1/02/2009
Launch of NeuralCode(v1.0) for
Neural Networks Trading.



Free spreadsheet for downloading of Stock Quotes to Excel

This document will show you how to automate the download of the Stock Quotes from Yahoo Finance (http://finance.yahoo.com) to Excel. We will be using a simple programming language called Visual Basic for Applications (VBA) and will be going through the steps, one at a time to allow you to construct the model without prior knowledge of VBA. The completed model is also available for download below.

1. Launch Excel and create a new workbook. Call this workbook AutomatedDownloadData.xls.

In Microsoft Excel 2003: Go to Tools->Macro->Visual Basic Editor

In Microsoft Excel 2007/2010/2013: Go to Developer->Visual Basic

2. Right click on VBAProject (your Excel Filename) and select Insert->Module. A default Module1 will be inserted. Click on Module1 and paste the VBA code below on the right hand side of the Visual Basic Editor. The following VBA code allows you to download data from Yahoo Finance using a specified Stock symbol, Start Date and End Date.




Sub GetStock(ByVal stockSymbol As String, 
ByVal StartDate As Date, ByVal EndDate As Date)
    
  Dim DownloadURL As String
  Dim StartMonth, StartDay, StartYear As String 
  Dim EndMonth, EndDay, EndYear As String
  StartMonth = Format(Month(StartDate)-1, "00")
  StartDay = Format(Day(StartDate), "00")
  StartYear = Format(Year(StartDate), "00")
    
  EndMonth = Format(Month(EndDate)-1, "00")
  EndDay = Format(Day(EndDate), "00")
  EndYear = Format(Year(EndDate), "00")
  DownloadURL="URL;http://table.finance.yahoo.com/table.csv?s=" 
                            + stockSymbol 
                            + "&a=" + StartMonth + "&b=" 
                            + StartDay + "&c=" + StartYear 
                            + "&d=" + EndMonth + "&e=" 
                            + EndDay + "&f=" 
                            + EndYear + "&g=d&ignore=.csv"
    
  With ActiveSheet.QueryTables.Add(Connection:=DownloadURL, 
         Destination:=Range("$A$1"))

        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = "20"
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
  End With
  ActiveWindow.SmallScroll Down:=-12
  Columns("A:A").TextToColumns Destination:=Range("A1"),_
        DataType:=xlDelimited, 
        TextQualifier:=xlDoubleQuote,_
        ConsecutiveDelimiter:=False,_
        Tab:=True,_ 
        Semicolon:=False, Comma:=True, Space:=False,_
        Other:=False,_ 
        FieldInfo:=Array(Array(1, 1), Array(2, 1),_
        Array(3, 1), Array(4, 1),_
        Array(5, 1), Array(6, 1), Array(7, 1))
  Columns("A:F").EntireColumn.AutoFit
End Sub

Sub Download()
   Call GetStock("YHOO", "02/01/2007", "09/05/2008")
End Sub



3. Click on Save and then File->Close to return to Microsoft Excel. In Excel, try out the downloading of data by going to

Excel 2003: Go to Tools->Macro->Macros…, select the Download macro and click run
Excel 2007/2010/2013: Go to Developer->Macros, select the Download macro and click run.
and press the Enter key. This will initiate a download of Stock Prices from Yahoo! Finance.

What we have written is a subroutine that downloads stock data automatically and can be executed by Excel. It takes "stockSymbol", "StartDate" and "EndDate" as parameters.

In the whole block of code above, the most important line is the following.


With ActiveSheet.QueryTables.Add(Connection:=DownloadURL, 
Destination:=Range("$A$1"))


It basically says that we will be downloading data from DownloadURL and placing the result into the cell A1. The DownloadURL is constructed based on the parameters explained below.

http://table.finance.yahoo.com/table.csv?s=YHOO&a=01&b=01&c=2007&d=08&e=05&f=2008&g=d&ignore=.csv

  • “s=YHOO” means to download the stock prices of Yahoo. YHOO is the stock symbol of Yahoo.
  • “a=01&b=01&c=2007” specifies the start date in Month, Day, Year. You might have noticed that the month is subtracted with 1, which is the format required by Yahoo.
  • “d=08&e=05&f=2008” specifies the end date in Month, Day, Year. You might have noticed that the month is subtracted with 1, which is the format required by Yahoo.
You can also find the implementation of this example in the workbook DownloadStockQuotes.xls.

Download

Free spreadsheet to download Stock Quotes - v1.0

System Requirements

Microsoft® Windows XP®, Microsoft® Windows Vista®, Windows 7 or Windows 8
Windows Server 2003, 2008, 2012
512 MB RAM
5 MB of Hard Disk space
Excel 2002, 2003, XP, 2007, 2010, 2013

License

By downloading this software from our web site, you agree to the terms of our license agreement.

Download

DownloadStockQuotes.zip (Zip Format - 231 KB)








 

Copyright (c) 2007-2014, ConnectCode Pte Ltd. All Rights Reserved.


Trademark Acknowledgements

Microsoft and Microsoft Excel are registered trademarks of Microsoft Corporation. Avery is the trademark of Avery Dennison Corporation. All other product names are trademarks, registered trademarks, or service marks of their respective owners