2020年3月10日火曜日

1日に+5%以上変動した日を持つそれぞれの月の月間騰落率を計算する




length(index(SP5))
# w <- last(SP5[,4][2:17659]/as.vector(SP5[,4][1:17658]),100)
w <- SP5[,4][1:length(index(SP5))-1]/as.vector(SP5[,4][2:length(index(SP5)) ])
head(w)
#               Close
# 1950-01-04 1.0114046
# 1950-01-05 1.0047478
# 1950-01-06 1.0029533
# 1950-01-09 1.0058893
# 1950-01-10 0.9970727
# 1950-01-11 1.0035231
i <- index(w[w >1.05])
head(i)
# [1] "1950-06-26" "1955-09-26" "1962-05-28" "1987-10-16" "1987-10-19" "1987-10-26"
v <- c()
# calculate seq. num and store it into v.
for(d in seq(1,length(i),1)){v <- append(v,(length(w[paste(as.character(as.Date(index(w[1]))),as.character(as.Date(i[d])),sep='::')])))}
v
substr(as.character(index(w[i])),1,7)
#[1] "1950-06" "1955-09" "1962-05" "1986-09" "1987-10" "1987-10" "1987-10" "1988-01" "1989-10"
#[10] "1997-10" "1998-08" "2000-04" "2001-09" "2008-09" "2008-10" "2008-10" "2008-10" "2008-10"
#[19] "2008-11" "2008-11" "2008-11" "2008-11" "2008-11" "2008-11" "2009-01" "2009-02" "2011-08"
#[28] "2011-08" "2020-03"
# unique(substr(as.character(index(w[i])),1,7))
to.monthly(SP5)[unique(substr(as.character(index(w[i])),1,7))][,4] /to.monthly(SP5)[unique(substr(as.character(index(w[i])),1,7))][,1]


        SP5.Close
 6 1950 0.9424614
 9 1955 1.0069172
 5 1962 0.9140099
 9 1986 0.9145614
10 1987 0.7823696
 1 1988 1.0403480
10 1989 0.9748245
10 1997 0.9655223
 8 1998 0.8542033
 4 2000 0.9692042
 9 2001 0.9182766
 9 2008 0.9056786
10 2008 0.8321379
11 2008 0.9252274
 1 2009 0.9146059
 2 2009 0.8930858
 8 2011 0.9429827
 3 2020 0.9234369

0 件のコメント: