Quandl API MATLAB issues

조회 수: 4 (최근 30일)
Davin
Davin 2014년 10월 1일
답변: Davin 2014년 10월 1일
Hello,
I would greatly appreciate to have some feedback of users of the QUANDL data source linked with MATLAB.
I am trying pull out data from Quandl into Matlab. Quandl do have an API available to be used in Matlab.
so my code is the following :
series = {500 stocks ticker}
series1 = {100 stocks ticker}
if selectedItem == 1 && selectedItem1 == 1 && selectedItem3 == 1 SD = sprintf('%02d-%02d-%04d',SMM,SDD, SYYYY) ED = sprintf('%02d-%02d-%04d',EMM,EDD, EYYYY)
for i = 1:numel(series)
for j = 1:numel(series1)
if strcmp(Tick,series{i}) && strcmp(Tick, series1{j})
Source = strcat('GOOG/NASDAQ_', Tick)
Quandl = Quandl.get(Source, 'trim_start', SD, 'trim_end', ED, 'authcode', 'jtwf2XzCdzxhAKrXxNkm')
end
end
seriesinter = ismember(series,series1);
series(~seriesinter);
seriesnyse = series(~seriesinter);
for h = 1:numel(seriesnyse)
if strcmp(Tick,seriesnyse{h})
Source = strcat('GOOG/NYSE_', Tick)
Quandl = Quandl.get(Source, 'trim_start', SD, 'trim_end', ED, 'authcode', 'jtwf2XzCdzxhAKrXxNkm')
end
end
end
When the ticker is a technological stocks, its entering the first loop, and its connected to Quandl and retrieving the data.
But whenever I call for a non technological stocks, it goes in the second loop, but it just cant retrieve the data, it gives me the following error :
Undefined variable Quandl.
Error in Equities>pushbutton2_Callback (line 919) Quandl = Quandl.get(Source, 'trim_start', SD, 'trim_end', ED, 'authcode', 'jtwf2XzCdzxhAKrXxNkm')
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in Equities (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)Equities('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
which means its recognising the Quandl.get, I dont understand at all why its working in the first loop and not in the second one... When i do a step by step and I put a breakpoint before Quandl in the 2 loop, all the parameters to be used in the API function is good and when I execute it manually in MATLAB it works, but when its in the code, i get this error...
any suggestions?
Thank you very much
Davin.

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 10월 1일
편집: Geoff Hayes 2014년 10월 1일
Davin - you seem to have three cell arrays of stocks. The set of all 500 stocks in series, the set of 100 NASDAQ stocks in series1, and the set of 400 NYSE stocks in seriesnyse.
The code then loops over all of the 500 stocks of series, and then has two inner loops - the first to loop over all NASDAQ stocks and the second to loop over all NYSE stocks. This checking with the two inner loops is repeated 500 times even once we have read the stock data. Is this the intent, especially as Tick, which is the stock you wish to get data from, doesn't ever change?
It may be easier to avoid the looping and just use find to see if Tick is in which cell array. This will greatly simplify the code and hopefully make it easier to debug because it isn't all that clear why the above code would fail with that error. Try the following
if selectedItem == 1 && selectedItem1 == 1 && selectedItem3 == 1
SD = sprintf('%02d-%02d-%04d',SMM,SDD, SYYYY);
ED = sprintf('%02d-%02d-%04d',EMM,EDD, EYYYY);
% use find to return the index of Tick within the cell array
idxSeries = find(strcmp(series,Tick));
idxSeries1 = find(strcmp(series1,Tick));
stockData = [];
if ~isempty(idxSeries) && ~isempty(idxSeries1)
Source = strcat('GOOG/NASDAQ_', Tick);
stockData = Quandl.get(Source, 'trim_start', SD, 'trim_end', ED, 'authcode', ...
'jtwf2XzCdzxhAKrXxNkm');
else
seriesinter = ismember(series,series1);
seriesnyse = series(~seriesinter);
idxSeriesNyse = find(strcmp(seriesnyse,Tick));
if ~isempty(idxSeriesNyse)
Source = strcat('GOOG/NYSE_', Tick);
stockData = Quandl.get(Source, 'trim_start', SD, 'trim_end', ED, 'authcode', ...
'jtwf2XzCdzxhAKrXxNkm');
end
end
if ~isempty(stockData)
% do stuff with stock data
end
end
Note how we avoid the looping and so once we've found our stocks, then we are finished.
When it came to initializing the data/output from the Quandl.get call, I replaced your local variable of Quandl with stockData because you should try and avoid naming your variables with names that already exist for some other purpose. If I tried the following lines of code
Quandl = Quandl.get('NSE/OIL');
Quandl = Quandl.get('NSE/OIL');
the first call would work, but the second would fail with the Attempt to reference field of non-structure array error. This makes sense because Quandl is now a local variable and not the package.
I tried the above code with a very simple set of data
series = {'AAON','AAPL','ARP'};
series1 = {'AAON','AAPL'};
with Tick alternating between 'ARP' and 'AAPL'.

추가 답변 (1개)

Davin
Davin 2014년 10월 1일
Thanks very much Geoff, indeed, I was trying to make a Union between the series because the technological stocks in SP 500 are common to the NASDAQ then pick the NASDAQ source and create another series, seriesinter for the NYSE source. I agree there is 2 loops which can be skipped. I didn't know the syntax find, which makes the code much 'lighter'.
Concerning the Quandl, you are right, using it more than 2 times and with same names, can create some issues.
Thank you very much for the precious help.
Davin.
P.s : your modification does the job. )

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by