Compare last close to previous candles (pine-script)

Compare last close to previous candles (pine-script)

Problem Description:

I have the script below that plots a line of the previous candle close

How would I go about comparing that price to the past 50 candles and plotting vertical lines on any candles with the same closing price?

study("Candle Close", overlay=true)
cus_color = input(#FFFFFF, "Color")
cus_width = input(1, "Width")

var line lastPriceLine =, 0, 0, 0)
line.set_xy1(lastPriceLine, bar_index-500, close[1])
line.set_xy2(lastPriceLine, bar_index, close[1])
line.set_color(lastPriceLine, cus_color)
line.set_width(lastPriceLine, cus_width)

Solution – 1

You can use a for loop and loop through last n bars. Then compare each close price in your loop with the last closed price and draw a line if they are the same.

// This source code is subject to the terms of the Mozilla Public License 2.0 at
// © vitruvius
indicator("My script", overlay=true, scale=scale.none)

if barstate.islast, high, "Close: " + str.tostring(close[1]), yloc=yloc.abovebar, textcolor = color.white)

    for i=2 to 200
        if (close[1] == close[i])
  , close[i], bar_index-i, close[i] + 1, extend=extend.both)
  , high, "Close: " + str.tostring(close[i]), yloc=yloc.abovebar, textcolor = color.white)

enter image description here

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.