Skip to main content

SmartTimeSeries

A Smart TimeSeries is an expression that is used to build TimeSeries on demand

Introduction​

Smart TimeSeries are an easy but powerful way of defining TimeSeries derivations.

A Smart TimeSeries consists of the following:

Base TimeSeries​

The base TimeSeries is mandatory and can be either a real TimeSeries or another Smart TimeSeries. The base TimeSeries is usually used in the expression using the variable name BASE.

Expression​

The expression is also mandatory and is a string formula used to build the TimeSeries.

  • It can be a simple operation like BASE*1.2
  • It can use built-in functions like scale(BASE, 'MONTHLY', 'summed')
  • It can use functions in the #TimeSeriesScript (if you have not specified a custom script) like fill(BASE, 'LINEAR')
  • It can use your own functions in a specified custom script like myfunction(BASE)

Script​

The script is an optional saved custom script with user defined functions. These functions can then be used in the expression. The function script can also import the default #TimeSeriesScript, so your functions can use those too.

References to other TimeSeries​

You can optionally add references to other TimeSeries or Smart TimeSeries that can also be used in your expression

Construction​

You construct a Smart TimeSeries using one of the following constructors:

// Specify base TimeSeries id and expression
st = SmartTimeSeries("CORN:CLOSE", "fill(BASE,'LINEAR')")

// Specify a start date, calendar and expression
// This can be used to create a TimeSeries that has the same scalar value from the start date till now
// Or you can use an expression to build a TimeSeries
st = SmartTimeSeries("2022-01-01", "MONTHLY", "12.5")

Properties​

A SmartTimeSeries has the following properties:

NameDescriptionType
baseTimeSeriesThe TimeSeries that this Smart TimeSeries uses for a baseID String, TimeSeries or Reference
expressionThe expression used to generate the TimeSeriesString
scriptOptional custom script name used with the expressionString
currencyOptional currency of the TimeSeries, defaults to base TimeSeries currencyString
unitsOptional units of the TimeSeries, defaults to base TimeSeries unitsString
calendarThe calendar that this timeseries usesCalendar
descriptionThe description of this timeseriesString
identityThe configured identitiesObject
localValuesThe list of time observations for this timeseries, returned as a list of TimeValues in the local timezone of this TimeSeriesList
nameThe name of the timeseriesString
observationsThe list of values for this timeseries, returned as a list of VarsList
observedThe observed setting of the timeseries used for aggregatingTimeSeriesObserved
precisionThe precision configuration for this timeseriesPrecision
sourceThe source of this timeseriesString
startThe starting date/time of the timeseriesDate
tenorThe tenor of this timeseriesString
timezoneThe timezone of this timeseries, defaults to UTCString
valueTypeThe value type of the timeseries (TRACKED or BASIC)TimeValueType
valuesThe list of time observations for this timeseries, returned as a list of TimeValuesList
info

If a currency and/or units are set, the base TimeSeries and any other TimeSeries added to this Smart TimeSeries will be converted to the specified currency and units

A SmartTimeSeries also supports adding dynamic properties, e.g.

sc = SmartTimeSeries("SMART_TEST:CLOSE", "BASE*1.02")
sc.product = "CORN"

Any dynamic properties added to a SmartTimeSeries can be used in the expression including references to other TimeSeries. Here are some examples of using properties within the Smart TimeSeries expression:

// Example using another TimeSeries
st1 = SmartTimeSeries("TEST_SMART_TS:ASK", "(BASE+BID)/2")
st1.BID = ref("data","TEST_SMART_TS:BID")

// Example using a numeric factor
st2 = SmartTimeSeries("TEST_SMART_TS:CLOSE", "BASE*factor")
st2.factor=1.2

Methods​

A SmartTimeSeries has the following methods:

NameDescriptionReturn Type
build()Builds a TimeSeriesTimeSeries
addCheck(check)Add a quality check directly to this TimeseriesTimeSeries
addValue(value)Adds a new value to the end of the timeseries using the calendarTimeSeries
applyPrecision()Apply the precision defined to all the observations of this timeseriesVoid
convert(currency, units)Convert this timeseries using currency and unitsTimeSeries
from(start)Creates a new TimeSeries from a specified start dateTimeSeries
getLast(date)Get the value at the supplied date or the latest value before that dateVar
getLastNValues(n)Returns a list of the last N values from this TimeSeriesList
last(n)Creates a new TimeSeries using the last n values from this TimeSeriesTimeSeries
range(start, end)Creates a new TimeSeries from a range of values from this TimeSeriesTimeSeries
withRange(start, end)Changes this timeseries to match the provided date rangeVoid

Below is an example of creating a Smart TimeSeries and building a TimeSeries using it:

st1 = SmartTimeSeries("TEST_SMART_TS:CLOSE", "BASE*factor)
st1.factor=1.2

t1 = st1.build()
print t1.values

Adding to an object and saving​

A SmartTimeSeries needs to be added to an object in order to save it to the database, e.g.

CORN_TEST = Object()
CORN_TEST.category = "TEST"

cf = SmartTimeSeries("TEST_SMART_TS:CLOSE", "interpolate(BASE,'LINEAR')")
CORN_TEST.FILLED = cf

save ${object: CORN_TEST}