'Drawing Lines From Pivots

I am trying to draw a trend line connecting previous pivot highs and previous pivot lows IF the last pivot high is higher than the previous pivot high AND the last pivot low is lower than the previous pivot low. This is to help spot broadening formations.

I am trying to accomplish this with a for loop:

'''
index=0
result=0
for i = 0 to 50
    if ta.pivothigh(10,10)[index] > ta.pivothigh(10,10)[index+1] and ta.pivotlow(10,10)[index] < ta.pivotlow(10,10)[index+1]
        index := index
        break
    result := result+1
    index := index+1

bf_upper = line.new(bar_index[index+1], ta.pivothigh(10,10)[index+1], bar_index[index], ta.pivothigh(10,10)[index], extend=extend.right)
line.delete(bf_upper[1])
bf_lower = line.new(bar_index[index+1], ta.pivotlow(10,10)[index+1], bar_index[index], ta.pivotlow(10,10)[index], extend=extend.right)
line.delete(bf_lower[1])
'''

Not able to get it working properly and would appreciate some help how I can achieve this.



Sources

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

Source: Stack Overflow

Solution Source