Search code examples
rlistlapplyfinancequantmod

Add return of stock data with function Delt() to a list of dataframe


I have a list of data frames with stock data and want to add a new column with return of the stock price to each data frame in the list. The data frames looks as:

        ABB.ST.Open ABB.ST.High ABB.ST.Low ABB.ST.Close ABB.ST.Volume ABB.ST.Adjusted       Date
    1         166.4       167.4      165.5        166.0        944525        162.1181 2015-01-02
    2         165.6       166.6      164.4        164.4       1013670        160.5555 2015-01-05
    3         161.2       161.6      159.1        159.1       2546062        155.3794 2015-01-07
    4         160.3       163.3      159.2        163.0       1946157        159.1882 2015-01-08
    5         162.8       162.8      160.0        160.6       1885969        156.8444 2015-01-09
    6         161.5       162.6      160.6        161.1       1795966        157.3327 2015-01-12
    7         159.8       160.6      158.3        160.0       2445040        156.2584 2015-01-13
    Delt.1.arithmetic
1                  NA
2       -0.0066959806
3       -0.0127628163
4        0.0168292916
5       -0.0002389207
6        0.0141549696
7       -0.0044953302

This goes on down till the date od today, i.e. approx 1300 rows.

Now want to add the return of the first column ABB.ST.Open with the function Delt from package quantmod.

This code work when I apply it to just the data frame, outside the list:

ABB.ST$Return <- Delt(ABB.ST[,1])

But how do I add this column to all data frame in a list?

My list name is master_df and I have tried this code, but it does not work...

Return <- function(x){
  master_df[[x]]$Return <- Delt(master_df[[x]][,1])
} 

test2 <- lapply(master_df, Return(x))

UPDATE:

I have data.frame in the list, where my list name is master_df, this is my code where I download data from yahoo finance:

as.matrix(LargeCapOMXS[,7]) is my list of symbols names from yahoo

Stocklist <- as.matrix(LargeCapOMXS[,7]) #get names of stocks
master_df <- list() #create list to fill with data

for(i in seq(length(Stocklist))){ #loop to fill list with downloaded stock data, by company
  Stockindex = Stocklist[i]
  getSymbols(Stockindex, src="yahoo", from="2015-01-01", to="2020-01-01", verbose = TRUE)
  master_df[[i]] <- as.data.frame(get(Stockindex))
  master_df[[i]]$Date = row.names(master_df[[i]])
  row.names( master_df[[i]]) = NULL
}
}

Solution

  • Below is some code to get the data from yahoo, into a list and add the Delt column to each xts object. No need for creating data.frames. You can always do this later.

    library(quantmod)
    
    stock_list <- c("ABB", "ABT")
    
    master_df <- lapply(stock_list, getSymbols, from = "2015-01-01", to = "2020-01-01", auto.assign = FALSE)
    names(master_df) <- stock_list
    
    # need to merge the xts with the outcome of Delt as Delt only returns 1 column
    # function Op selects the open column.
    master_df <- lapply(master_df, function(x) merge(x, Delt(Op(x))))
    

    Or using tidyquant if you want a giant data.frame.

    library(tidyquant)
    library(dplyr)
    
    master_df_tq <- tq_get(stock_list)
    master_df_tq <- master_df_tq %>% 
      group_by(symbol) %>%
      tq_mutate(select = open,
                mutate_fun = Delt)