Search code examples
pine-scriptpine-script-v5pine-script-v4

How can i detect equal highs and lows?


enter image description here

i am trying to make a script that detects equal highs and low and the draw a line from point a to point b or c ect like shown in the picture, but i do not know how to go about it. as pinescript beginner i have been learning to plot lines until yesterday but now i am stuck on how to implement the concept, can i please have some suggestions and methods on how to do it? thank you in advance


Solution

  • You need to draw a line that starts at the bar_index of the candle in which its high is equal to the current bar high, and continue the line until the current bar_index. The easiest way I could think of is that on each bar, you'll create an array of all the high price of previous candles. Then you'll get the index position of the first element that equals to the current high. This will give you the offset of the bar from the current bar_index. After that, you just need to draw the line. The only issue is that with this code, you'll need to determine how far into the past you want to check, but other than that, it works:

    //@version=5
    indicator("My script", overlay=true)
    
    len = input.int(defval=1000, title="Amount of candles to check backwards", minval=1)
    
    highArray = array.new_float(len)
    
    for i=1 to len - 1
        array.set(highArray, i, high[i])
    
    indexOfLastHighEqual = array.indexof(highArray, high)
    
    if indexOfLastHighEqual > 0
        newLineHigh = line.new(bar_index - indexOfLastHighEqual, high, bar_index, high, style=line.style_dotted, color=color.green )
    
    // line.delete(newLineHigh[1])
    
    
    lowArray = array.new_float(len)
    
    for i=1 to len - 1
        array.set(highArray, i, low[i])
    
    indexOfLastLowEqual = array.indexof(highArray, low)
    
    if indexOfLastLowEqual > 0
        newLineLow = line.new(bar_index - indexOfLastLowEqual, low, bar_index, low, style=line.style_dotted, color=color.red )
    
    // line.delete(newLineLow[1])