technical
technical copied to clipboard
Please add tradingview RMA indicator
Hi, any ideas how to implement TradingView Pine RMA ?
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.
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])
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.
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
Any update on this?
@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
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