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.

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
Sassa
  • 1,673
  • 2
  • 16
  • 30
  • 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.) – not2qubit Jan 24 '18 at 21:25
  • 1
    How do you know what it outputs? Are you able to console.log? How do you console.log in pine-script? – zero_cool Feb 07 '18 at 04:48
  • 1
    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? – Wayne Filkins Oct 10 '19 at 06:11
  • @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 :( – Allart Jan 05 '22 at 15:04

1 Answers1

8

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

SercioSoydanov
  • 980
  • 10
  • 29
Daniel Xu
  • 96
  • 2
  • thanks, original tv function goes to infinity in few bars – norbertas.gaulia Mar 02 '18 at 11:37
  • 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 :( – Wayne Filkins Oct 10 '19 at 05:52
  • @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://stackoverflow.com/a/52905254/13714686 – August Kimo Jul 20 '21 at 13:22
  • @AugustKimo ema of yesterday!? As in 24 hours back? I thought it was the value of one bar ago... https://stackoverflow.com/questions/68707500/whats-the-difference-between-close-and-close1-in-pinescript#:~:text=1%20Answer-,1,close%20of%20the%20current%20bar. – Allart Jan 05 '22 at 15:06
  • 1
    @Allart Yes one bar ago is correct/what I should've said - depends on the ema length and candle timeframe being used. – August Kimo Jan 06 '22 at 16:07
  • @AugustKimo Could you maybe answer my question here https://stackoverflow.com/questions/70594051/calculate-replicate-rsi-from-tradingviews-pine-script? Im trying to recreate the RSI and are struggeling alot with the RMA function. – Allart Jan 10 '22 at 11:19
  • https://www.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]? – Brandon Ros Aug 07 '22 at 20:20