TimeSeriesClient | R Documentation |
This is the client class that manages the connection to the API server on your behalf. It allows you to query for all your timeseries and to create/modify new timeseries.
Methods Supported
GetAllItems : Allows you to query for all the current timeseries available for your use.
GetItem : Allows you to download the details of a specific timeseries item.
GetTimeseriesDateRange : Allows you to determine the supported timeseries dates between supplied start and
end dates at a specified frequency.
CreateItem : Allows you to create a new timeseries item with up to 130 years of daily data.
UpdateItem : Allows you to update an existing timeseries.
DeleteItem : Allows you to delete an existing timeseries.
TimeSeriesClient object
DatastreamR::DSConnect
-> TimeSeriesClient
useNaNforNotANumber
If Enabled, NaN is appears in output response instead of NULL
TimeseriesResponseType
Response type
new()
User details can be supplied from a config file or passed directly as parameters in the constructor of the derived user object type class. (See the DSConnect superclass for a description of the connection parameters required)
TimeSeriesClient$new( config = NULL, username = NULL, password = NULL, proxies = NULL, sslCer = NULL )
config
Configuration File path
username
Your Datastream Id
password
Your Password
proxies
Proxies if any
sslCer
Path to CA bundle certificates file
Timeseries Properties:
useNaNforNotANumber : Non-trading days are stored as double NaNs on Datastream, JSON protocol permits NaNs as valid numbers.
Thus, all the NULLs in the converted to NaNs in the JSON requests. Responses contain the NULLs, But this should be converted
to Nans for Plotting purposes. If you want to receive NaN float values, set useNaNforNotANumber to TRUE, any NULLs in the returned
array of float values will be converted to NaNs.
TimeSeriesClient object
.checkValidTimeseriesId()
A helper method to check the timeseries Id
TimeSeriesClient$.checkValidTimeseriesId(inputId)
inputId
: Timeseries Id
NULL if Timeseries id is valid else error string
.checkTimeSeriesReqValidity()
A helper method to check some of the mandatory fields of timeseries for its validity
TimeSeriesClient$.checkTimeSeriesReqValidity(tsItem)
tsItem
Timeseries Item
NULL if Timeseries id is valid else error string
.checkKeyTimeseriesProperties()
A helper method to check the Timeseries properties
TimeSeriesClient$.checkKeyTimeseriesProperties(tsItem)
tsItem
Timeseries Item
NULL if Timeseries id is valid else error string
.asGetAllResponse()
A helper method which converts the JSON response to GetAllResponse Object
TimeSeriesClient$.asGetAllResponse(jsonDict)
jsonDict
JSON Response
DSUserObjectGetAllResponse object
.asGetResponse()
A helper method which converts the JSON response to GetResponse Object
TimeSeriesClient$.asGetResponse(jsonDict)
jsonDict
JSON Response
DSUserObjectResponse object
.jsonRequestEncoder()
A helper method that reformats the raw request to JSON format
TimeSeriesClient$.jsonRequestEncoder(request)
request
Raw request
return JSON formatted list
.jsonResponseDecoder()
A helper method that converts JSON Response to a given class response type
TimeSeriesClient$.jsonResponseDecoder(jsonResp, responseType)
jsonResp
JSON Response
responseType
GetResponse or GetAllResponse type
return DSUserObjectGetAllResponse or DSUserObjectGetResponse object
GetAllItems()
This method returns all the current timeseries you can use in Datastream queries.
TimeSeriesClient$GetAllItems()
DSUserObjectGetAllResponse object
GetItem()
GetItem returns the details for an individual timeseries.
TimeSeriesClient$GetItem(itemId)
itemId
: a valid timeseries Id.
DSUserObjectResponse object
CreateItem()
This method attempts to create the given DSTimeSeriesRequestObject via the API service
TimeSeriesClient$CreateItem(newItem, overWrite = FALSE, skipItemReturn = FALSE)
newItem
A DSTimeSeriesRequestObject containing the data used for creating the Timeseries.
overWrite
If the given Timeseries Id already exists on the system, the create call will be rejected. Set overWrite = True to overwrite the existing item with new Timeseries.
skipItemReturn
: Upon successful creation of an item, the server requests the new item from the mainframe and returns it in the response object. For faster processing, set skipItemReturn = True to skip returning the object in the response
DSUserObjectResponse object
UpdateItem()
This method attempts to modify a timeseries item using the given DSTimeSeriesRequestObject via the API service
TimeSeriesClient$UpdateItem(item, skipItemReturn = FALSE)
item
A DSTimeSeriesRequestObject containing the data used for creating the Timeseries.
skipItemReturn
Upon successful creation of an item, the server requests the new item from the mainframe and returns it in the response object. For faster processing, set skipItemReturn = True to skip returning the object in the response.
DSUserObjectResponse object
DeleteItem()
DeleteItem allows you to delete an existing timeseries
TimeSeriesClient$DeleteItem(itemId)
itemId
a valid timeseries Id.
No return value
GetTimeseriesDateRange()
This method allows you to determine the supported dates between supplied start and end dates at a specified frequency.
TimeSeriesClient$GetTimeseriesDateRange( startDate, endDate, frequency = DSUserObjectFrequency$Daily )
startDate
A date specifying the beginning of the date range
endDate
A date specifying the end of the date range
frequency
A DSUserObjectFrequency enumeration defining if the frequency should be daily, weekly, monthly, quarterly or yearly.
DSTimeSeriesDateRangeResponse object
clone()
The objects of this class are cloneable with this method.
TimeSeriesClient$clone(deep = FALSE)
deep
Whether to make a deep clone.
: You need a Datastream ID which is permissioned to access the Datastream APIs. In addition, this ID also needs to be permissioned to access the custom user object service. Attempting to access this service without these permissions will result in a permission denied error response.
: For Daily and Weekly frequencies, if the supplied startDate falls on a weekend or a trading holiday, the returned
starting date will be the first trading day before the given start date. If the supplied endDate falls on a weekend or a
trading holiday, the returned final date will be the last trading day before the given end date. For Weekly frequencies,
this will be the last date which matches the day of the week for the first returned start date.
For Monthly, Quarterly and Yearly frequencies, the returned dates are always the 1st day of each month, quarter or year.
The returned start and end dates are always the 1st days of the requested month, quarter or year that the given start
and end dates fall within.
: For Daily and Weekly frequencies, if the supplied startDate falls on a weekend or a trading holiday, the returned
starting date will be the first trading day before the given start date. If the supplied endDate falls on a weekend or a
trading holiday, the returned final date will be the last trading day before the given end date. For Weekly frequencies,
this will be the last date which matches the day of the week for the first returned start date.
For Monthly, Quarterly and Yearly frequencies, the returned dates are always the 1st day of each month, quarter or year.
The returned start and end dates are always the 1st days of the requested month, quarter or year that the given start
and end dates fall within.
{
# first logon with your credentials.
# Creating a TimeSeriesClient instance with your credentials
# automatically logs on for you.
timeseriesClient = TimeSeriesClient$new(NULL, 'YourID', 'YourPwd')
# query for all your current timeseries items
itemsResp = timeseriesClient$GetAllItems()
if (!is.null(itemsResp))
{
if (itemsResp$ResponseStatus != DSUserObjectResponseStatus$UserObjectSuccess)
{
# Your Datastream Id might not be permissioned for managing
# user created items on this API
print(paste('GetAllItems failed with error ',
names(DSUserObjectResponseStatus)[[itemsResp$ResponseStatus + 1]],
': ', itemsResp$ErrorMessage))
}
else if (!is.null(itemsResp$UserObjects) & itemsResp$UserObjectsCount > 0)
{
# You do have access to some timeseries
# Here we just put the timeseries details into a dataframe and list them
print(paste('GetAllItems returned', itemsResp$UserObjectsCount, 'timeseries items.'))
df = data.frame()
for (tsItem in itemsResp$UserObjects)
{
if (!is.null(tsItem))
{
rowdata = list(Id = tsItem$Id,
LastModified = tsItem$LastModified,
StartDate = ifelse(!is.null(tsItem$DateInfo),as.character(tsItem$DateInfo$StartDate),""),
EndDate =ifelse(!is.null(tsItem$DateInfo),as.character(tsItem$DateInfo$EndDate), ""),
Frequency = ifelse(!is.null(tsItem$DateInfo), tsItem$DateInfo$Frequency, 0),
NoOfValues = ifelse(!is.null(tsItem$DateRange), tsItem$DateRange$ValuesCount , 0),
Desc = tsItem$Description)
df = rbind(df, rowdata)
}
}
print(df)
}
}
#Example to show how to GetItem
# query for a specific timeseries
tsName = 'TSZZZ001'
tsResponse = timeseriesClient$GetItem(tsName)
# You may want to put the timeseries request response handling into a common function.
if (!is.null(tsResponse))
{
# Any request dealing with a single user created item returns a DSUserObjectResponse.
# This has ResponseStatus property that indicates success or failure
if (tsResponse$ResponseStatus != DSUserObjectResponseStatus$UserObjectSuccess)
{
print(paste('Request failed for timeseries', tsName, 'with error',
names(DSUserObjectResponseStatus)[[tsResponse$ResponseStatus+1]],
':', tsResponse$ErrorMessage))
}
else if (!is.null(tsResponse$UserObject))
{
# The timeseries item won't be returned if you set SkipItem true
# in CreateItem or UpdateItem
# Here we simply display the timeseries data using a dataframe.
tsItem = tsResponse$UserObject
metadata = c (Id = tsItem$Id,
Desc = tsItem$Description,
LastModified = as.character(tsItem$LastModified),
StartDate = ifelse (!is.null(tsItem$DateInfo), as.character(tsItem$DateInfo$StartDate), NULL),
EndDate = ifelse(!is.null(tsItem$DateInfo),as.character(tsItem$DateInfo$EndDate), NULL),
Frequency = ifelse(!is.null(tsItem$DateInfo),
names(DSUserObjectFrequency)[[tsItem$DateInfo$Frequency + 1]], NULL),
NoOfValues = ifelse(!is.null(tsItem$DateRange), tsItem$DateRange$ValuesCount , 0))
df = data.frame(metadata)
print(df)
if (!is.null(tsItem$DateRange))
{
df = data.frame(Dates = sapply(tsItem$DateRange$Dates,
FUN = function(x){ return (as.character(x)) }),
Values = sapply(tsItem$DateRange$Values,
FUN = function(x){ ifelse (is.null(x),
return (NA_character_ ), return (x) )} ))
# Values if NULL, is printed as <NA> because, while
# convertind list to vector either by using as.vector or sapply,
# the NULL values in the list are deleted. and thus there will
# be mismatch in no of rows and cannot be put in a dataframe
print(df)
}
}
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.