我正在尝试分析 R 中的烛台形态 Marubozu。到目前为止,我能够下载不同的股票数据并在一个股票数据中使用“烛台”库找到形成。我想自动化这个过程,以便我可以同时在许多股票上运行 CSPMarubozu 函数。
我的主要问题是我无法真正理解如何将数据列表传递给这个函数。尝试使用 for 循环(尝试 1)执行此操作时出现以下错误:“CSPMarubozu 中的错误((名称(stocks_list [i])),n = 20,ATRFactor = 0.8,:价格系列必须包含开盘价、最高价、最低价并关闭。”我知道,我无法将字符变量传递给这个函数,但我找不到没有“”标记的获取索引名称的方法。(例如“AMZN”,我只需要 AMZN“
我的另一个尝试(尝试 2)是用 lapply() 函数来做,但同样的问题发生了
这是我的代码:
#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))