正负数的累计返回

问题描述

我正在尝试使用cummulative_return函数(以下给出)来查找以下4个向量的累积返回,但结果与预期的不同。

library(TTR) # ROC function

a = c(-1000,-2000,-3000,-4000,-5000,-6000,-7000,-8000)
b = c(1,2,3,4,5,6,7,8)
c = c(-7,-1,-20,-50,-93,112,212)
d = c(7,1,20,50,93,-112,-212)

cummulative_return <- function(x){
  y <- ROC(x,type = c("discrete"),na.pad = T) * sign(lag(x))
  cumprod(na.omit(y) + 1) - 1
}

cummulative_return(a)
[1] -1 -1 -1 -1 -1 -1 -1
cummulative_return(b)
[1] 1 2 3 4 5 6 7
cummulative_return(c)
[1]   0.8571429 -34.4285714  15.7142857   1.3400000   6.4980645  13.1927650
cummulative_return(d)
[1]  -0.8571429   1.8571429   6.1428571  12.2857143 -17.0000000  -2.7142857

仅向量'a'的答案不正确,预期结果应为

cummulative_return(a)
[1] -1 -2 -3 -4 -5 -6 -7

您能指出函数cummulative_return中的错误吗?

而且,在任何库中都有类似的函数可以找到负数和正数的累积返回而没有此类错误吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)