Search code examples
chartspine-scripttradingindicatorpine-script-v4

How to find the bar which is the highest high green bar within 5 bars, excluding the current bar?


I would like to find the green bar which is the highest high bar of last 5 bars, and not the current bar (the current bar may be the highest green one, so I need to exclude it)

I tried this but it seems not correct

find = high == highest(high, 5) and close > open and bar_index > 0 and bar_index < 6
numb = barssince(find)
high_of_that_bar = high[numb]
low_of_that_bar = low[numb])

Thank you for helping me


Solution

  • This should do what you want.

    //@version=5
    indicator("My script")
    
    i_bars = input.int(5, "Bars back")
    
    var float   green_hi    = 0
    var float   green_lo    = 0
    var label   lbl         = label.new(na, na, "")
    
    if barstate.islast
        green_hi := 0
        for i = 1 to i_bars
            if close[i] > open[i] and high[i] > green_hi
                green_hi := high[i]
                green_lo := low[i]
    
        label.set_xy(lbl, bar_index+1, high)
        label.set_text(lbl, str.format("green within last {2} bars\nhigh = {0}\nlow = {1}", green_hi, green_lo, i_bars))
    

    This shows the fractional bar-index for intraday timeframes

    //@version=5
    indicator("My script")
    
    i_bars = input.int(5, "Bars back")
    
    var float   green_hi    = 0
    var float   green_lo    = 0
    var label   lbl         = label.new(na, na, "")
    var int     mybarindex  = na
    var int     mybar       = na
    var int     myi         = na
    
    if barstate.islast
        green_hi := 0
        for i = 1 to i_bars
            if close[i] > open[i] and high[i] > green_hi
                green_hi := high[i]
                green_lo := low[i]
                mybarindex := bar_index
                mybar := bar_index - i
                myi := i
    
        label.set_xy(lbl, bar_index+1, high)
        label.set_text(lbl, str.format("green within last {2} bars\nhigh = {0,number,#.#######}\nlow = {1,number,#.#######}\nmybar = {3}\nmybarindex = {4}\nmyi = {5}", green_hi, green_lo, i_bars, mybar, mybarindex, myi))
    

    I got a second opinion, and it seems bar_index is not fractional after all.
    str.format() just separates thousands with commas.

    This makes it clear:

    //@version=5
    indicator("My script")
    
    i_bars = input.int(5, "Bars back")
    
    var float   green_hi    = 0
    var float   green_lo    = 0
    var label   lbl         = label.new(na, na, "")
    var int     mybarindex  = na
    var int     mybar       = na
    var int     myi         = na
    
    if barstate.islast
        green_hi := 0
        for i = 1 to i_bars
            if close[i] > open[i] and high[i] > green_hi
                green_hi := high[i]
                green_lo := low[i]
                mybarindex := bar_index
                mybar := bar_index - i
                myi := i
    
        label.set_xy(lbl, bar_index+1, high)
        label.set_text(lbl, str.format("green within last {2} bars\nhigh = {0,number,#.#######}\nlow = {1,number,#.#######}\nmybar = {3}\nmybarindex = {4}\nmyi = {5}", green_hi, green_lo, i_bars, mybar, mybarindex, myi))
    
    if barstate.islastconfirmedhistory
        label.new(bar_index - 10, high, str.format("{0}", bar_index + 0.1))