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



  • 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)

  • administrators

    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.