Search code examples
pine-scriptpine-script-v5tradingview-api

Connect the high value of all the candles with a specific condition


At the first I want to search for a candle that its high value is higher than previous and next candle. For example, suppose we have three candle's high value: high[0] , high[1] and high[2] . The condition is high[0]<high[1] And high[1]>high[2] . Then all the candles's high with this condition connect to each other with plot command. My code is connected all the candles after the candle that has this condition.

//@version=5
indicator("My script" , overlay = true)
plot(high<high[1] and high[1]>high[2] ? high[1] : na)

Solution

  • When you find a new pivot, you should draw the line from the last pivot, and record the new pivot as the last pivot.
    A working example :

    //@version=5
    indicator("My script", overlay = true)
    
    var LastPivot = high
    var LastPivotIndex = bar_index
    if high < high[1] and high[1] > high[2]
        line.new(bar_index[1], high[1], LastPivotIndex, LastPivot, color=color.red, width=2)
        LastPivot := high[1]
        LastPivotIndex := bar_index[1]
    

    Gives : enter image description here