tq_mutate
is a very flexible wrapper for various xts
,
quantmod
and TTR
functions. The main advantage is the
results are returned as a tibble
and the
function can be used with the tidyverse
.ohlc_fun
is one of the various quantmod
Open, High, Low, Close (OHLC) functions.
The function returns a column or set of columns from data
that are passed to the
mutate_fun
. In Example 1 below, Cl
returns the "close" price and sends
this to the mutate function, periodReturn
.
mutate_fun
is the function that performs the work. In Example 1, this
is periodReturn
, which calculates the period returns. The ...
functions are additional arguments passed to the mutate_fun
. Think of
the whole operation in Example 1 as the close price, obtained by ohlc_fun = Cl
,
is being sent to the periodReturn
function along
with additional arguments defining how to perform the period return, which
includes period = "daily"
and type = "log"
.
tq_mutate_xy
is designed to enable working with (1) mutatation
functions that require two primary inputs (e.g. EVWMA, VWAP, etc) and (2) data
that is not in OHLC format. Example 2 shows the first benefit in action:
using the EVWMA function that uses volume to defind the moving average period.
The two variables do not fall into a single OHLC code (i.e. CV does not exist).
The xy form gets us out of this problem. Example 3 shows the second benefit
in action: Some functions are useful to non-OHLC data, and defining x = price
allows us to mutate WTI crude prices from daily to monthly periodicity.
tq_mutate_
and tq_mutate_xy_
are setup for Non-Standard
Evaluation (NSE). This enables programatically changing column names by modifying
the text representations. Example 4 shows the difference in implementation.
Note that character strings are being passed to the variables instead of
unquoted variable names. See vignette("nse")
for more information.