Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    KDJ indicator error

    Indicators/Strategies/Analyzers
    1
    1
    265
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • 李杰
      李杰 last edited by

      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 .

      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
      $(document).ready(function () { app.coldLoad(); }); }