forked from 321k/Google-Trends
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rescale
51 lines (44 loc) · 1.11 KB
/
Rescale
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
library(quantmod)
downloadDir="C:/Users/erik.johansson/Downloads"
setwd(downloadDir)
year=c(2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013)
GT.ts=list()
n=1
for(i in year){
for(j in 1:12){
URL=URL_GT(keyword="FTSE 100", year=i, month=j, length=3)
tempDir=downloadGT(URL, downloadDir)
data=read.csv(tempDir, header=F, blank.lines.skip=F)
start=which(data[,1]=="")[1]+3
stop=which(data[,1]=="")[2]-1
data=data[start:stop,]
data[,1]=as.Date(data[,1], "%Y-%m-%d")
data[,2]=as.numeric(as.character(data[,2]))
rownames(data)=data[,1]
data[3]=NA
#Calculate % change
for(k in 2:nrow(data)){
data[k, 3]=data[k,2]/data[k-1,2]
}
GT.ts[[n]]=data
n=n+1
}
}
output=as.xts(GT.ts[[1]][3])
for(i in 2:length(GT.ts)){
data=as.xts(GT.ts[[i]][3])
output=merge(output, data)
output$new=NA
for(j in 1:nrow(output)){
if(is.finite(mean(output[j,],na.rm=T))) output[j,3]=mean(output[j,],na.rm=T)
}
output=output[,3]
names(output)="d_svi"
}
#Start value
startvalue=100
series=vector()
series[1]=100
for(i in 2:nrow(output)){
series[i]=series[i-1]*output[i,]
}