Jitesh Yadav
Jitesh Yadav

Reputation: 21

How to remove the error from Pinescript code

I need some help. TIME FRAME = 5 mins. I want an indicator to plot the open and high till the first opposite color candle. Basically:

First candle positive and second candle negative, then plot high and low for the first 10 mins (price range of first two candles).

First candle positive and second candle positive and third candle negative, then plot high and low for the first 15 mins (price range of first two candles).

First candle is positive and second candle positive and third candle positive and the fourth candle is negative, then plot high and low for the first 20 mins (price range of first 4 candles).

I am getting the error: Mismatched input 'if' expecting 'end of line without line continuation'


// Declare a variable to track if the first red candle has been found
first_red_candle = false

// Declare variables to store the high and low of the day after the first red candle
high_after_first_red = na
low_after_first_red = na

// Iterate through the candles
for i = 0 to bar_index
  // Check if the current candle is red
  if close[i] < open[i]
    // Check if this is the first red candle
    if not first_red_candle
      // Set the first red candle flag to true
      first_red_candle = true
    // Otherwise, update the high and low of the day after the first red candle
    else
      high_after_first_red := max(high_after_first_red, high[i])
      low_after_first_red := min(low_after_first_red, low[i])

// Plot the high and low of the day after the first red candle
plot(high_after_first_red)
plot(low_after_first_red)


Upvotes: 1

Views: 722

Answers (2)

user11717481
user11717481

Reputation: 1602


//@version=5  
indicator("Script") 
bar_index=1000

bool first_red_candle = false 

float high_after_first_red = 1
float low_after_first_red = 1

for i = 0 to bar_index
    bool cond = ((close[i] < open[i]) and not first_red_candle)
    if not cond
        high_after_first_red := math.max(high_after_first_red, high[i])
        low_after_first_red := math.min(low_after_first_red, low[i]) 
    else
        first_red_candle := not first_red_candle 
 
c_1 = high_after_first_red ? color.red : low_after_first_red ? color.gray : color.red
c_2 = low_after_first_red ? color.red : high_after_first_red ? color.green : color.red

plot(high_after_first_red, style=plot.style_line, linewidth=2, color=c_1) 
plot(low_after_first_red, style=plot.style_line, linewidth=2, color=c_2)  

enter image description

Upvotes: 0

G.Lebret
G.Lebret

Reputation: 3058

The indentation in your actual code is made of 2 spaces, instead you must use a TAB indentation.
I tested your code with tabulation indentation and no errors.

Upvotes: 1

Related Questions