'Triangle to draw a triangle on a swing high

Hi I'm trying to draw a triangle every time there is a swing high. I define a swing high as when there are three candles where the first candle's high is lower than the second candle's high and the third candle's high is lower than the second candle's high.

//@version=4
study(title = "Swing High", overlay = true)
if(high[1] > high and high[2] < high[1])

    plotshape(bar_index[1], shape.triangleup)

Right now i'm getting the error "line 5: Cannot use 'plotshape' in local scope."



Solution 1:[1]

Error message is clear. You cannot have that funciton in a local scope (if block etc.).

However, you can apply your condition directly in the plotshape() function like so:

//@version=4
study(title = "Swing High", overlay = true)

swing_high = (high[1] > high and high[2] < high[1])

plotshape(swing_high[1], style=shape.triangleup)

enter image description here

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 vitruvius