File Exchange

image thumbnail

hist_stock_data(sta​rt_date, end_date, varargin)

version (8.41 KB) by Josiah Renfree
Used to retrieve historical stock data for a user-specified date range


Updated 10 Aug 2017

View Version History

View License

This program uses the Yahoo! Finance website to download and sort historical stock prices or dividend data for a user-specified time period. The user can either supply the program with individual ticker symbols or with a text file containing a list of ticker symbols. The program returns a structure array that holds information on the date and either historical price or dividend data. The user must specify the date range for the desired information, and has the option of specifying a sample frequency of daily, weekly, or monthly. Refer to the help section in the m-file for data formats and options.

Cite As

Josiah Renfree (2020). hist_stock_data(start_date, end_date, varargin) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (106)

Jason Xu

Nathan Smith

Yahoo Finance was always a great source to download historical data. It was for free, but it also had its downsides in terms of data quality.

I think in the end, any data is only worth what you pay for. Here is an interesting article about attractive Yahoo Finance API alternatives:
But nothing is really comparable to what we know from Yahoo Finance.

For me, paying $50 a month for data is not really an option, but I also found an interesting free alternative called NinjaTrader. The downside here is that you have to download all tickers separately.

Anyway, the data is reliable since it's from their tick-by-tick data provider Kinetick.
I hope that helps.

Ali Komai

I experience i minor problem. When downloading data always the most recent date (yesterday) is missing. Did someone manage to fix this?

Ali Komai

Filippo you must be doing something wrong. This here works great (at least for me)!

Filippo Viglongo

sorry for the previous evaluation of 5 stars. the program works but , unfortunately data downloaded are wrong! there is no relation between market data and dates specified. I think the problem is always the same ,Yahoo's API doesn't work anymore.

Filippo Viglongo

Great job! Thanks !

Parker Campbell

Matthew Cooper

Any way to get additional information such as market cap? I find this function is about 2.6x faster than get_yahoo_stockdata3.

Ali Komai

Kenneth Segura

Fernando Esteves

Hello everyone.
I just uploaded a friendly GUI that use this file. Find it with this link:

vittorio manente

Hi Josiah,

is there any reason why the script does not download data after January the 1st 2018?


David Galé

Could you post the whole function?


