business-science / tidyquant

Bringing financial analysis to the tidyverse
https://business-science.github.io/tidyquant/
Other
852 stars 175 forks source link

Evaluation error: High-Low matrix (HL) and volume vector must be specified.. #71

Open wa2003 opened 6 years ago

wa2003 commented 6 years ago

Thank you for such a good package, I just learn to use it , it help me a lot. I have a question, I use EMV function in TTR, but when I use select to select High, Low and Volume, it have errors. I have tried select=High:Volume, select=High:Low:Volume, select=NULL, but they all have errors. Pls help me, thx you in advance.

EMV(HL, volume, n = 9, maType, vol.divisor = 10000, ...)

Error in mutate_impl(.data, dots) : Evaluation error: High-Low matrix (HL) and volume vector must be specified..

library(TTR)

tt<- dt %>% group_by(Code) %>% tq_mutate(select=High:Volume, mutate_fun= EMV, n=3,col_rename=c("EMV3.emv","EMV3.maEMV")) %>% tq_mutate(select=High:Volume, mutate_fun= EMV, n=5,col_rename=c("EMV5.emv","EMV5.maEMV")) %>%

mdancho84 commented 6 years ago

Thanks for the kind words.

It's difficult to help you with this because we need a reproducible example meaning we have all information needed to quickly re-create the problem you are experiencing. This means all libraries and code that generates the issue. Also, the GitHub issues is for software bugs. If you are looking for a how-to, I suggest posting your request to Stack Overflow with the tag: tidyquant.

wa2003 commented 6 years ago

I have tried it several times。。。

library(TTR) library(tidyquant) tt<-ttrc %>% tq_mutate(select=High:Volume, mutate_fun= EMV, n=3,col_rename=c("EMV3.emv","EMV3.maEMV"))

DavisVaughan commented 6 years ago

I see what you are saying @wa2003. Normally I would suggest tq_mutate_xy() for this kind of thing, but the x argument only takes 1 column at a time and you need to pass in 2 for x (high and low) and 1 for y (volume).

tidyquant isn't currently set up for that logic. Instead, you can do this:

suppressPackageStartupMessages(library(tidyquant))

data(FANG)

# Doesnt work
# FANG %>%
#   group_by(symbol) %>%
#   tq_mutate_xy(x = high:low, y = volume, mutate_fun = EMV, n = 3, col_rename = c("EMV3.emv", "EMV3.maEMV"))

FANG_emv <- FANG %>%
  group_by(symbol) %>%
  nest() %>%
  mutate(EMV = map(data, ~as.tibble(EMV(HL     = select(.x, high, low), 
                                        volume = .x$volume, 
                                        n      = 3)))) %>%
  unnest()

select(FANG_emv, high, low, volume, emv, maEMV) 
#> # A tibble: 4,032 x 5
#>     high   low    volume           emv         maEMV
#>    <dbl> <dbl>     <dbl>         <dbl>         <dbl>
#>  1 28.18 27.42  69846400            NA            NA
#>  2 28.47 27.59  63140600  3.205534e-05            NA
#>  3 28.93 27.83  72715400  5.294622e-05            NA
#>  4 29.79 28.65  83781800  1.142971e-04  6.643288e-05
#>  5 29.60 28.86  45871300  1.613207e-06  5.628550e-05
#>  6 30.60 29.49 104787700  8.633164e-05  6.741397e-05
#>  7 31.45 30.28  95316400  1.006544e-04  6.286641e-05
#>  8 31.96 31.10  89598000  6.382933e-05  8.360511e-05
#>  9 32.21 30.62  98892800 -1.848961e-05  4.866469e-05
#> 10 31.71 29.88 173242600 -6.549208e-05 -6.717454e-06
#> # ... with 4,022 more rows