How TradingView Pine Script RMA function works internally?
Asked Answered
B

1

5

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.

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)

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

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.

Budde answered 2/1, 2018 at 4:34 Comment(4)
So what are you trying to do? On one hand you say you want to "re-implement" the same rma() as built-in one. You even post a link to it. But then the script you show is totally different! Also, please provide a screenshot of your results. (Most people are simply too lazy to copy paste and try on their own on T.V. to help out.)Sharleensharlene
How do you know what it outputs? Are you able to console.log? How do you console.log in pine-script?Fortin
has anyone been able to convert rma() to MQL or C or even Python? I'm having trouble with this. What was OP trying to convert it to?Fanlight
@WayneFilkins Im trying the same, its so hard to understand there pine script code. Just doesn't seem to make any sense. Hoped below answer would give explanation but it's just showing a working example :(Karonkaross
M
11

Below is the correct implementation:

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))

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

Monotone answered 24/2, 2018 at 6:54 Comment(7)
thanks, original tv function goes to infinity in few barsBoulevardier
Does anyone know how I can program this into MQL? I don't know what sum[1] is, because if it's just a function then it doesn't run on every candle, so how is there a sum[1]? I have converted it all to MQL except nz(sum[1]) i'm stumped on that part :(Fanlight
@WayneFilkins sum[1] is the ema yesterday ~ pine uses historical data for any value in a series. I was confused about this too at first. See this answer: https://mcmap.net/q/2029488/-tradingview-pinescript-work-with-the-operatorBerne
@AugustKimo ema of yesterday!? As in 24 hours back? I thought it was the value of one bar ago... #68708000.Karonkaross
@Karonkaross Yes one bar ago is correct/what I should've said - depends on the ema length and candle timeframe being used.Berne
@AugustKimo Could you maybe answer my question here #70594551? Im trying to recreate the RSI and are struggeling alot with the RMA function.Karonkaross
tradingview.com/pine-script-reference/v5 -> ta.rma How does sum go from 0.0 (float) to sum[1] an array? Is it a typo and they mean src[1]?Dragrope

© 2022 - 2024 — McMap. All rights reserved.