I'm trying to analyze candlestick formation Marubozu in R. So far I was able to download the different stocks data and find the formation using "Candlesticks" library in one stock data. I would like to automate that process so that I can run the CSPMarubozu function on many stocks at the same time.
My main problem is that I cannot really understand how can I pass the list of data to this function. While trying to do it with for loop (Try 1) I get following error: "Error in CSPMarubozu((names(stocks_list[i])), n = 20, ATRFactor = 0.8, : Price series must contain Open, High, Low and Close." I know, that I can't pass the character variable to this function, but I can't find the way to get index names without the "" mark. (ex. "AMZN" and I need just AMZN"
My other try (Try 2) was to do it with lapply() function but the same problem occurs
Here is my code:
#install.packages(candlesticks)
library(candlesticks)
library(tidyquant)
library(quantmod)
#List of stock codes
stocks <- c("AAPL","MSFT","GOOG","GOOGL","AMZN","TSLA","FB","ADI","ASML","ADBE","NTES","NFLX","JD","CSCO","AVGO","COST","PEP","CMCSA","PYPL","INTC","QCOM","INTU","TXN","TMUS","HON","AMAT","SBUX","CHTR","ISRG","AMGN","MRNA","ADP","LRCX","MU","TEAM","BKNG","GILD","MDLZ","CSX","PDD","MRVL","WDAY","REGN","KLAC","NXPI","ADSK","MELI","LULU","ILMN","ZM")
#Timestamp
start = '2019-01-01'
end = '2020-01-01'
#Stocks data download (creates XTS - objects)
for (i in stocks)
{
i <- getSymbols(i, src = "yahoo", from = start, to = end)
}
#Stocks data download (creates list of XTS - objects
stocks_list <- list()
for (i in stocks){
stocks_list[[i]] <- getSymbols(i, src = "yahoo", from = start, to = end, auto.assign=FALSE, return.class="xts")
}
#Finding Marubozu Candlestick in one stock
AMZN_marubozu<- CSPMarubozu(AMZN, n=20, ATRFactor=.8, maxuppershadowCL=.017, maxlowershadowCL=.017)
#Finding Marubozu Candlestick in the whole list of Stocks
#Try 1
for (i in names(stocks_list)){
names(stocks_list[i]) <- CSPMarubozu((names(stocks_list[i])), n=20, ATRFactor=.8, maxuppershadowCL=.017, maxlowershadowCL=.017)
}
#Try 2
DoMaru <- function(name)
{
CSPMarubozu(name, n=20, ATRFactor=.8, maxuppershadowCL=.017, maxlowershadowCL=.017)
}
apply_Marubozu <- lapply(stocks_list, DoMaru(stocks_list$name))