-3

I`m getting the following error :Compilation error. Line 27: Syntax error at input 'end of line without line continuation' .Any idea what is wrong here ?

//@version=5
strategy("Consecutive", overlay=true)

// Define variables for the MACD and signal line
macd = macd(close, 12, 26)
signal = sma(macd, 9)

// Define a variable to keep track of the number of consecutive green candles
greenCandleCount = 0

// Loop through each bar in the chart
for i = 1 to bar_count - 1
    // If the current bar is green, increment the green candle count
    if close[i] > open[i]
        greenCandleCount := greenCandleCount + 1
    else
        greenCandleCount := 0
        plot(na)
    end

    // If we have 3 consecutive green candles and the MACD crosses down, plot the "LONG" message
    if greenCandleCount == 3 and macd[i] < signal[i] and macd[i+1] > signal[i+1]
        plot(high, "LONG", color=green, linewidth=2)
    else
        plot(na)
    end 
end

To plot the indicator on the chart, it does not compile .

  • 2
    This reeks of ChatGPT code. Aside from the fact that it's banned on SO, just keep it in mind that you won't be able to write working Pine code with it, and making _something_ and then asking people on SO to fix it is definitely not the way to go. – beeholder Dec 16 '22 at 11:00

1 Answers1

0

You cannot use plot() in a local loop. I also don't think you need a for loop either.

If you want to check if there are 3 consecutive green candles and if the MACD crossed down, you can do:

is_green = close > open
is_macd_cross = ta.crossunder(macd, signal)
barssince_macd_cross = ta.barssince(is_macd_cross)
is_green_cnt = math.sum(is_green, 3) // Count the number of green bars in the last 3 candles

is_cond = (is_green_cnt ==3) and (barssince_macd_cross > 0) and (barssince_macd_cross <= 3)
plot(is_cond ? high : na, "LONG", color.green, 2)

Haven't tested this, just wanted to give you an idea.

vitruvius
  • 15,740
  • 3
  • 16
  • 26