Colour on a condition
Colour that follows a condition: the candles themselves recoloured while the trend holds, and one average line that switches colour with it as a single continuous stroke.
//@xfscript 1
indicator("Trend Colour")
let length: int = input.int(34, "length", "Length", 2, 400, 1)
let paint_candles: bool = input.bool(true, "paintCandles", "Recolour candles")
let up_color: color = input.color("#22C55E", "upColor", "Up")
let down_color: color = input.color("#EF4444", "downColor", "Down")
let flat_color: color = input.color("#9CA3AF", "flatColor", "Flat")
let fade: float = input.float(0.65, "fade", "Candle opacity", 0.1, 1, 0.05)
let basis: float = ta.ema(close, length)
let slope: float = ta.change(basis, 1)
let rising: bool = slope > 0
let falling: bool = slope < 0
let width: float = ta.stdev(close, length)
var trend_color: color = "#9CA3AF"
trend_color := flat_color
if rising {
trend_color := up_color
} else if falling {
trend_color := down_color
}
plot.candle_color(paint_candles && (rising || falling), trend_color, fade)
plot.line(basis, trend_color, 3, "solid")
plot.band(basis + width, basis - width, up_color, down_color)
plot.marker(ta.crossover(close, basis), low, up_color, "circle", "below", 7, 2)
plot.marker(ta.crossunder(close, basis), high, down_color, "circle", "above", 7, 2)