user3125707
user3125707

Reputation: 409

How can I recolor the very first candle on tradingview.com using pine script if it breaches a particular price?

I would like to color the first candle to a different color in a 15 minute chart based on certain conditions. Is this possible?

I am using the below code to color the first candle in either yellow or purple if it meets some conditions but it seems to color all the subsequent candles as well.

study(title="OR", shorttitle="OpeningRange", overlay=true)
up15on = input(true, title="15 Minute Opening Range High")
down15on = input(true, title="15 Minute Opening Range Low")
is_newbar(res) => change(time(res)) != 0 
adopt(r, s) => security(syminfo.tickerid, r, s) 
high_range = valuewhen(is_newbar('D'),high,0)
low_range = valuewhen(is_newbar('D'),low,0)
candle_color = close>adopt('15', high_range) ? color.purple :
 close<adopt('15', low_range) ? color.yellow :
 na
barcolor( candle_color )

Upvotes: 0

Views: 1371

Answers (1)

alexgrover
alexgrover

Reputation: 1043

I assume that by first candle you actually mean the last one and not the candle where bar_index = 0

In which case you can make use of show_last in barcolor as follows:

barcolor(candle_color,show_last=1)

This will only color the last candle on the chart. If you actually mean the first candle so bar_index=0 then use:

barcolor(barstate.isfirst ? candle_color : na)

Upvotes: 1

Related Questions