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)
-
Instead of
sell
when you are in the market, useclose
(which obviously closes the existing position)And use the
< 30
and> 70
comparisons as the entry points if you are not in the market. -
@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.
-
@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 Wow I missed the date on this one.... old.