add an indicator to a strategy
Indicators are typically standard technical or statistical analysis outputs, such as moving averages, bands, or pricing models.
add.indicator(strategy, name, arguments, parameters = NULL, label = NULL, ..., enabled = TRUE, indexnum = NULL, store = FALSE)
- an object (or the name of an object) type 'strategy' to add the indicator to
- name of the indicator function -- must correspond to an R function
- default arguments to be passed to an indicator function when executed
- 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
- 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
- any other passthru parameters
- TRUE/FALSE whether the indicator is enabled for use in applying the strategy, default TRUE
- if you are updating a specific indicator,
labelor the index number in the $indicators list to update.
- TRUE/FALSE whether to store the strategy in the .strategy environment, or return it. default FALSE
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.
parameters are named lists that
describe the arguments to be passed to the indicator
arguments is for defining any non-default
arguments to be passed to the function named in the
name of the indicator. For example, the
argument to a moving average function may be defined as
If you look at the demo scripts, you'll notice that we
quote(mktdata) in setting up indicators,
signals, or rules. This tells R to delay evaluation via
quote(), and to use the special variable
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
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
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.
label is not supplied, NULL default will be
converted to '
paste'd to the end of either
the returned column names or the respective column number
when applying it to
strategywas the name of a strategy, the name. It it was a strategy, the updated strategy.
## 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)