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/

    Beginer help / Simple RSI strategy

    General Code/Help
    4
    5
    2215
    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.
    • M
      Mike last edited by

      Hello,

      Very beginner question, I wanted to build a very simple RSI strategy which is long/short. In other words, it does reversals so we are always in position. Can anyone guide me how to begin, so far I'm able to do long-only strategy but struggling with coding reversal logic

      class RSIStrategy(bt.Strategy):

      def __init__(self):
          self.rsi = bt.indicators.RSI_SMA(self.data.close, period=14)
      
      def next(self):
          if not self.position:
              if self.rsi < 30:
                  self.buy(size=100)
          else:
              if self.rsi > 70:
                  self.sell(size=100)
      run-out 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        Instead of sell when you are in the market, use close (which obviously closes the existing position)

        And use the < 30 and > 70 comparisons as the entry points if you are not in the market.

        1 Reply Last reply Reply Quote 0
        • Jeffrey C F Wong
          Jeffrey C F Wong last edited by

          @Mike , I am also a fellow newbie at python/algo trading and I am trying to achive something similar, again just like you, I couldn't find any reference on this except this post. Do you mind sharing your code on how to exactly do this? Thank you.

          1 Reply Last reply Reply Quote 0
          • run-out
            run-out @Mike last edited by

            @Mike said in Beginer help / Simple RSI strategy:

            if not self.position:

            If you are always in a position, then this part of the code will only execute once. Once you do you first entry, you will be in self.position, and never get past this if statement a second time.

            Here is an excellent discussion similar to your question. Try researching here and in other community threads. This has been discussed frequently.

            https://community.backtrader.com/topic/24/how-do-i-stop-and-reverse-on-same-bar/10

            run-out 1 Reply Last reply Reply Quote 2
            • run-out
              run-out @run-out last edited by

              @run-out Wow I missed the date on this one.... old.

              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(); }); }