{"id":4930,"date":"2013-11-29T09:00:19","date_gmt":"2013-11-29T14:00:19","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=4930"},"modified":"2013-12-02T08:38:21","modified_gmt":"2013-12-02T13:38:21","slug":"download-daily-stock-data-from-yahoo-and-google","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2013\/11\/29\/download-daily-stock-data-from-yahoo-and-google\/","title":{"rendered":"Download Daily Stock Data from Yahoo! and Google"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/3208495\">Sean<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/43627-download-daily-data-from-google-and-yahoo--finance\">Download Daily Data from Google and Yahoo! Finance<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/311066\">Michael Weidman<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Background<a name=\"1\"><\/a><\/h3>\r\n   <p>This submission contains a suite of tools for getting daily stock prices from Yahoo! and Google.  There are utility functions\r\n      for converting between Yahoo! and Google's formatting and to calculate adjusted closing prices, (which Yahoo! does automatically\r\n      but which Google does not).\r\n   <\/p>\r\n   <p>As a side note, Michael Weidman was one of the first few people I met when I started working at MathWorks.  He taught me everything\r\n      I know about the <a href=\"\">Kolmogorov-Smirnov test ( <tt>kstest()<\/tt> )<\/a> and was a frequent resource for all of us when it came to math questions (Thanks!).  He left MathWorks a little over a year\r\n      ago to pursue new ventures across the pond.\r\n   <\/p>\r\n   <h3>Acquiring Financial Data<a name=\"2\"><\/a><\/h3>\r\n   <p>For this simple example, we will use Michael's tools to pull financial data from Yahoo! and calculate an efficient frontier.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Selected symbols<\/span>\r\nsymbols = {<span style=\"color: #A020F0\">'TSLA'<\/span>,<span style=\"color: #A020F0\">'MSFT'<\/span>,<span style=\"color: #A020F0\">'GOOG'<\/span>,<span style=\"color: #A020F0\">'MAR'<\/span>,<span style=\"color: #A020F0\">'SAM'<\/span>,<span style=\"color: #A020F0\">'PG'<\/span>,<span style=\"color: #A020F0\">'YHOO'<\/span>};\r\n\r\n<span style=\"color: #228B22\">% Retrieve data using Michael's function<\/span>\r\ndata = getYahooDailyData(symbols,<span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'01\/01\/2011'<\/span>, <span style=\"color: #A020F0\">'10\/31\/2013'<\/span>, <span style=\"color: #A020F0\">'dd\/mm\/yyyy'<\/span>); <span style=\"color: #228B22\">%start, end, format<\/span><\/pre><p>Data is a struct array of tables!<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">disp(data)<\/pre><pre style=\"font-style:oblique\">    TSLA: [730x7 table]\r\n    MSFT: [730x7 table]\r\n    GOOG: [730x7 table]\r\n     MAR: [730x7 table]\r\n     SAM: [730x7 table]\r\n      PG: [730x7 table]\r\n    YHOO: [730x7 table]\r\n<\/pre><p>The <a href=\"\"><tt>table<\/tt><\/a> is a new MATLAB data container in R2013b.  Tables make it easy to store heterogenous column data in one container.  Michael\r\n      has included a check to see if the instance of MATLAB is new enough to have tables, if it isn't, the code uses datasets, the\r\n      table's predecessor from the <a href=\"https:\/\/www.mathworks.com\/products\/statistics\/\">Statistics Toolbox<\/a>.\r\n   <\/p>\r\n   <p>Let's take a look at a table by displaying the first 10 rows of Tesla's data:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">disp(data.TSLA(1:10,:));<\/pre><pre style=\"font-style:oblique\">       Date       Open     High      Low     Close      Volume      AdjClose\r\n    __________    _____    _____    _____    _____    __________    ________\r\n    7.3451e+05    26.84       27     25.9    26.62     1.283e+06    26.62   \r\n    7.3451e+05    26.66    26.95    26.02    26.67    1.1874e+06    26.67   \r\n    7.3451e+05    26.48     26.9    26.19    26.83    1.4467e+06    26.83   \r\n    7.3451e+05    26.83       28    26.81    27.88    2.0612e+06    27.88   \r\n    7.3451e+05       28    28.58     27.9    28.24    2.2479e+06    28.24   \r\n    7.3451e+05    28.17    28.68    28.05    28.45    1.3427e+06    28.45   \r\n    7.3451e+05    28.59    28.71    26.92    26.96    1.7102e+06    26.96   \r\n    7.3452e+05    27.01     27.4    26.52    26.96     9.644e+05    26.96   \r\n    7.3452e+05    26.96    26.97    26.16    26.22     7.236e+05    26.22   \r\n    7.3452e+05    26.15    26.58    25.61    25.75     1.192e+06    25.75   \r\n<\/pre><p>We can see that the table is displaying the separate variables and their names.  The best part is that we can still index\r\n      it like a matrix using {}.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">h = plot(data.TSLA{:,1},<span style=\"color: #0000FF\">...<\/span><span style=\"color: #228B22\">   Date<\/span>\r\n         data.TSLA{:,2:5},<span style=\"color: #0000FF\">...<\/span><span style=\"color: #228B22\"> 2nd:5th variables are valuations<\/span>\r\n         <span style=\"color: #A020F0\">'DisplayName'<\/span>,<span style=\"color: #0000FF\">...<\/span><span style=\"color: #228B22\">    Variable names for the legend<\/span>\r\n         data.TSLA.Properties.VariableNames(2:5));\r\n\r\nlegend(h,<span style=\"color: #A020F0\">'location'<\/span>,<span style=\"color: #A020F0\">'northwest'<\/span>); <span style=\"color: #228B22\">%add legend<\/span>\r\naxis <span style=\"color: #A020F0\">tight<\/span>; <span style=\"color: #228B22\">% tight axes<\/span>\r\ndatetick(<span style=\"color: #A020F0\">'x'<\/span>,<span style=\"color: #A020F0\">'mmmyy'<\/span>,<span style=\"color: #A020F0\">'keepticks'<\/span>,<span style=\"color: #A020F0\">'keeplimits'<\/span>); <span style=\"color: #228B22\">%date tick<\/span>\r\nylabel(<span style=\"color: #A020F0\">'Stock Price (US$)'<\/span>);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/main\/main_01.png\"> <p>Now that we have the data we can use it to figure out how to get rich quickly by calculating the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Efficient_frontier\">efficient frontier<\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Extract the closing valuations into an array:<\/span>\r\nassetsStruct = structfun(@(x)x.Close,data.',<span style=\"color: #A020F0\">'UniformOutput'<\/span>,false);\r\n<span style=\"color: #228B22\">% Uses x.Close to extract the \"Close\" variable from each table.<\/span>\r\nassets = struct2array(assetsStruct); <span style=\"color: #228B22\">% Convert to array<\/span>\r\n\r\n<span style=\"color: #228B22\">% Convert to returns<\/span>\r\nassetReturns = tick2ret(assets);\r\n\r\n<span style=\"color: #228B22\">% Create a portfolio object with the asset names. (Financial Toolbox req'd)<\/span>\r\np = Portfolio(<span style=\"color: #A020F0\">'AssetList'<\/span>,symbols);\r\n\r\n<span style=\"color: #228B22\">% Estimate the asset moments: Mean and Covariance, for the asset returns.<\/span>\r\np = p.estimateAssetMoments(assetReturns);\r\n\r\n<span style=\"color: #228B22\">% Set default constraints such that the weights sum up to one and to allow<\/span>\r\n<span style=\"color: #228B22\">% long only position (no shorting or shortselling: weights can't be negative)<\/span>\r\np = p.setDefaultConstraints;\r\n\r\n<span style=\"color: #228B22\">% Calculate frontier, risk and return<\/span>\r\nfrontierWeights = p.estimateFrontier(20);\r\nfrontierRisk    = p.estimatePortRisk(frontierWeights);\r\nfrontierReturns = p.estimatePortReturn(frontierWeights);\r\n\r\n<span style=\"color: #228B22\">% Visualize efficient frontier<\/span>\r\nfigure;\r\nplot(frontierRisk,frontierReturns, <span style=\"color: #A020F0\">'*-r'<\/span>,<span style=\"color: #A020F0\">'LineWidth'<\/span>,2)\r\nlegend(<span style=\"color: #A020F0\">'Efficient Frontier'<\/span>,<span style=\"color: #A020F0\">'Location'<\/span>,<span style=\"color: #A020F0\">'SouthEast'<\/span>)\r\ntitle(<span style=\"color: #A020F0\">'Efficient Frontier'<\/span>);\r\nxlabel(<span style=\"color: #A020F0\">'Risk'<\/span>);\r\nylabel(<span style=\"color: #A020F0\">'Return'<\/span>);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/main\/main_02.png\"> <h3>The Rest of It<a name=\"7\"><\/a><\/h3>\r\n   <p>In addition to giving us easy to use tools to get data from Yahoo! and Google, Michael has also managed to use quite a few\r\n      of the other features in the MATLAB language.  These include: regular expressions, tables, the new <a href=\"\"><tt>functiontests<\/tt><\/a> capabilities for running function-based unit tests in the MATLAB unit testing framework, version checking, and safe-guarding\r\n      against exceptions or errors. For example, when I first ran the above this morning before my laptop had found an internet\r\n      connection:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/main\/Error.PNG\"> <\/p>\r\n   <h3>Datafeed Toolbox<a name=\"8\"><\/a><\/h3>\r\n   <p>For additional options or access to many other popular Financial Data Servers such as Bloomberg&reg; or Thomson Reuters&reg;, MathWorks\r\n      has the <a href=\"https:\/\/www.mathworks.com\/products\/datafeed\/\">Datafeed Toolbox<\/a>.  This toolbox provides direct access to the servers through their APIs and allows for realtime as well as intraday tick\r\n      information if requested.\r\n   <\/p>\r\n   <h3>Comments<a name=\"9\"><\/a><\/h3>\r\n   <p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=4930#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/43627-download-daily-data-from-google-and-yahoo--finance#comments\">comment<\/a> for Michael.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_09e27f97fb424516b3e20404c4d18090() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='09e27f97fb424516b3e20404c4d18090 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 09e27f97fb424516b3e20404c4d18090';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Sean de Wolski';\r\n        copyright = 'Copyright 2013 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_09e27f97fb424516b3e20404c4d18090()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2013b<br><\/p>\r\n<\/div>\r\n<!--\r\n09e27f97fb424516b3e20404c4d18090 ##### SOURCE BEGIN #####\r\n%% Download Daily Data from Google\u00c2\u00ae and Yahoo!\u00c2\u00ae Finance\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/3208495 Sean>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/43627-download-daily-data-from-google-and-yahoo--finance\r\n% Download Daily Data from Google and Yahoo! Finance> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/311066 Michael Weidman>.\r\n%\r\n\r\n%% Background\r\n% This submission contains a suite of tools for getting daily stock prices\r\n% from Yahoo! and Google.  There are utility functions for converting\r\n% between Yahoo! and Google's formatting and to calculate adjusted closing\r\n% prices, (which Yahoo! does automatically but which Google does not).\r\n%\r\n% As a side note, Michael Weidmam was one of the first few people I met when I\r\n% started working at MathWorks.  He taught me everything I know about the\r\n% <\r\n% Kolmogorov-Smirnov test ( |kstest()| )> and was a frequent resource for all\r\n% of us when it came to math questions (Thanks!).  He left MathWorks a\r\n% little over a year ago to pursue new ventures across the pond.\r\n\r\n%% Acquiring Financial Data\r\n% For this simple example, we will use Michael's tools to pull financial\r\n% data from Yahoo! and calculate an efficient frontier.\r\n%\r\n\r\n% Selected symbols\r\nsymbols = {'TSLA','MSFT','GOOG','MAR','SAM','PG','YHOO'}; \r\n\r\n% Retrieve data using Michael's function\r\ndata = getYahooDailyData(symbols,...\r\n    '01\/01\/2011', '10\/31\/2013', 'dd\/mm\/yyyy'); %start, end, format\r\n\r\n%% \r\n% Data is a struct array of tables!\r\n\r\ndisp(data)\r\n\r\n%%\r\n% The <\r\n% |table|> is a new MATLAB data container in R2013b.  Tables make it easy\r\n% to store heterogenous column data in one container.  Michael has included\r\n% a check to see if the instance of MATLAB is new enough to have tables, if\r\n% it isn't, the code uses datasets, the table's predecessor from the\r\n% <https:\/\/www.mathworks.com\/products\/statistics\/ Statistics Toolbox>.\r\n% \r\n% Let's take a look at a table by displaying the first 10 rows of Tesla's\r\n% data:\r\n\r\ndisp(data.TSLA(1:10,:));\r\n\r\n%%\r\n% We can see that the table is displaying the separate variables and their\r\n% names.  The best part is that we can still index it like a matrix using {}.\r\n\r\nh = plot(data.TSLA{:,1},...   Date\r\n         data.TSLA{:,2:5},... 2nd:5th variables are valuations\r\n         'DisplayName',...    Variable names for the legend\r\n         data.TSLA.Properties.VariableNames(2:5)); \r\n\r\nlegend(h,'location','northwest'); %add legend\r\naxis tight; % tight axes\r\ndatetick('x','mmmyy','keepticks','keeplimits'); %date tick\r\nylabel('Stock Price (US$)');\r\n\r\n%%\r\n% Now that we have the data we can use it to figure out how to get rich\r\n% quickly by calculating the\r\n% <http:\/\/en.wikipedia.org\/wiki\/Efficient_frontier efficient frontier>.\r\n\r\n% Extract the closing valuations into an array:\r\nassetsStruct = structfun(@(x)x.Close,data.','UniformOutput',false);\r\n% Uses x.Close to extract the \"Close\" variable from each table.\r\nassets = struct2array(assetsStruct); % Convert to array\r\n\r\n% Convert to returns\r\nassetReturns = tick2ret(assets);\r\n\r\n% Create a portfolio object with the asset names. (Financial Toolbox req'd)\r\np = Portfolio('AssetList',symbols);\r\n\r\n% Estimate the asset moments: Mean and Covariance, for the asset returns.\r\np = p.estimateAssetMoments(assetReturns);\r\n\r\n% Set default constraints such that the weights sum up to one and to allow\r\n% long only position (no shorting or shortselling: weights can't be negative)\r\np = p.setDefaultConstraints;\r\n\r\n% Calculate frontier, risk and return\r\nfrontierWeights = p.estimateFrontier(20);\r\nfrontierRisk    = p.estimatePortRisk(frontierWeights);\r\nfrontierReturns = p.estimatePortReturn(frontierWeights);\r\n\r\n% Visualize efficient frontier\r\nfigure;\r\nplot(frontierRisk,frontierReturns, '*-r','LineWidth',2)\r\nlegend('Efficient Frontier','Location','SouthEast')\r\ntitle('Efficient Frontier');\r\nxlabel('Risk');\r\nylabel('Return');\r\n\r\n%% The Rest of It\r\n% In addition to giving us easy to use tools to get data from Yahoo! and\r\n% Google, Michael has also managed to use quite a few of the other features\r\n% in the MATLAB language.  These include: regular expressions, tables, the\r\n% new\r\n% <\r\n% |functiontests|> capabilities for running function-based unit tests in\r\n% the MATLAB unit testing framework, version checking, and safe-guarding\r\n% against exceptions or errors. For example, when I first ran the above this\r\n% morning before my laptop had found an internet connection:\r\n%\r\n% <<Error.PNG>>\r\n%\r\n\r\n%% Datafeed Toolbox\r\n% For additional options or access to many other popular Financial Data\r\n% Servers such as Bloomberg\u00c2\u00ae or Thomson Reuters\u00c2\u00ae, MathWorks has the\r\n% <https:\/\/www.mathworks.com\/products\/datafeed\/ Datafeed Toolbox>.  This\r\n% toolbox provides direct access to the servers through their APIs and\r\n% allows for realtime as well as intraday tick information if requested.\r\n\r\n%% Comments\r\n%\r\n% Give it a try and let us know what\r\n% you think <https:\/\/blogs.mathworks.com\/pick\/?p=4930#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/43627-download-daily-data-from-google-and-yahoo--finance#comments comment> for Michael.\r\n%\r\n##### SOURCE END ##### 09e27f97fb424516b3e20404c4d18090\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/main\/main_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Sean's pick this week is Download Daily Data from Google and Yahoo! Finance by Michael Weidman.\r\n      \r\n   \r\n   Background\r\n   This submission contains a suite of tools for getting... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2013\/11\/29\/download-daily-stock-data-from-yahoo-and-google\/\">read more >><\/a><\/p>","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4930"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=4930"}],"version-history":[{"count":14,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4930\/revisions"}],"predecessor-version":[{"id":4945,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4930\/revisions\/4945"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=4930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=4930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=4930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}