我正在尝试使用库TTR为所有符号导出技术指标。迭代方法实际上需要花费数小时并消耗大量内存。
是否可以创建函数并按符号对 eod_completewNA_woutRepeats 进行分组并将其应用到每个分组,最终结果是汇总列表( eod_completewNA_techInd )?
>list_symbols是符号名称的字符列表
head(list_symbols)
[1] "ACAD" "ACGL" "ACH" "ACHC" "ACHN" "ACIW"
head(tempHolder)
Date Open High Low Close Volume
4629 2015-08-24 30.0 32.2 30.0 32.2 20
9876 2015-08-25 33.1 33.7 32.5 33.0 141
15123 2015-08-26 33.0 33.0 33.0 33.0 0
20367 2015-08-27 33.0 33.0 33.0 33.0 0
25609 2015-08-28 35.2 35.2 34.8 34.8 440
30857 2015-08-31 34.8 34.8 34.8 34.8 0
昂贵的操作
for(lister in list_symbols)
{
tempHolder <- c()
tempHolder <- eod_completewNA_woutRepeats[which(eod_completewNA_woutRepeats$symbol==lister),,drop=F][,-2]
colnames(tempHolder) <- c("Date","Open","High","Low","Close","Volume")
#
#https://www.oipapio.com/question-12219593
#rownames(tempHolder) <- tempHolder$date
bbands <- c()
adx <- c()
ema <- c()
sma <- c()
macd <- c()
rsi <- c()
stochOsc <- c()
#ARK breaks this with too many values at the same value
bbands <- BBands( tempHolder[,c("High","Low","Close")] )
adx <- ADX( tempHolder[,c("High","Low","Close")] )
ema <- data.frame(EMA(tempHolder[,"Close"], n=20)[,drop=FALSE])
sma <- data.frame(SMA(tempHolder[,"Close"], n=20)[,drop=FALSE])
# MACD
macd <- MACD( tempHolder[,"Close"] )
# RSI
rsi <- RSI(tempHolder[,"Close"])
# Stochastics
stochOsc <- stoch(tempHolder[,c("High","Low","Close")])
View(tempHolder$Date)
rownames(tempHolder) <- tempHolder$Date
eod <<- tempHolder[which(tempHolder$Date=="2016-10-14"),,drop=F]
xts_holder <- as.xts(tempHolder)
nrow(tempHolder)
tech_ind <- cbind(lister,tempHolder,bbands,adx,ema,sma,macd,rsi,stochOsc)
colnames(tech_ind)[1] <- "Symbol"
if(is.null(eod_completewNA_techInd))
{
eod_completewNA_techInd <- tech_ind
}
if(!is.null(eod_completewNA_techInd))
{
colnames(tech_ind)
eod_completewNA_techInd <- rbind(eod_completewNA_techInd,tech_ind)
}
#print(nrow(eod_completewNA_techInd))
}
我正在绑定单个结果:
tech_ind <- cbind(lister,tempHolder,bbands,adx,ema,sma,macd,rsi,stochOsc)
然后重新绑定以创建聚合
eod_completewNA_techInd <- rbind(eod_completewNA_techInd,tech_ind)
附录
head(eod_completewNA_woutRepeats)
date symbol open high low close volume
1 2015-08-24 ACAD 33.7600 35.8300 32.2300 33.6000 1474700
3 2015-08-24 ACGL 22.6667 22.7233 21.4267 22.3933 2314200
4 2015-08-24 ACH 7.5600 7.9200 7.5400 7.7200 260300
5 2015-08-24 ACHC 69.0900 70.8800 58.7000 69.7100 1823400
6 2015-08-24 ACHN 6.9800 7.2500 6.7100 7.2500 4556400
8 2015-08-24 ACIW 20.9500 21.8600 20.4500 20.9400 916300