add.indicator(strategy, name, arguments, parameters = NULL, label = NULL, ..., enabled = TRUE, indexnum = NULL, store = FALSE)
label
or the index number in the $indicators
list to update.strategy
was the name of a strategy, the name. It
it was a strategy, the updated strategy.
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 'paste
'd to the end of either
the returned column names or the respective column number
when applying it to mktdata
.
quote
applyIndicators
add.signal
link{add.rule}
## 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)
Run the code above in your browser using DataLab