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

时间:2018-05-12 21:35:55

标签: python algorithmic-trading pine-script

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

然而,松树脚本允许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

谢谢,

4 个答案:

答案 0 :(得分:1)

来自pine脚本文档:

rsi(x,y)

“如果x是一个序列,而y是一个整数,则x是一个源序列,而y是一个长度。

“如果x是一个序列而y是一个序列,则x和y被认为是2个计算出的向上和向下变化的MA”

因此,如果您同时具有向上和向下的变化系列,rsi将像这样工作:

u = ["your upward changes series"]
d = ["your downward changes series"]

rsi = 100 - 100 / (1 + u / d) //No need for a period value

答案 1 :(得分: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所说的相对强弱指数,不是吗?

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

答案 2 :(得分:0)

我不是Python或其他方面的专家,但我认为您正在尝试除以零。

RSI的等式是:

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

其中

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

方程式中的逻辑似乎无法说明以下事实:如果向下的rma等于零,则RS的分母为零。只要价格连续14个周期下降或RSI处于任何周期,就会发生这种情况。

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

在下面的第6行上:每当down 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)

答案 3 :(得分: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。尚未得到答复。