Skip to content
All library documents

Resampling Equity Prices and Calculating RSI and EMA in tidyquant

Article Quant Q&A · Author: geodex

Summary

The document explains how to convert daily equity data to weekly observations in tidyquant and then calculate RSI and EMA from weekly closing prices. It recommends using the time-series `to.period` transformation rather than `periodReturns` when the goal is weekly prices. One approach keeps closing prices only; another transforms the full open, high, low, close, and volume data.

The accepted answer shows that RSI and EMA can be added in separate mutation steps, with explicit column names to distinguish their outputs. The resulting 14-period indicators operate over 14 weekly observations, which may differ from the intended horizon. An alternative answer corrects the argument used for a return transformation but notes that this produces returns rather than prices, raising a question about whether price-based indicators should be calculated on returns. The examples are specific to the package version and API described.

Key ideas

  • Use a period aggregation function to convert daily stock data into weekly observations while retaining prices.
  • A closing-price-only transformation and a full OHLCV transformation are both presented.
  • Calculate RSI and EMA in separate mutation steps and assign distinct output column names.
  • A 14-period indicator on weekly data spans 14 weeks, which may not match the desired interval.
  • A weekly return series is different from weekly closing prices and may not suit price-based indicators.

Tags

Full text
# Computing multiple indicators in tidyquant


# 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
```

- How do I transform the Closing stock prices to weekly?; and

- 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).

## Answer by Matt Dancho (score 1, accepted)

https://quant.stackexchange.com/a/32148

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")
```

## Answer by Richi Wa (score 0)

https://quant.stackexchange.com/a/31987

Answer to 1):

```
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?

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.