Trading View Indicator

31 Views Asked by At

I want to develop a Trading View Indicator in which the Candle turns Blue if TR<ATR. I tried the below code but no result.

//@version=5
indicator("TR < ATR Color Change", overlay=true)

// Calculate True Range (TR)
tr = math.abs(high - low)
tr += math.max(math.abs(high - close), math.abs(low - close))

// Calculate Average True Range (ATR)
atr = math.avg(tr, 14) // Adjust the period as needed

// Define coloring condition
color = if tr < atr
    color.blue
else 
    color.maroon

// Plot the candles with coloring
plotcandle(close, high, low, open, color=color, wickcolor=color)
1

There are 1 best solutions below

1
Rakesh Poluri On

Using barcolor() is better than trying to use plotcandle() for coloring candles. Also, ternary conditional operators (?:) are more efficient for your color designation. Try the following:

//@version=5
indicator("TR < ATR Color Change", overlay=true)

// Calculate True Range (TR)
tr = math.abs(high - low)
tr += math.max(math.abs(high - close), math.abs(low - close))

// Calculate Average True Range (ATR)
atr = math.avg(tr, 14) // Adjust the period as needed

// Define coloring condition
color = tr < atr ? color.blue : color.maroon

barcolor(color)