Code covered by the BSD License  

Highlights from
dowload FRED data and US Constant Maturity Treasury Rates

dowload FRED data and US Constant Maturity Treasury Rates

by

 

dowload FRED data and US Constant Maturity Treasury Rates

data_FRED=func_download_FRED_DATA(FRED_INPUTS,JOIN_TYPE)
function data_FRED=func_download_FRED_DATA(FRED_INPUTS,JOIN_TYPE)

%DESCRIPTION: the function returns the dataset containing the inner join of
%             the time series indicated by the user through the FRED IDs 
%             contained in the  FRED_INPUTS variable
% 
%INPUTS: FRED_INPUTS is and Nx2 cell array containing:
%           a) the FRED time series IDs in column 1 and
%           b) the new time series names in column 2
%        Example:
%                 FRED_INPUTS={  'BAMLC1A0C13Y',   'IGOAS_1_3';		
%                                'BAMLC2A0C35Y',   'IGOAS_3_5';
%                                'BAMLC3A0C57Y',   'IGOAS_5_7';
%                                'BAMLC4A0C710Y',  'IGOAS_7_10';	
%                                'BAMLC7A0C1015Y', 'IGOAS_10_15';
%                                'BAMLC8A0C15PY',  'IGOAS_15_100'};
%                 JOIN_TYPE='inner'
%
%OUTPUTS: data_FRED is the output organized as a dataset.
%         The output is an inner or an outer (default) join over the Date variable of all the selected time series
%
%  Please note that the downloaded data is subject to the Federal Reserve Bank 
%  of St. Louis  terms and conditions(http://research.stlouisfed.org/legal.html)
%
% Copyright (c) 2013, Oana Alexandra Damian. All rights reserved.
% 
% Redistribution and use in source or in binary forms, with or without modification, are permitted provided that the redistributions of source 
% code or of binary form must retain the above copyright notice and the following disclaimer in the source code and in documentation and/or 
% other materials provided with the distribution.
%
% DISCLAIMER
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.


if numel(FRED_INPUTS(1,1,:))==1 
   FRED_INPUTS(:,2)=FRED_INPUTS(:,1); 
end    
    
 if nargin<=1
     JOIN_TYPE='outer';
 else
     if sum(ismember({JOIN_TYPE},{'inner' 'outer'}))==0
         JOIN_TYPE='outer';
     end
 end   
fprintf('\n\n\n=========================================')       
fprintf('\n FRED extraction started')   
data_FRED=dataset();
for idx_fred=1:numel(FRED_INPUTS(:,1))
    fprintf('\n  %s',FRED_INPUTS{idx_fred,1})
    data_Temp=dataset();
    FRED_file=strcat('http://research.stlouisfed.org/fred2/data/',FRED_INPUTS{idx_fred,1},'.txt');
    FRED_file_temporary=strcat(FRED_INPUTS{idx_fred,1},'.txt');
    [filestr,status] = urlwrite(FRED_file,FRED_file_temporary);
    if status==1
        FID=fopen(FRED_file_temporary);
        xxx=textscan(FID,'%s   %s');
        POZ_SERIES_START=find(ismember(xxx{1,2},{'VALUE'})==1);
        data_Temp.Date=xxx{1,1}(POZ_SERIES_START+1:end);
        eval(strcat('data_Temp.',FRED_INPUTS{idx_fred,2},'=xxx{1,2}(POZ_SERIES_START+1:end);'))
        export(data_Temp,'file','TEMP_FRED.txt','delimiter',',');
        data_Temp=dataset('file','TEMP_FRED.txt','delimiter',',');
        fclose all;
        if idx_fred==1
            data_FRED=data_Temp;
        else
            data_FRED=join(data_FRED,data_Temp,'type',JOIN_TYPE,'Keys',{'Date'});
            poz=ismember(data_FRED.Properties.VarNames,{'Date_right'});
            data_FRED=data_FRED(:,not(poz));
            data_FRED.Properties.VarNames{1}='Date';
        end
    else
        fprintf('/n %s not accesible at %s',FRED_INPUTS{idx_fred,1},FRED_file)
    end

end
 data_FRED.Date=datenum(data_FRED.Date,'yyyy-mm-dd');
fprintf('\n FRED extraction finished')
fprintf('\n=========================================\n\n')    

Contact us