I have multiple dataframes that contain stock market data in the form:
Open High Low Close Volume
I am trying to get an average (over a given period) of the last row of each stock and combine them into a single data frame like so:
Name SMA
StockA 15.1
StockB 34.44
I have a simple function that calculates the mean value and formats it correctly. It works on when I run it on a single stock (dataframe). However when I try and use lapply to apply the function to a list of all the dataframes, I get the error:
Error in x[,Close]: incorrect number of dimensions.
symbols is the list of all stock dataframes.
Any help would be greatly appreciated.
require(TTR)
require(quantmod)
symbols <- c("XLF", "XLK", "XLU", "XLE", "XLP", "XLF", "XLB", "XLV", "XLY")
getSymbols(symbols, src='yahoo', from = '2016-01-01')
fun1<-function(x,Close) {
mean1<-SMA(x[,Close],5)
mean2<-tail(mean1,1)
df_name<-deparse(substitute(x))
print(mean2,paste(df_name))
}
df<-lapply(symbols,fun1)
final_df <- do.call(rbind, df)