RSI line to show different colour at different levels

172 Views Asked by At

I'm using a basic RSI indicator that also has Bollinger bands on it as an overlay. I want my RSI line to show different colour at different levels. I want the line to be in green colour when it is above 60, red when it is below 40, and yellow when it is between 60 & 40.

here is the code:

//@version=5
indicator ("Bollinger bands on RSI", overlay = false)

// INPUT CONTROLS

LENGTHSMA = input.int(title = "SMA", defval = 20, minval = 1, maxval = 30, step= 1)
MULTI = input.int(title = "MULTI", defval = 2, minval = 1, maxval = 3, step = 1)
LENGTHRSI =  input.int(title= "LRSI", defval = 14, minval = 1, maxval = 30, step = 1) 


// TRADING INDICATORS

//SMA = ta.sma(close, LENGTHSMA)
//STD = ta.stdev (RSI,20)
//upperband = SMA + (STD * 2)
//lowerband = SMA - (STD *2)

// Bollinger band on RSI indicator
RSI = ta.rsi(close, LENGTHRSI)
STD = ta.stdev(RSI, LENGTHSMA)
SMA= ta.sma (RSI, LENGTHSMA)

upperband = SMA + (STD * MULTI) 
lowerband = SMA - (STD * MULTI)

plot (upperband)
plot (lowerband)
plot (SMA)

plot (RSI, color = color.new(color.red, 0), linewidth = 1)
hline (80)
hline (60)
hline (40)
hline (20)

I actually tried modifying the above code in accordance to different answers(from this website), but since I don't know how to write code(I got the above one on studying a YouTube video) I failed miserably.
1

There are 1 best solutions below

0
On

You can use the ternary operator in your plot statement to change the color

plot (RSI, color = RSI > 60 ? color.green : RSI < 40 ? color.red : color.yellow, linewidth = 1)