I am trying to get the peak price that is closest to the current price, above the current price, and within the last 15 candles
For example, there may be 3 peaks in the last 15 candles. I am trying to sort them to get the peak price that is closest to the price of the most recent candle, and above the most recent candle's price, regardless of which peak happened most recently
I tryed to set that up, but currently it is plotting the close price to the chart and not the target prices
--
How can I get the peak price that is closest to the current price, above the current price, and within the last 15 candles?
--
The Code so far:
//@version=5
indicator(title="peak", overlay = true)
peak = close[0] < close[1] and close[1] > close[2]
////previouscandle = ta.valuewhen(peak, close[1], 0)
////barssince_last_peak = ta.barssince(peak)
////targetPrice = barssince_last_peak <= 15 and barssince_last_peak > 0 ? previouscandle : na
startprice = close
targetprice = close
//loop through the last 15 candles
for i=0 to 15-1
//if a peak price is greater than the start price set its peak price to the targetpricenew variable
targetpricenew = ta.valuewhen(peak and close[1] > startprice, close[1], 0)
// if the distance between targetpricenew's peak is less than the distance between the current targetprices's peak
if ( targetpricenew - startprice ) < ( targetprice - startprice )
//Set the targetpricenew peak as the targetprice
targetprice = targetpricenew
//plot the targetprice
plot(targetprice, style = plot.style_linebr)