I want to write a indicator KDJ ,but the result is different with panda,here is two code:
backtrader is
class KDJ(bt.Indicator):
lines = ("K","D","J",)
params=(('period_me1', 12), ('period_me2', 26), ('period_signal', 9),)
def __init__(self):
self.l.high = bt.indicators.Highest(self.data.high, period = self.p.period_signal)
self.l.low = bt.indicators.Lowest(self.data.low, period = self.p.period_signal)
self.l.rsv=100*bt.DivByZero(self.data_close-self.l.low, self.l.high-self.l.low, zero=None)
self.l.K=bt.indicators.EMA(self.l.rsv,period=2)
self.l.D=bt.indicators.EMA(self.l.K,period=2)
self.l.J=3*self.l.K-2*self.l.D
pandas is
low_list = df['low'].rolling(9, min_periods=9).min()
high_list = df['high'].rolling(9, min_periods=9).max()
low_list.fillna(value = 0, inplace = True)
high_list.fillna(value = 0, inplace = True)
rsv = (df['close'] - low_list) / (high_list - low_list) * 100
df['K'] = pd.DataFrame(rsv).ewm(com=2).mean()
df['D'] = df['K'].ewm(com=2).mean()
df['J'] = 3 * df['K'] - 2 * df['D']
I found rsv result is same but kdj is different;maybe ewm(com=2) error .