EM_RSI Gradient Candles
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © EffyewMoney

//@version=4
study(title="EM's RSI Gradient Candles", overlay = true, shorttitle="EM's RSI Gradient Candles")

//TradingView standard RSI calculation
src = close, len = input(14, minval=1, title="RSI Length")
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))

//User settings (RSI setting is in RSI section above)
emalength = input(defval = 12, title = "EMA Length", type = input.integer)
labelson = input(true, title = "Labels On/Off", group = "Label Settings", inline = "1")
labelhist = input(true, title = "Show Past Labels", group = "Label Settings", inline = "1")

//Defining ranges by 10% increments
tier1 = rsi <= 10
tier2 = rsi > 10 and rsi <= 20
tier3 = rsi > 20 and rsi <= 30
tier4 = rsi > 30 and rsi <= 40
tier5 = rsi > 40 and rsi <= 50
tier6 = rsi > 50 and rsi <= 60
tier7 = rsi > 60 and rsi <= 70
tier8 = rsi > 70 and rsi <= 80
tier9 = rsi > 80 and rsi <= 90
tier10 = rsi >= 90

//Assign color based on range
barcolor(tier10 ? #0A2900 : na)
barcolor(tier9 ? #175a01 : na)
barcolor(tier8 ? #259b00 : na)
barcolor(tier7 ? #57FF1F : na)
barcolor(tier6 ? #AFE79C : na)
barcolor(tier5 ? #FF8585 : na)
barcolor(tier4 ? #FF4747 : na)
barcolor(tier3 ? #CC0000 : na)
barcolor(tier2 ? #8F0000 : na)
barcolor(tier1 ? #3D0000 : na)


//Calculate RSI EMA
rsiema = ema(rsi, emalength)

//Identify RSI/EMA crosses
rsicrossover = rsi > rsiema and rsi[1] <= rsiema[1]
rsicrossunder = rsi < rsiema and rsi[1] >= rsiema[1]

//Create Labels
if(labelson and rsicrossover)
    labeluptext = tostring("RSI over EMA")
    LabelUp = label.new(x = bar_index, y = na, text = labeluptext, yloc = yloc.abovebar, color = input((color.lime), group = "Label Settings", inline = "2"), textcolor = color.white, style = label.style_triangleup, size = size.small)
    //Delete old labels
    if(labelhist == false)
        label.delete(LabelUp[1])
if(labelson and rsicrossunder)
    labeldowntext = tostring("RSI under EMA")
    LabelDown = label.new(x = bar_index, y = na, text = labeldowntext, yloc = yloc.belowbar, color = input((color.red), group = "Label Settings", inline = "2"), textcolor = color.white, style = label.style_triangledown, size = size.small)
    if(labelhist == false)
        label.delete(LabelDown[1])



//Enable the following settings to turn on matching wicks and borders. 
//Warning: due to limitations of pine script it WILL require more patience when choosing settings. A workaround exists, but it's extra effort, not as pretty, and still sometimes flashes the wrong colors on mouseover
//Workaround: Place the indicator at the top of the object tree. Disable each Bar Color box you don't want to see, then find the corresponding slot in the plotcandle section and set the opacity to 0%
//Wick and Border color reference
//wickcol = input(false, title = "Matching Wick and Borders")
//color wicknborder = tier1 ? #3D0000 : tier2 ? #8F0000 : tier3 ? #CC0000 : tier4 ? #FF4747 : tier5 ? #FF8585 : tier6 ? #AFE79C : tier7 ? #57FF1F : tier8 ? #259b00 : tier9 ? #175a01 : tier10 ? #0A2900 : na
//Set borders and wicks to match candle colors.
//plotcandle(wickcol ? open : na, wickcol ? high : na, wickcol ? low : na, wickcol ? close : na, wickcolor = wicknborder, bordercolor = wicknborder, color = wicknborder)