1

Basically TTR allows to get technical indicator of a ticker and data should be vertical like:

Date         Open   High    Low     Close
2014-05-16  16.83   16.84   16.63   16.71
2014-05-19  16.73   16.93   16.66   16.80
2014-05-20  16.80   16.81   16.58   16.70

but my data frame is like:

   Sdate    Edate   Tickers Open_1  Open_2  Open_3  High_1  High_2  High_3  Low_1   Low_2   Low_3   Close_1 Close_2 Close_3
2014-05-16  2014-07-21  TK  31.6    31.8    32.2    32.4    32.4    33.0    31.1    31.5    32.1    32.1    32.1    32.7 
2014-05-17  2014-07-22  TGP 25.1    24.8    25.0    25.1    25.3    25.8    24.1    24.4    24.9    24.8    25.0    25.6 
2014-05-18  2014-07-23  DNR 3.4     3.5     3.8     3.6     3.8     4.1     3.3     3.5     3.8     3.5     3.7     3.9

As you see I have multiple tickers and time range. I went over package TTR and it does not state how to get technical indicator from which is horizontally made and multiple tickers. My original data has 50days and thousands tickers. To do this, I just knew that, I need to make lists for each tickers, but I'm confused how to do this. How do I achieve this?

1 Answers1

3

You can get data in vertical shape by using pivot_longer :

out <- tidyr::pivot_longer(df, cols = -c(Sdate,Edate, Tickers), 
             names_to = c('.value', 'num'), 
             names_sep = '_')
out

# A tibble: 9 x 8
#  Sdate      Edate      Tickers num    Open  High   Low Close
#  <chr>      <chr>      <chr>   <chr> <dbl> <dbl> <dbl> <dbl>
#1 2014-05-16 2014-07-21 TK      1      31.6  32.4  31.1  32.1
#2 2014-05-16 2014-07-21 TK      2      31.8  32.4  31.5  32.1
#3 2014-05-16 2014-07-21 TK      3      32.2  33    32.1  32.7
#4 2014-05-17 2014-07-22 TGP     1      25.1  25.1  24.1  24.8
#5 2014-05-17 2014-07-22 TGP     2      24.8  25.3  24.4  25  
#6 2014-05-17 2014-07-22 TGP     3      25    25.8  24.9  25.6
#7 2014-05-18 2014-07-23 DNR     1       3.4   3.6   3.3   3.5
#8 2014-05-18 2014-07-23 DNR     2       3.5   3.8   3.5   3.7
#9 2014-05-18 2014-07-23 DNR     3       3.8   4.1   3.8   3.9

If you want to split the above data into list of dataframes based on Ticker you can use split.

split(out, out$Tickers)

data

df <- structure(list(Sdate = c("2014-05-16", "2014-05-17", "2014-05-18"
), Edate = c("2014-07-21", "2014-07-22", "2014-07-23"), Tickers = c("TK", 
"TGP", "DNR"), Open_1 = c(31.6, 25.1, 3.4), Open_2 = c(31.8, 
24.8, 3.5), Open_3 = c(32.2, 25, 3.8), High_1 = c(32.4, 25.1, 
3.6), High_2 = c(32.4, 25.3, 3.8), High_3 = c(33, 25.8, 4.1), 
    Low_1 = c(31.1, 24.1, 3.3), Low_2 = c(31.5, 24.4, 3.5), Low_3 = c(32.1, 
    24.9, 3.8), Close_1 = c(32.1, 24.8, 3.5), Close_2 = c(32.1, 
    25, 3.7), Close_3 = c(32.7, 25.6, 3.9)), 
class = "data.frame", row.names = c(NA, -3L))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • I think that the OP wants to separate the data into different data frames, one for each ticker. So there would be three dfs in this case, with the data of TK, TGP, DNR, respectively. Those data frames could be assembled into a list. I did something like that myself some time ago, but I don't remember at the moment how this is best achieved. – RHertel Sep 05 '20 at 05:23
  • if that is the case we can split this dataframe into list of dataframes based on `Tickers`. – Ronak Shah Sep 05 '20 at 06:16
  • Yes, I think this would be a good thing. Then, the data in each list item could be analyzed with the TTR package. – RHertel Sep 05 '20 at 06:35
  • @Ronak Shah Thank you so much! It perfectly works. Can I ask one more question? I should get technical indicator by wring like; bbands <- BBands( out[,c("High","Low","Close")] ). As you know my data is stored as lists, so there are more than one data frame. What should I do to import for all tickers? – Dongchul Park Sep 05 '20 at 22:55
  • @DongchulPark I think something like this `d1 <- split(out, out$Tickers)` and then with `lapply` : `lapply(d1, function(x) BBands( x[,c("High","Low","Close")]))` – Ronak Shah Sep 06 '20 at 00:06
  • @RonakShah Thank you!! It works. – Dongchul Park Sep 06 '20 at 00:43