// @version=3
// Original concept by Giorgos Siligardos (Stocks and Commodities Magazine, June 2003)
// Original TV implementation by HPotter ([login to view URL])
study(title="Reverse RSI", overlay=true)
Level1 = input(defval=50, minval=1, title="Mid Level")
Level2 = input(defval=60, minval=1, title="Uptrend Limit")
Level3 = input(defval=40, minval=1, title="Downtrend Limit")
Level4 = input(defval=70, minval=1, title="Overbought Level")
Level5 = input(defval=30, minval=1, title="Oversold Level")
RSILen = input(defval=14, minval=1, title="RSI Length")
ZLen = input(defval=14, minval=1, title="Zero Lag MA Length")
showcross = input(true, "Show cross over/under")
colormidline = input(false, title="Color Midline")
CurrentRes = input(true, title="Use Current Chart Resolution?")
CustomRes = input("240", title="Custom Timeframe? Uncheck Box Above (E.g. 1M, 5D, 240 = 4Hours)")
src = log(close)
// -------------------------- RSI Calc --------------------------------//
reRSI(RSILen, Length) =>
ExpPer = 2 * RSILen - 1
K = 2 / (ExpPer + 1)
AUC = 0.0
AUC := iff(src > src[1], K * (src - src[1]) + (1 - K) * nz(AUC[1], 1), (1-K) * nz(AUC[1], 1))
ADC = 0.0
ADC := iff(src > src[1], (1-K) * nz(ADC[1], 1), K * (src[1] - src) + (1 - K) * nz(ADC[1], 1))
nVal = (RSILen - 1) * (ADC * Length / (100 - Length) - AUC)
nRes = iff(nVal >= 0, src + nVal, src + nVal * (100 - Length) / Length)
nl1 = reRSI(RSILen, Level1)
nl2 = reRSI(RSILen, Level2)
nl3 = reRSI(RSILen, Level3)
nl4 = reRSI(RSILen, Level4)
nl5 = reRSI(RSILen, Level5)
// -------------------------- ZEMA --------------------------------//
zema(src, len) =>
ema1=ema(src, len)
ema2=ema(ema1, len)
d=ema1-ema2
zlema=ema1+d
zl1 = zema(nl1, ZLen)
zl2 = zema(nl2, ZLen)
zl3 = zema(nl3, ZLen)
zl4 = zema(nl4, ZLen)
zl5 = zema(nl5, ZLen)
// -------------------------- MTF + Custom Setup --------------------------------//
res = CurrentRes ? period : CustomRes
nL1 = exp(security(tickerid, res, zl1))
nL2 = exp(security(tickerid, res, zl2))
nL3 = exp(security(tickerid, res, zl3))
nL4 = exp(security(tickerid, res, zl4))
nL5 = exp(security(tickerid, res, zl5))
// -------------------------- Plotting --------------------------------//
color = nL1 >= nL1[1] ? lime : red
crossdn = high < nL4[1] and high[1] >= nL4[1]
crossup = low > nL5[1] and low[1] <= nL5[1]
plotshape(showcross and crossdn ? high : na, location=[login to view URL], style=[login to view URL], color=red, size=[login to view URL], text="Sell", textcolor=white, transp=0, offset=-1)
plotshape(showcross and crossup ? low : na, location=[login to view URL], style=[login to view URL], color=green, size=[login to view URL], text="Buy", textcolor=white, transp=0, offset=-1)
plot(nL1, color=colormidline ? color : black, title="Mid Level - Usually 50", linewidth=1, transp=60)
pu1 = plot(nL2, color=yellow, title="Uptrend Limit", linewidth=1, transp=100)
pd1 = plot(nL3, color=yellow, title="Downtrend Limit", linewidth=1, transp=100)
pu2 = plot(nL4, color=red, title="Overbought Level", linewidth=1, transp=55)
pd2 = plot(nL5, color=lime, title="Oversold Level", linewidth=1, transp=55)
fill(pu1,pu2, maroon, transp=80)
fill(pd1,pd2, green, transp=80)