library(quantmod)
#input symbol, start and end dates:
my_symbol<-readline("What symbol would you like analyzed?")
start_date <- readline("When do you want the series to start?")
end_date <- readline("When do you want the series to end?")
start_date2 <- readline("When do you want the scaled series to start?")
end_date2 <- readline("When do you want the scaled series to end?")
data1 <- as.data.frame(getSymbols(my_symbol, from = start_date, to = end_date, env = NULL))
data2<- getSymbols(my_symbol, from = start_date2, to = end_date2, env = NULL)
data3<- as.ts(data2[,4])
section<-length(data3)/3
#divide subset into 3 sections
subdata1<-data3[1:section]
subdata2<-data3[section:(section*2)]
subdata3<-data3[(section*2):(section*3)]
section2<-length(subdata1)
testdata1<-data1[,4]
testdata2<-testdata1[1:section2]
#Iterate through testdata
len2<-length(subdata1)
while (x < 0.67){
x<-0.66
resize<-approx(seq_along(subdata1), subdata1, n = len2*x)$y
len<-length(resize)
for (i in testdata1){
testdata2<-testdata1[(1+i):(len+i)]
corl<-cor(testdata2, resize)
if (corl>0.6){
maxes<-c(corl, len, i)
#print(maxes)
}
print(len)
}
x<-(x+0.01)
}
while循环中的我的X变量保持静态并且不会改变。考虑到while循环在某个时间后中断,这很奇怪。长度保持不变,如行打印(len),
可以做些什么?
答案 0 :(得分:0)
只是将我的评论作为答案,因为他们帮助解决/解决问题。
如果给定while
循环只执行一次。一开始你有x<-0.66
,最后是x<-(x+0.01)
,它会使x=0.67
导致你退出循环,因为循环的条件是while (x<0.67)
。
你应该在循环外移动x的初始设置,否则每次都会重置它,如果循环在第一次运行时没有退出,它将无限循环。然后,要多次运行循环,您可以将x<-(x+0.01)
替换为x<-(x+0.001)
。