File Exchange

image thumbnail

Yahoo Finance and Quandl data downloader

version 1.131 (545 KB) by Artem Lenskiy
Yahoo Finance and Quandl data downloader

156 Downloads

Updated 07 Mar 2021

From GitHub

View Version History

View license on GitHub

The toolbox contains two functions:

(a) getMarketDataViaYahoo()
% INPUT:
% symbol - is a ticker symbol i.e. 'AMD', 'BTC-USD'
% startdate - the market data will be requested from this data
% enddate - the market data will be requested till this date
% interval - the market data will be returned in this intervals
% supported intervals are '1d', '5d', '1wk', '1mo', '3mo'
%
% OUTPUT:
% data - is a retrieved dataset returned as a table

data = getMarketDataViaYahoo('AMD', '1-Jan-2018', datetime('today'), '5d'); % Downloads AMD share historic price

(b) getMarketDataViaQuandl()
% INPUT:
% set_name - is a dataset name e.g. 'WIKI/AAPL'
% startdate - the market data will be requested from this data
% enddate - the market data will be requested till this date
% collapse - the market data will be returned in this intervals
% supported intervals are 'daily', 'weekly', 'monthly', 'quarterly', 'annual'
% key - user's api key
%
% OUTPUT:
% data - is a retrieved dataset returned as a table

opec_orb_raw = getMarketDataViaQuandl('OPEC/ORB', '1-Jan-2018', date(), 'weekly'); % Downloads historic OPEC basket price from Quandl

For a complete list of free datasets provided by Quandl check https://www.quandl.com/search?filters=%5B%22Free%22%5D

Examples:

(a) Yahoo Finance
disp('Request historical YTD Bitcoin price and plot Close, High and Low');
initDate = '1-Jan-2018';
symbol = 'BTC-USD';
btcusd = getMarketDataViaYahoo(symbol, initDate);
btcusdts = timeseries([btcusd.Close, btcusd.High, btcusd.Low], datestr(btcusd(:,1).Date));
btcusdts.DataInfo.Units = 'USD';
btcusdts.Name = symbol;
btcusdts.TimeInfo.Format = "dd-mm-yyyy";
plot(btcusdts);
legend({'Close', 'High', 'Low'});

(b) Quandl
dataset = 'LBMA/GOLD';
initDate = '1-Jan-2018';
lbma_gold_raw = getMarketDataViaQuandl(dataset, initDate, date(), 'daily');
lbma_gold_ts = timeseries(lbma_gold_raw.("EURO(AM)"), datestr(lbma_gold_raw.Date));
lbma_gold_ts.DataInfo.Units = 'USD';
lbma_gold_ts.Name = dataset;
lbma_gold_ts.TimeInfo.Format = "dd-mm-yyyy";
figure, plot(lbma_gold_ts);

Cite As

Artem Lenskiy (2021). Yahoo Finance and Quandl data downloader (https://github.com/Lenskiy/Yahoo-Quandl-Market-Data-Donwloader/releases/tag/v1.131), GitHub. Retrieved .

Comments and Ratings (106)

Yu Huang

Thank you very much for this update on the code. It's working for me.

Hi,

thanks for sharing this very useful code. Is there any way we can fetch ratios like P/E or P/B for a specific ticker from yahoo finance or finviz?

thanks

Joseph Ludsfer

Good morning Artem, please can you make your code work with a vector of assets prices request? (I mean being able to download prices for more than one asset in a single request ?)
Thank you

Joseph Ludsfer

Jason Olasky

Some raw Yahoo price data is returned as strings rather than numeric and needs to be converted, otherwise works well.

Chris Brobeck

Jacob Wong

mlp94

Hello, not working for me. I get the following warning:
Warning: Identifier: MATLAB:webservices:TimeoutMessage: The connection to URL
'https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1388534400&period2=1616004553&interval=1d&events=history&frequency=1d&guccounter=1&includeAdjustedClose=true'
timed out after 5.000 seconds. The reason is "Waiting for response header". Perhaps the server is not
responding or weboptions.Timeout needs to be set to a higher value.
Any hint? Thanks!

vlad ap

I used the same ticker as in your example and also 'AAPL' and 'AMZN'. The code didn't work for the past couple of days but works today again.

Artem Lenskiy

@vald what ticker symbol are you requesting?

vlad ap

Great code, but yahoo changed something again and it stopped working. It says "The options.ContentType value, 'table', does not match the 'json' content type obtained from URL". Changing it to 'json' doesn't work either.

Christian Würth

Gustavo Schwartz

Great! This is the function I was looking for. Simple and efficient. Thanks!!!

Victor Grubîi

Thanks

Mario Trevino

Thanks for updating the function!!! For me it stopped working on the 5th or march 2021. With this update is working again perfectly. Million thanks

Natalie Wijaya

The updated version works very well. Thank you buddy... :))

