Computing multiple indicators in tidyquant

I am trying to get multiple indicator values such as RSI and EMA for equities using the package tidyquant in R. I tried the example from the vignette which is:

tq_get("AAPL", get = "stock.prices")%>% tq_transform_xy_(x_fun = 'close', transform_fun = 'periodReturn',period = 'weekly') %>% tq_mutate(x_fun = Cl, mutate_fun = RSI, n = 14, period = 'weekly') %>% tq_mutate(x_fun = Cl, mutate_fun = EMA, n = 14, period = 'weekly')


resulting to an error:

Error in match(x, table, nomatch = 0L) : argument ".x" is missing, with no default

1. How do I transform the Closing stock prices to weekly?; and
2. How do I get the RSI and EMA in one tq_mutate line with RSI and EMA column names? (Both result to EMA column names).

I'll be using tidyquant version 0.3.0 to answer this question, which changes from x_fun to ohlc_fun in tq_mutate and tq_transform and adds the new col_rename argument to solve situations like yours with non-intuitive column names.

Part 1: How do I transform the Closing stock prices to weekly?

You are using the periodReturns function from quantmod, and you more likely want to use the to.period function from xts.

There's a couple of ways to do it. The first version is what you are attempting. The second is an alternative using the quantmod OHLC notation.

1A, Using tq_transform_xy_ which returns the closing prices only:

tq_get("AAPL", get = "stock.prices") %>%
tq_transform_xy_(x = 'close', transform_fun = 'to.period', period = 'weeks')


1B, Using tq_transform which returns the open, high, low, close, and volume prices if you use ohlc_fun = OHLCV:

tq_get("AAPL", get = "stock.prices") %>%
tq_transform(ohlc_fun = OHLCV, transform_fun = to.period, period = 'weeks')


Part 2: How do I get the RSI and EMA in one tq_mutate line with RSI and EMA column names?

The code snippet you are using pipes (%>%) the transformed period returns into the RSI and EMA mutations. This is probably not what you want to do. Rather, you first want to transform the data to weekly periodicity using the xts function to.period (ref. Part 1). Once you have the weekly periodicity, you can calculate the RSI and EMA using weekly closing prices with number of periods, n = 14 (note this is a 14 week interval because of the periodicity change, which may not be what you want). Here's how I would do this:

tq_get("AAPL", get = "stock.prices") %>%
tq_transform(ohlc_fun = OHLCV, transform_fun = to.period, period = "weeks") %>%
tq_mutate(ohlc_fun = Cl, mutate_fun = RSI, n = 14, col_rename = "RSI.14") %>%
tq_mutate(ohlc_fun = Cl, mutate_fun = EMA, n = 14, col_rename = "EMA.14")

• nice one matt. Im stil learning the syntax for tidyquant and can get quite confusing. – geodex Jan 31 '17 at 21:51
• No problem. I'm sure your not the only one! Let me know if you run into anything else. – Matt Dancho Feb 1 '17 at 23:35

library(tidyquant)
tq_get("AAPL", get = "stock.prices")%>%
tq_transform_xy_(x = 'close', transform_fun = 'periodReturn',period = 'weekly')


this gives you

 A tibble: 525 × 2
date weekly.returns
<dttm>          <dbl>


Appearantly the vignette is not up to date if you look at the help of tq_transform_xy_ then you see that the argument should be x.

Doing this you get rid of the error. But you get returns. I am not an expert in technical trading but does it makes sense to calculate an EMA on returns? Wouldn't you rather do this on prices?

• Sorry I made an error. I did not realize that I have put in ' periodReturns ' in ' tranform_fun_xy ' . THat should have been ' EMA '. With the second question I think that would be impossible to put it in one line. – geodex Jan 20 '17 at 20:25