'Locating Peak from current bar
I would like to locate the Peak Bar from current bar. This series are as much as 100 bars
H1= high[2]>high[0] and high[2]>=high[1] and high[2]>high[3] and high[2]>high[4]?1:0
H2= high[3]>high[1] and high[3]>=high[2] and high[3]>high[4] and high[3]>high[5]?2:0
H3= high[4]>high[2] and high[4]>=high[3] and high[4]>high[5] and high[4]>high[6]?3:0
H4= high[5]>high[3] and high[5]>=high[4] and high[5]>high[6] and high[5]>high[7]?4:0
H5= high[6]>high[4] and high[6]>=high[5] and high[6]>high[7] and high[6]>high[8]?5:0
........
H96=high[97]>high[95] and high[97]=>high[96] and high[97]>high[98] and high[97]>high[99]?96:0
H97=high[98]>high[96] and high[98]=>high[97] and high[98]>high[99] and high[98]>high[100]?97:0
H98=high[99]>high[97] and high[99]=>high[98] and high[99]>high[100] and high[99]>high[101]?98:0
H99=high[100]>high[98] and high[100]=>high[99] and high[100]>high[101] and high[100]>high[102]?99:0
It can't make it. The script calculates the current bar but also keeps re-calculating the previous bar and 3rd last bars in series ... Eventually, I cannot locate the Peak Bar.
I know finding Peak is easy.
H1= high[2]>high[0] and high[2]>=high[1] and high[2]>high[3] and high[2]>high[4]?1:0
Then use plot function to offset -2 and label it with "P".
However, addressing the Peak from current bar is very difficult. Could someone offer me a help please?
Solution 1:[1]
Does this help you out?
//@version=5
indicator('Swing Hi/Lo', overlay=true)
var int barsback = input.int(3, 'Bars back to check for a swing', 0)
swing_high = ta.pivothigh(barsback, barsback)
swing_low = ta.pivotlow(barsback, barsback)
plotshape(swing_high, style=shape.arrowdown, location=location.abovebar, color=color.red, offset=-barsback)
plotshape(swing_low, style=shape.arrowup, location=location.belowbar, color=color.green, offset=-barsback)
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 | Bjorn Mistiaen |
