Search code examples
pine-scriptpine-script-v5

Compilation error. Line 10: Extraneous input ':' expecting 'end of line without line continuation'


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)

Solution

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