1

I do not understand why I get the ATR and DonchianChannel columns twice, so that in the end the tibble has the column ATR and and ATR.1, also the column DonchianChannel and DonchianChannel.1. The values also differs to each other? Can someone maybe help? Thanks.

tq_get(c("DTE.DE", "SAP.DE"), get = "stock.prices", from = "2017-12-01")  %>%    
group_by(symbol) %>% 
tq_mutate(select = c("high", "low", "close"), n=14, mutate_fun = ATR)   %>%
tq_mutate(select = c("high", "low"), mutate_fun = DonchianChannel)



# A tibble: 38 x 15
# Groups:   symbol [2]
  symbol       date   open   high    low  close   volume adjusted    tr   atr    ATR  ATR.1 DonchianChannel     mid DonchianChannel.1
   <chr>     <date>  <dbl>  <dbl>  <dbl>  <dbl>    <dbl>    <dbl> <dbl> <dbl>  <dbl>  <dbl>           <dbl>   <dbl>             <dbl>
1 DTE.DE 2017-12-01 15.065 15.140 14.975 14.975 14796786   14.975    NA    NA     NA     NA              NA      NA                NA
2 DTE.DE 2017-12-04 15.140 15.345 15.050 15.245 13463710   15.245 0.370    NA 15.345 14.975              NA      NA                NA
3 DTE.DE 2017-12-05 15.335 15.425 15.265 15.275  9877870   15.275 0.180    NA 15.425 15.245              NA      NA                NA
4 DTE.DE 2017-12-06 15.200 15.425 15.175 15.360  8934058   15.360 0.250    NA 15.425 15.175              NA      NA                NA
5 DTE.DE 2017-12-07 15.405 15.690 15.370 15.620 13318348   15.620 0.330    NA 15.690 15.360              NA      NA                NA
6 DTE.DE 2017-12-08 15.710 15.730 15.520 15.520 13255747   15.520 0.210    NA 15.730 15.520              NA      NA                NA
7 DTE.DE 2017-12-11 15.505 15.505 15.255 15.330 11920247   15.330 0.265    NA 15.520 15.255              NA      NA                NA
8 DTE.DE 2017-12-12 15.360 15.370 15.160 15.280 10615751   15.280 0.210    NA 15.370 15.160              NA      NA                NA
9 DTE.DE 2017-12-13 15.270 15.325 15.165 15.200  8826821   15.200 0.160    NA 15.325 15.165              NA      NA                NA
10 DTE.DE 2017-12-14 15.260 15.270 15.025 15.175 12976932   15.175 0.245    NA 15.270 15.025           15.73 15.3525            14.975
# ... with 28 more rows
Sven
  • 443
  • 2
  • 10

1 Answers1

1

In the function documentations you will read that TTR::ART returns a object of the same class as HLC or a matrix (if try.xts fails) containing the columns: tr, atr, trueHigh and trueLow. TTR::DonchianChannel returns a object of the same class as HL or a matrix (if try.xts fails) containing the columns: high, mid, low.

tq_mutate renames the newly created columns so that they do not conflict with existing column names., e.g. with strings like "open", "high", "low" and/or "close" etc. (This is the job of the function replace_bad_names inside tq_mutate, have a look at the source code here.)

For example, if you'd use tq_transmute instead of tq_mutate, you can see that the names of columns are the ones mentioned in ?TTR::ATR. (The result is an object with only newly created columns, hence no naming conflict.)

library(tidyquant)
tq_get(c("DTE.DE", "SAP.DE"), get = "stock.prices", from = "2017-12-01") %>%    
 group_by(symbol) %>% 
 tq_transmute(select = c("high", "low", "close"), n = 14, mutate_fun = ATR) %>% 
 names(.)
[1] "symbol"   "date"     "tr"       "atr"      "trueHigh" "trueLow" 

So your code works fine. However, you could rename your columns to know what they mean.

library(tidyquant)
tq_get(c("DTE.DE", "SAP.DE"), get = "stock.prices", from = "2017-12-01") %>%
 group_by(symbol) %>% 
 tq_mutate(select = c("high", "low", "close"), n=14, mutate_fun = ATR) %>% 
 tq_mutate(select = c("high", "low"), mutate_fun = DonchianChannel) %>% 
 dplyr::rename(ATR_trueHigh = ATR,
               ATR_trueLow = ATR.1,
               DC_low = DonchianChannel,
               DC_mid = mid,
               DC_high = DonchianChannel.1)

Check also the col_rename argument of tq_mutate.

Sven
  • 443
  • 2
  • 10
markus
  • 25,843
  • 5
  • 39
  • 58
  • Thanks Markus. I understood so far. But in the end for me there are no duplicate columns as "trueHigh" and "trueLow" are new created ones. When running the source script you posted with the link I get only unique columns, despite "symbol" and "date". – Sven Jan 01 '18 at 16:56
  • `tib1 <- tq_get(c("DTE.DE", "SAP.DE"), get = "stock.prices", from = "2017-12-01") %>% group_by(symbol) name_list_tib1 <- colnames(tib1) tib2 <- tq_get(c("DTE.DE", "SAP.DE"), get = "stock.prices", from = "2017-12-01") %>% group_by(symbol) %>% tq_transmute(select = c("high", "low", "close"), n = 14, mutate_fun = ATR) name_list_tib2 <- colnames(tib2) name_list <- c(name_list_tib1, name_list_tib2) name_list` – Sven Jan 01 '18 at 16:56
  • @Sven you're right. It's the `replace_bad_names` function that modifies `trueHigh` and `trueLow` (in case of `TTR::ATR`) since they contain `"high"` and `"low"`, respectively (see [line 341](https://github.com/business-science/tidyquant/blob/master/R/tq_mutate.R#L341) in the link I provided). Will edit my answer. – markus Jan 01 '18 at 18:22