'Pine Script - if / else if statement

I get the error : line 56: Mismatched input 'if' expecting 'end of line without line continuation'. on my code BUT when I delete some line of code under it the error disappear and the code compile.

Here the code with the error (line 56 is the 3rd line) :

SL = if long == 'long' and (close[1]-low[1])/low[1] > 0.012
    Truncate(low[1], 2)
else if long == 'long' and (close[1]-low[1])/low[1] < 0.012
    Truncate(low[1]-low[1]*0.017, 2)
else if long == 'long_renversement' and renversement_red_golong == 'yes'
    Truncate(math.avg(low[1], close[1]), 2)
else if long == 'long_renversement' and renversement_green_golong == 'yes'
    Truncate(math.avg(low[1], open[1]), 2)
else if long == 'long_continuation' and (close[1]-(math.avg(close[1], open[1]))/(math.avg(close[1], open[1]) > 0.012
    Truncate(math.avg(close[1], open[1]), 2)
else if long == 'long_continuation' and (close[1]-(math.avg(close[1], open[1]))/(math.avg(close[1], open[1]) < 0.012
    Truncate(open[1], 2)
else if short == 'short' and (high[1]-close[1])/close[1] > 0.012
    Truncate(high[1], 2)
else if short == 'short' and (high[1]-close[1])/close[1] < 0.012
    Truncate(high[1]+high[1]*0.017, 2) 
else if short == 'short_renversement' and renversement_red_goshort == 'yes'
    Truncate(math.avg(high[1], open[1]), 2)
else if short == 'short_renversement' and renversement_green_goshort == 'yes'
    Truncate(math.avg(high[1], close[1]), 2)
else if short == 'short_continuation' and ((math.avg(close[1], open[1])-close[1])/close[1] > 0.012
    Truncate(math.avg(close[1], open[1]), 2)
else if short == 'short_continuation' and ((math.avg(close[1], open[1])-close[1])/close[1] < 0.012
    Truncate(open[1], 2)

And here the code when i delete the end of it (run without any error) :

SL = if long == 'long' and (close[1]-low[1])/low[1] > 0.012
    Truncate(low[1], 2)
else if long == 'long' and (close[1]-low[1])/low[1] < 0.012
    Truncate(low[1]-low[1]*0.017, 2)
else if long == 'long_renversement' and renversement_red_golong == 'yes'
    Truncate(math.avg(low[1], close[1]), 2)
else if long == 'long_renversement' and renversement_green_golong == 'yes'
    Truncate(math.avg(low[1], open[1]), 2)

If I delete less than that, the error stay there... I'm so confused...



Solution 1:[1]

The compiler error is not helpful, but the issue is that you have several misplaced brackets in your if/else block (which is counted as a single block, which is why the compiler sends you to the line where the block starts instead of the actual line with the issue):

// Five opening brackets, three closing brackets
else if long == 'long_continuation' and (close[1]-(math.avg(close[1], open[1]))/(math.avg(close[1], open[1]) > 0.012
<...>
else if long == 'long_continuation' and (close[1]-(math.avg(close[1], open[1]))/(math.avg(close[1], open[1]) < 0.012
// Three opening brackets, two closing brackets
else if short == 'short_continuation' and ((math.avg(close[1], open[1])-close[1])/close[1] > 0.012
<...>
else if short == 'short_continuation' and ((math.avg(close[1], open[1])-close[1])/close[1] < 0.012

Fix these and the code should compile properly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 beeholder