ANTONIO RUBIA

Capitán Díaz

Joseph Ludsfer

Excellent, everything is fine. Once again Thank you Artem.

Artem Lenskiy

@Antonio, @Kevin, @Gabor, Could you please download the functions once again and test them (the fileexchange did not sync the files with GitHub, now they should be in sync)?
I’ve also added BigMacExample.m to tests the functions. The example downloads price of Big Mac from Quandl and price of gold from Yahoo and plot prices of Big Mac in gold and in USD.

@Daniel, I will add this functionality next week.

@Joseph, @Syed, @Mark thanks!

Joseph Ludsfer

Github version is working. Thanks Shah

syed shah

The version at GitHub is working, thank Artem for this excellent function.

Marc Burock

I second everyone that this is a great function Artem! Hope you or others can get it working again.

syed shah

Hi, this is not working anymore. Can you please look into that?
Thanks

Joseph Ludsfer

Great work!, I am using the 2020b version, but cannot get market data with the code provided for Yahoo, I repeatedly run the code with various symbols. but returns "Check ticker symbol and that Yahoo provides data for it". Again, thank you for your work. any advice will be welcomed.

Gabor

Perfect greatly used function unfortunately not working anymore. The update runs but returns: 'Check ticker symbol and that Yahoo provides data for it'
Checked with 'AAPL' and other symbols as you most likely aware of this problem. Than you for your work! Please advise

Kevin Johnson

Get: 'Check ticker symbol and that Yahoo provides data for it' using AAPL ticker

ANTONIO RUBIA

Hi Artem, first of all thaks for this truly amazing function. Unfortunately, it's not working yet. In my case, I get the error flag "Check ticker symbol and that Yahoo provides data for it" when running the Bitcoin example included in the downloadable file. Same message when I try to run the code for other random stocks.

This is one of the best functionalities in the community, the error is originated in some change Yahoo introduced on March 3rd. Looking forward to being able to use it again :)

Daniel Reppel

Is there an option to doenload the 7d in 1min steps ?
like ...
https://query1.finance.yahoo.com/v8/finance/chart/^GDAXI?range=7d&interval=1m

Artem Lenskiy

@Neal, I've updated the function, should work now.

Neal Bambha

Yahoo Finance site changed today? Now returning "Service Unavailable"

Bastian Beyfuss

Holger Hoffmann

@Artem - Sorry, that was version 2016. Just updated and it works perfectly fine! Excellent - thx for providing this

Artem Lenskiy

@Holger, what version of Matlab are you using?

Holger Hoffmann

Hi,
not working for me. Trying to reproduce the example I get:
Undefined variable "matlab" or class "matlab.net.URI".

