【发布时间】:2018-05-09 02:36:29
【问题描述】:
我想计算RSI函数,给出如下:
RSI = 100 * RS / ( 1 + RS ), where RS = n_up / n_down
and n_up( t ) = ( 1 - b ) * n_up( t - 1 )
+ b * U( t ),
and n_down( t ) = ( 1 - b ) * n_down( t - 1 )
+ b * D( t ).
where U( t ) = 1 for P( t ) > P( t - 1 ) and
0 otherwise;
and D( t ) = 1 for P( t ) < P( t - 1 ) and
0 otherwise.
这是我的代码:
p <- data[,6]
rsi <- function(P,t,n)
{
U <- function(P,t)
{
if (diff(P)[t] > 0)
{
return(1)
} else {
return(0)
}
}
D <- function(P,t)
{
if (diff(P)[t] < 0)
{
return(1)
} else {
return(0)
}
}
recursive.n_up <- function(P,t,b)
{
return((1-b)*recursive.n_up(P,t-1,b) + b*U(P,t))
}
recursive.n_down <- function(P,t,b)
{
return((1-b)*recursive.n_down(P,t-1,b) + b*D(P,t))
}
b <- 2/(n+1)
rs <- function(P,t,b)
{
return(recursive.n_up(P,t,b)/recursive.n_down(P,t,b))
}
return(100*rs(P,t,b)/(1+rs(P,t,b)))
}
n <- 14
RSI <- rep(0,length(p)-1)
for (i in 1:length(RSI))
{
RSI[i] <- rsi(p,i,n)
}
print(RSI)
我收到一条消息错误说明:
C 堆栈使用 7970184 太接近了
极限
所以我想知道我的算法设计很糟糕,还是在使用递归函数时会出现这种情况?谢谢你帮我解决这个问题。
【问题讨论】:
标签: r recursion quantitative-finance