First, to directly answer the two points:
indicators are not necessary to a strategy, the only hard and fast requirement is that you need at least one rule that will create orders, or at least one rule in the
order
slot that will create transactions.the
strategy
object contains only the specification of the strategy, nothing more, see below.
Next, to explain what's going on:
quantstrat
makes extensive use of delayed execution to allow for code reuse. The strategy
object is a storage place for the specification of the strategy. It may be applied to one or more portfolios (as created by initPortf()
) by using the portfolios=
argument.
The strategy specification is only a storehouse of how you want to apply the strategy later. Until you call applyStrategy(...)
, nothing is evaluated. This allows for useful properties like using the same strategy object to test multiple different parameter sets, or applying against different portfolio constructions and constituents, without changing the strategy specification.
The strategy object itself is not changed by applyStrategy
. Inside applyStrategy
, a special internal object called mktdata
is created which will be modified by the execution of the indicators, signals, and rules contained in the strategy specification.
The mktdata
object is constructed, by default, from retrieving the object containing your historical data from the .GlobalEnv
, or some other environment specified by the user. There will be one of these objects created for each symbol in the portfolio, and maintained inside the function scope of applyStrategy
.
When the indicators and signals are applied, the most common pattern is for these functions is to return a vector of the same length as the mktdata time series, or a time series object with the same index as the mktdata time series. If this pattern is followed, these columns will be added to the mktdata object and available for later indicator, signal, and rule functions to make use of. indicators and signals are presumed to always be not path dependent, and rules, by default, are path dependent.
The example signal functions, such as sigCrossover
and sigThreshold
, make use of this pattern to access and compare columns that exist in mktdata. ruleSignal
, an example rule, looks for points where signals have some particular value, and then acts on that information to create orders.
External references: