我有一个松树脚本,我正在尝试将其转换为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
谢谢
我不是 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)
来自松树脚本文档:
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
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。这还没有得到答复。
表示 x 和 y 系列
pine_rsi( x, y ) =>
res = 100 - 100 / (1 + x / y)
res
如何实现的..它不计入第二个系列:?
没有所谓的">第二"系列。
让我们回顾一下代码,
原文是这样的:
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命名的相对强弱指数,不是吗?
因此,只需按照调用签名的规定传递正确的数据类型,您就完成了。