0

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]



Roboman Robo
  • 599
  • 2
  • 5
  • 16

1 Answers1

0

You can return the value of close when a peak has happened using the ta.valuewhen() function.

You can check the amount of bars passed since the last time a peak has happened using ta.barssince() function.

By using them both, you can get the price of close the last time a peak has happened only if ta.barssince() returns a number which is less then 15:

//@version=5
indicator(title="peak", overlay = true)

peak = close[0] < close[1] and close[1] > close[2]

previouscandle = ta.valuewhen(peak, close, 0)
barssince_last_peak = ta.barssince(peak)

targetPrice = barssince_last_peak <= 15 and barssince_last_peak > 0 ? previouscandle : na

plot(targetPrice, style = plot.style_linebr)

EDIT:

Following your comment, I would get the closest price of peak in the last 15 bars to current close using arrays. I would use 2 arrays - to the first one I'll add all the peak close price in the last 15 bars. To the second I'll add the index of that bar. That means that if I have a peak 2 bars ago and 5 bars ago, the first array will have the elements of close[2] and close[5] and the second array will have the elements 2 and 5.

Then I would loop the first array and get the lowest difference between the current close and each element. Once I'll get the lowest one, I'll get the index of the element, and use it to get the index of the bar from the second array.

After all that, I basically have the the history reference number of the close price which is peak and within the last 15 bars:

//@version=5
indicator(title="peak", overlay = true)

peak = close[0] < close[1] and close[1] > close[2]

peak_array = array.new_float()
peak_array_index = array.new_int()

for i=1 to 15
    if peak[i]
        array.push(peak_array, close[i])
        array.push(peak_array_index, i)

float closest_close_dif = 99999999999999
int index_of_peak_in_peak_array = na

for [index, _peak] in peak_array
    closest_close_dif := math.min(math.abs(close - _peak), closest_close_dif)
    index_of_peak_in_peak_array := closest_close_dif != closest_close_dif[1] ? index : index_of_peak_in_peak_array

closest_peak_index = array.get(peak_array_index, index_of_peak_in_peak_array)

plot(close[closest_peak_index])

Hope that make sense :)

mr_statler
  • 1,913
  • 2
  • 3
  • 14
  • Thanks! Ran it, and works to get the most recent peaks timeline wise that is within the last 15 candles, great, but how can I get the peak that is closest to the current price and within the last 15 candles? For example, there may be 3 peaks in the last 15 candles How can I get the peak that's price is closest to the price of the most recent candle, regardless of which peak happened first? – Roboman Robo Nov 29 '22 at 20:02
  • Looks good, that makes since, but when I run it it seems to always give the value of the peak that is farthest back from the current candle. Example if the range is 15 candles , it seems to always give the peak that is closest to positition 15 , even when a differnt peak's price is closer to the current candles price. – Roboman Robo Nov 30 '22 at 03:42
  • I've calculated "closest" as the absolute difference between the current `close` price to the `peak` `close` price (so it doesn't matter if the price is above or below the current `close`). Is that what you wanted? – mr_statler Nov 30 '22 at 08:04
  • That's fine that will work, but it's just not getting the peak that is closest in price. It's always getting the one that is farthest back in the timeline. Here is a screenshot of what I'm trying to do. https://ibb.co/m6tpmP2 . Trying to get the peak that is within the last 15 candles and closest to the current price, regardless of it position in the 15 candles. In the example image the closest price price happens to be the most recent peak but looking for the peak that is closest mathmatically to the current price regardless of it position in the 15 candles. – Roboman Robo Nov 30 '22 at 17:50
  • Tested if the array had peaks in it by setting this line to the peak in positon 0 closest_peak_index = array.get(peak_array_index, 0) and I got the error. in array.get function index 0 is out if bounds, array size is 0. It seems the array is emply – Roboman Robo Dec 01 '22 at 04:43