make_timer: Create an asset timer from an indicator.

Description Usage Arguments Details Value See Also Examples

Description

Generate an asset timer based on a previously generated indicator and either an asset return series or price series.

Usage

1
2
3
make_timer(data, cols = c("rets", "indicator"), retval = "timedrets",
  thresh = 1, mult = c(1, 0), on = "days", offset = 0,
  seriestype = "rets", vote = NULL)

Arguments

data

An xts series containing an indicator column and either a prices column or a returns column.

cols

A length 2 vector specifying the column names for the returns (or prices) and the indicator respectively. The returns, if provided, are assumed to be simple (discrete) returns.

retval

Specifies which column(s) are included in the returned xts matrix. This is a vector containing the desired column names to subset. Valid strings include: c('rets', 'timedrets', 'timer', 'timerlag', 'ec', 'timedec'), with the following effects. Default value is 'timedrets'.

  • 'rets' and 'timedrets': The discrete returns from the asset (not lagged), and the timed version, lagged by one period.

  • 'timer' and 'timerlag': The binary timers, with values as specified by the mult parameter. For example, if mult = c(1,0) then the timer value is either 1 (above threshold) or 0 (less or equal to the threshold). 'timerlag' is the lagged version of the timer (by a single period).

  • 'ec' and 'timedec': The equity curve, ignoring any leading NAs. 'ec' is the equity curve from 'rets', and thus without the timer. 'timedec' is the equity curve from 'timedrets', based on the 1 period lagged timer.

thresh

The numeric threshold of the indicator value used to time the asset. Default is 0. See details.

mult

A length 2 numeric vector. The first value is the multiplier used when the indicator is ABOVE the threshold, otherwise the second value is used. Default c(1, 0).

on

The sampling period at which to compute the timer. This value is passed to endpoints(). Typical values include 'days', 'weeks', 'months', 'quarters' and 'years'.

offset

The number of periods to add to offset the sampling dates. Default is 0.

seriestype

Specifies the type of asset series provided to generate the timer. Can be either 'rets', for discrete asset returns, or 'prices' for asset prices. Default is 'rets'.

vote

A length 2 numeric vector or NULL (default). This is used to provide additional filtering of the internal timer via a form of hysteresis. It assumes that mult = c(1,0), the default, otherwise some unpredictable results may occur. The internal timer is examined via a rolling window of size sum(vote) + 1. The number of 1s are counted within the window. If the count is > vote[1], then the timer is declared to be one, otherwise it is zero. For example, and given that mult = c(1,0), then the internal timer is 1 when above the threshold, and 0 otherwise. If vote = c(15,5), then a rolling window of 15 + 5 + 1 = 21 is applied (the +1 needed to have a minimum window size of 1). If at least 15 observations of the internal timer are 1, then the timer used will be one. Otherwise, it will be 0, which is equivalent to at least 5 observations being zero. If NULL, then no rolling vote is applied and the timer returned is identical to the internal timer.

Details

An xts matrix (data) is provided containing a column of asset returns (or prices) and another column with an indicator of numeric or logical class. The function examines each indicator value and compares it to the threshold 'thresh' to create a series of 'timed returns'. If the indicator value is ABOVE the threshold, the timed return is the asset return multiplied by the first multiplier value (default 1). Otherwise, the timed return is the asset return multiplied by the second multiplier value (default 0). See examples.

Value

An xts matrix with columns as specified by parameter retval.

See Also

endpoints()

Examples

1
2
3
4
5
6
7
8
data <- xts_data[,1]
data$rets <- ROC(data[,1], type="discrete")
data$indicator <- funapply(data$rets, FUN=mean, windows=200)
x <- make_timer(data)
y <- make_timer(data, mult = c(1, 0), retval = c('timedrets', 'timer', 'ec', 'timedec'))
z <- cbind(data, y)
z["2014-12", ]
xtsplot(z[, c('ec', 'indicator_timedec')], main = "make_timer() Example")

jeanmarcgp/xtsanalytics documentation built on May 19, 2019, 12:38 a.m.