i'm a pine script beginner coder
i wanna make an indicator for average candle length all histogram bars will be black except that it's tale > 10% of (high-low) of the candle
Default color is black
green_upper_tale is the upper tale of the green candle
red_upper_tale is the upper tale of the red candle
green_lower_tale is the lower tale of the green candle
red_lower_tale is the lower tale of the red candle
actually_bar_length is the over all length of the candle
//@version=3
study("Candle Length")
len = input(20, minval=1, title="# of Bars to calculate average")
sum = 0.0
bar_color = black // Default color
for i = 0.0 to len-1
sum := sum + (high[i] - low[i])
green_upper_tale = 0 // the upper tale of the green candle
red_upper_tale = 0 // the upper tale of the red candle
green_lower_tale = 0 // the lower tale of the green candle
red_lower_tale = 0 // the lower tale of the red candle
multiplier = 1.0
multiplier := iff(close <= 10.0, 10000.0, multiplier)
multiplier := iff(close >= 10.0, 100.0, multiplier)
active_bar_length = (close-open)*multiplier
actually_bar_length = (high-low) // the over all length of the candle
// for GREEN candles
if (close > open)
green_upper_tale = high-close // the upper tale of the green candle
green_lower_tale = open-low // the lower tale of the green candle
if (green_upper_tale > (actually_bar_length*0.1)) // if the green_upper_tale > (actually_bar_length/10) the candle bar will be blue
bar_color := blue
// for RED candles
if (close < open)
red_upper_tale = high-open // the upper tale of the red candle
red_lower_tale = close-low // the lower tale of the red candle
if (red_lower_tale > (actually_bar_length*0.1)) // if the red_lower_tale > (actually_bar_length/10) the candle bar will be yellow
bar_color := yellow
if (active_bar_length > 0)
active_bar_length := active_bar_length * 1
if (active_bar_length < 0)
active_bar_length := active_bar_length * -1
plot((sum/len)*multiplier)
plot(active_bar_length, color=bar_color, title="test1", style=histogram, linewidth=3)
the problem is histogram bars always black !!
You forgot to use the variable assignment operator for green and red candles check here:
// for GREEN candles
if (close > open)
green_upper_tale = high-close // the upper tale of the green candle
green_lower_tale = open-low // the lower tale of the green candle
// for RED candles
if (close < open)
red_upper_tale = high-open // the upper tale of the red candle
red_lower_tale = close-low // the lower tale of the red candle
Just change those =
s to :=
and it will be fine.
Except, you should also change your xxx_tale
variables to float type.
green_upper_tale = 0.0 // the upper tale of the green candle
red_upper_tale = 0.0 // the upper tale of the red candle
green_lower_tale = 0.0 // the lower tale of the green candle
red_lower_tale = 0.0 // the lower tale of the red candle