使用tq_mutate添加指标会生成重复的列

时间:2017-12-31 12:50:24

标签: r indicator tidyquant

我不明白为什么我两次得到ATR和DonchianChannel列,所以最后tibble有ATR和ATR.1列,还有DonchianChannel和DonchianChannel.1列。价值观也各不相同?有人可以帮忙吗?感谢。

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

1 个答案:

答案 0 :(得分:1)

在函数文档中,您将看到TTR::ART 返回与HLC相同类的对象或包含列的矩阵(如果try.xts失败): {{1} },tratrtrueHightrueLow 返回与HL相同类的对象或包含列的矩阵(如果try.xts失败): TTR::DonchianChannelhigh,{{1 }}

mid重命名新创建的列,以便它们不会与现有列名冲突。使用lowtq_mutate"open"和/或"high"等字符串(这是"low"内函数"close"的作用,看一下源代码here。)

例如,如果您使用replace_bad_names代替tq_mutate,则可以看到tq_transmute列是tq_mutate中提到的列。 (结果是一个只有新创建的列的对象,因此没有命名冲突。)

names

所以你的代码运行正常。但是,您可以重命名列以了解它们的含义。

?TTR::ATR

同时检查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" 的{​​{1}}参数。