TradingView Pine Script RMA 功能如何在内部工作? [英] How TradingView Pine Script RMA function works internally?

查看:195
本文介绍了TradingView Pine Script RMA 功能如何在内部工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 TradingView pinescript 重新实现 rma 函数,但我无法使其输出与原始结果相同的结果功能.

I'm trying to re-implement the rma function from TradingView pinescript but I cannot make it output the same result as the original function.

这里是我开发的代码,代码基本就是ema函数,但是和rma函数plot结果相差很大的时候图表:

Here is the code I developed, the code is basically the ema function, but it differs greatly from the rma function plot result when charting:

//@version=3
study(title = "test", overlay=true)

rolling_moving_average(data, length) =>
    alpha = 2 / (length + 1)
    sum = 0.0
    for index = length to 0
        if sum == 0.0
            sum := data[index]
        else
            sum := alpha * data[index] + (1 - alpha) * sum

atr2 = rolling_moving_average(close, 5)
plot(atr2, title="EMAUP2", color=blue)

atr = rma(close, 5)
plot(atr, title="EMAUP", color=red)

所以我的问题是 rma 函数如何在内部工作,以便我可以实现它的克隆?

So my question is how is the rma function works internally so I can implement a clone of it?

附注.这是文档的链接 https://www.tradingview.com/study-script-reference/#fun_rma 它确实显示了一种可能的实现,但在运行时不起作用.

PS. Here is the link to the documentation https://www.tradingview.com/study-script-reference/#fun_rma It does show a possible implementation, but it does not work when running it.

推荐答案

以下是正确的实现:

plot(rma(close, 15))

// same on pine, but much less efficient
pine_rma(x, y) =>
	alpha = 1/y
    sum = 0.0
    sum := alpha * x + (1 - alpha) * nz(sum[1])
plot(pine_rma(close, 15))

TradingView 上的代码有错误,alpha 应该是 1/y 而不是 y.这个维基百科页面有正确的 RMA 公式维基百科 - 移动平均线

There is a mistake in the code on TradingView, the alpha should be 1/y not y. This Wikipedia page has the correct formula for RMA Wikipedia - moving averages

这篇关于TradingView Pine Script RMA 功能如何在内部工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