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.)
}
#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))
CodePudding user response:
This downloads stocks and then shows three different equivalent ways of processing each stock. We use dim(...) but that would be replaced with whatever processing is desired. Note that if x is an xts object for a stock having OHLC as well as adjusted close and volume then Op(x), Hi(x), Lo(x), Cl(x), Ad(x) and Vo(x) are the vectors of Open, High, Low, Close, Adjusted Close and Volume.
Although the code below seems preferable getSymbols(stocks); L <- mget(stocks)
also works to put the stocks loose into your workspace and then collect them into a list L.
library(quantmod)
stocks <- c("AAPL", "MSFT", "GOOG")
getSymbols(stocks, env = e <- new.env())
# 1. iterate over components of e
out1 <- eapply(e, function(x) { dim(x) })
L <- as.list(e)
# 2. iterate over names
out2 <- Map(function(nm) { x <- L[[nm]]; dim(x) }, names(L))
out2a <- Map(function(nm) { x <- e[[nm]]; dim(x) }, ls(e))
# 3. iterate over components of list L
out3 <- Map(function(x) { dim(x) }, L)
str(out1)
## List of 3
## $ AAPL: int [1:2] 3764 6
## $ GOOG: int [1:2] 3764 6
## $ MSFT: int [1:2] 3764 6
identical(out1, out2)
## [1] TRUE
identical(out1, out2a)
## [1] TRUE
identical(out1, out3)
## [1] TRUE