Main Content

fetch

Request data from Haver Analytics database

Description

data = fetch(c,variable) returns historical data for the specified Haver Analytics® variable, where c is a haver, haverdirect, or haverview connection object.

example

data = fetch(c,variable,startdate,enddate) returns historical data between the dates startdate and enddate, where c is a haver, haverdirect, or haverview connection object.

example

data = fetch(c,variable,startdate,enddate,period) returns historical data in time periods specified by period, where c is either a haver or a haverdirect connection object.

example

data = fetch(c,variable,startdate,enddate,haverParams) returns historical data between the dates startdate and enddate, where haverParams is an optional comma-separated list of HaverView™ API flags and c is a haverview connection object. For more information, see the HaverView API documentation on the Haver Analytics website.

example

[data,response] = fetch(c,___) also returns a response message, where c is a haverview connection object.

example

Examples

collapse all

Connect to a Haver Analytics database.

c = haver('c:\work\haver\usecon.dat');

Retrieve all historical data for the Haver Analytics variable FFED. The descriptor for this variable is Federal Funds [Effective] Rate (% p.a.).

variable = "FFED"; % return data for FFED

data = fetch(c,variable);

Display the first three rows of data.

data(1:3,:)
ans =

     715511.00          2.38
     715512.00          2.50
     715515.00          2.50

data contains the numeric representation of the date in the first column and the closing value in the second column.

Close the Haver Analytics connection.

close(c)

Create a DLX Direct connection by using the haverdirect function and specifying the Haver Analytics database that you want to connect to. In this example, replace USECON with the name of your database.

databasename = "USECON";
c = haverdirect(databasename)
c = 

  haverdirect with properties:

        DatabaseName: "USECON"
        DatetimeType: ''
    DataReturnFormat: ''

Retrieve historical data from January 1, 2005, through December 31, 2005, for the FFED variable and display the first three rows.

variable = "FFED"; 
startdate = "01/01/2005"; 
enddate = "12/31/2005"; 
  
data = fetch(c,variable,startdate,enddate);

data(1:3,:)   
ans =

     732343.00          2.28
     732371.00          2.50
     732402.00          2.63

Connect to the Haver Analytics database.

c = haver('c:\work\haver\usecon.dat');

Retrieve the information of the Haver Analytics variable FFED. The descriptor for this variable is Federal Funds [Effective] Rate (% p.a.).

variable = "FFED";

x = info(c,variable); 

info returns the structure x containing fields describing the Haver Analytics variable.

Retrieve quarterly data. When you specify a date that is outside the date range in the variable, you might experience unexpected results. To prevent this outcome, use the EndDate field for the end of the date range.

startdate = '06/01/2000';  % start of date range
enddate = x.EndDate;       % end of date range
period = 'q';              % quarterly data

data = fetch(c,variable,startdate,enddate,period)

Display the first three rows of data.

data(1:3,:)
ans =

     730759.00          6.52
     730851.00          6.50
     730941.00          5.61

data contains the numeric representation of the date in the first column and the closing value in the second column.

Close the Haver Analytics connection.

close(c)

Connect to the Haver Analytics database.

c = haver('c:\work\haver\usecon.dat');

Adjust the display format for currency.

format bank

Set the data return format to table using the DataReturnFormat property of the haver object. Also, set the date and time return format to a datetime array using the DatetimeType property.

c.DataReturnFormat = 'table';
c.DatetimeType = 'datetime';

Retrieve historical data from January 1, 2005, through December 31, 2005, for ABQ and display the results. ABQ provides bankruptcy filings in the US.

variable = "ABQ"; % return data for ABQ
startdate = '01/01/2005'; % start of date range
enddate = '12/31/2005';   % end of date range

data = fetch(c,variable,startdate,enddate)
ans =

  4×2 table

            Time            TotalBankruptcyFilings_U_S__Units_
    ____________________    __________________________________

    31-Mar-2005 00:00:00                401149.00             
    30-Jun-2005 00:00:00                467333.00             
    30-Sep-2005 00:00:00                542002.00             
    31-Dec-2005 00:00:00                667431.00             

data is a table with the date and time in the first variable and the bankruptcy amount in the second variable. The first variable is a datetime array.

