pine脚本如何使用2个系列而不是1个周期来计算RSI?

问题描述 投票:1回答:3

我有一个松树脚本,我试图转换为python。

但是,pine脚本允许RSI有2个系列作为输入而不是传统的系列和句点。

我的问题是如何实现,我尝试了他们的文档实现,但它不计入第二个系列:

pine_rsi(x, y) => 
u = max(x - x[1], 0) // upward change
d = max(x[1] - x, 0) // downward change
rs = rma(u, y) / rma(d, y)
res = 100 - 100 / (1 + rs)
res

谢谢,

python algorithmic-trading pine-script
3个回答
0
投票

如何实现..它不计入第二个系列:?

没有“第二”系列这样的东西


我们来看看代码, 原来看起来像这样:

pine_rsi( x, y ) => 
     u   = max(x - x[1], 0) // upward change
     d   = max(x[1] - x, 0) // downward change
     rs  = rma(u, y) / rma(d, y)
     res = 100 - 100 / (1 + rs)
     res

然而,如果我们将逻辑解码为更好的可读形状,我们得到:

pine_rsi(        aTimeSERIE, anRsiPERIOD ) => 
     up   = max( aTimeSERIE
               - aTimeSERIE[1], 0 )     //         upward changes

     down = max( aTimeSERIE[1]
               - aTimeSERIE,    0 )     //       downward changes

     rs   = ( rma( up,   anRsiPERIOD )  //  RMA-"average" gains  over period
            / rma( down, anRsiPERIOD )  //  RMA-"average" losses over period
              )

     res  = 100 - 100 / ( 1 + rs )      //  

     res

这正是J. Welles Wilder所说的相对强弱指数,不是吗?

因此,只需传递正确的数据类型,就像调用签名所规定的那样,您就完成了。


0
投票

我不是Python的专家或任何东西,但我认为你试图除以零。

RSI的等式是:

RSI= 100 - { 100 \ (1+RS) }

哪里

RS = SMMA(U,n) / SMMA(D,n)

如果向下rma等于零,则等式中的逻辑似乎不能解释RS在分母中将具有零的事实。只要价格趋势连续14个周期或无论RSI的期限如何,都会出现这种情况。

松树编辑器脚本通过在发生上述情况时将RSI设置为100来解决此问题。

在下面的第6行:每当下行rma项等于0时,RSI就切换到100.线的第二部分仅在代码不除零时执行。

1  //@version=3
2  study(title="Relative Strength Index", shorttitle="RSI")
3  src = close, len = input(14, minval=1, title="Length")
4  up = rma(max(change(src), 0), len)
5  down = rma(-min(change(src), 0), len)
6  rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
7  plot(rsi, color=purple)
8  band1 = hline(70)
9  band0 = hline(30)
10 fill(band1, band0, color=purple, transp=90)

0
投票

在Pine Script中实际上存在“第二系列”这样的事情。从文档:

rsi(x,y)

"If x is a series and y is integer then x is a source series and y is a length.
If x is a series and y is a series then x and y are considered to be 2 calculated MAs for upward and downward changes"

但是它没有解释目的是什么,因为rsi()函数没有长度输入 - Pine应该对数据做什么?

像OP一样,我也想知道2系列作为输入的目的,移植到python。这还没有得到回答。

© www.soinside.com 2019 - 2024. All rights reserved.