File Exchange

image thumbnail

Yahoo Finance and Quandl data downloader

version 1.01 (569 KB) by Artem Lenskiy
Yahoo Finance and Quandl data downloader

136 Downloads

Updated 16 May 2020

GitHub 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 (2020). Yahoo Finance and Quandl data downloader (https://www.github.com/Lenskiy/market-data-functions), GitHub. Retrieved .

Comments and Ratings (54)

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

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?

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);

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

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.

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

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

It just works. Thank you.

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.

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 ?

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

Brook Dawit

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?

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

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.

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

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

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.

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().

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

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

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.

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

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

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

Kevin Chng

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

Updates

1.01

Thanks to Christian, Xiang Chen Fixed, Ryan Hendry for pointing out the bug. The most recent sample was not loaded. The problem was in two slightly different formats data is returned by Quandl and Yahoo. The bug is fixed.

0.934

Fixed a bug

0.933

.

0.932

Fixed a bug in getMarketDataViaQuandl()

0.931

.

0.93

fixed bug in getMarketDataViaQuandl()

0.92

Added Quandl data downloader

0.91

404 error has been dealt with

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

Inspired: Yahoo! Finance Data Loader