Pranjal Chaubey
Pranjal Chaubey

Reputation: 61

Pine Script: How to find the highest bars between two bar indices?

I want to figure out the highest bars between a given starting bar_index and ending bar_index.

For example,
if starting_bar_index = 5 and ending_bar_index = 12,
I want to find out the value of the highest high between bar indices 5 and 12 (both inclusive), and the bar_index of highest high bar.

Please note that the current bar_index can be greater than the ending_bar_index. We are effectively looking in a past window and trying to figure out the highest bar.

Upvotes: 0

Views: 2181

Answers (1)

Bjorn Mistiaen
Bjorn Mistiaen

Reputation: 6905

This should do it:

//@version=4
study("My Script", overlay=true)

var int     starting_bar_index      = input(10010, "starting_bar_index",   input.integer, minval=0)
var int     ending_bar_index        = input(10040, "ending_bar_index",     input.integer, minval=0)
var float   highest_high            = na

var int     lookback_bars           = ending_bar_index - starting_bar_index + 1

hh = highest(lookback_bars)

if bar_index == ending_bar_index
    highest_high := hh

myLabel = label.new(bar_index, high, tostring(highest_high, "#.##"), yloc=yloc.abovebar)
label.delete(myLabel[1])

Upvotes: 2

Related Questions