简体   繁体   中英

Pinescript error- trading view indicator - how to fix it please?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM