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/

    RSI is buying at wrong value

    General Code/Help
    1
    3
    146
    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.
    • Y
      ylu last edited by

      Hello everyone, I am working on improve my TA skills to hit a career on Investment Banking so that i am interested on backtests by backtrader.
      I have a problem about RSI indicator. I am writing many statements but it is still close.buy over RSI 70.
      Here is my code related RSI.

          def __init__(self):
              self.EMAh = bt.talib.EMA(self.data.close, timeperiod = 8)
              self.EMAo = bt.talib.EMA(self.data.close, timeperiod = 18)
              self.EMAy = bt.talib.EMA(self.data.close, timeperiod = 27)
      
              self.crossoverhy = self.EMAh - self.EMAy               
              self.crossoveroy = self.EMAo - self.EMAy               
              self.crossoverho = self.EMAh - self.EMAo
              self.x = self.EMAy - self.data.close[0]
              self.y = self.EMAo - self.data.close[0]
              self.z = self.EMAh - self.data.close[0]
              # Control marketh trend  
              self.sma = bt.indicators.SMA(self.data.close, period=self.p.smaperiod)
              self.smadir = self.sma - self.sma(-self.p.dirperiod)
              self.rsi = bt.talib.RSI(self.data.close, period=36)
              self.trendline = self.data.close[0] - self.sma
      
      

      and,

          def next(self):
              if self.order:
                  return 
              if not self.position:
                  if self.trendline > 0:
                          if self.rsi < 45:
                              self.order = self.buy()
      
              if self.position:
                  if self.trendline > 0:
                      if self.rsi > 70:
                          self.close()
      
      

      Here is plot:
      cc31d515-6298-4196-a85f-6028ccff9a82-image.png

      What is wrong with my code, what it is not selling at this point and if I keep adding 3EMA method it is buying when RSI 80.
      Why this is happening ?
      Thank you for your time.

      1 Reply Last reply Reply Quote 0
      • Y
        ylu last edited by

        Check this out;
        a4ad95a5-a442-481e-9c89-3492519c34a4-image.png
        and this one;
        3accda91-a1d3-4de0-ba78-1c86ad1fa4c1-image.png

        1 Reply Last reply Reply Quote 0
        • Y
          ylu last edited by

          Now it did not close position.. what is wrong ??
          c05198f9-1a2f-4aab-960b-c1501b6dd3ea-image.png

              def next(self):
                  if self.order:
                      return  # pending order execution
                  if not self.position:
                      if self.trendline > 0:
                          if self.rsi < 45:
                              self.order = self.buy()
          
                  if self.position:
                          if self.rsi >= 65:
                              self.close()
                          if self.data.close[0] <= self.sma:
                              self.close()
          
          

          Is that happening to only me ??

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors