YellowHunter
Pine Script Rookie
Pine Script Rookie
Posts: 3
Joined: December 31st, 2022
Contact: TradingView Profile

How can I detect any change in Linear Regression Candle color from red to green or green to red

Assume lrcopen, lrchigh, lrclow, lrcclose are the open, high, low, close of Linear Regression Candle which is plotted using plotcandle(). I want to check and compare the lrcopen < lrcclose value for last two closed Linear Regression Candle to see whether it changes from false to true or vice versa after plotting

//@version=4
study(title="Humble LinReg Candles", shorttitle="LinReg Candles", format=format.price, precision=4, overlay=true)

signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 200, defval =7)
sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=true)

lin_reg = input(title="Lin Reg", type=input.bool, defval=true)
linreg_length = input(title="Linear Regression Length", type=input.integer, minval = 1, maxval = 200, defval = 11)

bopen = lin_reg ? linreg(open, linreg_length, 0) : open
bhigh = lin_reg ? linreg(high, linreg_length, 0) : high
blow = lin_reg ? linreg(low, linreg_length, 0) : low
bclose = lin_reg ? linreg(close, linreg_length, 0) : close

r = bopen < bclose

signal = sma_signal ? sma(bclose, signal_length) : ema(bclose, signal_length)

plotcandle(r ? bopen : na, r ? bhigh : na, r ? blow: na, r ? bclose : na, title="LinReg Candles", color= color.green, wickcolor=color.green, bordercolor=color.green, editable= true)
plotcandle(r ? na : bopen, r ? na : bhigh, r ? na : blow, r ? na : bclose, title="LinReg Candles", color=color.red, wickcolor=color.red, bordercolor=color.red, editable= true)

// I want to detect the change of color of just plotted Linear Regression Candle here

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: How can I detect any change in Linear Regression Candle color from red to green or green to red

You can use the historical reference for previous bars such as lrcopen[1], lrchigh[1], lrclow[1], lcclose[1] to reference the previous bar values and lrcopen[2], lrchigh[2], lrclow[2], lcclose[2] for the closed bar 2 bars ago.

I hope this helps

Return to “Pine Script Q&A”