Hi F. Nucera:
Yahoo did change the URL a little bit. Simply add "&crumb=dqdli/qZEvR" to the end of the following string
['', ...
That is,
['', ...
then the function will work again.

F. Nucera

Hi everybody, it seems that the function really does not work anymore. I have tried both with Matlab 2010 and Matlab 2015. Did someone else experiece the same problems? Thanks. F.

F. Nucera

Hi, it is just me or Yahoo Finance has recently implemented some changes such as the function does not work anymore? I am experience a lot of troubles in downloading stock data at the monthly 'mo' frequency. In particular, I get an empty structure most of the times. Thanks. F.

Agnieszka Borowska

Fernando Esteves

Hello Josiah.
I was having troubles when I try to download for some stocks the daily historic data for a long period (more than 15 years), your function just gave me a shorter period of data.
I solved it just changing the code for some instructions of a previous version of you function.
I changed:
data = textscan(temp, '%s%f%f%f%f%f%f', 'delimiter', ',', ...
'Headerlines', 1);

[stocks(idx).Date, stocks(idx).Open, stocks(idx).High, ...
stocks(idx).Low, stocks(idx).Close, ...
stocks(idx).AdjClose, stocks(idx).Volume] = deal(data{:});
[date, op, high, low, cl, adj_close, volume] = ...

stocks(idx).Ticker = tickers{i}; % get ticker symbol
stocks(idx).Date = date; % save date data
stocks(idx).Open = str2double(op); % save opening price data
stocks(idx).High = str2double(high); % save high price data
stocks(idx).Low = str2double(low); % save low price data
stocks(idx).Close = str2double(cl); % save closing price data
stocks(idx).AdjClose = str2double(adj_close); % save adjustied close data
stocks(idx).Volume = str2double(volume); % save volume data

Thanks for your excellent work.



Fernando Esteves

Harry Smith

Darcy Cordell

Yi Zhao

Hi Josiah, thanks for making this wonderful script! I noticed that all tickers are downloaded in series, and each one will take some minimum time (likely some overhead). I wonder if you would consider introducing some parallelism, like using parfor instead of for. You will have to pre-allocate the struct array and use sliced index.

Josiah Renfree

Just made some more updates. Most notably the previous version had Volume and Adjusted Close values swapped. It now also allows for inputting multiple ticker symbols as a single cell array (instead of multiple parameters in the function call) and expressing the start and end dates as Matlab datenums.

F. Nucera

Hi Josiah, it works perfectly now! Thank you so much! I just hope that yahoo finance does not perform anymore changes/closures.

Josiah Renfree

@F. Nucera, I just updated the code so that it doesn't use the posixtime function. You should be able to run it now without any issues. But please let me know if you still have problems.

F. Nucera

Thanks a lot for your reply. I think indeed that the problem lies with my Matlab version (2013). It seems to me that for version 2013 datetime function was not available. Do you think that there is a solution to overcome this problem? Thanks

Josiah Renfree

@F. Nucera, can you tell me exactly how you're calling the function? What version of Matlab are you using? It's possible the datetime function was recently introduced and you're using an older version of Matlab that doesn't have it.

F. Nucera

Hi Josiah, thanks for this! I am still having troubles though. I get the following error message when using the code. "Undefined function 'datetime' for input arguments of type 'char'." Any idea how to fix it?

Josiah Renfree

@Brian Wenzel, I just added that functionality today. Thanks for the suggestion. Download the latest version and see the help section for getting dividend data. Essentially all you need to change is adding the parameters pair ('type', 'div') into the function call.


Thank you. It's working great, and was worth the wait.

Brian Wenzel

Is there a way to download the dividends and dividend dates?

Josiah Renfree

In case anyone is following this, this program is now working once again. Many thanks to Derrick Cox for his help with solving this problem.

Dominique Berthold

jaakko lehtoalho: In general, it has nothing to do with stock split. The code fails to retrieve correctly every time some data are missing. Here's another example with the ETFs from iShares. Data are missing on 13-Jan-2012 and therefore, data prior that day won't be retrieved.
Try for example: stocks = GetHistoricGoogle('IVV', 'Jan 31, 2011', 'Jun 15, 2017')

jaakko lehtoalho

Dominique Berthold: Maybe it has something to do with the stock split that happened around that day. There are two tickers for Aplhabet (Google), GOOG & GOOGL. Try fetching with GOOGL instead

Dominique Berthold

Thank you for the code. It seems to work well except when some data are missing. As an example, when trying to retrieve data of Google stocks (GOOG), for the last 4 years, one gets data only back to 26-Mar-14. Before that date, there's no data for volume. Any idea to fix that issue?

Dominique Berthold

jaakko lehtoalho

Here is a code that fetches data from Google. Works with .txt file that contains the tickers. Not my own work but modified from codes written by Chris Reeves & Josiah Renfree:

Example: stocks = GetHistoricGoogle(tickers, '01/01/2006', '31/12/2014')


function [ stocks ] = GetHistoricGoogle(tickers,start_date,end_date)
% Modified from codes written by Josiah Renfree & Chris Reeves (A2X Capital LLC)
% query date ranges from google finance
startDateNum = datenum(start_date);
endDateNum = datenum(end_date);

startDateStr = datestr(startDateNum,'mmm dd, yyyy');
endDateStr = datestr(endDateNum,'mmm dd, yyyy');

stocks = struct([]); % initialize data structure

fid = fopen('tickers2.txt', 'r'); % Select your txt file that contains the tickers
tickers = textscan(fid, '%s'); tickers = tickers{:};

h = waitbar(0, 'Please Wait...'); % create waitbar
idx = 1; % idx for current stock data

% Cycle through each ticker symbol and retrieve historical data
for i = 1:length(tickers)

% Update waitbar to display current ticker
waitbar((i-1)/length(tickers),h,sprintf('%s %s %s%0.2f%s', ...
'Retrieving stock data for',tickers{i},'(',(i-1)*100/length(tickers),'%)'))

% Download historical data using the Google Finance website

url = strcat('',tickers{i},'&startdate=',startDateStr,'&enddate=',endDateStr,'&output=csv');
url = strrep(url,' ','%20');
response = urlread(url);
data = textscan(response,'%s %f %f %f %f %f','delimiter',',','HeaderLines',1);

% Put data into appropriate variables
[stocks(idx).Date, stocks(idx).Open, stocks(idx).High, ...
stocks(idx).Low, stocks(idx).Close, stocks(idx).Volume,] = deal(data{:});

stocks(idx).Ticker = tickers{i}; % Store ticker symbol

idx = idx + 1; % Increment stock index

% update waitbar

close(h) % close waitbar


derrick cox

I did manage to find a way to pull data from yahoo finance. I don't know if this is still an issue. was very quick and 2 lines

primeSite = [''];
fullList = urlread(primeSite,'post',{usernameString, passwordSrting})

I would recommend creating a dummy account due to possible bad coding practices in Matlab...

but period2 is 1498095958....that would be today (june 21 2017, and 9:10:52)....june 20 2017 at the same time is 86400 seconds period1 = 1498095958-86400. I imported a char list with the code above. To further this, if you want a year just go >> period1 = 1498095958-86400*365.

If you're looking for a dynamic solution to down all history, meaning find the start date...I have nothing. I would consider a loop that converges to a first valid period1. If you run a try catch method, should be quick 15 iterations if you do it right.

Denis Alaev

It seems that after several changes Yahoo Finance closed their API forever. The API was closed on May 15, one month ago already.

I've tested several alternatives and found that the best one for those who used Yahoo Finance. They provide raw data, adjusted closes and splits/dividends.

They also have CSV output, with very similar format for Yahoo Finance users. It’s easy, basically, you need to do only two things:

1. Change all urls like ‘‘ to ‘‘
2. And add only one new parameter: api_token=OeAFFmMliFG5orCUuwAKQ8l4WWFQ67YX.

Also there is a data provider, looks good, but they much more expensive, have no data for Mutual Funds and API is very different in compare to Yahoo Finance. Then you need to significantly change your code.


Has anyone managed to get it working recently? it seems that it is not downloading the data from yahoo anymore ...

Jeffrey Kern

Josiah, I thing the "strange time format" is unix time. You can get this using the "posixtime" command, e.g. posixtime(datetime('23-may-2017')). Unfortunately there is a bigger problem with Yahoo Finance right now. They require a cookie "crumb".

Maurice Chia

Does anyone know how to link the info in this post to hist_stock_data? It apparently solves the cookie issue

Maurice Chia

Does anyone know how to link the info in this post to hist_stock_data?

Josiah Renfree

Hi everyone,

It appears that Yahoo changed their Finance system yet again, so I believe this function is currently not working. The new url calls appear to use cookies and some strange time format, but I'll see if I can get it to work. If not, I may have to try and switch over to a different service (e.g. Google finance), but that may take some time.

Things are quite busy at the moment, but I'll try to get around to this soon. In the meantime, you may want to check out the "Alternate Method" Historical Stock Data Downloader. I haven't tried it myself, but it parses out the actual webpages instead of reading csv data, so it may be less prone to having issues.

sean cleary

Any idea on the new interface? Been trying a few with no luck?,

I can get this to work with Google finance but they don't provide NASDAQ index numbers unfortunately

Erhan Eker

I think yahoo change their interface

sean cleary

Great app. Not working today however. Possible due to server downtime on yahoo side. For example the link just days engineers looking at it

Erhan Eker

Pontus Lohman

Isn't working for me anymore just stays at 0% for a while then returns an empty struct

Josiah Renfree

@Prem Ram, just to verify, are you using the latest version of the function that I updated on April 18? It's possible that using R2008b is causing problems, but I wouldn't think so. Even I had problems using a newer version of Matlab before I updated the function a couple of weeks ago. Feel free to private message me if you'd like me to help you debug.

Mens Sana

@Josiah Renfree I am using a very old version of Matlab (R2008b) -I don't know if that is causing my current problems. Because my old code isn't working either.

Josiah Renfree

@Prem Ram, I just tried it and it worked fine for me. Make sure you are using the latest version that I uploaded on April 18. Yahoo recently changed something on their server which required an update.

Mens Sana

Doesn't work. Neither do any of the other functions I used previously for downloading data. I don't know if this is due to a url change or that older Matlab functions don't work, because the url entered in a browser with the same fields gets the csv file:

Josiah Renfree

@David Holland, make sure you download the latest version that came out on April 18, 2017. I think Yahoo recently changed their server from http to https, so any old code will not function properly.

Christopher Jones

Octavio Garcia

I suppose you update the file to change http by https in urlread for avoid the problems occurred in Yahoo Finance from 17 April 2017

David Holland

Its not working for me.....

Virgil Stokes

Nice! Is there anyway to get currency exchange rates (e.g. EUR/USD)?

Waqar Haider

Anyway i can edit the data so it is on one if it was an excel spreadsheet


Chetan Rawal

Simple and straightforward data fetching for a hobbyist like me.


Thanks a lot! Well done.


doesn't run varagin




Zoe Zhang

love it!

Dineshkumar Hari

I'm a hopeless noob, please help me with this I'm currently working on Brownian Modelling of Timeseries
??? Input argument "start_date" is undefined.

Error in ==> hist_stock_data at 68
bd = start_date(1:2); % beginning day

Dineshkumar Hari

I'm a hopeless noob, please help me with this I'm currently working on Brownian Modelling of Timeseries
??? Input argument "start_date" is undefined.

Error in ==> hist_stock_data at 68
bd = start_date(1:2); % beginning day



I have a very simple code for downloading the historical prices from yahoo finance. If I use:

start_date = '01032012';
end_date = '11072012';
stocks = hist_stock_data(start_date, end_date, '^GSPC');
stocks.Date = stocks.Date(end:-1:1); stocks.Close = stocks.Close(end:-1:1); Prices=stocks.Close

then the vector prices has 4 decimals. However, if I instead of '^GSPC' use 'EURUSD=X', then the 3rd and 4th decimals are always 0, i.e. the numbers are rounded. However, on the website

the quotes have 4 decimals. Is there a way to download currency data with 4 decimals instead of 2?

Thank you very much for your time and consideration!


Zhongwei Tian


Hi again, I solved it :)... nice class.



Hi Joshiah,

Sorry for the silly question. The code runs fine. But somehow I can't access the data which I downloaded from yahoo... do you have an example how to access them?



Josiah Renfree

@Matt: Unfortunately, daily is the highest frequency that the program can obtain. I'm simply pulling data off of the Yahoo! Finance Historical Price webpage, in which you'll see that your only options are Daily, Weekly, or Monthly.

@David: As I mentioned to Matt, I pull data off of the Historical Prices section of the Yahoo! Finance page. Unfortunately, you'll notice that for the symbol JPYUSD=X, there is no Historical Prices section. I'm not sure why Yahoo doesn't include that, though, as their charts include historical data.

David Wozabal

If I try to retrieve the exchange rate between JPY and USD by typing

hist_stock_data('01012005','28112010', 'JPYUSD=X', 'frequency', 'w')

I get back

ans =

0x0 struct array with no fields.

Other ticker symbols seem to work fine with me. What am I doing wrong?

Matt Fetterman

Can it be set for higher frequency data? Maybe every hour or every fifteen minutes? Good program !

mklcst mklcst

Very good! But I think that we need to sync the data and not to have each series in different dimension!

Sunil Tyagi

Thanks it is useful

Josiah Renfree

To answer Gary's question, the data to account for stock splits can be found in the returned structure array under the variable AdjClose.


Josiah, code works well. Thanks.
Say, Is there any way to access, download or otherwise account for stock spits? Using 'LMT' as an example, dates 01/01/1979 to present, for example: Code dutifully downloads historical data but is radically different from that displayed on Yahoo. This is due, largely, to the stock splits. Any way to include these details?

Janice Hawie

sean brown

I find this source
it works great. try it.

David Succaria

Hi Gents,

While I agree with you that there are some free charting as well as some historical data which can be downloaded sparingly, I would suggest that to do real trend analysis, modeling and systems development for algorithmic trading strategies, along with associative research and back testing. You need a complete local database of historical data to use with your charting and analysis package of choice. To see the dramatic difference involved between looking up symbol data online and havinga complete database try the free trial offered by these guys...

They have end of day data and intraday high frequency historical data, across multiple asset classes. Best of all they include all delisted issues for a survivorship free database.

Happy research.


Al McJeff

Yes very good.

Francisco Azeredo

Thank you for the code, it works like a charm.

One request: Can the code be modified to download the dividend series as well?

raphael rubin

I have a problem because of the prompt which opens up when downloading the file.
How do I get over that problem?

Laura Chico

delivers as promised. thank you for submitting.

Gee Zee

thanks for the code

Cassio Carvalho

It works very well!

jim cha

Kevin T

Great file. Well written and documented.

Bonus: works on R13 without single modification.

bob n

Hi! Where can i get information on the yahoo api from where you are pulling the stocks info?? i've tried to find info on yahoo's site but i cant. any suggestions?


Works well. Thank you.

Lyle Ask

Great! With a little modification of the date format, it works well with the Financial Time Series Toolbox.

Austin Kalb

Worked great out of the box!

Chris Troutner

Hey! Thank you so much for making such an awesome script! This was exactly what I was looking for and you saved me so much time. It worked perfectly on the first try.

Tsai wei-che

useful code

Olivier B.

Works well for me too !

John D'Errico

It seems to work now. My thanks to the author for getting it to work. I can't complain at all about the help - its really quite readable. This could be a useful utility.

Happy User

That fixed it, thanks

Josiah Renfree

I've uploaded a new m-file that hopefully corrects for this, so it should be up in the next couple of days.

The only thing I've changed though is that in the urlread command, 'tickers(i)' is now 'tickers{i}'. I'm guessing that the problem was that varargins is a cell array, and the program was retrieving the data as a cell, and not converting it to a string. So if you don't want to wait around for the new file to upload, then just make that small little change.

If that does alleviate the problem, could you let me know? It's hard for me to tell exactly what's causing this error, as this program is working perfectly for me.

Sorry that some of you are experiencing some troubles with this. I'm hoping that other people are having more success with this.

John D'Errico

I tried the new version. It gets further, but still dies.

stocks = hist_stock_data('01012000','27012008','TMA')

??? Undefined function or method 'eq' for input arguments of type 'cell'.

Error in ==> iofun/private/urlreadwrite at 14
protocol = urlChar(1:min(find(urlChar==':'))-1);

14 protocol = urlChar(1:min(find(urlChar==':'))-1);

Josiah Renfree

I noticed that error a couple days ago, and uploaded a newer version that corrected this error, but apparently Mathworks hasn't posted it yet. So yes you have a valid point regarding the tickers with fewer than 4 characters, but the new version already corrects for this. Hopefully Mathworks will post it tomorrow.

As for the date inputs, the m-file documents on the format it needs to be. I know I could have put in better error-checking and what not, but if you follow the directions of the m-file then it should be straightforward. It states the first date is the beginning time, and the second is the ending time. Also, they are input as strings, with the format ddmmyyyy. I believe it should all be there in the help.

Thanks for the comments and let me know if you find anything buggy with the new version.

John D'Errico

Buggy code. BEWARE!

I'm running R2007a, and I too get errors for all test cases I've tried, including those given as examples by the author. I also tried changing the urlread call as suggested. So then I looked at the errors returned. Things are done like this:

77 if strcmp(varargin{1}(end-3:end),'.txt') % If text file supplied

If the user supplies stock ticker symbols for stocks that are shorter then 4 characters, then what do you think will happen? My guess is its an error.

This code is well documented. The help is good. But the code is sloppy, in the sense that it is not carefully written. There are no error checks. Are the dates provided valid dates? Are they even the correct length as dates? Are the two dates increasing in order? Are they even strings? It merely indexes into them as vectors.

More importantly, the other arguments are indexed as strings also. The code looks at the last 4 characters of a string to see if it is '.txt'. But if the string is a ticker symbol that is only 3 characters long, oops!

I really wanted to give this a lower rating. "needs improvement" seemed about right at first given the outright bugs. On the other hand, the code is well documented, it has an H1 line, it has lots of comments internally, and the author seems willing to repair the problems.

My advice to the author is to please test your code better. Also put in error checks, to make sure that you get the arguments that you expect. If not, then have it return a friendly error message that tells the user what they did wrong.

I'll happily return to this code in a few days to look for improvements.

Josiah Renfree

It's not giving me that error on the Matlab version I'm running, so it's hard for me to pinpoint what the problem is. But, I believe that it may be that it's inputting the ticker symbol into the urlread command as a cell array.

In the urlread command, you'll find the command 'tickers(i)'. Try changing that to 'tickers{i}' instead, so that it's not returning it as a cell array but as a string.

Let me know if that solves your problem, so that I can make the necessary corrections for future versions of the program

Olivier B.

got an error at urlread level with the provided Goog example ... any hint ?

??? Undefined function or method 'eq' for input arguments of type 'cell'.

Error in ==> urlreadwrite at 14
protocol = urlChar(1:min(find(urlChar==':'))-1);

Error in ==> urlread at 68
[urlConnection,errorid,errormsg] = urlreadwrite(mfilename,urlChar);

Error in ==> hist_stock_data at 95
[temp, status] = urlread(strcat(''...

MATLAB Release Compatibility
Created with R2016a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!