dysonance / Indicators.jl

Financial market technical analysis & indicators in Julia
Other
214 stars 60 forks source link

Indicators RSI is not like Talib RSI #26

Open Sinansi opened 4 years ago

Sinansi commented 4 years ago

RSI from Indicators.jl is not the same as TALib RSI. There is 95% correlation between them, thus 5% deviation.

IND_RSI = Indicators.rsi(close, n=14) TLB_RSI = tlb.RSI(close, timeperiod=14)

replace!(IND_RSI, NaN => 0.0) replace!(TLB_RSI, NaN => 0.0)

julia> cor(IND_RSI, TLB_RSI) 0.9482340199922739

I compared RSI values against Equis MetaStock, and found that TALib RSI is the one giving the correct values. TALib RSI matches exactly MetaStock RSI.

I didnt compare every indicator to TALib, but it seems fine with Momentum. The correlation between Indicators.jl Momentum and TALib Momentum is 1.0.

IND_MOM = Indicators.momentum(close, n=14) TLB_MOM = tlb.MOM(close, timeperiod=14)

replace!(IND_MOM, NaN => 0.0) replace!(TLB_MOM, NaN => 0.0)

julia> cor(IND_MOM, TLB_MOM) 1.0

Thank you!

dysonance commented 4 years ago

@Sinansi Thanks for raising this, I appreciate the detailed question. Some of the difference may come from having different default conventions for computing the moving average for the relative strength. It seems like the TA-Lib function uses simple moving average, where the default for the Indicators rsi function is exponential moving average. I tried an experiment using rsi(px, n=14, ma=sma) and got somewhat closer, though it still doesn't perfectly replicate the TA-Lib result.

I'm having trouble discerning from the TA-Lib docs/source what the source of the difference might be. Would you happen to have a sample of RSI code that replicates the TA-Lib result that I can compare with? Happy to dive in and figure out the disconnect, but if you have a starting point from which I can compare methodology that would help a great deal.

Sinansi commented 4 years ago

@dysonance thanks for the reply!

Indeed, you are correct. I totally forgot that some implementations of RSI, internally adds a smoother to the raw indicator.

I found some source code of TA-Lib in C, and they dont use SMA neither EMA. They internally apply Welles Wilder's Moving Average, which is slightly different from than the traditional moving averages.

Here is the Welles Wilder Moving Average, Quote from (www.incrediblecharts.com)

The standard exponential moving average formula converts the time period to a fraction using the formula EMA% = 2/(n + 1) where n is the number of days. For example, the EMA% for 14 days is 2/(14 days +1) = 13.3%. Wilder, however, uses an EMA% of 1/14 which equals 7.1%. This equates to a 27-day exponential moving average using the standard formula.

This is why comparing Indicators.rsi with Talib.RSI give slightly different correlation.

Sinansi commented 4 years ago

@dysonance I wanted to send you a link for TALib source code as requested, but their website is broken. I have sent you the source code by email.

hchaudhary1 commented 9 months ago

You can look at the Pandas TA code as well: https://github.com/twopirllc/pandas-ta/blob/main/pandas_ta/momentum/rsi.py

This matches very close to TA-Lib