1

I keep getting compilation errors, I am new to pinescript and cant figure out where I am going wrong.

//@version=5

// Define the RSI period
period = 14

// Calculate the RSI
rsi = rsi(close, period)

// Check for bullish divergence
   if (rsi < 30) and (rsi < rsi[1]) and (close > close[1]):
    # Bullish divergence detected, go long
    strategy.entry("Long", long=true)
    strategy.exit("Exit long", "Long", profit=50, stop=-50)

// Check for bearish divergence
    if (rsi > 70) and (rsi > rsi[1]) and (close < close[1]):
     # Bearish divergence detected, go short
     strategy.entry("Short", short=true)
     strategy.exit("Exit short", "Short", profit=50, stop=-50)

1 Answers1

0

You error comes from the indentation of your 'if' block.
Change you indentation and your code to :

//@version=5
// Define the RSI period
period = 14

rsi = ta.rsi(close, period)

// Check for bullish divergence
if (rsi < 30) and (rsi < rsi[1]) and (close > close[1])
    // Bullish divergence detected, go long
    strategy.entry("Long", strategy.long)
    strategy.exit("Exit long", "Long", profit=50, stop=-50)

// Check for bearish divergence
if (rsi > 70) and (rsi > rsi[1]) and (close < close[1])
    // Bearish divergence detected, go short
    strategy.entry("Short", strategy.short)
    strategy.exit("Exit short", "Short", profit=50, stop=-50)
G.Lebret
  • 2,826
  • 2
  • 16
  • 27
  • Thank you for checking this out for me. When I was posting the question, it made me make indentations, I hope that wasn't the cause. When I try your code, I get this error now: 7:30:45 PM — Compilation error. The script must have one indicator() or strategy() or library() function call – Official Jayo Jan 09 '23 at 00:34
  • the code you provided required I added: study("ta-lib") strategy("My Trading plan") – Official Jayo Jan 11 '23 at 00:51