计算时间序列图上的区域

时间:2013-12-10 19:52:50

标签: database r performance database-design graphics

我的问题是我想计算时间序列图中的某个特定区域。

这是图表: enter image description here

这是我生成时间序列的代码:

  x1<-rnorm(250,0.4,0.9)
x <- as.matrix(x1)
t <- ts(x[,1], start=c(1,1), frequency=30)
plot(t,xlim=c(2,4),main="Daily closing price of Walterenergie",ylab="Adjusted close Returns",xlab="Times",col="blue")
abline(0,0)

所以我的问题是如何计算绿色区域,然后分别用红色计算区域。

如果你需要图表:

######我的投资组合表现图
num.points <- 250
x.vals <- 1:num.points
values <- x1

plot(x.vals, values, t="l", pch=20, main="Daily closing price", xlab="Times", ylab="Adjusted close Returns", las=1)
abline(h=0, col="darkgray", lwd=2)

crossings <- values[-length(values)] * values[-1]
crossings <- which(crossings < 0)

# You can draw the points to check (uncomment following line)
# points(x.vals[crossings], values[crossings], col="red", pch="X")

intersections <- NULL
for (cr in crossings)
  {
  new.int <- cr + abs(values[cr])/(abs(values[cr])+abs(values[cr+1]))
  intersections <- c(intersections, new.int)
  }

# Again, let's check the intersections
#points(intersections, rep(0, length(intersections)), pch=20, col="red", cex=0.7)

last.intersection <- 0
for (i in intersections)
  {
  ids <- which(x.vals<=i & x.vals>last.intersection)
  poly.x <- c(last.intersection, x.vals[ids], i)
  poly.y <- c(0, values[ids], 0)
  if (max(poly.y) > 0)
    {
    col="green"
    }
  else
    {
    col="red"
    }
  polygon(x=poly.x, y=poly.y, col=col)

  last.intersection <- i
  }

0 个答案:

没有答案