{"id":9032,"date":"2017-12-15T09:00:54","date_gmt":"2017-12-15T14:00:54","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=9032"},"modified":"2017-12-15T15:28:59","modified_gmt":"2017-12-15T20:28:59","slug":"download-stock-data","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2017\/12\/15\/download-stock-data\/","title":{"rendered":"Download Stock Data"},"content":{"rendered":"<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871\">Jiro<\/a>&#8216;s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/37502-historical-stock-data-download--alternate-method-\">&#8220;Historical Stock Data Download (alternate Method)&#8221;<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3570958\">Captain Awesome<\/a>.<\/p><p>I&#8217;m not an expert in stocks, but I stumbled upon this entry when I received a question from a training customer asking about the now removed <a href=\"https:\/\/www.mathworks.com\/products\/datafeed.html\">Datafeed Toolbox<\/a> function, <tt>yahoo<\/tt>. Earlier this year, Yahoo! changed their API, which prevented the function from connecting to the service. As of R2017a, the <tt>yahoo<\/tt> function has been removed from the toolbox.<\/p><p>My customer wanted to know alternatives to fetching stock data from Yahoo!. The workaround I heard was to use lower-level functions to read the raw data from the website and scrape for the necessary data. As I was writing some sample code, I decided to see if there were any useful functions on the File Exchange. While there are a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?term=yahoo+stock\">number of entries<\/a> for fetching stock data from the web, this entry by Captain Awesome was the first one that I came across that worked out of the box. Captain Awesome had accounted for the API change.<\/p><p>Here is an example of fetching daily Google stock data from Jan 2010.<\/p><pre class=\"codeinput\">stock = get_yahoo_stockdata3(<span class=\"string\">'GOOG'<\/span>,<span class=\"string\">'2010\/1\/1'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">stock = \r\n  struct with fields:\r\n\r\n           ticker: 'GOOG'\r\n       dataSource: 'Yahoo Finance'\r\n       dataUpdate: '14-Dec-2017 23:42:24'\r\n         errorMsg: ''\r\n         dataFreq: 'd'\r\n        dividends: []\r\n           splits: [1&times;1 struct]\r\n            range: '04-Jan-2010 to 13-Dec-2017'\r\n         varnotes: {7&times;4 cell}\r\n         DateTime: [2002&times;1 double]\r\n        openPrice: [2002&times;1 double]\r\n        highPrice: [2002&times;1 double]\r\n         lowPrice: [2002&times;1 double]\r\n       closePrice: [2002&times;1 double]\r\n           volume: [2002&times;1 double]\r\n    adjClosePrice: [2002&times;1 double]\r\n<\/pre><p>If you look at the code, you&#8217;ll notice that it&#8217;s simply reading the website with the appropriate queries, and doing a bunch of text processing to extract data.<\/p><p><b>Comments<\/b><\/p><p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=9032#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/37502-historical-stock-data-download--alternate-method-#comment\">comment<\/a> for Captain Awesome.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_75cbeb1c35f74812848d8d6adeab8bd8() {\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='75cbeb1c35f74812848d8d6adeab8bd8 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 75cbeb1c35f74812848d8d6adeab8bd8';\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        copyright = 'Copyright 2017 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_75cbeb1c35f74812848d8d6adeab8bd8()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2017b<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2017b<br><\/p><\/div><!--\r\n75cbeb1c35f74812848d8d6adeab8bd8 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871 Jiro>'s\r\n% pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/37502-historical-stock-data-download--alternate-method- \"Historical\r\n% Stock Data Download (alternate Method)\"> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3570958 Captain\r\n% Awesome>.\r\n%\r\n% I'm not an expert in stocks, but I stumbled upon this entry when I\r\n% received a question from a training customer asking about the now removed\r\n% <https:\/\/www.mathworks.com\/products\/datafeed.html Datafeed Toolbox>\r\n% function, |yahoo|. Earlier this year, Yahoo! changed their API, which\r\n% prevented the function from connecting to the service. As of R2017a, the\r\n% |yahoo| function has been removed from the toolbox.\r\n%\r\n% My customer wanted to know alternatives to fetching stock data from\r\n% Yahoo!. The workaround I heard was to use lower-level functions to read\r\n% the raw data from the website and scrape for the necessary data. As I was\r\n% writing some sample code, I decided to see if there were any useful\r\n% functions on the File Exchange. While there are a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?term=yahoo+stock\r\n% number of entries> for fetching stock data from the web, this entry by\r\n% Captain Awesome was the first one that I came across that worked out of\r\n% the box. Captain Awesome had accounted for the API change.\r\n%\r\n% Here is an example of fetching daily Google stock data from Jan 2010.\r\n\r\nstock = get_yahoo_stockdata3('GOOG','2010\/1\/1')\r\n\r\n%%\r\n% If you look at the code, you'll notice that it's simply reading the\r\n% website with the appropriate queries, and doing a bunch of text\r\n% processing to extract data.\r\n%\r\n% *Comments*\r\n%\r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=9032#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/37502-historical-stock-data-download--alternate-method-#comment\r\n% comment> for Captain Awesome.\r\n\r\n##### SOURCE END ##### 75cbeb1c35f74812848d8d6adeab8bd8\r\n-->","protected":false},"excerpt":{"rendered":"<p>Jiro&#8216;s pick this week is &#8220;Historical Stock Data Download (alternate Method)&#8221; by Captain Awesome.I&#8217;m not an expert in stocks, but I stumbled upon this entry when I received a&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2017\/12\/15\/download-stock-data\/\">read more >><\/a><\/p>","protected":false},"author":35,"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\/9032"}],"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\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=9032"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/9032\/revisions"}],"predecessor-version":[{"id":9036,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/9032\/revisions\/9036"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=9032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=9032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=9032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}