I want to get the value of the peak that is closest to the current price and within the last 15 candles.
The problem is I am getting the value of peaks outside of my limit of the last 15 candles and they are not the closest peak to the current price.
How can I get the value of the peak that is closest to the current price and within the last 15 candles?
The code so far
peak = close[0] < close[1] and close[1] > close[2]
numberofcandlestolookthrough = 15
previouscandle = close
currentcandle = close
goinglong = ( close[0] > close[1] )
goingshort = ( close[0] < close[1] )
currentcandlelongpeakstarget = 0.0
targetprice = 0.0
seriesshortner(_src, _a, _b) =>
newseries = 0.0
for i = _a to _b - 1
newseries := _src[i]
newseries
shortenedclose = seriesshortner(close, 0, 20)
//if a peak
if (peak)
if(goinglong)
//loook throuhtb last number of candle
for i = 0 to numberofcandlestolookthrough-1
//if a peak
if (peak[i] == true)
//if peak higher than or equal to curent candles price
if (shortenedclose[i+1] >= currentcandle )
//Save as long peak target
currentcandlelongpeakstarget := shortenedclose[i+1]
currentcandlelongpeakstargetsorted := ta.highest(currentcandlelongpeakstarget,4)
targetprice := currentcandlelongpeakstargetsorted[0]