How can I refresh the script on each new candle?

722 views Asked by At

I have this script to make the current, unclosed bar transparent

//@version=4
study("", "", true)
i_transp = 80

barcolor(barstate.islast ? close > open ? color.new(color.teal, i_transp) : color.new(color.red, i_transp) : na)

The problem is the candle stays transparent even when a new one appears, until I refresh the page. Can I do this automatically with pine script? Or is there a way to change any closed candles to solid colour?

Thank you!

1

There are 1 answers

0
e2e4 On BEST ANSWER

Don't know why barstate.islast is not working in this example (as well as barstate.isrealtime, barstate.ishistory - all combination of those have the same issue).

But I found the way to achieve your goal by using barstate.isconfirmed:

//@version=4
study("", "", true)
i_transp = 80

var color c_teal = color.new(color.teal, i_transp)
var color c_red = color.new(color.red, i_transp)

color c_barcolor = close > open ? c_teal : c_red

barcolor(not barstate.isconfirmed ? c_barcolor : na)

// or
// barcolor(barstate.isconfirmed ? na : c_barcolor)