add.indicator: add an indicator to a strategy

Description Usage Arguments Details Value See Also Examples

View source: R/indicators.R

Description

Indicators are typically standard technical or statistical analysis outputs, such as moving averages, bands, or pricing models.

Usage

1
2
add.indicator(strategy, name, arguments, parameters = NULL, label = NULL,
  ..., enabled = TRUE, indexnum = NULL, store = FALSE)

Arguments

strategy

an object (or the name of an object) type 'strategy' to add the indicator to

name

name of the indicator function – must correspond to an R function

arguments

default arguments to be passed to an indicator function when executed

parameters

vector of strings naming parameters to be saved for apply-time definition,default NULL, only needed if you need special names to avoid argument collision

label

arbitrary text label for indicator output. This will also be used as the name of the indicator list when it is stored. NULL default will be converted to '<name>.ind'

...

any other passthru parameters

enabled

TRUE/FALSE whether the indicator is enabled for use in applying the strategy, default TRUE

indexnum

if you are updating a specific indicator, the label or the index number in the $indicators list to update.

store

TRUE/FALSE whether to store the strategy in the .strategy environment, or return it. default FALSE

Details

Indicators are always path-independent, and should be constructed from vectorized functions where possible.

Indicators are applied before signals and rules, and the output of indicators may be used as inputs to construct signals or fire rules.

arguments and parameters are named lists that describe the arguments to be passed to the indicator function. arguments is for defining any non-default arguments to be passed to the function named in the name of the indicator. For example, the x argument to a moving average function may be defined as x=quote(Cl(mktdata))

If you look at the demo scripts, you'll notice that we often use quote(mktdata) in setting up indicators, signals, or rules. This tells R to delay evaluation via quote(), and to use the special variable mktdata.

mktdata is typically created internally to quantstrat by looking in the global environment for a time series of prices or returns. mktdata may also contain other data you've manipulated outside quantstrat, though where possible you should use quantstrat to contain all the logic for the strategy, to aid in maintenance and modifications.

The use of quote() tells R to not evaluate what's inside the quote until the function is evaluated later. By the time that code is evaluated, mktdata will be populated with the correct price information based on the contents of whatever portfolio you are evaluating the strategy on.

parameters is another named list, and normally will not be needed. If you have multiple indicator, signal, or rule functions share the that both share the same argument names and will need to have different values passed to those arguments as defined parameters at apply-time, then you may need to give them unique names so that delayed evaluation can sort it all out for you at apply-time. We will endeavor to get an example of named parameters into the demo scripts.

if label is not supplied, NULL default will be converted to '<name>.ind' unless there already exists an indicator with that label in which case it will be appended with a number (i.e. '<name>.ind.2', '<name>.ind.3', etc.). If the indicator function returns multiple columns, the label will be paste'd to the end of either the returned column names or the respective column number when applying it to mktdata.

Value

if strategy was the name of a strategy, the name. It it was a strategy, the updated strategy.

See Also

quote applyIndicators add.signal link{add.rule}

Examples

1
2
3
4
5
6
7
8
9
## Not run: 
strategy("example", store=TRUE)
getSymbols("SPY", src='yahoo')
add.indicator('example', 'SMA', arguments=list(x=quote(Ad(SPY)), n=20))
str(getStrategy('example')$indicators)
out <- applyIndicators('example', SPY)
tail(out)

## End(Not run)

naturalsmen/quantstrat documentation built on May 23, 2017, 10:38 a.m.