Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to group by company when computing different technical indicators from the TTR package in R?

I am trying to compute various technical indicators that will be used to predict the stock price movement of around 100 stocks. For example, I want to calculate the Average True Range (ATR) for each of the companies in my time series, which I am trying to do with the TTR package in R. However, the problem is that I can’t figure out how to do it for each of the companies in my dataset. I have tried to use dplyr::group_by(company) which doesn’t work.

data_ATR <- data_price %>%
  dplyr::group_by(company) %>%
  TTR::ATR(data_price[,c("PRICE HIGH", "PRICE LOW", "CLOSING PRICE")], n=14)

Any help would be greatly appreciated!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We may use group_modify here

library(dplyr)
data_price %>%
  dplyr::group_by(company) %>%
  dplyr::group_modify(~ as.data.frame(TTR::ATR(.[,c("PRICE HIGH", 
     "PRICE LOW", "CLOSING PRICE")], n=14)))

Using a reproducible example

library(TTR)
data(ttrc)
ttrc %>% 
 mutate(company = as.integer(gl(n(), 25, n()))) %>% 
 group_by(company) %>% 
 group_modify(~ as.data.frame(ATR(.[c("High", "Low", "Volume")], n = 14))) %>% 
ungroup
# A tibble: 5,550 × 5
   company       tr   atr trueHigh trueLow
     <int>    <dbl> <dbl>    <dbl>   <dbl>
 1       1      NA     NA       NA   NA   
 2       1 1870903.    NA  1870906    3.09
 3       1 3099503.    NA  3099506    3.08
 4       1 2274154.    NA  2274157    3.07
 5       1 2086755.    NA  2086758    3.08
 6       1 2166345.    NA  2166348    3.1 
 7       1 3441795.    NA  3441798    3.14
 8       1 7550745.    NA  7550748    3.2 
 9       1 4853309.    NA  4853312    3.22
10       1 5814822.    NA  5814825    3.28
# … with 5,540 more rows
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading