technical icon indicating copy to clipboard operation
technical copied to clipboard

Please add tradingview RMA indicator

Open 2W-12 opened this issue 3 years ago • 7 comments

Hi, any ideas how to implement TradingView Pine RMA ?

2W-12 avatar Jan 07 '21 06:01 2W-12

hm, based on the description, it might be something around the following:

dataframe['rsi'] = ta.RSI(dataframe)
# Either ta-lib
dataframe['rma'] = ta.EMA(dataframe, price='rsi')
# or native pandas
dataframe['rma'] = dataframe['rsi'].rolling(window).ewm()

Now the big problem is how the EMA is calculated. pandas allows multiple parameters to specify the calculation algorithm - but i'm not sure which one gives 1/alpha.

I'm also not sure how ta-lib calculates it precisely - and their docs in this regards is quite lacking.

xmatthias avatar Jan 07 '21 06:01 xmatthias

Unfortunately it doesn't give correct results.

Example:

close = np.array([4086.29, 4310.01, 4509.08, 4130.37, 3699.99, 3660.02, 4378.48, 4640.0, 5709.99, 5950.02])
# with period=3 on tradingview   rma(close, 3) will give following results
rma = np.array([np.nan, np.nan, 4301.79333333, 4244.65222222, 4063.09814815, 3928.73876543, 4078.65251029, 4265.76834019, 4747.17556013, 5148.12370675])

2W-12 avatar Jan 07 '21 07:01 2W-12

As mentioned above - the parameters for the ewm() function in pandas are up to testing / analysis - as there's different ways to calculate a EMA ... pandas should support most of them - but how to configure them is ... not well documented if you ask me.

xmatthias avatar Jan 07 '21 08:01 xmatthias

You could try this library instead and see if it gives better results: https://technical-analysis-library-in-python.readthedocs.io/en/latest/ta.html

synackSA avatar May 12 '21 04:05 synackSA

Any update on this?

renu285 avatar Nov 02 '21 10:11 renu285

@2W-12 The following is pretty close to what you want: Are you sure your rma result from TradingView is correct, how did you observe the results?

df = pd.DataFrame(data=[4086.29, 4310.01, 4509.08, 4130.37, 3699.99, 3660.02, 4378.48, 4640.0, 5709.99, 5950.02], columns=['close'])
length=3
df['close'].ewm(alpha=(1.0/length),adjust=False,min_periods=length).mean()

Result:

0            NaN
1            NaN
2    4276.935556
3    4228.080370
4    4052.050247
5    3921.373498
6    4073.742332
7    4262.494888
8    4744.993259
9    5146.668839
Name: close, dtype: float64

C0D3D3V avatar Jan 17 '22 14:01 C0D3D3V

TradingView forces the first entry to be the SMA of the first n entries, so the following leads to exactly the same result:

import pandas as pd
pd.set_option("display.precision", 8)
df = pd.DataFrame(data=[4086.29, 4310.01, 4509.08, 4130.37, 3699.99, 3660.02, 4378.48, 4640.0, 5709.99, 5950.02], columns=['close'])
length=3
df['rma'] = df['close'].copy()
df['rma'].iloc[:length] = df['rma'].rolling(length).mean().iloc[:length]
df['rma'] = df['rma'].ewm(alpha=(1.0/length),adjust=False).mean()

Result:

     close            rma
0  4086.29            NaN
1  4310.01            NaN
2  4509.08  4301.79333333
3  4130.37  4244.65222222
4  3699.99  4063.09814815
5  3660.02  3928.73876543
6  4378.48  4078.65251029
7  4640.00  4265.76834019
8  5709.99  4747.17556013
9  5950.02  5148.12370675

C0D3D3V avatar Jan 17 '22 14:01 C0D3D3V