Close the Haver Analytics connection.

close(c)

Create a HaverView connection using a valid authentication token and database name. In this example, replace 681b61cf-dc7d-4698-a168-74c79b0c1a04 with your authentication token and replace USECON with the name of your database.

token = "681b61cf-dc7d-4698-a168-74c79b0c1a04";
databasename = "USECON";
c = haverview(token,databasename)
c = 
    
  haverview with properties:
    
    DatabaseName: "USECON"
         TimeOut: 200.00

Retrieve time series data by specifying two HaverView API flags as name-value arguments. This example retrieves data for the variable named AB with flags set for annual aggregation and percentage period.

variable = "AB";
startdate = datetime("01-Jan-2015");
enddate = datetime("01-Jan-2021");
data = fetch(c,variable,startdate,enddate,"aggregation"=10,"function"="DIFF%")
data =
    
  7x1 timetable
    
       Time        nSeriesData
    ___________    ___________
    
    01-Jan-2015       0.90    
    01-Jan-2016       1.02    
    01-Jan-2017       0.92    
    01-Jan-2018       0.85    
    01-Jan-2019       0.98    
    01-Jan-2020       1.02    
    01-Jan-2021       0.85

Create a HaverView connection using a valid authentication token and database name. In this example, replace 681b61cf-dc7d-4698-a168-74c79b0c1a04 with your authentication token and replace USECON with the name of your database.

token = "681b61cf-dc7d-4698-a168-74c79b0c1a04";
databasename = "USECON";
c = haverview(token,databasename)
c = 
    
  haverview with properties:
    
    DatabaseName: "USECON"
         TimeOut: 200.00

Retrieve time series data and response message by specifying two Haver API flags as name-value arguments. This example retrieves data for a variable named "AB" with flags set for annual aggregation and percentage period.

variable = "AB";
startdate = datetime("01-Jan-2015");
enddate = datetime("01-Jan-2021");
[data.response] = fetch(c,variable,startdate,enddate,"aggregation"=10,"function"="DIFF%")
data =
    
  7x1 timetable
    
       Time        nSeriesData
    ___________    ___________
    
    01-Jan-2015       0.90    
    01-Jan-2016       1.02    
    01-Jan-2017       0.92    
    01-Jan-2018       0.85    
    01-Jan-2019       0.98    
    01-Jan-2020       1.02    
    01-Jan-2021       0.85

response = 
    
  ResponseMessage with properties:
    
    StatusLine: 'HTTP/1.1 200 OK'
    StatusCode: OK
        Header: [1x12 matlab.net.http.HeaderField]
          Body: [1x1 matlab.net.http.MessageBody]
     Completed: 0

Input Arguments

collapse all

Haver Analytics connection, specified as the following connection objects:

  • haver, haverdirect, or haverview in the first two syntaxes

  • haver or haverdirect in the third syntax

  • haverview in the fourth and fifth syntaxes

Haver Analytics variable, specified as a character vector, string scalar, cell array of character vectors, or string array to denote which historical data to retrieve. For information about the variables that you can use, see the API documentation on the Haver Analytics website.

Example: "FFED"

Data Types: char | string | cell

Start date of the date range to retrieve data, specified as a character vector, string scalar, or MATLAB date number for haver and haverdirect input objects. You can also specify the start date as a datetime for haverview input objects.

Data Types: double | char | string | datetime

End date of the date range to retrieve data, specified as a character vector, string scalar, or MATLAB date number for haver and haverdirect input objects. You can also specify the end date as a datetime for haverview input objects.

Data Types: double | char | string | datetime

Period, specified as one of these values that denotes the time period for the historical data:

  • 'd' for daily values

  • 'w' for weekly values

  • 'm' for monthly values

  • 'q' for quarterly values

  • 'a' for annual values

HaverView API arguments, specified as a comma-separated list. For more information, see the HaverView API documentation on the Haver Analytics website.

Output Arguments

collapse all

Historical data, returned as a matrix with the numeric representation of the date in the first column and the value in the second column.

HTTP request response, returned as a ResponseMessage object. For details, see matlab.net.http.ResponseMessage.

Version History

Introduced in R2007a