0
如何在不同時段使用不同長度參數計算R中的移動平均線(或其他技術指標)?移動平均線移動長度
require(quantmod)
library(chron)
library(caTools)
## rm(list=ls())
# Get the data
from.dat <- as.Date("2015-01-01")
#to.dat <- as.Date("2000-01-01")
ES<-getSymbols("^GSPC" , from = from.dat,auto.assign = FALSE) # to today
data<-ES
rm(ES)
data<-data[,6]
names(data)<-c("ES")
# create vectore with dummy values
data$num <- rep(10,length(data$ES))
for(i in 2:length(data$num)){
if (data$num[i-1] == 10) {data$num[i] <- 5}
}
# moving average with moving length
data$sma<-SMA(data$ES,data$num)
plot(data$ema)
該圖甚至不顯示移動平均值。如何使用具有移動長度參數的技術指標?
謝謝Floo0,但我一直在尋找一些更一般的腳本。 如果我有一個具有更多「長度」元素的矢量,並且隨時間變化,該怎麼辦? – gcats