Error in getMarketDataViaYahoo (line 55)
uri = matlab.net.URI(['https://finance.yahoo.com/quote/', upper(symbol), '/history'],...

Any hint?

Howard Labido

Please check here https://www.wiscon-tech.com/system-solution/
if your data are provided by Yahoo! finance.

Paul Safier

Pavel Smith

Artem Lenskiy

@Jun thanks for the solution, I updated the repository.
@JRP, I've tried PYPL, FIT, NVAX and AdjClose is equal to Close prices, not sure what could be done at this point, since this is what Yahoo returns.
@Evgeny Gerasimov, Yahoo does not store dividends in a returned CSV file unfortunately.

Miguel Lopez

Jun

The function works well but cannot retrieve data before Jan 1st, 1970. The problem I believe is the "uint64" conversion of the start and end dates and the use of posix time. Change "uint64" to "int64" solves the problem.

Evgeny Gerasimov

Artem, hi! Thanks for your excellent work.
How to get the dividends data in the same table with OHLC prices?

JRP

On some it reports the AdjClose and on some it doesn't. It may work for CONE and it may work for AAPL but you can verify that the AdjClose price is incorrect using others -- PYPL, FIT, NVAX, SNAP. For those stocks I just listed the Close = AdjClose, which is incorrect.

marc de vernon

Checked it on CONE, a Nasdaq stock. Seems to be working fine. Thanks very much.

JRP

Something is wrong with the AdjClose price value that it pulls. I'm not sure what value that is there, but it isn't the AdjClose price at end of day. It looks like it's the Close price but still confused about that since it doesn't march Close price either.

endystrike

Works fine after your last update, thank you!

Allen Zhang

Artem Lenskiy

DavyJonesLocker, indeed seems Yahoo has changed the interface a bit. I have modified the code, it should work now.

DavyJonesLocker

anyone know why the getMarketDataViaYahoo() doesn't return data for a whole host of Tickers such as CSCO, GOOGL, CVS etc. About 50 or so out of the S&P500. There were returning data until about 5 days ago and the respective URL seems fine when pasted into a web browsers

@Artem Thanks for the function though!

Paul Bower

David Fuhrman

Rafael Rebouças

Thanks, Works fine!

Igor Kuruc

Artem Lenskiy

@Christian, @Thomas Heuser, @Xiang Chen, @Ryan Hendry, the bug has been fixed. It was caused by slightly different forms of Quandl and Yahoo data.

Artem Lenskiy

@Denys Volkov, I just tested for EURUSD=X and it did return data:
initDate = '1-Jan-2014';
symbol = 'EURUSD=X';
eurusd_yahoo_raw = getMarketDataViaYahoo(symbol, initDate);
eurusd_yahoo= timeseries([eurusd_yahoo_raw.Close, eurusd_yahoo_raw.High, eurusd_yahoo_raw.Low], datestr(eurusd_yahoo_raw(:,1).Date));
eurusd_yahoo.DataInfo.Units = 'USD';
eurusd_yahoo.Name = symbol;
eurusd_yahoo.TimeInfo.Format = "dd-mm-yyyy";

figure, plot(eurusd_yahoo);
legend({'Close', 'High', 'Low'},'Location', 'northwest');

Christian

Hi all,
I just had a look through the tool (getMarketDataViaYahoo), as I had the same problem that the last data was not available (in my case 06 May 2020). Actually the the data is available in the downloaded data. I assume there is an issue in line 130. When replacing the -2 by -1 then you get the last data line, which is the one of the current day in your table also

Thomas Heuser

Hello,

I really like the script! Very nice work.

Unfortunately I experience the same issue. In my case most recent data for ETF SPY or SXR8.DE is missing. I looked at Yahoo directly and for SPY all recent data is available (but not for SXR8.DE).

So far I add the missing data manually, but automatically by the script would be really great.

Greetings

Xiang Chen

Hi Artem, thank you for the amazing tool. It's damn helpful. I want to report the same problem mentioned below, it seems the data for the most recent trading day is always missing. It's Mar 29 today and the last trading day is Mar 27 but it excludes Mar 27 when I set the enddate to be 'today'. It would be appreciated if you could help fix it. Thanks!

YUCHU YANG

Thank you so much for this. Finally got the yahoo data.

Weidong Lin

Many thanks for the codes!

Ryan Hendry

Hi Artem, thanks for making this excellent program. I have a question, does yahoo not allow extraction of data from less than 24 hours ago? I tried to get all data from Nov 1st 2019 to today, and it did not include this friday, March 6, it stopped at March 5th.

Is there any way to fix this?

Michael Muhle

Artem, I am using Matlab ver 1209.b and am getting several error messages. Would appreciate you help on how to fix.
>> aaplusd_yahoo_raw = getMarketDataViaYahoo(symbol, initDate);
Dot indexing is not supported for variables of this type.
Error in matlab.net.http.RequestLine/set.RequestTarget (line 104)
if ~value.EncodedPath.startsWith('/')
Error in matlab.net.http.RequestLine/finish (line 197)
obj.RequestTarget = target;
Error in matlab.net.http.RequestMessage/completeInternal (line 754)
obj.RequestLine = obj.RequestLine.finish(uri,~isempty(proxyURI),obj.Method);

Error in matlab.net.http.RequestMessage/send (line 453)
[completedURI, obj] = obj.completeInternal(uri, options);
Error in getMarketDataViaYahoo (line 73)
[response, ~, ~] = requestObj.send(uri, options);

Denys Volkov

Hello Artem! Thank you very much for such useful function.
But it doesn't work for currency pairs for me. For example, i try: symbol = 'EURUSD=X'; and i have this error: Check ticker symbol and that Yahoo provides data for it

Daniel Dalla Palma

Artem, i have a problem, sometimes the code extract for the same time period, data with different length and this in a for cycle block the process of adding the last extraction in a table to the previous one. I need an advice on how to fix it in order to keep develop my portfolio optimization for the university. Tks man

Artem Lenskiy

Martin, thanks for spotting the bug. I just fixed it.

Martin Stolle

or sth. like this: if(strcmp(string(response.StatusCode), '404'))

Martin Stolle

Artem, nice job on the function (getMarketDataViaYahoo). Check Line 111, it seems the stringcompare shoud rather be strcmp(response.Statuscode, 'NotFound').

Valeri Disko

It just works. Thank you.

hossein kazemi

andreastasia

Artem Lenskiy

Afonso Moreira, hmm not sure then, I tested it once again and it worked.
Sometimes, when you copy and paste code from online or powerpoint presentations, unprintable characters are added. Please, check that the file does not have those hidden characters (e.g. \x0d). Matlab should highlight those with a red tilde symbol.

Afonso Moreira

Artem Lenskiy i am using MacOS 10.14.5 with Matlab 2019a. Tried to retrieve Apple with symbol 'AAPL'. Thanks

Artem Lenskiy

Afonso Moreira, what is your Matlab version ? What symbols did try to retrieve ?

Afonso Moreira

Has anybody got this error "Invalid character code sequence detected." when using this function?
thanks

XJ_CHEN

Finally, I can find one package working. But my problem is whether I could get different stock market indexes from this funciton?

michio

Dominic Nocon

Brook Dawit

Jesseca Nivens

Excellent! Thanks for sharing.

Artem Lenskiy

Martin, unfortunately Yahoo Finance does not provide historical data for "GC=F" gold futures. You can check it here https://finance.yahoo.com/quote/GC=F?p=GC=F as you can see there is no "historical data" tab.
I will try to spare some time in July and extend the toolbox to other data sources as well.

Martin

I tried to get gold futures: 'GC=F' symbol. I got: Dot indexing is not supported for variables of this type.
Anyway around it?

Lautaro Parada

works like a charm, thanks man!

Artem Lenskiy

b, I just tried and it worked for me, please double check that you have a stable internet connection.

b

Trying to use this example, but gives the error:
------------------------------------------------------------
Error using matlab.net.http.RequestMessage/sendOneRequest
(line 1269)
The error "Connection refused" occurred while communicating
with URL
'https://finance.yahoo.com/quote/AMD/history?period1=1558435865&period2=1558435865&interval=5d&filter=history&frequency=5d&guccounter=1'.

Error in matlab.net.http.RequestMessage/send (line 484)
obj.sendOneRequest(connector, options, [],
...

Error in getMarketDataViaYahoo (line 66)
[response, ~, ~] = requestObj.send(uri, options);

Error in getYahooFinanceData (line 4)
data = getMarketDataViaYahoo('AMD', '1-Jan-2018',
datetime('today'), '5d');

Caused by:
The error "Connection refused" occurred while
communicating with URL
'https://finance.yahoo.com/quote/AMD/history?period1=1558435865&period2=1558435865&interval=5d&filter=history&frequency=5d&guccounter=1'.
The error "Connection refused" occurred while
communicating with URL
'https://finance.yahoo.com/quote/AMD/history?period1=1558435865&period2=1558435865&interval=5d&filter=history&frequency=5d&guccounter=1'.

Error in matlab.net.http.internal.HTTPConnector/sendRequest
(line 324)
obj.ContentType = connection.ContentType; % may be
empty if no Content-Type

Error in matlab.net.http.RequestMessage/sendOneRequest (line
1138)
[~, history] = connector.sendRequest(credInfo,
history);

Error in matlab.net.http.RequestMessage/send (line 484)
obj.sendOneRequest(connector, options, [],
...

Error in getMarketDataViaYahoo (line 66)
[response, ~, ~] = requestObj.send(uri, options);

Error in getYahooFinanceData (line 4)
data = getMarketDataViaYahoo('AMD', '1-Jan-2018',
datetime('today'), '5d');

PGrant

Brandon Schleter

Easy to use. just set the function in the path, and in the command window, just simply remove the 'function' word. This seems elementary but for people like me, just make sure you are doing this. Super easy formula thank you.

Artem Lenskiy

Hi Konstantin, sorry for a late reply. I did check the code now and it still works. It might take some time to download a list of symbols though. So, please try downloading a single symbol first.

Konstantin K

I have one code stopped working?

Artem Lenskiy

Hi Jose, I've just checked the example and it work for me.
Can you please tell me what version of Matlab you are using? I am pretty sure the problem is in Matlab version. Yours, most probably does not support strings. Strings were added in 2016b. Please see https://mathworks.com/help/matlab/characters-and-strings.html

jose saballos

when I run the example to get the data from Yahoo I got the following error:
Error: File: getMarketDataViaYahoo.m Line: 63 Column: 13
Creating a string using double quotes is not supported. Use the string
function.

how can I solve this issue? Thanks

Alberto Burchi

WOW... IT WORKS!

Artem Lenskiy

@Avishek Dusoye, sorry pal but this question should be directed to Yahoo!. Please check here https://finance.yahoo.com/lookup if your symbols are provided by Yahoo! finance.

Avishek Dusoye

Looks good! I don't undertand the use of the format for the ticker e.g USD-ZAR does not work and for UK-100. Could you specfic on this please, where can i get the list of ticker?

Artem Lenskiy

@ Gian Maria Aprigliano make sure the function getMarketDataViaYahoo() is in your Matlab path i.e. either you should have it in your local folder or you should use function call addpath to add the path to getMarketDataViaYahoo() to your Matlab path, see https://au.mathworks.com/help/matlab/ref/addpath.html for details.
@ Ravosh Samari, glad you figured it out. I did no test this function on versions below 2018.
@ Stephen Forczyk, it is possible by parsing the webpage, however this is a different mechanism from what getMarketDataViaYahoo() implements. As far as I know Yahoo does not provide companies info (P/E values etc) packaged into a single file, so it will not be possible to modify getMarketDataViaYahoo().

Gian Maria Aprigliano

I still can not make it work, could you show me the script or function you use?
every time I try it tells me that I have not declared the variable:
getMarketDataViaYahoo

Ravosh Samari

Ignore the last comment, working on r2014a and r2014b introduced datetime

Ravosh Samari

datetime doesn't seem to work for me. It throws an error Undefined function 'datetime' for
input arguments of type 'char'." where ever it is used. Also the following line crumb = "\"; is no valid.

Stephen Forczyk

Is it possible to get earning per shape and P/E values from YAHOO Finance? Could your tool be modified to get this?

dan berkowitz

Brice Dupoyet

This is amazing, thank you so much for sharing your hard work, brilliant!!! :-)

Noah van Hartesveldt

Great! Thanks so much, could not figure out how to format output data on my code. Hoping to add functionality for finer granularity (<1day)

Széplaki István

Kevin Chng

It is really great. It has been headache quite long for me....Salute!

Juan Ignacio Peña

Széplaki István

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

Inspired: Yahoo! Finance Data Loader

Community Treasure Hunt

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

Start Hunting!